WSMAPIPropStorage.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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. #include <new>
  17. #include <kopano/platform.h>
  18. #include "WSMAPIPropStorage.h"
  19. #include "Mem.h"
  20. #include <kopano/ECGuid.h>
  21. #include <kopano/ECInterfaceDefs.h>
  22. #include "SOAPUtils.h"
  23. #include "WSUtil.h"
  24. #include <kopano/Util.h>
  25. #include "pcutil.hpp"
  26. #include <kopano/charset/convert.h>
  27. /*
  28. *
  29. * This is a PropStorage object for use with the WebServices storage platform
  30. *
  31. */
  32. #define START_SOAP_CALL retry:
  33. #define END_SOAP_CALL \
  34. if (er == KCERR_END_OF_SESSION && this->m_lpTransport->HrReLogon() == hrSuccess) \
  35. goto retry; \
  36. hr = kcerr_to_mapierr(er, MAPI_E_NOT_FOUND); \
  37. if(hr != hrSuccess) \
  38. goto exit;
  39. WSMAPIPropStorage::WSMAPIPropStorage(ULONG cbParentEntryId,
  40. LPENTRYID lpParentEntryId, ULONG cbEntryId, LPENTRYID lpEntryId,
  41. ULONG ulFlags, KCmd *lpCmd, std::recursive_mutex &data_lock,
  42. ECSESSIONID ecSessionId, unsigned int ulServerCapabilities,
  43. WSTransport *lpTransport) :
  44. ECUnknown("WSMAPIPropStorage"), lpDataLock(data_lock),
  45. m_ulFlags(ulFlags), m_lpTransport(lpTransport)
  46. {
  47. CopyMAPIEntryIdToSOAPEntryId(cbEntryId, lpEntryId, &m_sEntryId);
  48. CopyMAPIEntryIdToSOAPEntryId(cbParentEntryId, lpParentEntryId, &m_sParentEntryId);
  49. this->lpCmd = lpCmd;
  50. this->ecSessionId = ecSessionId;
  51. this->ulServerCapabilities = ulServerCapabilities;
  52. lpTransport->AddSessionReloadCallback(this, Reload, &m_ulSessionReloadCallback);
  53. }
  54. WSMAPIPropStorage::~WSMAPIPropStorage()
  55. {
  56. // Unregister the notification request we sent
  57. if(m_bSubscribed) {
  58. ECRESULT er = erSuccess;
  59. LockSoap();
  60. lpCmd->ns__notifyUnSubscribe(ecSessionId, m_ulConnection, &er);
  61. UnLockSoap();
  62. }
  63. FreeEntryId(&m_sEntryId, false);
  64. FreeEntryId(&m_sParentEntryId, false);
  65. m_lpTransport->RemoveSessionReloadCallback(m_ulSessionReloadCallback);
  66. }
  67. HRESULT WSMAPIPropStorage::QueryInterface(REFIID refiid, void **lppInterface)
  68. {
  69. REGISTER_INTERFACE2(WSMAPIPropStorage, this);
  70. REGISTER_INTERFACE2(IECPropStorage, &this->m_xECPropStorage);
  71. return MAPI_E_INTERFACE_NOT_SUPPORTED;
  72. }
  73. HRESULT WSMAPIPropStorage::Create(ULONG cbParentEntryId,
  74. LPENTRYID lpParentEntryId, ULONG cbEntryId, LPENTRYID lpEntryId,
  75. ULONG ulFlags, KCmd *lpCmd, std::recursive_mutex &lpDataLock,
  76. ECSESSIONID ecSessionId, unsigned int ulServerCapabilities,
  77. WSTransport *lpTransport, WSMAPIPropStorage **lppPropStorage)
  78. {
  79. HRESULT hr = hrSuccess;
  80. auto lpStorage = new(std::nothrow) WSMAPIPropStorage(cbParentEntryId,
  81. lpParentEntryId, cbEntryId, lpEntryId, ulFlags, lpCmd,
  82. lpDataLock, ecSessionId, ulServerCapabilities,
  83. lpTransport);
  84. if (lpStorage == nullptr)
  85. return MAPI_E_NOT_ENOUGH_MEMORY;
  86. hr = lpStorage->QueryInterface(IID_WSMAPIPropStorage, (void **)lppPropStorage);
  87. if(hr != hrSuccess)
  88. delete lpStorage;
  89. return hr;
  90. }
  91. HRESULT WSMAPIPropStorage::HrReadProps(LPSPropTagArray *lppPropTags,ULONG *cValues, LPSPropValue *ppValues)
  92. {
  93. // this call should disappear
  94. return MAPI_E_NO_SUPPORT;
  95. }
  96. HRESULT WSMAPIPropStorage::HrLoadProp(ULONG ulObjId, ULONG ulPropTag, LPSPropValue *lppsPropValue)
  97. {
  98. ECRESULT er = erSuccess;
  99. HRESULT hr = hrSuccess;
  100. LPSPropValue lpsPropValDst = NULL;
  101. struct loadPropResponse sResponse;
  102. LockSoap();
  103. if (ulObjId == 0 && ((this->ulServerCapabilities & KOPANO_CAP_LOADPROP_ENTRYID) == 0)) {
  104. hr = MAPI_E_NO_SUPPORT;
  105. goto exit;
  106. }
  107. START_SOAP_CALL
  108. {
  109. if(SOAP_OK != lpCmd->ns__loadProp(ecSessionId, m_sEntryId, ulObjId, ulPropTag, &sResponse))
  110. er = KCERR_NETWORK_ERROR;
  111. else
  112. er = sResponse.er;
  113. }
  114. END_SOAP_CALL
  115. hr = ECAllocateBuffer(sizeof(SPropValue), (void **)&lpsPropValDst);
  116. if(hr != hrSuccess)
  117. goto exit;
  118. if(sResponse.lpPropVal == NULL) {
  119. hr = MAPI_E_NOT_FOUND;
  120. goto exit;
  121. }
  122. hr = CopySOAPPropValToMAPIPropVal(lpsPropValDst, sResponse.lpPropVal, lpsPropValDst);
  123. *lppsPropValue = lpsPropValDst;
  124. exit:
  125. UnLockSoap();
  126. return hr;
  127. }
  128. HRESULT WSMAPIPropStorage::HrWriteProps(ULONG cValues, LPSPropValue pValues, ULONG ulFlags)
  129. {
  130. // this call should disappear
  131. return MAPI_E_NO_SUPPORT;
  132. }
  133. HRESULT WSMAPIPropStorage::HrDeleteProps(const SPropTagArray *lpsPropTagArray)
  134. {
  135. // this call should disappear
  136. return MAPI_E_NO_SUPPORT;
  137. }
  138. HRESULT WSMAPIPropStorage::HrMapiObjectToSoapObject(MAPIOBJECT *lpsMapiObject, struct saveObject *lpSaveObj, convert_context *lpConverter)
  139. {
  140. HRESULT hr = hrSuccess;
  141. unsigned int size;
  142. unsigned int i;
  143. ULONG ulPropId = 0;
  144. GUID sServerGUID = {0};
  145. GUID sSIGUID = {0};
  146. if (lpConverter == NULL) {
  147. convert_context converter;
  148. return HrMapiObjectToSoapObject(lpsMapiObject, lpSaveObj, &converter);
  149. }
  150. if (lpsMapiObject->lpInstanceID) {
  151. lpSaveObj->lpInstanceIds = s_alloc<entryList>(nullptr);
  152. lpSaveObj->lpInstanceIds->__size = 1;
  153. lpSaveObj->lpInstanceIds->__ptr = s_alloc<entryId>(nullptr, lpSaveObj->lpInstanceIds->__size);
  154. memset(lpSaveObj->lpInstanceIds->__ptr, 0, lpSaveObj->lpInstanceIds->__size * sizeof(entryId));
  155. if ((m_lpTransport->GetServerGUID(&sServerGUID) != hrSuccess) ||
  156. (HrSIEntryIDToID(lpsMapiObject->cbInstanceID, (LPBYTE)lpsMapiObject->lpInstanceID, &sSIGUID, NULL, (unsigned int*)&ulPropId) != hrSuccess) ||
  157. (sSIGUID != sServerGUID) ||
  158. (CopyMAPIEntryIdToSOAPEntryId(lpsMapiObject->cbInstanceID, (LPENTRYID)lpsMapiObject->lpInstanceID, &lpSaveObj->lpInstanceIds->__ptr[0]) != hrSuccess)) {
  159. ulPropId = 0;
  160. FreeEntryList(lpSaveObj->lpInstanceIds, true);
  161. lpSaveObj->lpInstanceIds = NULL;
  162. }
  163. } else {
  164. lpSaveObj->lpInstanceIds = NULL;
  165. }
  166. // deleted props
  167. size = lpsMapiObject->lstDeleted.size();
  168. if (size != 0) {
  169. lpSaveObj->delProps.__ptr = s_alloc<unsigned int>(nullptr, size);
  170. lpSaveObj->delProps.__size = size;
  171. i = 0;
  172. for (auto id : lpsMapiObject->lstDeleted)
  173. lpSaveObj->delProps.__ptr[i++] = id;
  174. } else {
  175. lpSaveObj->delProps.__ptr = NULL;
  176. lpSaveObj->delProps.__size = 0;
  177. }
  178. // modified props
  179. size = lpsMapiObject->lstModified.size();
  180. if (size != 0) {
  181. lpSaveObj->modProps.__ptr = s_alloc<propVal>(nullptr, size);
  182. i = 0;
  183. for (const auto &prop : lpsMapiObject->lstModified) {
  184. SPropValue tmp = prop.GetMAPIPropValRef();
  185. if (PROP_ID(tmp.ulPropTag) == ulPropId)
  186. /* Skip the data if it is a Instance ID, If the instance id is invalid
  187. * the data will be replaced in HrUpdateSoapObject function. The memory is already
  188. * allocated, but not used. */
  189. if (/*lpsMapiObject->bChangedInstance &&*/ lpsMapiObject->lpInstanceID)
  190. continue;
  191. hr = CopyMAPIPropValToSOAPPropVal(&lpSaveObj->modProps.__ptr[i], &tmp, lpConverter);
  192. if(hr == hrSuccess)
  193. ++i;
  194. }
  195. lpSaveObj->modProps.__size = i;
  196. } else {
  197. lpSaveObj->modProps.__ptr = NULL;
  198. lpSaveObj->modProps.__size = 0;
  199. }
  200. // recursive process all children when parent is still present
  201. lpSaveObj->__size = 0;
  202. lpSaveObj->__ptr = NULL;
  203. if (lpsMapiObject->bDelete == false) {
  204. size = lpsMapiObject->lstChildren.size();
  205. if (size != 0) {
  206. lpSaveObj->__ptr = s_alloc<saveObject>(nullptr, size);
  207. size = 0;
  208. for (const auto &cld : lpsMapiObject->lstChildren)
  209. // Only send children if:
  210. // - Modified AND NOT deleted
  211. // - Deleted AND loaded from server (locally created/deleted items with no server ID needn't be sent)
  212. if ((cld->bChanged && !cld->bDelete) || (cld->ulObjId && cld->bDelete))
  213. hr = HrMapiObjectToSoapObject(cld, &lpSaveObj->__ptr[size++], lpConverter);
  214. lpSaveObj->__size = size;
  215. }
  216. }
  217. // object info
  218. lpSaveObj->bDelete = lpsMapiObject->bDelete;
  219. lpSaveObj->ulClientId = lpsMapiObject->ulUniqueId;
  220. lpSaveObj->ulServerId = lpsMapiObject->ulObjId;
  221. lpSaveObj->ulObjType = lpsMapiObject->ulObjType;
  222. return hr;
  223. }
  224. HRESULT WSMAPIPropStorage::HrUpdateSoapObject(MAPIOBJECT *lpsMapiObject, struct saveObject *lpsSaveObj, convert_context *lpConverter)
  225. {
  226. HRESULT hr;
  227. std::list<ECProperty>::const_iterator iterProps;
  228. SPropValue sData;
  229. ULONG ulPropId = 0;
  230. if (lpConverter == NULL) {
  231. convert_context converter;
  232. return HrUpdateSoapObject(lpsMapiObject, lpsSaveObj, &converter);
  233. }
  234. /* FIXME: Support Multiple Single Instances */
  235. if (lpsSaveObj->lpInstanceIds && lpsSaveObj->lpInstanceIds->__size) {
  236. /* Check which property this Instance ID is for */
  237. hr = HrSIEntryIDToID(lpsSaveObj->lpInstanceIds->__ptr[0].__size, lpsSaveObj->lpInstanceIds->__ptr[0].__ptr, NULL, NULL, (unsigned int*)&ulPropId);
  238. if (hr != hrSuccess)
  239. return hr;
  240. /* Instance ID was incorrect, remove it */
  241. FreeEntryList(lpsSaveObj->lpInstanceIds, true);
  242. lpsSaveObj->lpInstanceIds = NULL;
  243. /* Search for the correct property and copy it into the soap object, note that we already allocated the required memory... */
  244. for (iterProps = lpsMapiObject->lstModified.cbegin();
  245. iterProps != lpsMapiObject->lstModified.cend();
  246. ++iterProps) {
  247. sData = iterProps->GetMAPIPropValRef();
  248. if (PROP_ID(sData.ulPropTag) != ulPropId)
  249. continue;
  250. // Extra check for protect the modProps array
  251. if (lpsSaveObj->modProps.__size >= 0 &&
  252. static_cast<size_t>(lpsSaveObj->modProps.__size) >= lpsMapiObject->lstModified.size()) {
  253. /*
  254. * modProps.size+1 > lpsMapiObject->lstModified->size()
  255. * (a+1>b) transformed to (a>=b)
  256. */
  257. assert(false);
  258. return MAPI_E_NOT_ENOUGH_MEMORY;
  259. }
  260. hr = CopyMAPIPropValToSOAPPropVal(&lpsSaveObj->modProps.__ptr[lpsSaveObj->modProps.__size], &sData, lpConverter);
  261. if(hr != hrSuccess)
  262. return hr;
  263. ++lpsSaveObj->modProps.__size;
  264. break;
  265. }
  266. // Broken single instance ID without data.
  267. assert(iterProps != lpsMapiObject->lstModified.cend());
  268. }
  269. for (gsoap_size_t i = 0; i < lpsSaveObj->__size; ++i) {
  270. MAPIOBJECT find(lpsSaveObj->__ptr[i].ulObjType, lpsSaveObj->__ptr[i].ulClientId);
  271. auto iter = lpsMapiObject->lstChildren.find(&find);
  272. if (iter != lpsMapiObject->lstChildren.cend()) {
  273. hr = HrUpdateSoapObject(*iter, &lpsSaveObj->__ptr[i], lpConverter);
  274. if (hr != hrSuccess)
  275. return hr;
  276. }
  277. }
  278. return hrSuccess;
  279. }
  280. void WSMAPIPropStorage::DeleteSoapObject(struct saveObject *lpSaveObj)
  281. {
  282. if (lpSaveObj->__ptr) {
  283. for (gsoap_size_t i = 0; i < lpSaveObj->__size; ++i)
  284. DeleteSoapObject(&lpSaveObj->__ptr[i]);
  285. s_free(nullptr, lpSaveObj->__ptr);
  286. }
  287. if (lpSaveObj->modProps.__ptr) {
  288. for (gsoap_size_t i = 0; i < lpSaveObj->modProps.__size; ++i)
  289. FreePropVal(&lpSaveObj->modProps.__ptr[i], false);
  290. s_free(nullptr, lpSaveObj->modProps.__ptr);
  291. }
  292. s_free(nullptr, lpSaveObj->delProps.__ptr);
  293. if (lpSaveObj->lpInstanceIds)
  294. FreeEntryList(lpSaveObj->lpInstanceIds, true);
  295. }
  296. ECRESULT WSMAPIPropStorage::EcFillPropTags(struct saveObject *lpsSaveObj, MAPIOBJECT *lpsMapiObj)
  297. {
  298. for (gsoap_size_t i = 0; i < lpsSaveObj->delProps.__size; ++i)
  299. lpsMapiObj->lstAvailable.push_back(lpsSaveObj->delProps.__ptr[i]);
  300. return erSuccess;
  301. }
  302. ECRESULT WSMAPIPropStorage::EcFillPropValues(struct saveObject *lpsSaveObj, MAPIOBJECT *lpsMapiObj)
  303. {
  304. ECRESULT ec = erSuccess;
  305. convert_context context;
  306. for (gsoap_size_t i = 0; i < lpsSaveObj->modProps.__size; ++i) {
  307. ecmem_ptr<SPropValue> lpsProp;
  308. ec = ECAllocateBuffer(sizeof(SPropValue), &~lpsProp);
  309. if (ec != erSuccess)
  310. break;
  311. ec = CopySOAPPropValToMAPIPropVal(lpsProp, &lpsSaveObj->modProps.__ptr[i], lpsProp, &context);
  312. if (ec != erSuccess)
  313. break;
  314. lpsMapiObj->lstProperties.push_back(ECProperty(lpsProp));
  315. }
  316. return ec;
  317. }
  318. // sets the ulObjId from the server in the object (hierarchyid)
  319. // removes deleted sub-objects from memory
  320. // removes current list of del/mod props, and sets server changes in the lists
  321. HRESULT WSMAPIPropStorage::HrUpdateMapiObject(MAPIOBJECT *lpClientObj, struct saveObject *lpsServerObj)
  322. {
  323. lpClientObj->ulObjId = lpsServerObj->ulServerId;
  324. // The deleted properties have been deleted, so forget about them
  325. lpClientObj->lstDeleted.clear();
  326. // The modified properties have been sent. Delete them.
  327. lpClientObj->lstModified.clear();
  328. // The object is no longer 'changed'
  329. lpClientObj->bChangedInstance = false;
  330. lpClientObj->bChanged = false;
  331. // copy new server props to object (these are the properties we have received after saving the
  332. // object. For example, a new PR_LAST_MODIFICATION_TIME). These append lstProperties and lstAvailable
  333. // with the new properties.
  334. EcFillPropTags(lpsServerObj, lpClientObj);
  335. EcFillPropValues(lpsServerObj, lpClientObj);
  336. // We might have received a new instance ID from the server
  337. if (lpClientObj->lpInstanceID) {
  338. ECFreeBuffer(lpClientObj->lpInstanceID);
  339. lpClientObj->lpInstanceID = NULL;
  340. lpClientObj->cbInstanceID = 0;
  341. }
  342. /* FIXME: Support Multiple Single Instances */
  343. if (lpsServerObj->lpInstanceIds && lpsServerObj->lpInstanceIds->__size &&
  344. CopySOAPEntryIdToMAPIEntryId(&lpsServerObj->lpInstanceIds->__ptr[0], &lpClientObj->cbInstanceID, (LPENTRYID *)&lpClientObj->lpInstanceID) != hrSuccess)
  345. return MAPI_E_INVALID_PARAMETER;
  346. for (auto iterObj = lpClientObj->lstChildren.cbegin();
  347. iterObj != lpClientObj->lstChildren.cend(); ) {
  348. if ((*iterObj)->bDelete) {
  349. // this child was removed, so we don't need it anymore
  350. auto iterDel = iterObj;
  351. ++iterObj;
  352. FreeMapiObject(*iterDel);
  353. lpClientObj->lstChildren.erase(iterDel);
  354. continue;
  355. } else if (!(*iterObj)->bChanged) {
  356. // this was never sent to the server, so it is not going to be in the server object
  357. ++iterObj;
  358. continue;
  359. }
  360. // find by client id, and set server id
  361. gsoap_size_t i = 0;
  362. while (i < lpsServerObj->__size) {
  363. if ((*iterObj)->ulUniqueId == lpsServerObj->__ptr[i].ulClientId && (*iterObj)->ulObjType == lpsServerObj->__ptr[i].ulObjType)
  364. break;
  365. ++i;
  366. }
  367. if (i == lpsServerObj->__size) {
  368. // huh?
  369. assert(false);
  370. return MAPI_E_NOT_FOUND;
  371. }
  372. HrUpdateMapiObject(*iterObj, &lpsServerObj->__ptr[i]);
  373. ++iterObj;
  374. }
  375. return hrSuccess;
  376. }
  377. HRESULT WSMAPIPropStorage::HrSaveObject(ULONG ulFlags, MAPIOBJECT *lpsMapiObject)
  378. {
  379. ECRESULT er = erSuccess;
  380. HRESULT hr = hrSuccess;
  381. struct saveObject sSaveObj;
  382. struct loadObjectResponse sResponse;
  383. convert_context converter;
  384. HrMapiObjectToSoapObject(lpsMapiObject, &sSaveObj, &converter);
  385. LockSoap();
  386. // ulFlags == object flags, eg. MAPI_ASSOCIATE for messages, FOLDER_SEARCH on folders...
  387. START_SOAP_CALL
  388. {
  389. if (SOAP_OK != lpCmd->ns__saveObject(ecSessionId, m_sParentEntryId, m_sEntryId, &sSaveObj, ulFlags, m_ulSyncId, &sResponse))
  390. er = KCERR_NETWORK_ERROR;
  391. else
  392. er = sResponse.er;
  393. }
  394. if (er == KCERR_UNKNOWN_INSTANCE_ID) {
  395. /* Instance ID was unknown, we should resend entire message again, but this time include the instance body */
  396. hr = HrUpdateSoapObject(lpsMapiObject, &sSaveObj, &converter);
  397. if (hr != hrSuccess)
  398. goto exit;
  399. goto retry;
  400. }
  401. END_SOAP_CALL
  402. // Update our copy of the object with the IDs and mod props from the server
  403. hr = HrUpdateMapiObject(lpsMapiObject, &sResponse.sSaveObject);
  404. if (hr != hrSuccess)
  405. goto exit;
  406. // HrUpdateMapiObject() has now moved the modified properties of all objects recursively
  407. // into their 'normal' properties list (lstProperties). This is correct because when we now
  408. // open a subobject, it needs all the properties.
  409. // However, because we already have all properties of the top-level message in-memory via
  410. // ECGenericProps, the properties in
  411. exit:
  412. UnLockSoap();
  413. DeleteSoapObject(&sSaveObj);
  414. return hr;
  415. }
  416. ECRESULT WSMAPIPropStorage::ECSoapObjectToMapiObject(struct saveObject *lpsSaveObj, MAPIOBJECT *lpsMapiObject) {
  417. MAPIOBJECT *mo = NULL;
  418. ULONG ulAttachUniqueId = 0;
  419. ULONG ulRecipUniqueId = 0;
  420. // delProps contains all the available property tag
  421. EcFillPropTags(lpsSaveObj, lpsMapiObject);
  422. // modProps contains all the props < 8K
  423. EcFillPropValues(lpsSaveObj, lpsMapiObject);
  424. // delete stays false, unique id is set upon allocation
  425. lpsMapiObject->ulObjId = lpsSaveObj->ulServerId;
  426. lpsMapiObject->ulObjType = lpsSaveObj->ulObjType;
  427. // children
  428. for (gsoap_size_t i = 0; i < lpsSaveObj->__size; ++i) {
  429. switch (lpsSaveObj->__ptr[i].ulObjType) {
  430. case MAPI_ATTACH:
  431. AllocNewMapiObject(ulAttachUniqueId++, lpsSaveObj->__ptr[i].ulServerId, lpsSaveObj->__ptr[i].ulObjType, &mo);
  432. break;
  433. case MAPI_MAILUSER:
  434. case MAPI_DISTLIST:
  435. AllocNewMapiObject(ulRecipUniqueId++, lpsSaveObj->__ptr[i].ulServerId, lpsSaveObj->__ptr[i].ulObjType, &mo);
  436. break;
  437. default:
  438. AllocNewMapiObject(0, lpsSaveObj->__ptr[i].ulServerId, lpsSaveObj->__ptr[i].ulObjType, &mo);
  439. break;
  440. }
  441. ECSoapObjectToMapiObject(&lpsSaveObj->__ptr[i], mo);
  442. lpsMapiObject->lstChildren.insert(mo);
  443. }
  444. if (lpsMapiObject->lpInstanceID) {
  445. ECFreeBuffer(lpsMapiObject->lpInstanceID);
  446. lpsMapiObject->lpInstanceID = NULL;
  447. lpsMapiObject->cbInstanceID = 0;
  448. }
  449. /* FIXME: Support Multiple Single Instances */
  450. if (lpsSaveObj->lpInstanceIds && lpsSaveObj->lpInstanceIds->__size &&
  451. CopySOAPEntryIdToMAPIEntryId(&lpsSaveObj->lpInstanceIds->__ptr[0], &lpsMapiObject->cbInstanceID, (LPENTRYID *)&lpsMapiObject->lpInstanceID) != hrSuccess)
  452. return KCERR_INVALID_PARAMETER;
  453. return erSuccess;
  454. }
  455. // Register an advise without an extra server call
  456. HRESULT WSMAPIPropStorage::RegisterAdvise(ULONG ulEventMask, ULONG ulConnection)
  457. {
  458. m_ulConnection = ulConnection;
  459. m_ulEventMask = ulEventMask;
  460. return hrSuccess;
  461. }
  462. HRESULT WSMAPIPropStorage::GetEntryIDByRef(ULONG *lpcbEntryID, LPENTRYID *lppEntryID)
  463. {
  464. if (lpcbEntryID == NULL || lppEntryID == NULL)
  465. return MAPI_E_INVALID_PARAMETER;
  466. *lpcbEntryID = m_sEntryId.__size;
  467. *lppEntryID = (LPENTRYID)m_sEntryId.__ptr;
  468. return hrSuccess;
  469. }
  470. HRESULT WSMAPIPropStorage::HrLoadObject(MAPIOBJECT **lppsMapiObject)
  471. {
  472. ECRESULT er = erSuccess;
  473. HRESULT hr = hrSuccess;
  474. struct loadObjectResponse sResponse;
  475. MAPIOBJECT *lpsMapiObject = NULL;
  476. struct notifySubscribe sNotSubscribe{__gszeroinit};
  477. if (m_ulConnection) {
  478. // Register notification
  479. sNotSubscribe.ulConnection = m_ulConnection;
  480. sNotSubscribe.ulEventMask = m_ulEventMask;
  481. sNotSubscribe.sKey.__size = m_sEntryId.__size;
  482. sNotSubscribe.sKey.__ptr = m_sEntryId.__ptr;
  483. }
  484. LockSoap();
  485. if (!lppsMapiObject) {
  486. assert(false);
  487. er = KCERR_INVALID_PARAMETER;
  488. goto exit;
  489. }
  490. if (*lppsMapiObject) {
  491. // memleak detected
  492. assert(false);
  493. er = KCERR_INVALID_PARAMETER;
  494. goto exit;
  495. }
  496. START_SOAP_CALL
  497. {
  498. if (SOAP_OK != lpCmd->ns__loadObject(ecSessionId, m_sEntryId, ((m_ulConnection == 0) || m_bSubscribed)?NULL:&sNotSubscribe, m_ulFlags | 0x80000000, &sResponse))
  499. er = KCERR_NETWORK_ERROR;
  500. else
  501. er = sResponse.er;
  502. }
  503. //END_SOAP_CALL
  504. if (er == KCERR_END_OF_SESSION && m_lpTransport->HrReLogon() == hrSuccess)
  505. goto retry;
  506. hr = kcerr_to_mapierr(er, MAPI_E_NOT_FOUND);
  507. if (hr == MAPI_E_UNABLE_TO_COMPLETE) // Store does not exist on this server
  508. hr = MAPI_E_UNCONFIGURED; // Force a reconfigure
  509. if(hr != hrSuccess)
  510. goto exit;
  511. AllocNewMapiObject(0, 0, 0, &lpsMapiObject); // ulObjType, ulObjId and ulUniqueId are unknown here
  512. ECSoapObjectToMapiObject(&sResponse.sSaveObject, lpsMapiObject);
  513. *lppsMapiObject = lpsMapiObject;
  514. m_bSubscribed = m_ulConnection != 0;
  515. exit:
  516. UnLockSoap();
  517. return hr;
  518. }
  519. IECPropStorage* WSMAPIPropStorage::GetServerStorage() {
  520. return &this->m_xECPropStorage; // I am the server storage
  521. }
  522. //FIXME: one lock/unlock function
  523. HRESULT WSMAPIPropStorage::LockSoap()
  524. {
  525. lpDataLock.lock();
  526. return erSuccess;
  527. }
  528. HRESULT WSMAPIPropStorage::UnLockSoap()
  529. {
  530. //Clean up data create with soap_malloc
  531. if(lpCmd->soap) {
  532. soap_destroy(lpCmd->soap);
  533. soap_end(lpCmd->soap);
  534. }
  535. lpDataLock.unlock();
  536. return erSuccess;
  537. }
  538. HRESULT WSMAPIPropStorage::HrSetSyncId(ULONG ulSyncId) {
  539. m_ulSyncId = ulSyncId;
  540. return hrSuccess;
  541. }
  542. // Called when the session ID has changed
  543. HRESULT WSMAPIPropStorage::Reload(void *lpParam, ECSESSIONID sessionId) {
  544. static_cast<WSMAPIPropStorage *>(lpParam)->ecSessionId = sessionId;
  545. return hrSuccess;
  546. }
  547. // Interface IECPropStorage
  548. DEF_ULONGMETHOD0(WSMAPIPropStorage, ECPropStorage, AddRef, (void))
  549. DEF_ULONGMETHOD0(WSMAPIPropStorage, ECPropStorage, Release, (void))
  550. DEF_HRMETHOD0(WSMAPIPropStorage, ECPropStorage, QueryInterface, (REFIID, refiid), (void **, lppInterface))
  551. DEF_HRMETHOD0(WSMAPIPropStorage, ECPropStorage, HrReadProps, (LPSPropTagArray *, lppPropTags), (ULONG *, cValues), (LPSPropValue *, lppValues))
  552. DEF_HRMETHOD0(WSMAPIPropStorage, ECPropStorage, HrLoadProp, (ULONG, ulObjId), (ULONG, ulPropTag), (LPSPropValue *, lppsPropValue))
  553. DEF_HRMETHOD0(WSMAPIPropStorage, ECPropStorage, HrWriteProps, (ULONG, cValues), (LPSPropValue, lpValues), (ULONG, ulFlags))
  554. DEF_HRMETHOD0(WSMAPIPropStorage, ECPropStorage, HrDeleteProps, (const SPropTagArray *, lpsPropTagArray))
  555. DEF_HRMETHOD0(WSMAPIPropStorage, ECPropStorage, HrSaveObject, (ULONG, ulFlags), (MAPIOBJECT *, lpsMapiObject))
  556. DEF_HRMETHOD0(WSMAPIPropStorage, ECPropStorage, HrLoadObject, (MAPIOBJECT **, lppsMapiObject))
  557. IECPropStorage* WSMAPIPropStorage::xECPropStorage::GetServerStorage()
  558. {
  559. METHOD_PROLOGUE_(WSMAPIPropStorage, ECPropStorage);
  560. return pThis->GetServerStorage();
  561. }