archive.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 <new>
  19. #include "archive.h"
  20. #include <kopano/ECLogger.h>
  21. #include <kopano/ECGetText.h>
  22. #include <kopano/charset/convert.h>
  23. #include <kopano/mapi_ptr.h>
  24. #include "helpers/StoreHelper.h"
  25. #include "operations/copier.h"
  26. #include "operations/instanceidmapper.h"
  27. #include "ArchiverSession.h"
  28. #include "helpers/ArchiveHelper.h"
  29. #include <list>
  30. #include <sstream>
  31. #include <kopano/Util.h>
  32. #include <kopano/ECDebug.h>
  33. using namespace std;
  34. using namespace KC::helpers;
  35. using namespace KC::operations;
  36. typedef std::unique_ptr<Copier::Helper> HelperPtr;
  37. #ifdef UNICODE
  38. typedef std::wostringstream tostringstream;
  39. #else
  40. typedef std::ostringstream tostringstream;
  41. #endif
  42. void ArchiveResult::AddMessage(MessagePtr ptrMessage) {
  43. m_lstMessages.push_back(ptrMessage);
  44. }
  45. void ArchiveResult::Undo(IMAPISession *lpSession) {
  46. for (const auto i : m_lstMessages)
  47. Util::HrDeleteMessage(lpSession, i);
  48. }
  49. HRESULT Archive::Create(IMAPISession *lpSession, ArchivePtr *lpptrArchive)
  50. {
  51. if (lpSession == NULL || lpptrArchive == NULL)
  52. return MAPI_E_INVALID_PARAMETER;
  53. auto x = new(std::nothrow) Archive(lpSession);
  54. if (x == nullptr)
  55. return MAPI_E_NOT_ENOUGH_MEMORY;
  56. lpptrArchive->reset(x);
  57. return hrSuccess;
  58. }
  59. Archive::Archive(IMAPISession *lpSession)
  60. : m_ptrSession(lpSession, true)
  61. {
  62. }
  63. HRESULT Archive::HrArchiveMessageForDelivery(IMessage *lpMessage)
  64. {
  65. HRESULT hr = hrSuccess;
  66. ULONG cMsgProps;
  67. SPropArrayPtr ptrMsgProps;
  68. MsgStorePtr ptrStore;
  69. ULONG ulType;
  70. MAPIFolderPtr ptrFolder;
  71. StoreHelperPtr ptrStoreHelper;
  72. SObjectEntry refMsgEntry;
  73. ObjectEntryList lstArchives;
  74. ArchiverSessionPtr ptrSession;
  75. InstanceIdMapperPtr ptrMapper;
  76. HelperPtr ptrHelper;
  77. list<pair<MessagePtr,PostSaveActionPtr> > lstArchivedMessages;
  78. ArchiveResult result;
  79. ObjectEntryList lstReferences;
  80. MAPIPropHelperPtr ptrMsgHelper;
  81. static constexpr const SizedSPropTagArray(3, sptaMessageProps) =
  82. {3, {PR_ENTRYID, PR_STORE_ENTRYID, PR_PARENT_ENTRYID}};
  83. enum {IDX_ENTRYID, IDX_STORE_ENTRYID, IDX_PARENT_ENTRYID};
  84. if (lpMessage == NULL) {
  85. hr = MAPI_E_INVALID_PARAMETER;
  86. ec_log_warn("Archive::HrArchiveMessageForDelivery(): invalid parameter");
  87. goto exit;
  88. }
  89. hr = lpMessage->GetProps(sptaMessageProps, 0, &cMsgProps, &~ptrMsgProps);
  90. if (hr != hrSuccess) {
  91. ec_log_warn("Archive::HrArchiveMessageForDelivery(): GetProps failed %x", hr);
  92. goto exit;
  93. }
  94. refMsgEntry.sStoreEntryId.assign(ptrMsgProps[IDX_STORE_ENTRYID].Value.bin);
  95. refMsgEntry.sItemEntryId.assign(ptrMsgProps[IDX_ENTRYID].Value.bin);
  96. hr = m_ptrSession->OpenMsgStore(0, ptrMsgProps[IDX_STORE_ENTRYID].Value.bin.cb, reinterpret_cast<ENTRYID *>(ptrMsgProps[IDX_STORE_ENTRYID].Value.bin.lpb), &ptrStore.iid(), MDB_WRITE, &~ptrStore);
  97. if (hr != hrSuccess) {
  98. ec_log_warn("Archive::HrArchiveMessageForDelivery(): OpenMsgStore failed %x", hr);
  99. goto exit;
  100. }
  101. hr = StoreHelper::Create(ptrStore, &ptrStoreHelper);
  102. if (hr != hrSuccess) {
  103. ec_log_warn("Archive::HrArchiveMessageForDelivery(): StoreHelper::Create failed %x", hr);
  104. goto exit;
  105. }
  106. hr = ptrStoreHelper->GetArchiveList(&lstArchives);
  107. if (hr != hrSuccess) {
  108. ec_log_warn("Archive::HrArchiveMessageForDelivery(): StoreHelper::GetArchiveList failed %x", hr);
  109. goto exit;
  110. }
  111. if (lstArchives.empty()) {
  112. ec_log_debug("No archives attached to store");
  113. goto exit;
  114. }
  115. hr = ptrStore->OpenEntry(ptrMsgProps[IDX_PARENT_ENTRYID].Value.bin.cb, reinterpret_cast<ENTRYID *>(ptrMsgProps[IDX_PARENT_ENTRYID].Value.bin.lpb), &ptrFolder.iid(), MAPI_MODIFY, &ulType, &~ptrFolder);
  116. if (hr != hrSuccess) {
  117. ec_log_warn("Archive::HrArchiveMessageForDelivery(): StoreHelper::OpenEntry failed %x", hr);
  118. goto exit;
  119. }
  120. hr = ArchiverSession::Create(m_ptrSession, ec_log_get(), &ptrSession);
  121. if (hr != hrSuccess) {
  122. ec_log_warn("Archive::HrArchiveMessageForDelivery(): ArchiverSession::Create failed %x", hr);
  123. goto exit;
  124. }
  125. /**
  126. * @todo: Create an archiver config object globally in the calling application to
  127. * avoid the creation of the configuration for each message to be archived.
  128. */
  129. hr = InstanceIdMapper::Create(ec_log_get(), NULL, &ptrMapper);
  130. if (hr != hrSuccess) {
  131. ec_log_warn("Archive::HrArchiveMessageForDelivery(): InstanceIdMapper::Create failed %x", hr);
  132. goto exit;
  133. }
  134. // First create all (mostly one) the archive messages without saving them.
  135. ptrHelper.reset(new(std::nothrow) Copier::Helper(ptrSession,
  136. ec_log_get(), ptrMapper, nullptr, ptrFolder));
  137. if (ptrHelper == nullptr) {
  138. hr = MAPI_E_NOT_ENOUGH_MEMORY;
  139. goto exit;
  140. }
  141. for (const auto &arc : lstArchives) {
  142. MessagePtr ptrArchivedMsg;
  143. PostSaveActionPtr ptrPSAction;
  144. hr = ptrHelper->CreateArchivedMessage(lpMessage, arc, refMsgEntry, &~ptrArchivedMsg, &ptrPSAction);
  145. if (hr != hrSuccess) {
  146. ec_log_warn("Archive::HrArchiveMessageForDelivery(): CreateArchivedMessage failed %x", hr);
  147. goto exit;
  148. }
  149. lstArchivedMessages.push_back(make_pair(ptrArchivedMsg, ptrPSAction));
  150. }
  151. // Now save the messages one by one. On failure all saved messages need to be deleted.
  152. for (const auto &msg : lstArchivedMessages) {
  153. ULONG cArchivedMsgProps;
  154. SPropArrayPtr ptrArchivedMsgProps;
  155. SObjectEntry refArchiveEntry;
  156. hr = msg.first->GetProps(sptaMessageProps, 0,
  157. &cArchivedMsgProps, &~ptrArchivedMsgProps);
  158. if (hr != hrSuccess) {
  159. ec_log_warn("Archive::HrArchiveMessageForDelivery(): ArchivedMessage GetProps failed %x", hr);
  160. goto exit;
  161. }
  162. refArchiveEntry.sItemEntryId.assign(ptrArchivedMsgProps[IDX_ENTRYID].Value.bin);
  163. refArchiveEntry.sStoreEntryId.assign(ptrArchivedMsgProps[IDX_STORE_ENTRYID].Value.bin);
  164. lstReferences.push_back(refArchiveEntry);
  165. hr = msg.first->SaveChanges(KEEP_OPEN_READWRITE);
  166. if (hr != hrSuccess) {
  167. ec_log_warn("Archive::HrArchiveMessageForDelivery(): ArchivedMessage SaveChanges failed %x", hr);
  168. goto exit;
  169. }
  170. if (msg.second) {
  171. HRESULT hrTmp = msg.second->Execute();
  172. if (hrTmp != hrSuccess)
  173. ec_log_warn("Failed to execute post save action. hr=0x%08x", hrTmp);
  174. }
  175. result.AddMessage(msg.first);
  176. }
  177. // Now add the references to the original message.
  178. lstReferences.sort();
  179. lstReferences.unique();
  180. hr = MAPIPropHelper::Create(MAPIPropPtr(lpMessage, true), &ptrMsgHelper);
  181. if (hr != hrSuccess) {
  182. ec_log_warn("Archive::HrArchiveMessageForDelivery(): failed creating reference to original message %x", hr);
  183. goto exit;
  184. }
  185. hr = ptrMsgHelper->SetArchiveList(lstReferences, true);
  186. exit:
  187. // On error delete all saved archives
  188. if (FAILED(hr))
  189. result.Undo(m_ptrSession);
  190. return hr;
  191. }
  192. HRESULT Archive::HrArchiveMessageForSending(IMessage *lpMessage, ArchiveResult *lpResult)
  193. {
  194. HRESULT hr = hrSuccess;
  195. ULONG cMsgProps;
  196. SPropArrayPtr ptrMsgProps;
  197. MsgStorePtr ptrStore;
  198. StoreHelperPtr ptrStoreHelper;
  199. ObjectEntryList lstArchives;
  200. ArchiverSessionPtr ptrSession;
  201. InstanceIdMapperPtr ptrMapper;
  202. HelperPtr ptrHelper;
  203. list<pair<MessagePtr,PostSaveActionPtr> > lstArchivedMessages;
  204. ArchiveResult result;
  205. static constexpr const SizedSPropTagArray(2, sptaMessageProps) = {1, {PR_STORE_ENTRYID}};
  206. enum {IDX_STORE_ENTRYID};
  207. if (lpMessage == NULL) {
  208. hr = MAPI_E_INVALID_PARAMETER;
  209. goto exit;
  210. }
  211. hr = lpMessage->GetProps(sptaMessageProps, 0, &cMsgProps, &~ptrMsgProps);
  212. if (hr != hrSuccess) {
  213. ec_log_warn("Archive::HrArchiveMessageForSending(): GetProps failed %x", hr);
  214. goto exit;
  215. }
  216. hr = m_ptrSession->OpenMsgStore(0, ptrMsgProps[IDX_STORE_ENTRYID].Value.bin.cb, reinterpret_cast<ENTRYID *>(ptrMsgProps[IDX_STORE_ENTRYID].Value.bin.lpb), &ptrStore.iid(), 0, &~ptrStore);
  217. if (hr != hrSuccess) {
  218. ec_log_warn("Archive::HrArchiveMessageForSending(): OpenMsgStore failed %x", hr);
  219. goto exit;
  220. }
  221. hr = StoreHelper::Create(ptrStore, &ptrStoreHelper);
  222. if (hr != hrSuccess) {
  223. ec_log_warn("Archive::HrArchiveMessageForSending(): StoreHelper::Create failed %x", hr);
  224. goto exit;
  225. }
  226. hr = ptrStoreHelper->GetArchiveList(&lstArchives);
  227. if (hr != hrSuccess) {
  228. ec_log_err("Unable to obtain list of attached archives. hr=0x%08x", hr);
  229. SetErrorMessage(hr, _("Unable to obtain list of attached archives."));
  230. goto exit;
  231. }
  232. if (lstArchives.empty()) {
  233. ec_log_debug("No archives attached to store");
  234. goto exit;
  235. }
  236. hr = ArchiverSession::Create(m_ptrSession, ec_log_get(), &ptrSession);
  237. if (hr != hrSuccess) {
  238. ec_log_warn("Archive::HrArchiveMessageForSending(): ArchiverSession::Create failed %x", hr);
  239. goto exit;
  240. }
  241. /**
  242. * @todo: Create an archiver config object globally in the calling application to
  243. * avoid the creation of the configuration for each message to be archived.
  244. */
  245. hr = InstanceIdMapper::Create(ec_log_get(), NULL, &ptrMapper);
  246. if (hr != hrSuccess) {
  247. ec_log_warn("Archive::HrArchiveMessageForSending(): InstanceIdMapper::Create failed %x", hr);
  248. goto exit;
  249. }
  250. // First create all (mostly one) the archive messages without saving them.
  251. // We pass an empty MAPIFolderPtr here!
  252. ptrHelper.reset(new(std::nothrow) Copier::Helper(ptrSession,
  253. ec_log_get(), ptrMapper, nullptr, MAPIFolderPtr()));
  254. if (ptrHelper == nullptr) {
  255. hr = MAPI_E_NOT_ENOUGH_MEMORY;
  256. goto exit;
  257. }
  258. for (const auto &arc : lstArchives) {
  259. ArchiveHelperPtr ptrArchiveHelper;
  260. MAPIFolderPtr ptrArchiveFolder;
  261. MessagePtr ptrArchivedMsg;
  262. PostSaveActionPtr ptrPSAction;
  263. hr = ArchiveHelper::Create(ptrSession, arc, ec_log_get(), &ptrArchiveHelper);
  264. if (hr != hrSuccess) {
  265. SetErrorMessage(hr, _("Unable to open archive."));
  266. goto exit;
  267. }
  268. hr = ptrArchiveHelper->GetOutgoingFolder(&~ptrArchiveFolder);
  269. if (hr != hrSuccess) {
  270. ec_log_err("Failed to get outgoing archive folder. hr=0x%08x", hr);
  271. SetErrorMessage(hr, _("Unable to get outgoing archive folder."));
  272. goto exit;
  273. }
  274. hr = ptrArchiveFolder->CreateMessage(&ptrArchivedMsg.iid(), 0, &~ptrArchivedMsg);
  275. if (hr != hrSuccess) {
  276. ec_log_err("Failed to create message in outgoing archive folder. hr=0x%08x", hr);
  277. SetErrorMessage(hr, _("Unable to create archive message in outgoing archive folder."));
  278. goto exit;
  279. }
  280. hr = ptrHelper->ArchiveMessage(lpMessage, NULL, ptrArchivedMsg, &ptrPSAction);
  281. if (hr != hrSuccess) {
  282. SetErrorMessage(hr, _("Unable to copy message data."));
  283. goto exit;
  284. }
  285. ec_log_info("Stored message in archive");
  286. lstArchivedMessages.push_back(make_pair(ptrArchivedMsg, ptrPSAction));
  287. }
  288. // Now save the messages one by one. On failure all saved messages need to be deleted.
  289. for (const auto &msg : lstArchivedMessages) {
  290. hr = msg.first->SaveChanges(KEEP_OPEN_READONLY);
  291. if (hr != hrSuccess) {
  292. ec_log_err("Failed to save message in archive. hr=0x%08x", hr);
  293. SetErrorMessage(hr, _("Unable to save archived message."));
  294. goto exit;
  295. }
  296. if (msg.second) {
  297. HRESULT hrTmp = msg.second->Execute();
  298. if (hrTmp != hrSuccess)
  299. ec_log_warn("Failed to execute post save action. hr=0x%08x", hrTmp);
  300. }
  301. result.AddMessage(msg.first);
  302. }
  303. if (lpResult)
  304. std::swap(result, *lpResult);
  305. exit:
  306. // On error delete all saved archives
  307. if (FAILED(hr))
  308. result.Undo(m_ptrSession);
  309. return hr;
  310. }
  311. void Archive::SetErrorMessage(HRESULT hr, LPCTSTR lpszMessage)
  312. {
  313. tostringstream oss;
  314. LPTSTR lpszDesc;
  315. oss << lpszMessage << endl;
  316. oss << _("Error code:") << _T(" ") << convert_to<tstring>(GetMAPIErrorDescription(hr))
  317. << _T(" (") << tstringify(hr, true) << _T(")") << endl;
  318. if (Util::HrMAPIErrorToText(hr, &lpszDesc) == hrSuccess)
  319. oss << _("Error description:") << _T(" ") << lpszDesc << endl;
  320. m_strErrorMessage.assign(oss.str());
  321. }