ECMAPIDebug.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #include <kopano/platform.h>
  18. #include <kopano/mapi_ptr.h>
  19. #include "ECMAPIDebug.h"
  20. #include <kopano/ECDebug.h>
  21. using namespace std;
  22. namespace KC {
  23. HRESULT Dump(std::ostream &os, LPMAPIPROP lpProp, const std::string &strPrefix)
  24. {
  25. ULONG cValues;
  26. SPropArrayPtr ptrProps;
  27. std::string strObjType = "MAPIProp";
  28. if (lpProp == NULL)
  29. return MAPI_E_INVALID_PARAMETER;
  30. HRESULT hr = lpProp->GetProps(NULL, 0, &cValues, &~ptrProps);
  31. if (FAILED(hr))
  32. return hr;
  33. auto lpObjType = PCpropFindProp(ptrProps.get(), cValues, PR_OBJECT_TYPE);
  34. if (lpObjType) {
  35. switch (lpObjType->Value.l) {
  36. case MAPI_MESSAGE:
  37. strObjType = "Message";
  38. break;
  39. case MAPI_ATTACH:
  40. strObjType = "Attach";
  41. break;
  42. default:
  43. break;
  44. }
  45. }
  46. os << strPrefix << "Object type: " << strObjType << endl;
  47. os << strPrefix << "Properties (count=" << cValues << "):" << endl;
  48. for (ULONG i = 0; i < cValues; ++i)
  49. os << strPrefix << " " << PropNameFromPropTag(ptrProps[i].ulPropTag) << " - " << PropValueToString(&ptrProps[i]) << endl;
  50. // TODO: Handle more object types
  51. if (lpObjType && lpObjType->Value.l == MAPI_MESSAGE) {
  52. MessagePtr ptrMessage;
  53. MAPITablePtr ptrTable;
  54. ULONG ulCount = 0;
  55. hr = lpProp->QueryInterface(ptrMessage.iid(), &~ptrMessage);
  56. if (hr != hrSuccess)
  57. return hr;
  58. // List recipients
  59. hr = ptrMessage->GetRecipientTable(0, &~ptrTable);
  60. if (hr != hrSuccess)
  61. return hr;
  62. hr = ptrTable->GetRowCount(0, &ulCount);
  63. if (hr != hrSuccess)
  64. return hr;
  65. os << strPrefix << "Recipients (count=" << ulCount << "):" << endl;
  66. if (ulCount > 0) {
  67. SRowSetPtr ptrRows;
  68. while (true) {
  69. hr = ptrTable->QueryRows(64, 0, &ptrRows);
  70. if (hr != hrSuccess)
  71. return hr;
  72. if (ptrRows.empty())
  73. break;
  74. for (SRowSetPtr::size_type i = 0; i < ptrRows.size(); ++i) {
  75. auto lpRowId = PCpropFindProp(ptrRows[i].lpProps, ptrRows[i].cValues, PR_ROWID);
  76. os << strPrefix << " Recipient: ";
  77. if (lpRowId)
  78. os << lpRowId->Value.l;
  79. else
  80. os << "???";
  81. os << endl;
  82. for (ULONG j = 0; j < ptrRows[i].cValues; ++j)
  83. os << strPrefix << " " << PropNameFromPropTag(ptrRows[i].lpProps[j].ulPropTag) << " - " << PropValueToString(&ptrRows[i].lpProps[j]) << endl;
  84. }
  85. }
  86. }
  87. // List attachments
  88. hr = ptrMessage->GetAttachmentTable(0, &~ptrTable);
  89. if (hr != hrSuccess)
  90. return hr;
  91. hr = ptrTable->GetRowCount(0, &ulCount);
  92. if (hr != hrSuccess)
  93. return hr;
  94. os << strPrefix << "Attachments (count=" << ulCount << "):" << endl;
  95. if (ulCount > 0) {
  96. static constexpr const SizedSPropTagArray(1, sptaAttachProps) = {1, {PR_ATTACH_NUM}};
  97. hr = ptrTable->SetColumns(sptaAttachProps, TBL_BATCH);
  98. if (hr != hrSuccess)
  99. return hr;
  100. while (true) {
  101. SRowSetPtr ptrRows;
  102. hr = ptrTable->QueryRows(64, 0, &ptrRows);
  103. if (hr != hrSuccess)
  104. return hr;
  105. if (ptrRows.empty())
  106. break;
  107. for (SRowSetPtr::size_type i = 0; i < ptrRows.size(); ++i) {
  108. AttachPtr ptrAttach;
  109. if (ptrRows[i].lpProps[0].ulPropTag != PR_ATTACH_NUM)
  110. return hr;
  111. hr = ptrMessage->OpenAttach(ptrRows[i].lpProps[0].Value.l, &ptrAttach.iid(), 0, &~ptrAttach);
  112. if (hr != hrSuccess)
  113. return hr;
  114. os << strPrefix << " Attachment: " << ptrRows[i].lpProps[0].Value.l << endl;
  115. hr = Dump(os, ptrAttach, strPrefix + " ");
  116. if (hr != hrSuccess)
  117. return hr;
  118. }
  119. }
  120. }
  121. }
  122. else if (lpObjType && lpObjType->Value.l == MAPI_ATTACH) {
  123. AttachPtr ptrAttach;
  124. SPropValuePtr ptrAttachMethod;
  125. hr = lpProp->QueryInterface(ptrAttach.iid(), &~ptrAttach);
  126. if (hr != hrSuccess)
  127. return hr;
  128. hr = HrGetOneProp(ptrAttach, PR_ATTACH_METHOD, &~ptrAttachMethod);
  129. if (hr != hrSuccess)
  130. return hr;
  131. // TODO: Handle more attachment types.
  132. if (ptrAttachMethod->Value.l == ATTACH_EMBEDDED_MSG) {
  133. MessagePtr ptrMessage;
  134. hr = ptrAttach->OpenProperty(PR_ATTACH_DATA_OBJ, &ptrMessage.iid(), 0, 0, &~ptrMessage);
  135. if (hr != hrSuccess)
  136. return hr;
  137. os << strPrefix << "Embedded message:" << endl;
  138. hr = Dump(os, ptrMessage, strPrefix + " ");
  139. if (hr != hrSuccess)
  140. return hr;
  141. }
  142. }
  143. return hrSuccess;
  144. }
  145. } /* namespace */