mapitovcf.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 <memory>
  18. #include <new>
  19. #include <cstdlib>
  20. #include <libical/vcc.h>
  21. #include <libical/vobject.h>
  22. #include <mapicode.h>
  23. #include <mapiutil.h>
  24. #include <mapix.h>
  25. #include <kopano/memory.hpp>
  26. #include <kopano/platform.h>
  27. #include <kopano/mapiguidext.h>
  28. #include "mapitovcf.hpp"
  29. namespace KC {
  30. class mapitovcf_impl _kc_final : public mapitovcf {
  31. public:
  32. HRESULT add_message(IMessage *) _kc_override;
  33. HRESULT finalize(std::string *) _kc_override;
  34. private:
  35. VObject *to_prop(VObject *node, const char *prop, const SPropValue &value);
  36. VObject *to_prop(VObject *node, const char *prop, const wchar_t *value);
  37. std::string m_result;
  38. /*
  39. * Since we do not want downstream projects to add include paths for
  40. * libical, this is only in the implementation version.
  41. */
  42. };
  43. HRESULT create_mapitovcf(mapitovcf **ret)
  44. {
  45. *ret = new(std::nothrow) mapitovcf_impl();
  46. return *ret != nullptr ? hrSuccess : MAPI_E_NOT_ENOUGH_MEMORY;
  47. }
  48. VObject *mapitovcf_impl::to_prop(VObject *node, const char *prop,
  49. const SPropValue &s)
  50. {
  51. auto newnode = addProp(node, prop);
  52. if (newnode == nullptr)
  53. return nullptr;
  54. if (PROP_TYPE(s.ulPropTag) == PT_UNICODE)
  55. setVObjectUStringZValue(newnode, s.Value.lpszW);
  56. else if (PROP_TYPE(s.ulPropTag) == PT_STRING8)
  57. setVObjectStringZValue(newnode, s.Value.lpszA);
  58. return newnode;
  59. }
  60. VObject *mapitovcf_impl::to_prop(VObject *node, const char *prop,
  61. const wchar_t *value)
  62. {
  63. auto newnode = addProp(node, prop);
  64. if (newnode == nullptr)
  65. return nullptr;
  66. setVObjectUStringZValue(newnode, value);
  67. return newnode;
  68. }
  69. HRESULT mapitovcf_impl::add_message(IMessage *lpMessage)
  70. {
  71. HRESULT hr = hrSuccess;
  72. KCHL::memory_ptr<SPropValue> lpMessageClass;
  73. if (lpMessage == nullptr)
  74. return MAPI_E_INVALID_PARAMETER;
  75. hr = HrGetOneProp(lpMessage, PR_MESSAGE_CLASS_A, &~lpMessageClass);
  76. if (hr != hrSuccess)
  77. return hr;
  78. if (strcasecmp(lpMessageClass->Value.lpszA, "IPM.Contact") != 0)
  79. return MAPI_E_INVALID_PARAMETER;
  80. auto root = newVObject(VCCardProp);
  81. KCHL::memory_ptr<SPropValue> msgprop, msgprop2;
  82. hr = HrGetOneProp(lpMessage, PR_GIVEN_NAME, &~msgprop);
  83. HRESULT hr2 = HrGetOneProp(lpMessage, PR_SURNAME, &~msgprop2);
  84. if (hr == hrSuccess || hr2 == hrSuccess) {
  85. auto node = addGroup(root, VCNameProp);
  86. to_prop(node, VCGivenNameProp, *msgprop);
  87. to_prop(node, VCFamilyNameProp, *msgprop2);
  88. } else if (hr != MAPI_E_NOT_FOUND) {
  89. return hr;
  90. } else if (hr2 != MAPI_E_NOT_FOUND) {
  91. return hr2;
  92. }
  93. hr = HrGetOneProp(lpMessage, PR_DISPLAY_NAME, &~msgprop);
  94. if (hr == hrSuccess)
  95. to_prop(root, VCFullNameProp, *msgprop);
  96. else if (hr != MAPI_E_NOT_FOUND)
  97. return hr;
  98. hr = HrGetOneProp(lpMessage, PR_HOME_TELEPHONE_NUMBER, &~msgprop);
  99. if (hr == hrSuccess) {
  100. auto node = to_prop(root, VCTelephoneProp, *msgprop);
  101. to_prop(node, "TYPE", L"HOME");
  102. } else if (hr != MAPI_E_NOT_FOUND) {
  103. return hr;
  104. }
  105. hr = HrGetOneProp(lpMessage, PR_MOBILE_TELEPHONE_NUMBER, &~msgprop);
  106. if (hr == hrSuccess) {
  107. auto node = to_prop(root, VCTelephoneProp, *msgprop);
  108. to_prop(node, "TYPE", L"MOBILE");
  109. } else if (hr != MAPI_E_NOT_FOUND) {
  110. return hr;
  111. }
  112. /* Email */
  113. MAPINAMEID name;
  114. MAPINAMEID *namep = &name;
  115. name.lpguid = const_cast<GUID *>(&PSETID_Address);
  116. name.ulKind = MNID_ID;
  117. name.Kind.lID = 0x8083;
  118. KCHL::memory_ptr<SPropTagArray> proptag;
  119. hr = lpMessage->GetIDsFromNames(1, &namep, MAPI_CREATE, &~proptag);
  120. if (hr != hrSuccess)
  121. return hr;
  122. ULONG proptype = CHANGE_PROP_TYPE(proptag->aulPropTag[0], PT_UNICODE);
  123. hr = HrGetOneProp(lpMessage, proptype, &~msgprop);
  124. if (hr == hrSuccess)
  125. to_prop(root, VCEmailAddressProp, *msgprop);
  126. else if (hr != MAPI_E_NOT_FOUND)
  127. return hr;
  128. /* Write memobject */
  129. int len = 0;
  130. auto cresult = writeMemVObject(nullptr, &len, root);
  131. if (cresult == nullptr)
  132. return MAPI_E_NOT_ENOUGH_MEMORY;
  133. m_result = cresult;
  134. free(cresult);
  135. cleanVObject(root);
  136. return hrSuccess;
  137. }
  138. HRESULT mapitovcf_impl::finalize(std::string *s)
  139. {
  140. if (s == nullptr)
  141. return MAPI_E_INVALID_PARAMETER;
  142. *s = m_result;
  143. return hrSuccess;
  144. }
  145. } /* namespace */