vcftomapi.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright 2017 - Kopano 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 <kopano/zcdefs.h>
  17. #include <algorithm>
  18. #include <memory>
  19. #include <new>
  20. #include <kopano/ECRestriction.h>
  21. #include <kopano/charset/convert.h>
  22. #include <kopano/mapiext.h>
  23. #include <kopano/mapiguidext.h>
  24. #include <kopano/memory.hpp>
  25. #include <kopano/namedprops.h>
  26. #include <kopano/platform.h>
  27. #include <kopano/stringutil.h>
  28. #include <libical/vcc.h>
  29. #include <libical/vobject.h>
  30. #include <mapi.h>
  31. #include <mapiutil.h>
  32. #include <mapix.h>
  33. #include <vector>
  34. #include "vcftomapi.hpp"
  35. using namespace KCHL;
  36. namespace KC {
  37. class vcftomapi_impl _kc_final : public vcftomapi {
  38. public:
  39. /*
  40. * - lpPropObj to lookup named properties
  41. * - Addressbook (Global AddressBook for looking up users)
  42. */
  43. vcftomapi_impl(IMAPIProp *o) : vcftomapi(o) {}
  44. HRESULT parse_vcf(const std::string &) _kc_override;
  45. HRESULT get_item(IMessage *) _kc_override;
  46. private:
  47. HRESULT save_props(const std::list<SPropValue> &, IMAPIProp *);
  48. HRESULT handle_N(VObject *);
  49. HRESULT handle_TEL_EMAIL(VObject *);
  50. HRESULT vobject_to_prop(VObject *, SPropValue &, ULONG proptype);
  51. HRESULT vobject_to_named_prop(VObject *, SPropValue &, ULONG named_proptype);
  52. HRESULT unicode_to_named_prop(const wchar_t *, SPropValue &, ULONG named_proptype);
  53. };
  54. /**
  55. * Create a class implementing the ICalToMapi "interface".
  56. */
  57. HRESULT create_vcftomapi(IMAPIProp *prop, vcftomapi **ret)
  58. {
  59. if (prop == nullptr || ret == nullptr)
  60. return MAPI_E_INVALID_PARAMETER;
  61. *ret = new(std::nothrow) vcftomapi_impl(prop);
  62. return *ret != nullptr ? hrSuccess : MAPI_E_NOT_ENOUGH_MEMORY;
  63. }
  64. HRESULT vcftomapi_impl::handle_N(VObject *v)
  65. {
  66. VObjectIterator tt;
  67. for (initPropIterator(&tt, v); moreIteration(&tt); ) {
  68. auto vv = nextVObject(&tt);
  69. auto name = vObjectName(vv);
  70. SPropValue s;
  71. if (strcmp(name, VCFamilyNameProp) == 0) {
  72. auto ret = vobject_to_prop(vv, s, PR_SURNAME);
  73. if (ret != hrSuccess)
  74. return ret;
  75. props.push_back(s);
  76. } else if (strcmp(name, VCGivenNameProp) == 0) {
  77. auto ret = vobject_to_prop(vv, s, PR_GIVEN_NAME);
  78. if (ret != hrSuccess)
  79. return ret;
  80. props.push_back(s);
  81. }
  82. }
  83. return hrSuccess;
  84. }
  85. HRESULT vcftomapi_impl::handle_TEL_EMAIL(VObject *v)
  86. {
  87. bool tel = strcmp(vObjectName(v), VCTelephoneProp) == 0;
  88. VObjectIterator t;
  89. for (initPropIterator(&t, v); moreIteration(&t); ) {
  90. auto vv = nextVObject(&t);
  91. auto name = vObjectName(vv);
  92. SPropValue s;
  93. const char *namep = strcmp(name, "TYPE") == 0 ? vObjectStringZValue(vv) : name;
  94. auto tokens = tokenize(namep, ',');
  95. for (auto const &token : tokens) {
  96. /* telephone */
  97. if (tel && strcasecmp(token.c_str(), "HOME") == 0) {
  98. auto ret = vobject_to_prop(v, s, PR_HOME_TELEPHONE_NUMBER);
  99. if (ret != hrSuccess)
  100. return ret;
  101. props.push_back(s);
  102. }
  103. if (tel && strcasecmp(token.c_str(), "MOBILE") == 0) {
  104. auto ret = vobject_to_prop(v, s, PR_MOBILE_TELEPHONE_NUMBER);
  105. if (ret != hrSuccess)
  106. return ret;
  107. props.push_back(s);
  108. }
  109. if (tel)
  110. continue;
  111. /* email */
  112. vobject_to_named_prop(v, s, 0x8083);
  113. props.push_back(s);
  114. auto ret = unicode_to_named_prop(L"SMTP", s, 0x8082);
  115. if (ret != hrSuccess)
  116. return ret;
  117. props.push_back(s);
  118. }
  119. }
  120. return hrSuccess;
  121. }
  122. /**
  123. * Parses an ICal string (with a certain charset) and converts the
  124. * data in memory. The real MAPI object can be retrieved using
  125. * GetItem().
  126. */
  127. HRESULT vcftomapi_impl::parse_vcf(const std::string &ical)
  128. {
  129. HRESULT hr = hrSuccess;
  130. auto v = Parse_MIME(ical.c_str(), ical.length());
  131. if (v == nullptr)
  132. return MAPI_E_CORRUPT_DATA;
  133. auto v_orig = v;
  134. VObjectIterator t;
  135. for (initPropIterator(&t, v); moreIteration(&t); ) {
  136. v = nextVObject(&t);
  137. auto name = vObjectName(v);
  138. if (strcmp(name, VCNameProp) == 0) {
  139. hr = handle_N(v);
  140. if (hr != hrSuccess)
  141. return hr;
  142. } else if (strcmp(name, VCFullNameProp) == 0) {
  143. SPropValue s;
  144. hr = vobject_to_prop(v, s, PR_DISPLAY_NAME);
  145. if (hr != hrSuccess)
  146. return hr;
  147. props.push_back(s);
  148. } else if (strcmp(name, VCTelephoneProp) == 0 ||
  149. strcmp(name, VCEmailAddressProp) == 0) {
  150. hr = handle_TEL_EMAIL(v);
  151. if (hr != hrSuccess)
  152. return hr;
  153. }
  154. }
  155. cleanVObject(v_orig);
  156. return hr;
  157. }
  158. HRESULT vcftomapi_impl::vobject_to_prop(VObject *v, SPropValue &s, ULONG proptype)
  159. {
  160. auto value_type = vObjectValueType(v);
  161. if (value_type == VCVT_STRINGZ) {
  162. s.ulPropTag = CHANGE_PROP_TYPE(proptype, PT_STRING8);
  163. auto val = vObjectStringZValue(v);
  164. auto ret = MAPIAllocateBuffer(strlen(val) + 1, reinterpret_cast<void **>(&s.Value.lpszA));
  165. if (ret != hrSuccess)
  166. return ret;
  167. strcpy(s.Value.lpszA, val);
  168. }
  169. else if (value_type == VCVT_USTRINGZ) {
  170. s.ulPropTag = CHANGE_PROP_TYPE(proptype, PT_UNICODE);
  171. auto uval = vObjectUStringZValue(v);
  172. auto ret = MAPIAllocateBuffer(sizeof(wchar_t) * (wcslen(uval) + 1), reinterpret_cast<void **>(&s.Value.lpszW));
  173. if (ret != hrSuccess)
  174. return ret;
  175. wcscpy(s.Value.lpszW, uval);
  176. }
  177. return hrSuccess;
  178. }
  179. HRESULT vcftomapi_impl::vobject_to_named_prop(VObject *v, SPropValue &s,
  180. ULONG named_proptype)
  181. {
  182. HRESULT hr;
  183. MAPINAMEID name;
  184. MAPINAMEID *namep = &name;
  185. memory_ptr<SPropTagArray> proptag;
  186. name.lpguid = const_cast<GUID *>(&PSETID_Address);
  187. name.ulKind = MNID_ID;
  188. name.Kind.lID = named_proptype;
  189. hr = m_propobj->GetIDsFromNames(1, &namep, MAPI_CREATE, &~proptag);
  190. if (hr != hrSuccess)
  191. return hr;
  192. hr = vobject_to_prop(v, s, proptag->aulPropTag[0]);
  193. if (hr != hrSuccess)
  194. return hr;
  195. return hrSuccess;
  196. }
  197. HRESULT vcftomapi_impl::unicode_to_named_prop(const wchar_t *v, SPropValue &s,
  198. ULONG named_proptype)
  199. {
  200. MAPINAMEID name;
  201. MAPINAMEID *namep = &name;
  202. memory_ptr<SPropTagArray> proptag;
  203. name.lpguid = const_cast<GUID *>(&PSETID_Address);
  204. name.ulKind = MNID_ID;
  205. name.Kind.lID = named_proptype;
  206. HRESULT hr = m_propobj->GetIDsFromNames(1, &namep,
  207. MAPI_CREATE, &~proptag);
  208. if (hr != hrSuccess)
  209. return hr;
  210. s.ulPropTag = CHANGE_PROP_TYPE(proptag->aulPropTag[0], PT_UNICODE);
  211. auto ret = MAPIAllocateBuffer(sizeof(wchar_t) * (wcslen(v) + 1),
  212. reinterpret_cast<void **>(&s.Value.lpszW));
  213. if (ret != hrSuccess)
  214. return hr;
  215. wcscpy(s.Value.lpszW, v);
  216. return hrSuccess;
  217. }
  218. /**
  219. * Sets mapi properties in Imessage object from the icalitem.
  220. */
  221. HRESULT vcftomapi_impl::get_item(IMessage *msg)
  222. {
  223. if (msg == nullptr)
  224. return MAPI_E_INVALID_PARAMETER;
  225. SPropValue s;
  226. s.ulPropTag = CHANGE_PROP_TYPE(PR_MESSAGE_CLASS, PT_STRING8);
  227. s.Value.lpszA = const_cast<char *>("IPM.Contact");
  228. HRESULT hr = HrSetOneProp(msg, &s);
  229. if (hr != hrSuccess)
  230. return hr;
  231. return save_props(props, msg);
  232. }
  233. /**
  234. * Helper function for GetItem. Saves all properties converted from
  235. * ICal to MAPI in the MAPI object. Does not call SaveChanges.
  236. *
  237. * @param[in] lpPropList list of properties to save in lpMapiProp
  238. * @param[in] lpMapiProp The MAPI object to save properties in
  239. *
  240. * @return MAPI error code
  241. */
  242. HRESULT vcftomapi_impl::save_props(const std::list<SPropValue> &proplist,
  243. IMAPIProp *mapiprop)
  244. {
  245. memory_ptr<SPropValue> propvals;
  246. HRESULT hr = MAPIAllocateBuffer(proplist.size() * sizeof(SPropValue),
  247. &~propvals);
  248. if (hr != hrSuccess)
  249. return hr;
  250. size_t i = 0;
  251. for (const auto &prop : proplist)
  252. propvals[i++] = prop;
  253. auto ret = mapiprop->SetProps(i, propvals, nullptr);
  254. for (const auto &prop : proplist) {
  255. if (PROP_TYPE(prop.ulPropTag) == PT_UNICODE)
  256. MAPIFreeBuffer(prop.Value.lpszW);
  257. else if (PROP_TYPE(prop.ulPropTag) == PT_STRING8)
  258. MAPIFreeBuffer(prop.Value.lpszA);
  259. }
  260. return ret;
  261. }
  262. } /* namespace */