PublishFreeBusy.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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 <utility>
  19. #include <kopano/ECRestriction.h>
  20. #include "PublishFreeBusy.h"
  21. #include <kopano/namedprops.h>
  22. #include <kopano/mapiguidext.h>
  23. #include <iostream>
  24. #include <memory>
  25. #include <vector>
  26. #include <algorithm>
  27. #include <kopano/memory.hpp>
  28. #include <kopano/ECLogger.h>
  29. #include "recurrence.h"
  30. #include <kopano/MAPIErrors.h>
  31. #include "ECFreeBusyUpdate.h"
  32. #include "freebusyutil.h"
  33. #include "ECFreeBusySupport.h"
  34. #include <mapiutil.h>
  35. using namespace std;
  36. using namespace KCHL;
  37. namespace KC {
  38. #define START_TIME 0
  39. #define END_TIME 1
  40. /**
  41. * Publish free/busy information from the default calendar
  42. *
  43. * @param[in] lpSession Session object of user
  44. * @param[in] lpDefStore Store of user
  45. * @param[in] tsStart Start time to publish data of
  46. * @param[in] ulMonths Number of months to publish
  47. *
  48. * @return MAPI Error code
  49. */
  50. HRESULT HrPublishDefaultCalendar(IMAPISession *lpSession, IMsgStore *lpDefStore,
  51. time_t tsStart, ULONG ulMonths)
  52. {
  53. HRESULT hr = hrSuccess;
  54. std::unique_ptr<PublishFreeBusy> lpFreeBusy;
  55. object_ptr<IMAPITable> lpTable;
  56. memory_ptr<FBBlock_1> lpFBblocks;
  57. ULONG cValues = 0;
  58. ec_log_debug("current time %d", (int)tsStart);
  59. lpFreeBusy.reset(new PublishFreeBusy(lpSession, lpDefStore, tsStart, ulMonths));
  60. hr = lpFreeBusy->HrInit();
  61. if (hr != hrSuccess)
  62. return hr;
  63. hr = lpFreeBusy->HrGetResctItems(&~lpTable);
  64. if (hr != hrSuccess) {
  65. ec_log_info("Error while finding messages for free/busy publish, error code: 0x%x %s", hr, GetMAPIErrorMessage(hr));
  66. return hr;
  67. }
  68. hr = lpFreeBusy->HrProcessTable(lpTable, &~lpFBblocks, &cValues);
  69. if(hr != hrSuccess) {
  70. ec_log_info("Error while finding free/busy blocks, error code: 0x%x %s", hr, GetMAPIErrorMessage(hr));
  71. return hr;
  72. }
  73. if (cValues == 0) {
  74. ec_log_info("No messages for free/busy publish");
  75. return hr;
  76. }
  77. hr = lpFreeBusy->HrMergeBlocks(&+lpFBblocks, &cValues);
  78. if(hr != hrSuccess) {
  79. ec_log_info("Error while merging free/busy blocks, entries: %d, error code: 0x%x %s", cValues, hr, GetMAPIErrorMessage(hr));
  80. return hr;
  81. }
  82. ec_log_debug("Publishing %d free/busy blocks", cValues);
  83. hr = lpFreeBusy->HrPublishFBblocks(lpFBblocks, cValues);
  84. if (hr != hrSuccess)
  85. ec_log_info("Error while publishing free/busy blocks, entries: %d, error code: 0x%x %s", cValues, hr, GetMAPIErrorMessage(hr));
  86. return hr;
  87. }
  88. /**
  89. * Class handling free/busy publishing.
  90. * @todo validate input time & months.
  91. *
  92. * @param[in] lpSession
  93. * @param[in] lpDefStore
  94. * @param[in] tsStart
  95. * @param[in] ulMonths
  96. */
  97. PublishFreeBusy::PublishFreeBusy(IMAPISession *lpSession, IMsgStore *lpDefStore,
  98. time_t tsStart, ULONG ulMonths) :
  99. __propmap(7)
  100. {
  101. m_lpSession = lpSession;
  102. m_lpDefStore = lpDefStore;
  103. m_tsStart = tsStart;
  104. m_tsEnd = tsStart + (ulMonths * (30*24*60*60));
  105. UnixTimeToFileTime(m_tsStart, &m_ftStart);
  106. UnixTimeToFileTime(m_tsEnd , &m_ftEnd);
  107. }
  108. /**
  109. * Initialize object. Get named properties required for publishing
  110. * free/busy data.
  111. *
  112. * @return MAPI Error code
  113. */
  114. HRESULT PublishFreeBusy::HrInit()
  115. {
  116. HRESULT hr = hrSuccess;
  117. PROPMAP_INIT_NAMED_ID (APPT_STARTWHOLE, PT_SYSTIME, PSETID_Appointment, dispidApptStartWhole)
  118. PROPMAP_INIT_NAMED_ID (APPT_ENDWHOLE, PT_SYSTIME, PSETID_Appointment, dispidApptEndWhole)
  119. PROPMAP_INIT_NAMED_ID (APPT_CLIPEND, PT_SYSTIME, PSETID_Appointment, dispidClipEnd)
  120. PROPMAP_INIT_NAMED_ID (APPT_ISRECURRING, PT_BOOLEAN, PSETID_Appointment, dispidRecurring)
  121. PROPMAP_INIT_NAMED_ID (APPT_FBSTATUS, PT_LONG, PSETID_Appointment, dispidBusyStatus)
  122. PROPMAP_INIT_NAMED_ID (APPT_RECURRINGSTATE, PT_BINARY, PSETID_Appointment, dispidRecurrenceState)
  123. PROPMAP_INIT_NAMED_ID (APPT_TIMEZONESTRUCT, PT_BINARY, PSETID_Appointment, dispidTimeZoneData)
  124. PROPMAP_INIT (m_lpDefStore)
  125. ;
  126. exitpm:
  127. return hr;
  128. }
  129. /**
  130. * Create a contents table with items to be published for free/busy
  131. * times.
  132. *
  133. * @todo rename function
  134. * @todo use restriction class to create restrictions
  135. *
  136. * @param[out] lppTable Default calendar contents table
  137. *
  138. * @return MAPI Error code
  139. */
  140. HRESULT PublishFreeBusy::HrGetResctItems(IMAPITable **lppTable)
  141. {
  142. HRESULT hr = hrSuccess;
  143. object_ptr<IMAPIFolder> lpDefCalendar;
  144. object_ptr<IMAPITable> lpTable;
  145. SPropValue lpsPropStart;
  146. SPropValue lpsPropEnd;
  147. SPropValue lpsPropIsRecc;
  148. SPropValue lpsPropReccEnd;
  149. hr = HrOpenDefaultCalendar(m_lpDefStore, &~lpDefCalendar);
  150. if(hr != hrSuccess)
  151. return hr;
  152. hr = lpDefCalendar->GetContentsTable(0, &~lpTable);
  153. if(hr != hrSuccess)
  154. return hr;
  155. lpsPropStart.ulPropTag = PROP_APPT_STARTWHOLE;
  156. lpsPropStart.Value.ft = m_ftStart;
  157. lpsPropEnd.ulPropTag = PROP_APPT_ENDWHOLE;
  158. lpsPropEnd.Value.ft = m_ftEnd;
  159. lpsPropIsRecc.ulPropTag = PROP_APPT_ISRECURRING;
  160. lpsPropIsRecc.Value.b = true;
  161. lpsPropReccEnd.ulPropTag = PROP_APPT_CLIPEND;
  162. lpsPropReccEnd.Value.ft = m_ftStart;
  163. hr = ECOrRestriction(
  164. //ITEM[START] >= START && ITEM[START] <= END;
  165. ECAndRestriction(
  166. ECPropertyRestriction(RELOP_GE, PROP_APPT_STARTWHOLE, &lpsPropStart, ECRestriction::Cheap) + // item[start]
  167. ECPropertyRestriction(RELOP_LE, PROP_APPT_STARTWHOLE, &lpsPropEnd, ECRestriction::Cheap) // item[start]
  168. ) +
  169. //ITEM[END] >= START && ITEM[END] <= END;
  170. ECAndRestriction(
  171. ECPropertyRestriction(RELOP_GE, PROP_APPT_ENDWHOLE, &lpsPropStart, ECRestriction::Cheap) + // item[end]
  172. ECPropertyRestriction(RELOP_LE, PROP_APPT_ENDWHOLE, &lpsPropEnd, ECRestriction::Cheap) // item[end]
  173. ) +
  174. //ITEM[START] < START && ITEM[END] > END;
  175. ECAndRestriction(
  176. ECPropertyRestriction(RELOP_LT, PROP_APPT_STARTWHOLE, &lpsPropStart, ECRestriction::Cheap) + // item[start]
  177. ECPropertyRestriction(RELOP_GT, PROP_APPT_ENDWHOLE, &lpsPropEnd, ECRestriction::Cheap) // item[end]
  178. ) +
  179. ECAndRestriction(
  180. ECExistRestriction(PROP_APPT_CLIPEND) +
  181. ECPropertyRestriction(RELOP_EQ, PROP_APPT_ISRECURRING, &lpsPropIsRecc, ECRestriction::Cheap) +
  182. ECPropertyRestriction(RELOP_GE, PROP_APPT_CLIPEND, &lpsPropReccEnd, ECRestriction::Cheap)
  183. ) +
  184. ECAndRestriction(
  185. ECNotRestriction(ECExistRestriction(PROP_APPT_CLIPEND)) +
  186. ECPropertyRestriction(RELOP_LE, PROP_APPT_STARTWHOLE, &lpsPropEnd, ECRestriction::Cheap) +
  187. ECPropertyRestriction(RELOP_EQ, PROP_APPT_ISRECURRING, &lpsPropIsRecc, ECRestriction::Cheap)
  188. )
  189. ).RestrictTable(lpTable);
  190. if (hr != hrSuccess)
  191. return hr;
  192. *lppTable = lpTable.release();
  193. return hrSuccess;
  194. }
  195. /**
  196. * Calculates the freebusy blocks from the rows of the table.
  197. * It also adds the occurrences of the recurrence in the array of blocks.
  198. *
  199. * @param[in] lpTable restricted mapi table containing the rows
  200. * @param[out] lppfbBlocks array of freebusy blocks
  201. * @param[out] lpcValues number of freebusy blocks in lppfbBlocks array
  202. *
  203. * @return MAPI Error code
  204. */
  205. HRESULT PublishFreeBusy::HrProcessTable(IMAPITable *lpTable, FBBlock_1 **lppfbBlocks, ULONG *lpcValues)
  206. {
  207. HRESULT hr = hrSuccess;
  208. memory_ptr<OccrInfo> lpOccrInfo;
  209. FBBlock_1 *lpfbBlocks = NULL;
  210. recurrence lpRecurrence;
  211. ULONG ulFbStatus = 0;
  212. SizedSPropTagArray(7, proptags) =
  213. {7, {PROP_APPT_STARTWHOLE, PROP_APPT_ENDWHOLE,
  214. PROP_APPT_FBSTATUS, PROP_APPT_ISRECURRING,
  215. PROP_APPT_RECURRINGSTATE, PROP_APPT_CLIPEND,
  216. PROP_APPT_TIMEZONESTRUCT}};
  217. hr = lpTable->SetColumns(proptags, 0);
  218. if(hr != hrSuccess)
  219. return hr;
  220. while (true)
  221. {
  222. rowset_ptr lpRowSet;
  223. hr = lpTable->QueryRows(50, 0, &~lpRowSet);
  224. if(hr != hrSuccess)
  225. return hr;
  226. if(lpRowSet->cRows == 0)
  227. break;
  228. for (ULONG i = 0; i < lpRowSet->cRows; ++i) {
  229. TIMEZONE_STRUCT ttzInfo = {0};
  230. ulFbStatus = 0;
  231. if (lpRowSet->aRow[i].lpProps[3].ulPropTag != PROP_APPT_ISRECURRING ||
  232. !lpRowSet->aRow[i].lpProps[3].Value.b)
  233. {
  234. OccrInfo sOccrBlock;
  235. if (lpRowSet->aRow[i].lpProps[0].ulPropTag == PROP_APPT_STARTWHOLE)
  236. FileTimeToRTime(&lpRowSet->aRow[i].lpProps[0].Value.ft, &sOccrBlock.fbBlock.m_tmStart);
  237. if (lpRowSet->aRow[i].lpProps[1].ulPropTag == PROP_APPT_ENDWHOLE) {
  238. FileTimeToRTime(&lpRowSet->aRow[i].lpProps[1].Value.ft, &sOccrBlock.fbBlock.m_tmEnd);
  239. FileTimeToUnixTime(lpRowSet->aRow[i].lpProps[1].Value.ft, &sOccrBlock.tBaseDate);
  240. }
  241. if (lpRowSet->aRow[i].lpProps[2].ulPropTag == PROP_APPT_FBSTATUS)
  242. sOccrBlock.fbBlock.m_fbstatus = (FBStatus)lpRowSet->aRow[i].lpProps[2].Value.ul;
  243. hr = HrAddFBBlock(sOccrBlock, &+lpOccrInfo, lpcValues);
  244. if (hr != hrSuccess) {
  245. ec_log_debug("Error adding occurrence block to list, error code: 0x%x %s", hr, GetMAPIErrorMessage(hr));
  246. return hr;
  247. }
  248. continue;
  249. }
  250. if (lpRowSet->aRow[i].lpProps[4].ulPropTag != PROP_APPT_RECURRINGSTATE)
  251. continue;
  252. hr = lpRecurrence.HrLoadRecurrenceState((char *)(lpRowSet->aRow[i].lpProps[4].Value.bin.lpb),lpRowSet->aRow[i].lpProps[4].Value.bin.cb, 0);
  253. if (FAILED(hr)) {
  254. ec_log_err("Error loading recurrence state, error code: 0x%x %s", hr, GetMAPIErrorMessage(hr));
  255. continue;
  256. }
  257. if (lpRowSet->aRow[i].lpProps[6].ulPropTag == PROP_APPT_TIMEZONESTRUCT)
  258. ttzInfo = *(TIMEZONE_STRUCT*)lpRowSet->aRow[i].lpProps[6].Value.bin.lpb;
  259. if (lpRowSet->aRow[i].lpProps[2].ulPropTag == PROP_APPT_FBSTATUS)
  260. ulFbStatus = lpRowSet->aRow[i].lpProps[2].Value.ul;
  261. hr = lpRecurrence.HrGetItems(m_tsStart, m_tsEnd, ttzInfo, ulFbStatus, &+lpOccrInfo, lpcValues);
  262. if (hr != hrSuccess || !lpOccrInfo) {
  263. ec_log_err("Error expanding items for recurring item, error code: 0x%x %s", hr, GetMAPIErrorMessage(hr));
  264. continue;
  265. }
  266. }
  267. }
  268. if (lpcValues != 0 && lpOccrInfo != NULL) {
  269. hr = MAPIAllocateBuffer(sizeof(FBBlock_1)* (*lpcValues), (void**)&lpfbBlocks);
  270. if(hr != hrSuccess)
  271. return hr;
  272. for (ULONG i = 0 ; i < *lpcValues; ++i)
  273. lpfbBlocks[i] = lpOccrInfo[i].fbBlock;
  274. *lppfbBlocks = lpfbBlocks;
  275. lpfbBlocks = NULL;
  276. }
  277. return hrSuccess;
  278. }
  279. /**
  280. * Merge overlapping free/busy blocks.
  281. *
  282. * @param[in,out] lppfbBlocks In: generated blocks, Out: merged blocks
  283. * @param[in,out] lpcValues Number of blocks in lppfbBlocks
  284. *
  285. * @return MAPI Error code
  286. */
  287. HRESULT PublishFreeBusy::HrMergeBlocks(FBBlock_1 **lppfbBlocks, ULONG *lpcValues)
  288. {
  289. FBBlock_1 *lpFbBlocks = NULL;
  290. ULONG cValues = *lpcValues;
  291. ULONG ulLevel = 0;
  292. time_t tsLastTime = 0;
  293. TSARRAY sTsitem = {0,0,0};
  294. std::map<time_t , TSARRAY> mpTimestamps;
  295. std::vector <ULONG> vctStatus;
  296. std::vector <FBBlock_1> vcFBblocks;
  297. time_t tTemp = 0;
  298. ec_log_debug("Input blocks %ul", cValues);
  299. lpFbBlocks = *lppfbBlocks;
  300. for (ULONG i = 0; i < cValues; ++i) {
  301. sTsitem.ulType = START_TIME;
  302. sTsitem.ulStatus = lpFbBlocks[i].m_fbstatus;
  303. sTsitem.tsTime = lpFbBlocks[i].m_tmStart;
  304. RTimeToUnixTime(sTsitem.tsTime, &tTemp);
  305. // @note ctime adds \n character
  306. ec_log_debug("Blocks start %s", ctime(&tTemp));
  307. mpTimestamps[sTsitem.tsTime] = sTsitem;
  308. sTsitem.ulType = END_TIME;
  309. sTsitem.ulStatus = lpFbBlocks[i].m_fbstatus;
  310. sTsitem.tsTime = lpFbBlocks[i].m_tmEnd;
  311. mpTimestamps[sTsitem.tsTime] = sTsitem;
  312. }
  313. for (const auto &pts : mpTimestamps) {
  314. FBBlock_1 fbBlockTemp;
  315. sTsitem = pts.second;
  316. switch(sTsitem.ulType)
  317. {
  318. case START_TIME:
  319. if (ulLevel != 0 && tsLastTime != sTsitem.tsTime)
  320. {
  321. std::sort(vctStatus.begin(),vctStatus.end());
  322. fbBlockTemp.m_tmStart = tsLastTime;
  323. fbBlockTemp.m_tmEnd = sTsitem.tsTime;
  324. fbBlockTemp.m_fbstatus = (enum FBStatus)(vctStatus.size()> 0 ? vctStatus.back(): 0);// sort it to get max of status
  325. if(fbBlockTemp.m_fbstatus != 0)
  326. vcFBblocks.push_back(std::move(fbBlockTemp));
  327. }
  328. ++ulLevel;
  329. vctStatus.push_back(sTsitem.ulStatus);
  330. tsLastTime = sTsitem.tsTime;
  331. break;
  332. case END_TIME:
  333. if(tsLastTime != sTsitem.tsTime)
  334. {
  335. std::sort(vctStatus.begin(),vctStatus.end());// sort it to get max of status
  336. fbBlockTemp.m_tmStart = tsLastTime;
  337. fbBlockTemp.m_tmEnd = sTsitem.tsTime;
  338. fbBlockTemp.m_fbstatus = (enum FBStatus)(vctStatus.size()> 0 ? vctStatus.back(): 0);
  339. if(fbBlockTemp.m_fbstatus != 0)
  340. vcFBblocks.push_back(std::move(fbBlockTemp));
  341. }
  342. --ulLevel;
  343. if(!vctStatus.empty()){
  344. auto iterStatus = std::find(vctStatus.begin(), vctStatus.end(), sTsitem.ulStatus);
  345. if (iterStatus != vctStatus.end())
  346. vctStatus.erase(iterStatus);
  347. }
  348. tsLastTime = sTsitem.tsTime;
  349. break;
  350. }
  351. }
  352. // Free previously allocated memory
  353. if (*lppfbBlocks != NULL) {
  354. MAPIFreeBuffer(*lppfbBlocks);
  355. *lppfbBlocks = NULL;
  356. }
  357. HRESULT hr = MAPIAllocateBuffer(sizeof(FBBlock_1) * vcFBblocks.size(),
  358. reinterpret_cast<void **>(&lpFbBlocks));
  359. if (hr != hrSuccess)
  360. return hr;
  361. ULONG i = 0;
  362. for (const auto &vcblock : vcFBblocks)
  363. lpFbBlocks[i++] = vcblock;
  364. *lppfbBlocks = lpFbBlocks;
  365. *lpcValues = vcFBblocks.size();
  366. ec_log_debug("Output blocks %d", *lpcValues);
  367. return hrSuccess;
  368. }
  369. /**
  370. * Save free/busy blocks in public for current user
  371. *
  372. * @param[in] lpfbBlocks new blocks to publish
  373. * @param[in] cValues number of blocks to publish
  374. *
  375. * @return MAPI Error code
  376. */
  377. HRESULT PublishFreeBusy::HrPublishFBblocks(FBBlock_1 *lpfbBlocks, ULONG cValues)
  378. {
  379. HRESULT hr = hrSuccess;
  380. object_ptr<ECFreeBusyUpdate> lpFBUpdate;
  381. object_ptr<IMessage> lpMessage;
  382. object_ptr<IMsgStore> lpPubStore;
  383. memory_ptr<SPropValue> lpsPrpUsrMEid;
  384. time_t tsStart = 0;
  385. hr = HrOpenECPublicStore(m_lpSession, &~lpPubStore);
  386. if(hr != hrSuccess)
  387. return hr;
  388. hr = HrGetOneProp(m_lpDefStore, PR_MAILBOX_OWNER_ENTRYID, &~lpsPrpUsrMEid);
  389. if(hr != hrSuccess)
  390. return hr;
  391. hr = GetFreeBusyMessage(m_lpSession, lpPubStore, m_lpDefStore, lpsPrpUsrMEid[0].Value.bin.cb, reinterpret_cast<ENTRYID *>(lpsPrpUsrMEid[0].Value.bin.lpb), true, &~lpMessage);
  392. if(hr != hrSuccess)
  393. return hr;
  394. hr = ECFreeBusyUpdate::Create(lpMessage, &~lpFBUpdate);
  395. if(hr != hrSuccess)
  396. return hr;
  397. hr = lpFBUpdate->ResetPublishedFreeBusy();
  398. if(hr != hrSuccess)
  399. return hr;
  400. hr = lpFBUpdate->PublishFreeBusy(lpfbBlocks, cValues);
  401. if(hr != hrSuccess)
  402. return hr;
  403. FileTimeToUnixTime(m_ftStart, &tsStart);
  404. // @todo use a "start of day" function?
  405. tsStart = tsStart - 86400; // 24*60*60 = 86400 include current day.
  406. UnixTimeToFileTime(tsStart, &m_ftStart);
  407. return lpFBUpdate->SaveChanges(m_ftStart, m_ftEnd);
  408. }
  409. } /* namespace */