util.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 <cmath>
  19. #include <kopano/memory.hpp>
  20. #include <mapi.h>
  21. #include <mapix.h>
  22. #include <mapidefs.h>
  23. #include <mapiutil.h>
  24. #include <string>
  25. using namespace std;
  26. using namespace KCHL;
  27. #include "util.h"
  28. static std::string last_error;
  29. HRESULT mapi_util_createprof(const char *szProfName, const char *szServiceName,
  30. ULONG cValues, LPSPropValue lpPropVals)
  31. {
  32. HRESULT hr = hrSuccess;
  33. object_ptr<IProfAdmin> lpProfAdmin;
  34. object_ptr<IMsgServiceAdmin> lpServiceAdmin;
  35. object_ptr<IMAPITable> lpTable;
  36. rowset_ptr lpRows;
  37. const SPropValue *lpServiceUID = nullptr;
  38. static constexpr const SizedSPropTagArray(2, sptaMsgServiceCols) =
  39. {2, {PR_SERVICE_NAME_A, PR_SERVICE_UID}};
  40. // Get the MAPI Profile administration object
  41. hr = MAPIAdminProfiles(0, &~lpProfAdmin);
  42. if(hr != hrSuccess) {
  43. last_error = "Unable to get IProfAdmin object";
  44. return hr;
  45. }
  46. lpProfAdmin->DeleteProfile((LPTSTR)szProfName, 0);
  47. // Create a profile that we can use (empty now)
  48. hr = lpProfAdmin->CreateProfile((LPTSTR)szProfName, (LPTSTR)"", 0, 0);
  49. if(hr != hrSuccess) {
  50. last_error = "Unable to create new profile";
  51. return hr;
  52. }
  53. // Get the services admin object
  54. hr = lpProfAdmin->AdminServices((LPTSTR)szProfName, (LPTSTR)"", 0, 0, &~lpServiceAdmin);
  55. if(hr != hrSuccess) {
  56. last_error = "Unable to administer new profile";
  57. return hr;
  58. }
  59. // Create a message service (provider) for the szServiceName (see mapisvc.inf) service
  60. // (not coupled to any file or server yet)
  61. hr = lpServiceAdmin->CreateMsgService((LPTSTR)szServiceName, (LPTSTR)"", 0, 0);
  62. if(hr != hrSuccess) {
  63. last_error = "Service unavailable";
  64. return hr;
  65. }
  66. // optional, ignore error
  67. if (strcmp(szServiceName, "ZARAFA6") == 0)
  68. lpServiceAdmin->CreateMsgService((LPTSTR)"ZCONTACTS", (LPTSTR)"", 0, 0);
  69. // Strangely we now have to get the SERVICE_UID for the service we just added from
  70. // the table. (see MSDN help page of CreateMsgService at the bottom of the page)
  71. hr = lpServiceAdmin->GetMsgServiceTable(0, &~lpTable);
  72. if(hr != hrSuccess) {
  73. last_error = "Service table unavailable";
  74. return hr;
  75. }
  76. hr = lpTable->SetColumns(sptaMsgServiceCols, 0);
  77. if(hr != hrSuccess) {
  78. last_error = "Unable to set columns on service table";
  79. return hr;
  80. }
  81. // Find the correct row
  82. while(TRUE) {
  83. hr = lpTable->QueryRows(1, 0, &~lpRows);
  84. if(hr != hrSuccess || lpRows->cRows != 1) {
  85. last_error = "Unable to read service table";
  86. return hr;
  87. }
  88. auto lpServiceName = PCpropFindProp(lpRows->aRow[0].lpProps, lpRows->aRow[0].cValues, PR_SERVICE_NAME_A);
  89. if(lpServiceName && strcmp(lpServiceName->Value.lpszA, szServiceName) == 0)
  90. break;
  91. }
  92. // Get the PR_SERVICE_UID from the row
  93. lpServiceUID = PCpropFindProp(lpRows->aRow[0].lpProps, lpRows->aRow[0].cValues, PR_SERVICE_UID);
  94. if(!lpServiceUID) {
  95. last_error = "Unable to find service UID";
  96. return MAPI_E_NOT_FOUND;
  97. }
  98. // Configure the message service
  99. hr = lpServiceAdmin->ConfigureMsgService((MAPIUID *)lpServiceUID->Value.bin.lpb, 0, 0, cValues, lpPropVals);
  100. if (hr != hrSuccess)
  101. last_error = "Unable to setup service for provider";
  102. return hr;
  103. }
  104. HRESULT mapi_util_deleteprof(const char *szProfName)
  105. {
  106. object_ptr<IProfAdmin> lpProfAdmin;
  107. HRESULT hr = hrSuccess;
  108. // Get the MAPI Profile administration object
  109. hr = MAPIAdminProfiles(0, &~lpProfAdmin);
  110. if(hr != hrSuccess) {
  111. last_error = "Unable to get IProfAdmin object";
  112. return hr;
  113. }
  114. lpProfAdmin->DeleteProfile((LPTSTR)szProfName, 0);
  115. return hrSuccess;
  116. }
  117. std::string mapi_util_getlasterror()
  118. {
  119. return last_error;
  120. }