MAPIToICal.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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/zcdefs.h>
  18. #include <kopano/platform.h>
  19. #include <memory>
  20. #include <new>
  21. #include <kopano/memory.hpp>
  22. #include "MAPIToICal.h"
  23. #include <libical/ical.h>
  24. #include "nameids.h"
  25. #include "vconverter.h"
  26. #include "vevent.h"
  27. #include "vtodo.h"
  28. #include "vtimezone.h"
  29. #include "vfreebusy.h"
  30. #include <mapiutil.h>
  31. #include <mapicode.h>
  32. #include <mapix.h>
  33. #include <kopano/ecversion.h>
  34. #include "icalmem.hpp"
  35. namespace KC {
  36. class MapiToICalImpl _kc_final : public MapiToICal {
  37. public:
  38. MapiToICalImpl(LPADRBOOK lpAdrBook, const std::string &strCharset);
  39. virtual ~MapiToICalImpl();
  40. HRESULT AddMessage(LPMESSAGE lpMessage, const std::string &strSrvTZ, ULONG ulFlags) _kc_override;
  41. HRESULT AddBlocks(FBBlock_1 *pblk, LONG ulblocks, time_t tStart, time_t tEnd, const std::string &strOrganiser, const std::string &strUser, const std::string &strUID) _kc_override;
  42. HRESULT Finalize(ULONG ulFlags, std::string *strMethod, std::string *strIcal) _kc_override;
  43. HRESULT ResetObject(void) _kc_override;
  44. private:
  45. LPADRBOOK m_lpAdrBook;
  46. std::string m_strCharset;
  47. SPropTagArray *m_lpNamedProps = nullptr;
  48. /* since we don't want depending projects to add include paths for libical, this is only in the implementation version */
  49. icalcomponent *m_lpicCalender = nullptr;
  50. icalproperty_method m_icMethod = ICAL_METHOD_NONE;
  51. timezone_map m_tzMap; // contains all used timezones
  52. ULONG m_ulEvents = 0;
  53. HRESULT HrInitializeVCal();
  54. };
  55. /**
  56. * Create a class implementing the MapiToICal "interface".
  57. *
  58. * @param[in] lpAdrBook MAPI addressbook
  59. * @param[in] strCharset charset of the ical returned by this class
  60. * @param[out] lppMapiToICal The conversion class
  61. */
  62. HRESULT CreateMapiToICal(LPADRBOOK lpAdrBook, const std::string &strCharset, MapiToICal **lppMapiToICal)
  63. {
  64. if (!lpAdrBook || !lppMapiToICal)
  65. return MAPI_E_INVALID_PARAMETER;
  66. *lppMapiToICal = new(std::nothrow) MapiToICalImpl(lpAdrBook, strCharset);
  67. if (*lppMapiToICal == nullptr)
  68. return MAPI_E_NOT_ENOUGH_MEMORY;
  69. return hrSuccess;
  70. }
  71. /**
  72. * Init MapiToICal class
  73. *
  74. * @param[in] lpAdrBook MAPI addressbook
  75. * @param[in] strCharset charset of the ical returned by this class
  76. */
  77. MapiToICalImpl::MapiToICalImpl(LPADRBOOK lpAdrBook,
  78. const std::string &strCharset) :
  79. m_lpAdrBook(lpAdrBook), m_strCharset(strCharset)
  80. {
  81. // initialize empty ical data
  82. HrInitializeVCal();
  83. }
  84. /**
  85. * Frees all used memory in conversion
  86. *
  87. */
  88. MapiToICalImpl::~MapiToICalImpl()
  89. {
  90. if (m_lpicCalender)
  91. icalcomponent_free(m_lpicCalender);
  92. MAPIFreeBuffer(m_lpNamedProps);
  93. }
  94. /**
  95. * Initialize ical component with basic ical info
  96. */
  97. HRESULT MapiToICalImpl::HrInitializeVCal()
  98. {
  99. m_lpicCalender = icalcomponent_new(ICAL_VCALENDAR_COMPONENT);
  100. icalcomponent_add_property(m_lpicCalender, icalproperty_new_version("2.0"));
  101. icalcomponent_add_property(m_lpicCalender, icalproperty_new_prodid("-//Kopano//" PROJECT_VERSION_DOT_STR "-" PROJECT_SVN_REV_STR "//EN"));
  102. icalcomponent_add_property(m_lpicCalender, icalproperty_new_calscale("GREGORIAN"));
  103. return hrSuccess;
  104. }
  105. /**
  106. * Add a MAPI message to the ical VCALENDAR object.
  107. *
  108. * @param[in] lpMessage Convert this MAPI message to ICal
  109. * @param[in] strSrvTZ Use this timezone, used for Tasks only (or so it seems)
  110. * @param[in] ulFlags Conversion flags:
  111. * @arg @c M2IC_CENSOR_PRIVATE Privacy sensitive data will not be present in the ICal
  112. *
  113. * @return MAPI error code
  114. */
  115. HRESULT MapiToICalImpl::AddMessage(LPMESSAGE lpMessage, const std::string &strSrvTZ, ULONG ulFlags)
  116. {
  117. HRESULT hr = hrSuccess;
  118. std::unique_ptr<VConverter> lpVEC;
  119. std::list<icalcomponent*> lstEvents;
  120. icalproperty_method icMethod = ICAL_METHOD_NONE;
  121. KCHL::memory_ptr<SPropValue> lpMessageClass;
  122. TIMEZONE_STRUCT ttTZinfo = {0};
  123. bool blCensor = false;
  124. if(ulFlags & M2IC_CENSOR_PRIVATE)
  125. blCensor = true;
  126. if (lpMessage == nullptr)
  127. return MAPI_E_INVALID_PARAMETER;
  128. if (m_lpNamedProps == NULL) {
  129. hr = HrLookupNames(lpMessage, &m_lpNamedProps);
  130. if (hr != hrSuccess)
  131. return hr;
  132. }
  133. hr = HrGetOneProp(lpMessage, PR_MESSAGE_CLASS_A, &~lpMessageClass);
  134. if (hr != hrSuccess)
  135. return hr;
  136. if (strcasecmp(lpMessageClass->Value.lpszA, "IPM.Task") == 0) {
  137. hr = HrGetTzStruct(strSrvTZ, &ttTZinfo);
  138. if(hr == hrSuccess)
  139. m_tzMap[strSrvTZ] = ttTZinfo; // keep timezone pointer for tasks
  140. }
  141. if (strcasecmp(lpMessageClass->Value.lpszA, "IPM.Task") == 0)
  142. lpVEC.reset(new VTodoConverter(m_lpAdrBook, &m_tzMap, m_lpNamedProps, m_strCharset, blCensor, false, NULL));
  143. else if (strcasecmp(lpMessageClass->Value.lpszA, "IPM.Appointment") == 0 || strncasecmp(lpMessageClass->Value.lpszA, "IPM.Schedule", strlen("IPM.Schedule")) == 0)
  144. lpVEC.reset(new VEventConverter(m_lpAdrBook, &m_tzMap, m_lpNamedProps, m_strCharset, blCensor, false, NULL));
  145. else
  146. return MAPI_E_TYPE_NO_SUPPORT;
  147. // converts item to ical item (eg. IPM.Appointment to VEVENT)
  148. hr = lpVEC->HrMAPI2ICal(lpMessage, &icMethod, &lstEvents);
  149. if (hr != hrSuccess)
  150. return hr;
  151. for (auto ev : lstEvents) {
  152. ++m_ulEvents;
  153. icalcomponent_add_component(m_lpicCalender, ev);
  154. }
  155. if (m_icMethod != ICAL_METHOD_NONE && m_icMethod != icMethod)
  156. m_icMethod = ICAL_METHOD_PUBLISH;
  157. else
  158. m_icMethod = icMethod;
  159. return hrSuccess;
  160. }
  161. /**
  162. * Add MAPI freebusy blocks in a VFREEBUSY part.
  163. *
  164. * @param[in] lpsFbblk MAPI freebusy blocks
  165. * @param[in] ulBlocks Number of blocks present in lpsFbblk
  166. * @param[in] tStart Start time of the freebusy data
  167. * @param[in] tEnd End time of the freebusy data
  168. * @param[in] strOrganiser Email address of the organiser
  169. * @param[in] strUser Email address of an attendee
  170. * @param[in] strUID UID for the VFREEBUSY part
  171. *
  172. * @return MAPI error code
  173. */
  174. HRESULT MapiToICalImpl::AddBlocks(FBBlock_1 *lpsFbblk, LONG ulBlocks, time_t tStart, time_t tEnd, const std::string &strOrganiser, const std::string &strUser, const std::string &strUID)
  175. {
  176. icalcomponent *icFbComponent = NULL;
  177. if (m_lpicCalender == NULL) {
  178. m_lpicCalender = icalcomponent_new(ICAL_VCALENDAR_COMPONENT);
  179. icalcomponent_add_property(m_lpicCalender, icalproperty_new_version("2.0"));
  180. icalcomponent_add_property(m_lpicCalender, icalproperty_new_prodid("-//Kopano//" PROJECT_VERSION_DOT_STR "-" PROJECT_SVN_REV_STR "//EN"));
  181. }
  182. HRESULT hr = HrFbBlock2ICal(lpsFbblk, ulBlocks, tStart, tEnd,
  183. strOrganiser, strUser, strUID, &icFbComponent);
  184. if (hr != hrSuccess)
  185. return hr;
  186. m_icMethod = ICAL_METHOD_PUBLISH;
  187. icalcomponent_add_component(m_lpicCalender,icFbComponent);
  188. return hrSuccess;
  189. }
  190. /**
  191. * Create the actual ICal data.
  192. *
  193. * @param[in] ulFlags Conversion flags
  194. * @arg @c M2IC_NO_VTIMEZONE Skip the VTIMEZONE parts in the output
  195. * @param[out] strMethod ICal method (eg. PUBLISH)
  196. * @param[out] strIcal The ICal data in 8-bit string, charset given in constructor
  197. *
  198. * @return MAPI error code
  199. */
  200. HRESULT MapiToICalImpl::Finalize(ULONG ulFlags, std::string *strMethod, std::string *strIcal)
  201. {
  202. HRESULT hr = hrSuccess;
  203. icalmem_ptr ics;
  204. icalcomponent *lpVTZComp = NULL;
  205. if (strMethod == nullptr && strIcal == nullptr)
  206. return MAPI_E_INVALID_PARAMETER;
  207. // TODO: make flags force a publish method
  208. if (m_icMethod != ICAL_METHOD_NONE)
  209. icalcomponent_add_property(m_lpicCalender, icalproperty_new_method(m_icMethod));
  210. // no timezone block in VFREEBUSY data.
  211. if ((ulFlags & M2IC_NO_VTIMEZONE) == 0)
  212. {
  213. for (auto &tzp : m_tzMap) {
  214. hr = HrCreateVTimeZone(tzp.first, tzp.second, &lpVTZComp);
  215. if (hr == hrSuccess)
  216. icalcomponent_add_component(m_lpicCalender, lpVTZComp);
  217. }
  218. hr = hrSuccess;
  219. }
  220. ics.reset(icalcomponent_as_ical_string_r(m_lpicCalender));
  221. if (ics == nullptr)
  222. return MAPI_E_CALL_FAILED;
  223. if (strMethod)
  224. *strMethod = icalproperty_method_to_string(m_icMethod);
  225. if (strIcal)
  226. *strIcal = ics.get();
  227. return hr;
  228. }
  229. /**
  230. * Reset this class to be used for starting a new series of conversions.
  231. *
  232. * @return always hrSuccess
  233. */
  234. HRESULT MapiToICalImpl::ResetObject()
  235. {
  236. // no need to remove named properties
  237. if (m_lpicCalender)
  238. icalcomponent_free(m_lpicCalender);
  239. m_lpicCalender = NULL;
  240. m_icMethod = ICAL_METHOD_NONE;
  241. m_tzMap.clear();
  242. m_ulEvents = 0;
  243. // reset the ical data with emtpy calendar
  244. HrInitializeVCal();
  245. return hrSuccess;
  246. }
  247. } /* namespace */