CommonUtil.cpp 105 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025
  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 <string>
  20. #include <memory>
  21. #include <map>
  22. #include <utility>
  23. #include <kopano/memory.hpp>
  24. #include <kopano/ustringutil.h>
  25. #include <mapi.h>
  26. #include <mapidefs.h>
  27. #include <mapiutil.h>
  28. #include <mapitags.h>
  29. #include <mapicode.h>
  30. #include <cerrno>
  31. #include <iconv.h>
  32. #include <kopano/ECLogger.h>
  33. #include <kopano/CommonUtil.h>
  34. #include <kopano/ECTags.h>
  35. #include <kopano/ECGuid.h>
  36. #include <kopano/Util.h>
  37. #include <kopano/stringutil.h>
  38. #include <kopano/mapi_ptr.h>
  39. #include <kopano/charset/convert.h>
  40. #include <kopano/mapiext.h>
  41. #include "freebusytags.h"
  42. #include <edkguid.h>
  43. #include <kopano/mapiguidext.h>
  44. #include <edkmdb.h>
  45. #include <kopano/IECUnknown.h>
  46. #include <kopano/IECServiceAdmin.h>
  47. #include <kopano/EMSAbTag.h>
  48. #include <kopano/ECRestriction.h>
  49. #include <kopano/MAPIErrors.h>
  50. #include <sys/types.h>
  51. #include <sys/socket.h>
  52. #include <netdb.h>
  53. using namespace std;
  54. using namespace KCHL;
  55. namespace KC {
  56. #define PROFILEPREFIX "ec-adm-"
  57. /* Indexes of the sPropNewMailColumns property array */
  58. enum {
  59. NEWMAIL_ENTRYID, // Array Indexes
  60. NEWMAIL_PARENT_ENTRYID,
  61. NEWMAIL_MESSAGE_CLASS,
  62. NEWMAIL_MESSAGE_FLAGS,
  63. NUM_NEWMAIL_PROPS, // Array size
  64. };
  65. /* Newmail Notify columns */
  66. static constexpr const SizedSPropTagArray(4, sPropNewMailColumns) = {
  67. 4,
  68. {
  69. PR_ENTRYID,
  70. PR_PARENT_ENTRYID,
  71. PR_MESSAGE_CLASS_A,
  72. PR_MESSAGE_FLAGS,
  73. }
  74. };
  75. } /* namespace */
  76. bool operator ==(const SBinary &left, const SBinary &right)
  77. {
  78. return left.cb == right.cb && memcmp(left.lpb, right.lpb, left.cb) == 0;
  79. }
  80. bool operator <(const SBinary &left, const SBinary &right)
  81. {
  82. return left.cb < right.cb || (left.cb == right.cb && memcmp(left.lpb, right.lpb, left.cb) < 0);
  83. }
  84. namespace KC {
  85. const char *GetServerUnixSocket(const char *szPreferred)
  86. {
  87. const char *env = getenv("KOPANO_SOCKET");
  88. if (env && env[0] != '\0')
  89. return env;
  90. else if (szPreferred && szPreferred[0] != '\0')
  91. return szPreferred;
  92. else
  93. return "";
  94. }
  95. /**
  96. * Return the FQDN of this server (guessed).
  97. *
  98. * @return hostname string
  99. */
  100. std::string GetServerFQDN()
  101. {
  102. string retval = "localhost";
  103. int rc;
  104. char hostname[256] = {0};
  105. struct addrinfo hints = {0};
  106. struct addrinfo *aiResult = NULL;
  107. struct sockaddr_in saddr = {0};
  108. rc = gethostname(hostname, sizeof(hostname));
  109. if (rc != 0)
  110. goto exit;
  111. retval = hostname;
  112. rc = getaddrinfo(hostname, NULL, &hints, &aiResult);
  113. if (rc != 0)
  114. goto exit;
  115. // no need to set other contents of saddr struct, we're just intrested in the DNS lookup.
  116. saddr = *((sockaddr_in*)aiResult->ai_addr);
  117. // Name lookup is required, so set that flag
  118. rc = getnameinfo((const sockaddr*)&saddr, sizeof(saddr), hostname, sizeof(hostname), NULL, 0, NI_NAMEREQD);
  119. if (rc != 0)
  120. goto exit;
  121. if (hostname[0] != '\0')
  122. retval = hostname;
  123. exit:
  124. if (aiResult)
  125. freeaddrinfo(aiResult);
  126. return retval;
  127. }
  128. // FIXME: replace it to the right place
  129. #define pbGlobalProfileSectionGuid "\x13\xDB\xB0\xC8\xAA\x05\x10\x1A\x9B\xB0\x00\xAA\x00\x2F\xC4\x5A"
  130. /**
  131. * Creates a new profile with given information.
  132. *
  133. * A new Kopano profile will be created with the information given in
  134. * the paramters. See common/ECTags.h for possible profileflags. These
  135. * will be placed in PR_EC_FLAGS.
  136. * Any existing profile with the name in szProfName will first be removed.
  137. *
  138. * @param[in] username Username to logon with
  139. * @param[in] password Password of the username
  140. * @param[in] path In URI form. Eg. file:///var/run/kopano/server.sock
  141. * @param[in] szProfName Name of the profile to create
  142. * @param[in] ulProfileFlags See EC_PROFILE_FLAGS_* in common/ECTags.h
  143. * @param[in] sslkey_file May be NULL. Logon with this sslkey instead of password.
  144. * @param[in] sslkey_password May be NULL. Password of the sslkey_file.
  145. *
  146. * @return HRESULT Mapi error code.
  147. */
  148. HRESULT CreateProfileTemp(const wchar_t *username, const wchar_t *password,
  149. const char *path, const char* szProfName, ULONG ulProfileFlags,
  150. const char *sslkey_file, const char *sslkey_password,
  151. const char *app_version, const char *app_misc)
  152. {
  153. HRESULT hr = hrSuccess;
  154. object_ptr<IProfAdmin> lpProfAdmin;
  155. object_ptr<IMsgServiceAdmin> lpServiceAdmin;
  156. const SPropValue *lpServiceUID = nullptr;
  157. SPropValue sProps[9]; // server, username, password and profile -name and -flags, optional sslkey file with sslkey password
  158. object_ptr<IMAPITable> lpTable;
  159. rowset_ptr lpRows;
  160. int i;
  161. //-- create profile
  162. hr = MAPIAdminProfiles(0, &~lpProfAdmin);
  163. if (hr != hrSuccess) {
  164. ec_log_crit("CreateProfileTemp(): MAPIAdminProfiles failed %x: %s", hr, GetMAPIErrorMessage(hr));
  165. return hr;
  166. }
  167. lpProfAdmin->DeleteProfile((LPTSTR)szProfName, 0);
  168. hr = lpProfAdmin->CreateProfile((LPTSTR)szProfName, (LPTSTR)"", 0, 0);
  169. if (hr != hrSuccess) {
  170. ec_log_crit("CreateProfileTemp(): CreateProfile failed %x: %s", hr, GetMAPIErrorMessage(hr));
  171. return hr;
  172. }
  173. hr = lpProfAdmin->AdminServices((LPTSTR)szProfName, (LPTSTR)"", 0, 0, &~lpServiceAdmin);
  174. if (hr != hrSuccess) {
  175. ec_log_crit("CreateProfileTemp(): AdminServices failed %x: %s", hr, GetMAPIErrorMessage(hr));
  176. return hr;
  177. }
  178. hr = lpServiceAdmin->CreateMsgService((LPTSTR)"ZARAFA6", (LPTSTR)"", 0, 0);
  179. if (hr != hrSuccess) {
  180. ec_log_crit("CreateProfileTemp(): CreateMsgService ZARAFA6 failed: %s (%x)", GetMAPIErrorMessage(hr), hr);
  181. return hr;
  182. }
  183. // Strangely we now have to get the SERVICE_UID for the service we just added from
  184. // the table. (see MSDN help page of CreateMsgService at the bottom of the page)
  185. hr = lpServiceAdmin->GetMsgServiceTable(0, &~lpTable);
  186. if(hr != hrSuccess) {
  187. ec_log_crit("CreateProfileTemp(): GetMsgServiceTable failed %x: %s", hr, GetMAPIErrorMessage(hr));
  188. return hr;
  189. }
  190. // Find the correct row
  191. while(TRUE) {
  192. hr = lpTable->QueryRows(1, 0, &~lpRows);
  193. if(hr != hrSuccess) {
  194. ec_log_crit("CreateProfileTemp(): QueryRows failed %x: %s", hr, GetMAPIErrorMessage(hr));
  195. return hr;
  196. }
  197. if(lpRows->cRows != 1)
  198. break;
  199. auto lpServiceName = PCpropFindProp(lpRows->aRow[0].lpProps, lpRows->aRow[0].cValues, PR_SERVICE_NAME_A);
  200. if(lpServiceName && strcmp(lpServiceName->Value.lpszA, "ZARAFA6") == 0)
  201. break;
  202. }
  203. if(lpRows->cRows != 1) {
  204. ec_log_warn("CreateProfileTemp(): no rows found");
  205. return MAPI_E_NOT_FOUND;
  206. }
  207. // Get the PR_SERVICE_UID from the row
  208. lpServiceUID = PCpropFindProp(lpRows->aRow[0].lpProps, lpRows->aRow[0].cValues, PR_SERVICE_UID);
  209. if(!lpServiceUID) {
  210. ec_log_crit("CreateProfileTemp(): PCpropFindProp failed %x: %s", hr, GetMAPIErrorMessage(hr));
  211. return MAPI_E_NOT_FOUND;
  212. }
  213. i = 0;
  214. sProps[i].ulPropTag = PR_EC_PATH;
  215. sProps[i].Value.lpszA = const_cast<char *>(path != NULL && *path != '\0' ? path : "default:");
  216. ++i;
  217. sProps[i].ulPropTag = PR_EC_USERNAME_W;
  218. sProps[i].Value.lpszW = (WCHAR*)username;
  219. ++i;
  220. sProps[i].ulPropTag = PR_EC_USERPASSWORD_W;
  221. sProps[i].Value.lpszW = (WCHAR*)password;
  222. ++i;
  223. sProps[i].ulPropTag = PR_EC_FLAGS;
  224. sProps[i].Value.ul = ulProfileFlags;
  225. ++i;
  226. sProps[i].ulPropTag = PR_PROFILE_NAME_A;
  227. sProps[i].Value.lpszA = (char*)szProfName;
  228. ++i;
  229. if (sslkey_file) {
  230. // always add SSL keys info as we might be redirected to an SSL connection
  231. sProps[i].ulPropTag = PR_EC_SSLKEY_FILE;
  232. sProps[i].Value.lpszA = (char*)sslkey_file;
  233. ++i;
  234. if (sslkey_password) {
  235. sProps[i].ulPropTag = PR_EC_SSLKEY_PASS;
  236. sProps[i].Value.lpszA = (char*)sslkey_password;
  237. ++i;
  238. }
  239. }
  240. if (app_version) {
  241. sProps[i].ulPropTag = PR_EC_STATS_SESSION_CLIENT_APPLICATION_VERSION;
  242. sProps[i].Value.lpszA = (char*)app_version;
  243. ++i;
  244. }
  245. if (app_misc) {
  246. sProps[i].ulPropTag = PR_EC_STATS_SESSION_CLIENT_APPLICATION_MISC;
  247. sProps[i].Value.lpszA = (char*)app_misc;
  248. ++i;
  249. }
  250. hr = lpServiceAdmin->ConfigureMsgService((MAPIUID *)lpServiceUID->Value.bin.lpb, 0, 0, i, sProps);
  251. if (hr != hrSuccess)
  252. ec_log_crit("CreateProfileTemp(): ConfigureMsgService failed %x: %s", hr, GetMAPIErrorMessage(hr));
  253. return hr;
  254. }
  255. /**
  256. * Deletes a profile with specified name.
  257. *
  258. * @param[in] szProfName Name of the profile to delete
  259. *
  260. * @return HRESULT Mapi error code.
  261. */
  262. HRESULT DeleteProfileTemp(char *szProfName)
  263. {
  264. object_ptr<IProfAdmin> lpProfAdmin;
  265. HRESULT hr = hrSuccess;
  266. // Get the MAPI Profile administration object
  267. hr = MAPIAdminProfiles(0, &~lpProfAdmin);
  268. if (hr != hrSuccess)
  269. return hr;
  270. return lpProfAdmin->DeleteProfile((LPTSTR)szProfName, 0);
  271. }
  272. HRESULT HrOpenECAdminSession(IMAPISession **lppSession,
  273. const char *const app_version, const char *const app_misc,
  274. const char *szPath, ULONG ulProfileFlags, const char *sslkey_file,
  275. const char *sslkey_password)
  276. {
  277. return HrOpenECSession(lppSession, app_version, app_misc, KOPANO_SYSTEM_USER_W, KOPANO_SYSTEM_USER_W, szPath, ulProfileFlags, sslkey_file, sslkey_password);
  278. }
  279. HRESULT HrOpenECSession(IMAPISession **lppSession,
  280. const char *const app_version, const char *const app_misc,
  281. const wchar_t *szUsername, const wchar_t *szPassword, const char *szPath,
  282. ULONG ulProfileFlags, const char *sslkey_file, const char *sslkey_password,
  283. const char *profname)
  284. {
  285. HRESULT hr = hrSuccess;
  286. ULONG ulProfNum = 0;
  287. std::unique_ptr<char[]> szProfName(new char[strlen(PROFILEPREFIX)+10+1]);
  288. IMAPISession *lpMAPISession = NULL;
  289. if (profname == NULL) {
  290. ulProfNum = rand_mt();
  291. snprintf(szProfName.get(), strlen(PROFILEPREFIX)+10+1, "%s%010u", PROFILEPREFIX, ulProfNum);
  292. }
  293. else {
  294. strcpy(szProfName.get(), profname);
  295. }
  296. if (sslkey_file != NULL) {
  297. FILE *ssltest = fopen(sslkey_file, "r");
  298. if (!ssltest) {
  299. ec_log_crit("Cannot access %s: %s", sslkey_file, strerror(errno));
  300. // do not pass sslkey if the file does not exist
  301. // otherwise normal connections do not work either
  302. sslkey_file = NULL;
  303. sslkey_password = NULL;
  304. }
  305. else {
  306. // TODO: test password of certificate
  307. fclose(ssltest);
  308. }
  309. }
  310. hr = CreateProfileTemp(szUsername, szPassword, szPath, szProfName.get(), ulProfileFlags, sslkey_file, sslkey_password, app_version, app_misc);
  311. if (hr != hrSuccess) {
  312. ec_log_warn("CreateProfileTemp failed: %x: %s", hr, GetMAPIErrorMessage(hr));
  313. goto exit;
  314. }
  315. // Log on the the profile
  316. hr = MAPILogonEx(0, (LPTSTR)szProfName.get(), (LPTSTR)"", MAPI_EXTENDED | MAPI_NEW_SESSION | MAPI_NO_MAIL, &lpMAPISession);
  317. if (hr != hrSuccess) {
  318. ec_log_warn("MAPILogonEx failed: %x: %s", hr, GetMAPIErrorMessage(hr));
  319. goto exit;
  320. }
  321. *lppSession = lpMAPISession;
  322. exit:
  323. // always try to delete the temporary profile
  324. DeleteProfileTemp(szProfName.get());
  325. return hr;
  326. }
  327. HRESULT HrSearchECStoreEntryId(IMAPISession *lpMAPISession, BOOL bPublic, ULONG *lpcbEntryID, LPENTRYID *lppEntryID)
  328. {
  329. HRESULT hr = hrSuccess;
  330. rowset_ptr lpRows;
  331. object_ptr<IMAPITable> lpStoreTable;
  332. const SPropValue *lpEntryIDProp = nullptr;
  333. // Get the default store by searching through the message store table and finding the
  334. // store with PR_MDB_PROVIDER set to the kopano public store GUID
  335. hr = lpMAPISession->GetMsgStoresTable(0, &~lpStoreTable);
  336. if(hr != hrSuccess)
  337. return hr;
  338. while(TRUE) {
  339. hr = lpStoreTable->QueryRows(1, 0, &~lpRows);
  340. if (hr != hrSuccess || lpRows->cRows != 1)
  341. return MAPI_E_NOT_FOUND;
  342. if (bPublic) {
  343. auto lpStoreProp = PCpropFindProp(lpRows->aRow[0].lpProps,lpRows->aRow[0].cValues, PR_MDB_PROVIDER);
  344. if (lpStoreProp != NULL && memcmp(lpStoreProp->Value.bin.lpb, &KOPANO_STORE_PUBLIC_GUID, sizeof(MAPIUID)) == 0 )
  345. break;
  346. } else {
  347. auto lpStoreProp = PCpropFindProp(lpRows->aRow[0].lpProps,lpRows->aRow[0].cValues, PR_RESOURCE_FLAGS);
  348. if (lpStoreProp != NULL && lpStoreProp->Value.ul & STATUS_DEFAULT_STORE)
  349. break;
  350. }
  351. }
  352. lpEntryIDProp = PCpropFindProp(lpRows->aRow[0].lpProps, lpRows->aRow[0].cValues, PR_ENTRYID);
  353. if (lpEntryIDProp == nullptr)
  354. return MAPI_E_NOT_FOUND;
  355. // copy entryid so we continue in the same code piece in windows/linux
  356. return Util::HrCopyEntryId(lpEntryIDProp->Value.bin.cb,
  357. reinterpret_cast<ENTRYID *>(lpEntryIDProp->Value.bin.lpb),
  358. lpcbEntryID, lppEntryID);
  359. }
  360. HRESULT HrOpenDefaultStore(IMAPISession *lpMAPISession, IMsgStore **lppMsgStore) {
  361. return HrOpenDefaultStore(lpMAPISession, MDB_WRITE | MDB_NO_DIALOG | MDB_NO_MAIL | MDB_TEMPORARY, lppMsgStore);
  362. }
  363. HRESULT HrOpenDefaultStoreOffline(IMAPISession *lpMAPISession, IMsgStore **lppMsgStore)
  364. {
  365. HRESULT hr = hrSuccess;
  366. object_ptr<IMsgStore> lpMsgStore, lpProxedMsgStore;
  367. hr = HrOpenDefaultStore(lpMAPISession, MDB_WRITE | MDB_NO_DIALOG | MDB_NO_MAIL | MDB_TEMPORARY, &~lpMsgStore);
  368. if(hr != hrSuccess)
  369. return hr;
  370. hr = GetProxyStoreObject(lpMsgStore, &~lpProxedMsgStore);
  371. if (hr != hrSuccess)
  372. return hr;
  373. return lpProxedMsgStore->QueryInterface(IID_ECMsgStoreOffline, reinterpret_cast<void **>(lppMsgStore));
  374. }
  375. HRESULT HrOpenDefaultStoreOnline(IMAPISession *lpMAPISession, IMsgStore **lppMsgStore)
  376. {
  377. HRESULT hr = hrSuccess;
  378. object_ptr<IMsgStore> lpMsgStore, lpProxedMsgStore;
  379. hr = HrOpenDefaultStore(lpMAPISession, MDB_WRITE | MDB_NO_DIALOG | MDB_NO_MAIL | MDB_TEMPORARY, &~lpMsgStore);
  380. if(hr != hrSuccess)
  381. return hr;
  382. hr = GetProxyStoreObject(lpMsgStore, &~lpProxedMsgStore);
  383. if (hr != hrSuccess)
  384. return hr;
  385. return lpProxedMsgStore->QueryInterface(IID_ECMsgStoreOnline, reinterpret_cast<void **>(lppMsgStore));
  386. }
  387. HRESULT HrOpenStoreOnline(IMAPISession *lpMAPISession, ULONG cbEntryID, LPENTRYID lpEntryID, IMsgStore **lppMsgStore)
  388. {
  389. HRESULT hr = hrSuccess;
  390. object_ptr<IMsgStore> lpMsgStore, lpProxedMsgStore;
  391. if (lpMAPISession == nullptr || lppMsgStore == nullptr || lpEntryID == nullptr)
  392. return MAPI_E_INVALID_PARAMETER;
  393. hr = lpMAPISession->OpenMsgStore(0, cbEntryID, lpEntryID, &IID_IMsgStore, MDB_WRITE | MDB_NO_DIALOG | MDB_NO_MAIL | MDB_TEMPORARY, &~lpMsgStore);
  394. if (hr != hrSuccess)
  395. return hr;
  396. hr = GetProxyStoreObject(lpMsgStore, &~lpProxedMsgStore);
  397. if (hr != hrSuccess)
  398. return hr;
  399. return lpProxedMsgStore->QueryInterface(IID_ECMsgStoreOnline, reinterpret_cast<void **>(lppMsgStore));
  400. }
  401. HRESULT GetProxyStoreObject(IMsgStore *lpMsgStore, IMsgStore **lppMsgStore)
  402. {
  403. HRESULT hr = hrSuccess;
  404. object_ptr<IProxyStoreObject> lpProxyStoreObject;
  405. IECUnknown* lpECMsgStore = NULL;
  406. memory_ptr<SPropValue> lpPropValue;
  407. if (lpMsgStore == nullptr || lppMsgStore == nullptr)
  408. return MAPI_E_INVALID_PARAMETER;
  409. if (lpMsgStore->QueryInterface(IID_IProxyStoreObject, &~lpProxyStoreObject) == hrSuccess) {
  410. hr = lpProxyStoreObject->UnwrapNoRef((LPVOID*)lppMsgStore);
  411. if (hr != hrSuccess)
  412. return hr;
  413. (*lppMsgStore)->AddRef();
  414. return hrSuccess;
  415. } else if (HrGetOneProp(lpMsgStore, PR_EC_OBJECT, &~lpPropValue) == hrSuccess) {
  416. lpECMsgStore = (IECUnknown *)lpPropValue->Value.lpszA;
  417. if (lpECMsgStore == nullptr)
  418. return MAPI_E_INVALID_PARAMETER;
  419. return lpECMsgStore->QueryInterface(IID_IMsgStore, (void**)lppMsgStore);
  420. }
  421. // Possible object already wrapped, gives the original object back
  422. (*lppMsgStore) = lpMsgStore;
  423. (*lppMsgStore)->AddRef();
  424. return hrSuccess;
  425. }
  426. HRESULT HrOpenDefaultStore(IMAPISession *lpMAPISession, ULONG ulFlags, IMsgStore **lppMsgStore) {
  427. ULONG cbEntryID = 0;
  428. memory_ptr<ENTRYID> lpEntryID;
  429. HRESULT hr = HrSearchECStoreEntryId(lpMAPISession, FALSE, &cbEntryID, &~lpEntryID);
  430. if (hr != hrSuccess)
  431. return hr;
  432. return lpMAPISession->OpenMsgStore(0, cbEntryID, lpEntryID,
  433. &IID_IMsgStore, ulFlags, lppMsgStore);
  434. }
  435. HRESULT HrOpenECPublicStore(IMAPISession *lpMAPISession, IMsgStore **lppMsgStore){
  436. return HrOpenECPublicStore(lpMAPISession, MDB_WRITE | MDB_NO_DIALOG | MDB_NO_MAIL | MDB_TEMPORARY, lppMsgStore);
  437. }
  438. HRESULT HrOpenECPublicStoreOnline(IMAPISession *lpMAPISession, IMsgStore **lppMsgStore)
  439. {
  440. HRESULT hr = hrSuccess;
  441. object_ptr<IMsgStore> lpMsgStore, lpProxedMsgStore;
  442. hr = HrOpenECPublicStore(lpMAPISession, MDB_WRITE | MDB_NO_DIALOG | MDB_NO_MAIL | MDB_TEMPORARY, &~lpMsgStore);
  443. if(hr != hrSuccess)
  444. return hr;
  445. hr = GetProxyStoreObject(lpMsgStore, &~lpProxedMsgStore);
  446. if (hr != hrSuccess)
  447. return hr;
  448. return lpProxedMsgStore->QueryInterface(IID_ECMsgStoreOnline, reinterpret_cast<void **>(lppMsgStore));
  449. }
  450. HRESULT HrOpenECPublicStore(IMAPISession *lpMAPISession, ULONG ulFlags, IMsgStore **lppMsgStore)
  451. {
  452. ULONG cbEntryID = 0;
  453. memory_ptr<ENTRYID> lpEntryID;
  454. HRESULT hr = HrSearchECStoreEntryId(lpMAPISession, TRUE, &cbEntryID, &~lpEntryID);
  455. if(hr != hrSuccess)
  456. return hr;
  457. return lpMAPISession->OpenMsgStore(0, cbEntryID, lpEntryID,
  458. &IID_IMsgStore, ulFlags, lppMsgStore);
  459. }
  460. HRESULT HrGetECProviderAdmin(LPMAPISESSION lpSession, LPPROVIDERADMIN *lppProviderAdmin)
  461. {
  462. HRESULT hr = hrSuccess;
  463. object_ptr<IMsgServiceAdmin> lpMsgServiceAdmin;
  464. object_ptr<IMAPITable> lpServiceTable;
  465. SPropValue sPropRestrict;
  466. rowset_ptr lpsRowSet;
  467. const SPropValue *lpProviderUID = NULL;
  468. // Get the service admin
  469. hr = lpSession->AdminServices(0, &~lpMsgServiceAdmin);
  470. if(hr != hrSuccess)
  471. return hr;
  472. //Getdefault profile
  473. hr = lpMsgServiceAdmin->GetMsgServiceTable(0, &~lpServiceTable);
  474. if(hr != hrSuccess)
  475. return hr;
  476. // restrict the table
  477. sPropRestrict.ulPropTag = PR_SERVICE_NAME_A;
  478. sPropRestrict.Value.lpszA = const_cast<char *>("ZARAFA6");
  479. hr = ECContentRestriction(FL_FULLSTRING, PR_SERVICE_NAME_A,
  480. &sPropRestrict, ECRestriction::Cheap)
  481. .RestrictTable(lpServiceTable, 0);
  482. if(hr != hrSuccess)
  483. return hr;
  484. //Seek to the end
  485. hr = lpServiceTable->SeekRow(BOOKMARK_END, -1, NULL);
  486. if(hr != hrSuccess)
  487. return hr;
  488. hr = lpServiceTable->QueryRows(1, 0, &~lpsRowSet);
  489. if(hr != hrSuccess || lpsRowSet == NULL || lpsRowSet->cRows != 1)
  490. return hr == hrSuccess ? MAPI_E_NOT_FOUND : hr;
  491. // Get the Service UID
  492. lpProviderUID = PCpropFindProp(lpsRowSet->aRow[0].lpProps, lpsRowSet->aRow[0].cValues, PR_SERVICE_UID);
  493. if (lpProviderUID == nullptr)
  494. return MAPI_E_NOT_FOUND;
  495. // Get a admin provider pointer
  496. return lpMsgServiceAdmin->AdminProviders(reinterpret_cast<MAPIUID *>(lpProviderUID->Value.bin.lpb), 0, lppProviderAdmin);
  497. }
  498. HRESULT HrRemoveECMailBox(LPMAPISESSION lpSession, LPMAPIUID lpsProviderUID)
  499. {
  500. HRESULT hr = hrSuccess;
  501. object_ptr<IProviderAdmin> lpProviderAdmin;
  502. hr = HrGetECProviderAdmin(lpSession, &~lpProviderAdmin);
  503. if(hr != hrSuccess)
  504. return hr;
  505. return HrRemoveECMailBox(lpProviderAdmin, lpsProviderUID);
  506. }
  507. HRESULT HrRemoveECMailBox(LPPROVIDERADMIN lpProviderAdmin, LPMAPIUID lpsProviderUID)
  508. {
  509. HRESULT hr = hrSuccess;
  510. object_ptr<IProfSect> lpGlobalProfSect;
  511. memory_ptr<SPropValue> lpGlobalProps, lpNewProp;
  512. ULONG cValues = 0;
  513. ULONG cSize = 0;
  514. unsigned int i = 0;
  515. //Open global profile, add the store.(for show list, delete etc)
  516. hr = lpProviderAdmin->OpenProfileSection((LPMAPIUID)pbGlobalProfileSectionGuid, nullptr, MAPI_MODIFY , &~lpGlobalProfSect);
  517. if(hr != hrSuccess)
  518. return hr;
  519. // The the prop value PR_STORE_PROVIDERS
  520. static constexpr const SizedSPropTagArray(1, proptag) = {1, {PR_STORE_PROVIDERS}};
  521. hr = lpGlobalProfSect->GetProps(proptag, 0, &cValues, &~lpGlobalProps);
  522. if(hr == hrSuccess && lpGlobalProps->Value.bin.cb >= sizeof(MAPIUID))
  523. {
  524. hr = MAPIAllocateBuffer(sizeof(SPropValue), &~lpNewProp);
  525. if(hr != hrSuccess)
  526. return hr;
  527. cSize = lpGlobalProps->Value.bin.cb - sizeof(MAPIUID);
  528. hr = MAPIAllocateMore(cSize, lpNewProp, (void**)&lpNewProp->Value.bin.lpb);
  529. if(hr != hrSuccess)
  530. return hr;
  531. lpNewProp->Value.bin.cb = 0;
  532. lpNewProp->ulPropTag = PR_STORE_PROVIDERS;
  533. for (i = 0; i < lpGlobalProps->Value.bin.cb / sizeof(MAPIUID); ++i) {
  534. if(memcmp(lpGlobalProps->Value.bin.lpb+(sizeof(MAPIUID) * i), lpsProviderUID, sizeof(MAPIUID)) != 0)
  535. {
  536. memcpy(lpNewProp->Value.bin.lpb+lpNewProp->Value.bin.cb, lpGlobalProps->Value.bin.lpb+(sizeof(MAPIUID) * i), sizeof(MAPIUID));
  537. lpNewProp->Value.bin.cb += sizeof(MAPIUID);
  538. }
  539. }
  540. hr = lpGlobalProfSect->SetProps(1, lpNewProp, NULL);
  541. if(hr != hrSuccess)
  542. return hr;
  543. hr = lpGlobalProfSect->SaveChanges(0);
  544. if(hr != hrSuccess)
  545. return hr;
  546. }
  547. //Remove Store
  548. lpProviderAdmin->DeleteProvider(lpsProviderUID);
  549. //FIXME: handle unknown error 0x80070005 by delete. HACK: always succeed.
  550. return hrSuccess;
  551. }
  552. static HRESULT HrAddProfileUID(LPPROVIDERADMIN lpProviderAdmin, LPMAPIUID lpNewProfileUID)
  553. {
  554. ProfSectPtr ptrGlobalProfSect;
  555. ULONG cValues;
  556. SPropValuePtr ptrGlobalProps;
  557. ULONG csNewMapiUID;
  558. SPropValuePtr ptrNewProp;
  559. static constexpr const SizedSPropTagArray(1, sptaGlobalProps) = {1, {PR_STORE_PROVIDERS}};
  560. //Open global profile, add the store.(for show list, delete etc)
  561. HRESULT hr = lpProviderAdmin->OpenProfileSection(reinterpret_cast<MAPIUID *>(const_cast<char *>(pbGlobalProfileSectionGuid)),
  562. NULL, MAPI_MODIFY, &~ptrGlobalProfSect);
  563. if (hr != hrSuccess)
  564. return hr;
  565. // The prop value PR_STORE_PROVIDERS
  566. hr = ptrGlobalProfSect->GetProps(sptaGlobalProps, 0, &cValues, &~ptrGlobalProps);
  567. if (HR_FAILED(hr))
  568. return hr;
  569. if (ptrGlobalProps->ulPropTag != PR_STORE_PROVIDERS)
  570. ptrGlobalProps->Value.bin.cb = 0;
  571. // The new size of stores provider uid
  572. csNewMapiUID = ptrGlobalProps->Value.bin.cb + sizeof(MAPIUID); //lpNewProfileUID
  573. hr = MAPIAllocateBuffer(sizeof(SPropValue), &~ptrNewProp);
  574. if (hr != hrSuccess)
  575. return hr;
  576. hr = MAPIAllocateMore(csNewMapiUID, ptrNewProp, (LPVOID*)&ptrNewProp->Value.bin.lpb);
  577. if (hr != hrSuccess)
  578. return hr;
  579. ptrNewProp->Value.bin.cb = csNewMapiUID;
  580. ptrNewProp->ulPropTag = PR_STORE_PROVIDERS;
  581. if (ptrGlobalProps->Value.bin.cb > 0)
  582. memcpy(ptrNewProp->Value.bin.lpb, ptrGlobalProps->Value.bin.lpb, ptrGlobalProps->Value.bin.cb);
  583. memcpy(ptrNewProp->Value.bin.lpb + ptrGlobalProps->Value.bin.cb, lpNewProfileUID, sizeof(MAPIUID));
  584. hr = ptrGlobalProfSect->SetProps(1, ptrNewProp, NULL);
  585. if (hr != hrSuccess)
  586. return hr;
  587. return ptrGlobalProfSect->SaveChanges(0);
  588. }
  589. HRESULT HrAddECMailBox(LPMAPISESSION lpSession, LPCWSTR lpszUserName)
  590. {
  591. HRESULT hr = hrSuccess;
  592. object_ptr<IProviderAdmin> lpProviderAdmin;
  593. hr = HrGetECProviderAdmin(lpSession, &~lpProviderAdmin);
  594. if(hr != hrSuccess)
  595. return hr;
  596. return HrAddECMailBox(lpProviderAdmin, lpszUserName);
  597. }
  598. HRESULT HrAddECMailBox(LPPROVIDERADMIN lpProviderAdmin, LPCWSTR lpszUserName)
  599. {
  600. MAPIUID sNewProfileUID;
  601. SPropValue asProfileProps[1];
  602. if (lpProviderAdmin == NULL || lpszUserName == NULL)
  603. return MAPI_E_INVALID_PARAMETER;
  604. asProfileProps[0].ulPropTag = PR_EC_USERNAME_W;
  605. asProfileProps[0].Value.lpszW = (WCHAR*)lpszUserName;
  606. // Create the profile, now the profile is shown in outlook
  607. HRESULT hr = lpProviderAdmin->CreateProvider((TCHAR *)"ZARAFA6_MSMDB_Delegate",
  608. ARRAY_SIZE(asProfileProps), asProfileProps, 0, 0,
  609. &sNewProfileUID);
  610. if (hr != hrSuccess)
  611. return hr;
  612. return HrAddProfileUID(lpProviderAdmin, &sNewProfileUID);
  613. }
  614. HRESULT HrAddArchiveMailBox(LPPROVIDERADMIN lpProviderAdmin, LPCWSTR lpszUserName, LPCWSTR lpszServerName, LPMAPIUID lpProviderUID)
  615. {
  616. MAPIUID sNewProfileUID;
  617. SPropValue asProfileProps[2];
  618. if (lpProviderAdmin == NULL || lpszUserName == NULL ||
  619. lpszServerName == NULL)
  620. return MAPI_E_INVALID_PARAMETER;
  621. asProfileProps[0].ulPropTag = PR_EC_USERNAME_W;
  622. asProfileProps[0].Value.lpszW = (LPWSTR)lpszUserName;
  623. asProfileProps[1].ulPropTag = PR_EC_SERVERNAME_W;
  624. asProfileProps[1].Value.lpszW = (LPWSTR)lpszServerName;
  625. // Create the profile, now the profile is shown in outlook
  626. HRESULT hr = lpProviderAdmin->CreateProvider((TCHAR *)("ZARAFA6_MSMDB_archive"),
  627. ARRAY_SIZE(asProfileProps), asProfileProps, 0, 0,
  628. &sNewProfileUID);
  629. if (hr != hrSuccess)
  630. return hr;
  631. hr = HrAddProfileUID(lpProviderAdmin, &sNewProfileUID);
  632. if (hr != hrSuccess)
  633. return hr;
  634. if (lpProviderUID)
  635. *lpProviderUID = std::move(sNewProfileUID);
  636. return hrSuccess;
  637. }
  638. /**
  639. * Create a OneOff EntryID.
  640. *
  641. * @param[in] lpszName Displayname of object
  642. * @param[in] lpszAdrType Addresstype of EntryID. Mostly SMTP or ZARAFA.
  643. * @param[in] lpszAddress Address of EntryID, according to type.
  644. * @param[in] ulFlags Enable MAPI_UNICODE flag if input strings are WCHAR strings. Output will be unicode too.
  645. * @param[out] lpcbEntryID Length of lppEntryID
  646. * @param[out] lpplpEntryID OneOff EntryID for object.
  647. *
  648. * @return HRESULT
  649. *
  650. * @note If UNICODE strings are used, we must use windows UCS-2 format.
  651. */
  652. HRESULT ECCreateOneOff(LPTSTR lpszName, LPTSTR lpszAdrType, LPTSTR lpszAddress, ULONG ulFlags, ULONG* lpcbEntryID, LPENTRYID* lppEntryID)
  653. {
  654. std::string strOneOff;
  655. MAPIUID uid = {MAPI_ONE_OFF_UID};
  656. unsigned short usFlags = (((ulFlags & MAPI_UNICODE)?MAPI_ONE_OFF_UNICODE:0) | ((ulFlags & MAPI_SEND_NO_RICH_INFO)?MAPI_ONE_OFF_NO_RICH_INFO:0));
  657. if (lpszAdrType == NULL || lpszAddress == NULL)
  658. return MAPI_E_INVALID_PARAMETER;
  659. strOneOff.append(4, '\0'); // abFlags
  660. strOneOff.append((char *)&uid, sizeof(MAPIUID));
  661. strOneOff.append(2, '\0'); // version (0)
  662. strOneOff.append((char *)&usFlags, sizeof(usFlags));
  663. if(ulFlags & MAPI_UNICODE)
  664. {
  665. std::wstring wstrName;
  666. std::u16string strUnicode;
  667. if (lpszName)
  668. wstrName = (WCHAR*)lpszName;
  669. else
  670. wstrName = (WCHAR*)lpszAddress;
  671. strUnicode = convert_to<std::u16string>(wstrName);
  672. strOneOff.append((char*)strUnicode.c_str(), (strUnicode.length()+1)*sizeof(unsigned short));
  673. strUnicode = convert_to<std::u16string>(reinterpret_cast<wchar_t *>(lpszAdrType));
  674. strOneOff.append((char*)strUnicode.c_str(), (strUnicode.length()+1)*sizeof(unsigned short));
  675. strUnicode = convert_to<std::u16string>(reinterpret_cast<wchar_t *>(lpszAddress));
  676. strOneOff.append((char*)strUnicode.c_str(), (strUnicode.length()+1)*sizeof(unsigned short));
  677. } else {
  678. if (lpszName)
  679. strOneOff.append((char *)lpszName, strlen((char *)lpszName) + 1);
  680. else
  681. strOneOff.append(1, '\0');
  682. strOneOff.append((char *)lpszAdrType, strlen((char *)lpszAdrType) + 1);
  683. strOneOff.append((char *)lpszAddress, strlen((char *)lpszAddress) + 1);
  684. }
  685. HRESULT hr = MAPIAllocateBuffer(strOneOff.size(),
  686. reinterpret_cast<void **>(lppEntryID));
  687. if(hr != hrSuccess)
  688. return hr;
  689. memcpy(*lppEntryID, strOneOff.c_str(), strOneOff.size());
  690. *lpcbEntryID = strOneOff.size();
  691. return hrSuccess;
  692. }
  693. /**
  694. * Parse a OneOff EntryID. Fails if the input is not a correct OneOff EntryID. Returns strings always in unicode.
  695. *
  696. * @param[in] cbEntryID Length of lppEntryID
  697. * @param[in] lplpEntryID OneOff EntryID for object.
  698. * @param[out] strWName Displayname of object
  699. * @param[out] strWType Addresstype of EntryID. Mostly SMTP or ZARAFA.
  700. * @param[out] strWAddress Address of EntryID, according to type.
  701. *
  702. * @return HRESULT
  703. * @retval MAPI_E_INVALID_PARAMETER EntryID is not a OneOff EntryID.
  704. */
  705. HRESULT ECParseOneOff(const ENTRYID *lpEntryID, ULONG cbEntryID,
  706. std::wstring &strWName, std::wstring &strWType, std::wstring &strWAddress)
  707. {
  708. HRESULT hr = hrSuccess;
  709. MAPIUID muidOneOff = {MAPI_ONE_OFF_UID};
  710. auto lpBuffer = reinterpret_cast<const char *>(lpEntryID);
  711. unsigned short usFlags;
  712. std::wstring name;
  713. std::wstring type;
  714. std::wstring addr;
  715. if (cbEntryID < (8 + sizeof(MAPIUID)) || lpEntryID == NULL)
  716. return MAPI_E_INVALID_PARAMETER;
  717. if(*reinterpret_cast<const unsigned int *>(lpBuffer) != 0)
  718. return MAPI_E_INVALID_PARAMETER;
  719. lpBuffer += 4;
  720. if (memcmp(&muidOneOff, lpBuffer, sizeof(MAPIUID)) != 0)
  721. return MAPI_E_INVALID_PARAMETER;
  722. lpBuffer += sizeof(MAPIUID);
  723. if (*reinterpret_cast<const unsigned short *>(lpBuffer) != 0)
  724. return MAPI_E_INVALID_PARAMETER;
  725. lpBuffer += 2;
  726. memcpy(&usFlags, lpBuffer, sizeof(usFlags));
  727. lpBuffer += 2;
  728. if(usFlags & MAPI_ONE_OFF_UNICODE) {
  729. std::u16string str;
  730. str.assign(reinterpret_cast<std::u16string::const_pointer>(lpBuffer));
  731. // can be 0 length
  732. if ((hr = TryConvert(str, name)) != hrSuccess)
  733. return hr;
  734. lpBuffer += (str.length() + 1) * sizeof(unsigned short);
  735. str.assign(reinterpret_cast<std::u16string::const_pointer>(lpBuffer));
  736. if (str.length() == 0)
  737. return MAPI_E_INVALID_PARAMETER;
  738. if ((hr = TryConvert(str, type)) != hrSuccess)
  739. return hr;
  740. lpBuffer += (str.length() + 1) * sizeof(unsigned short);
  741. str.assign(reinterpret_cast<std::u16string::const_pointer>(lpBuffer));
  742. if (str.length() == 0)
  743. return MAPI_E_INVALID_PARAMETER;
  744. if ((hr = TryConvert(str, addr)) != hrSuccess)
  745. return hr;
  746. lpBuffer += (str.length() + 1) * sizeof(unsigned short);
  747. } else {
  748. /*
  749. * Assumption: This should be an old OneOffEntryID in the
  750. * windows-1252 charset.
  751. */
  752. string str;
  753. str = (char*)lpBuffer;
  754. // can be 0 length
  755. hr = TryConvert(lpBuffer, rawsize(lpBuffer), "windows-1252", name);
  756. if (hr != hrSuccess)
  757. return hr;
  758. lpBuffer += str.length() + 1;
  759. str = (char*)lpBuffer;
  760. if (str.length() == 0)
  761. return MAPI_E_INVALID_PARAMETER;
  762. if ((hr = TryConvert(str, type)) != hrSuccess)
  763. return hr;
  764. lpBuffer += str.length() + 1;
  765. str = (char*)lpBuffer;
  766. if (str.length() == 0)
  767. return MAPI_E_INVALID_PARAMETER;
  768. if ((hr = TryConvert(str, addr)) != hrSuccess)
  769. return hr;
  770. lpBuffer += str.length() + 1;
  771. }
  772. strWName = name;
  773. strWType = type;
  774. strWAddress = addr;
  775. return hrSuccess;
  776. }
  777. /**
  778. * Convert string to e-mail header format, base64 encoded with
  779. * specified charset.
  780. *
  781. * @param[in] input Input string
  782. * @param[in] charset Charset of input string
  783. * @return Output string in e-mail header format
  784. */
  785. std::string ToQuotedBase64Header(const std::string &input,
  786. const std::string &charset)
  787. {
  788. std::string output;
  789. output = "=?"+charset+"?B?";
  790. output += base64_encode((const unsigned char*)input.c_str(), input.length());
  791. output += "?=";
  792. return output;
  793. }
  794. /**
  795. * Convert string to e-mail header format, base64 encoded with
  796. * UTF-8 charset.
  797. *
  798. * @param[in] input Input wide string
  799. * @return Output string in e-mail header format
  800. */
  801. std::string ToQuotedBase64Header(const std::wstring &input)
  802. {
  803. return ToQuotedBase64Header(convert_to<std::string>("UTF-8", input, rawsize(input), CHARSET_WCHAR), "UTF-8");
  804. }
  805. /**
  806. * Convert string to e-mail format, quoted-printable encoded with
  807. * specified charset. Can optionally add markings used in e-mail
  808. * headers. If no special quoted printable entities were required, it
  809. * will return the input string.
  810. *
  811. * @param[in] input Input string
  812. * @param[in] charset Charset of input string
  813. * @param[in] header Use extra markings to make the string valid for e-mail headers
  814. * @param[in] imap Also encode \ and " for IMAP
  815. * @return quoted printable valid string
  816. */
  817. std::string ToQuotedPrintable(const std::string &input,
  818. const std::string &charset, bool header, bool imap)
  819. {
  820. ULONG i;
  821. string tmp;
  822. bool qp = false;
  823. const char digits[] = "0123456789ABCDEF";
  824. if (charset.empty())
  825. return input;
  826. // only email headers have this prefix
  827. if (header)
  828. tmp = "=?"+charset+"?Q?";
  829. for (i = 0; i < input.size(); ++i) {
  830. if ((unsigned char)input[i] > 127) {
  831. tmp.push_back('=');
  832. tmp.push_back(digits[((unsigned char)input[i]>>4)]);
  833. tmp.push_back(digits[((unsigned char)input[i]&0x0F)]);
  834. qp = true;
  835. continue;
  836. }
  837. switch ((unsigned char)input[i]) {
  838. case ' ':
  839. if (header)
  840. tmp.push_back('_'); // only email headers need this, don't set qp marker if only spaces are found
  841. else
  842. tmp.push_back(input[i]); // leave email body unaffected
  843. break;
  844. case '\r':
  845. case '\n':
  846. // no idea how a user would enter a \r\n in the subject, but it s doable of course ;)
  847. if (!header) {
  848. tmp.push_back(input[i]); // leave email body unaffected
  849. break;
  850. }
  851. tmp.push_back('=');
  852. tmp.push_back(digits[((unsigned char)input[i]>>4)]);
  853. tmp.push_back(digits[((unsigned char)input[i]&0x0F)]);
  854. qp = true;
  855. break;
  856. case '\t':
  857. case ',':
  858. case ';':
  859. case ':':
  860. case '_':
  861. case '@':
  862. case '(':
  863. case ')':
  864. case '<':
  865. case '>':
  866. case '[':
  867. case ']':
  868. case '?':
  869. case '=':
  870. tmp.push_back('=');
  871. tmp.push_back(digits[((unsigned char)input[i]>>4)]);
  872. tmp.push_back(digits[((unsigned char)input[i]&0x0F)]);
  873. qp = true;
  874. break;
  875. case '\\':
  876. case '"':
  877. // IMAP requires encoding of these 2 characters
  878. if (!imap) {
  879. tmp.push_back(input[i]);
  880. break;
  881. }
  882. tmp.push_back('=');
  883. tmp.push_back(digits[((unsigned char)input[i]>>4)]);
  884. tmp.push_back(digits[((unsigned char)input[i]&0x0F)]);
  885. qp = true;
  886. break;
  887. default:
  888. tmp.push_back(input[i]);
  889. };
  890. }
  891. if (header)
  892. tmp += "?=";
  893. if (qp)
  894. return tmp;
  895. else
  896. return input; // simple string was good enough
  897. }
  898. /**
  899. * Send a new mail notification to the store.
  900. *
  901. * Sends a notification to the given store with information of the
  902. * given lpMessage. This is to get the new mail popup in Outlook. It
  903. * is different from the create notification.
  904. *
  905. * @param[in] lpMDB The store where lpMessage was just created.
  906. * @param[in] lpMessage The message that was just created and saved.
  907. *
  908. * @return Mapi error code.
  909. */
  910. HRESULT HrNewMailNotification(IMsgStore* lpMDB, IMessage* lpMessage)
  911. {
  912. HRESULT hr = hrSuccess;
  913. // Newmail notify
  914. ULONG cNewMailValues = 0;
  915. memory_ptr<SPropValue> lpNewMailPropArray;
  916. NOTIFICATION sNotification;
  917. // Get notify properties
  918. hr = lpMessage->GetProps(sPropNewMailColumns, 0, &cNewMailValues, &~lpNewMailPropArray);
  919. if (hr != hrSuccess)
  920. return hr;
  921. // Notification type
  922. sNotification.ulEventType = fnevNewMail;
  923. // PR_ENTRYID
  924. sNotification.info.newmail.cbEntryID = lpNewMailPropArray[NEWMAIL_ENTRYID].Value.bin.cb;
  925. sNotification.info.newmail.lpEntryID = (LPENTRYID)lpNewMailPropArray[NEWMAIL_ENTRYID].Value.bin.lpb;
  926. // PR_PARENT_ENTRYID
  927. sNotification.info.newmail.cbParentID = lpNewMailPropArray[NEWMAIL_PARENT_ENTRYID].Value.bin.cb;
  928. sNotification.info.newmail.lpParentID = (LPENTRYID)lpNewMailPropArray[NEWMAIL_PARENT_ENTRYID].Value.bin.lpb;
  929. // flags if unicode
  930. sNotification.info.newmail.ulFlags = 0;
  931. // PR_MESSAGE_CLASS
  932. sNotification.info.newmail.lpszMessageClass = (LPTSTR)lpNewMailPropArray[NEWMAIL_MESSAGE_CLASS].Value.lpszA;
  933. // PR_MESSAGE_FLAGS
  934. sNotification.info.newmail.ulMessageFlags = lpNewMailPropArray[NEWMAIL_MESSAGE_FLAGS].Value.ul;
  935. // TODO: errors of NotifyNewMail should be demoted to a warning?
  936. return lpMDB->NotifyNewMail(&sNotification);
  937. }
  938. static void strupr(char *a)
  939. {
  940. while (*a != '\0') {
  941. *a = toupper(*a);
  942. ++a;
  943. }
  944. }
  945. // Create Search key for recipients
  946. HRESULT HrCreateEmailSearchKey(const char *lpszEmailType,
  947. const char *lpszEmail, ULONG *cb, LPBYTE *lppByte)
  948. {
  949. HRESULT hr = hrSuccess;
  950. memory_ptr<BYTE> lpByte;
  951. ULONG size;
  952. ULONG sizeEmailType;
  953. ULONG sizeEmail;
  954. size = 2; // : and \0
  955. sizeEmailType = (lpszEmailType)?strlen(lpszEmailType) : 0;
  956. sizeEmail = (lpszEmail)?strlen(lpszEmail) : 0;
  957. size = sizeEmailType + sizeEmail + 2; // : and \0
  958. hr = MAPIAllocateBuffer(size, &~lpByte);
  959. if(hr != hrSuccess)
  960. return hr;
  961. memcpy(lpByte, lpszEmailType, sizeEmailType);
  962. *(lpByte + sizeEmailType) = ':';
  963. memcpy(lpByte + sizeEmailType + 1, lpszEmail, sizeEmail);
  964. *(lpByte + size - 1) = 0;
  965. strupr(reinterpret_cast<char *>(lpByte.get()));
  966. *lppByte = lpByte.release();
  967. *cb = size;
  968. return hrSuccess;
  969. }
  970. /**
  971. * Get SMTP emailaddress strings in a set of properties
  972. *
  973. * @param[in] lpSession MAPI Session to use for the lookup (note: uses adressbook from this session)
  974. * @param[in] lpProps Properties to use to lookup email address strings
  975. * @param[in] cValues Number of properties pointed to by lpProps
  976. * @param[in] ulPropTagEntryID Property tag fo the entryid part of the recipient (eg PR_ENTRYID)
  977. * @param[in] ulPropTagName Property tag of the display name part of the recipeint (eg PR_DISPLAY_NAME)
  978. * @param[in] ulPropTagType Property tag of the address type of the recipient (eg PR_ADDRTYPE)
  979. * @param[in] ulPropTagEmailAddress Property tag of the email address part of the recipient (eg PR_EMAIL_ADDRESS)
  980. * @param[out] strName Return string for display name
  981. * @param[out] strType Return string for address type
  982. * @param[out] strEmailAddress Return string for email address
  983. *
  984. * This function is a utility function to retrieve the name/type/address information for a recipient. The recipient
  985. * may be a direct entry in a recipient table or point to an addressbook item.
  986. *
  987. * Data is retrieved from the following places (in order)
  988. * 1. Addressbook (if ulPropTagEntryID is available)
  989. * 2. Passed properties
  990. *
  991. * Also, the address will be resolved to SMTP if steps 1 and 2 did not provide one.
  992. */
  993. HRESULT HrGetAddress(IMAPISession *lpSession, LPSPropValue lpProps, ULONG cValues, ULONG ulPropTagEntryID, ULONG ulPropTagName, ULONG ulPropTagType, ULONG ulPropTagEmailAddress,
  994. std::wstring &strName, std::wstring &strType, std::wstring &strEmailAddress)
  995. {
  996. object_ptr<IAddrBook> lpAdrBook;
  997. if (lpSession == nullptr || lpProps == nullptr)
  998. return MAPI_E_INVALID_PARAMETER;
  999. // If entryid is invalid, there is no need in opening the addressbook,
  1000. // though we still call HrGetAddress with lpAdrBook
  1001. if (PpropFindProp(lpProps, cValues, ulPropTagEntryID))
  1002. // First, try through the entryid
  1003. lpSession->OpenAddressBook(0, nullptr, AB_NO_DIALOG, &~lpAdrBook);
  1004. // fallthrough .. don't mind if no Addressbook could not be created (probably never happens)
  1005. return HrGetAddress(lpAdrBook, lpProps, cValues, ulPropTagEntryID, ulPropTagName, ulPropTagType, ulPropTagEmailAddress, strName, strType, strEmailAddress);
  1006. }
  1007. /**
  1008. * Get SMTP emailaddress strings in a IMessage object
  1009. *
  1010. * @param[in] lpSession MAPI Session to use for the lookup (note: uses adressbook from this session)
  1011. * @param[in] lpMessage IMessage object to get address from
  1012. * @param[in] ulPropTagEntryID Property tag fo the entryid part of the recipient (eg PR_ENTRYID)
  1013. * @param[in] ulPropTagName Property tag of the display name part of the recipeint (eg PR_DISPLAY_NAME)
  1014. * @param[in] ulPropTagType Property tag of the address type of the recipient (eg PR_ADDRTYPE)
  1015. * @param[in] ulPropTagEmailAddress Property tag of the email address part of the recipient (eg PR_EMAIL_ADDRESS)
  1016. * @param[out] strName Return string for display name
  1017. * @param[out] strType Return string for address type
  1018. * @param[out] strEmailAddress Return string for email address
  1019. *
  1020. * This function is a utility function to retrieve the name/type/address information for a recipient. The recipient
  1021. * may be a direct entry in a recipient table or point to an addressbook item.
  1022. *
  1023. * Data is retrieved from the following places (in order)
  1024. * 1. Addressbook (if ulPropTagEntryID is available)
  1025. * 2. Passed properties
  1026. *
  1027. * Also, the address will be resolved to SMTP if steps 1 and 2 did not provide one.
  1028. */
  1029. HRESULT HrGetAddress(IMAPISession *lpSession, IMessage *lpMessage, ULONG ulPropTagEntryID, ULONG ulPropTagName, ULONG ulPropTagType, ULONG ulPropTagEmailAddress,
  1030. std::wstring &strName, std::wstring &strType, std::wstring &strEmailAddress)
  1031. {
  1032. HRESULT hr = hrSuccess;
  1033. SizedSPropTagArray(4, sptaProps) = { 4, { ulPropTagEntryID, ulPropTagName, ulPropTagType, ulPropTagEmailAddress } };
  1034. ULONG cValues = 0;
  1035. memory_ptr<SPropValue> lpProps;
  1036. if (lpSession == nullptr || lpMessage == nullptr)
  1037. return MAPI_E_INVALID_PARAMETER;
  1038. hr = lpMessage->GetProps(sptaProps, 0, &cValues, &~lpProps);
  1039. if(FAILED(hr))
  1040. return hr;
  1041. return HrGetAddress(lpSession, lpProps, cValues, ulPropTagEntryID,
  1042. ulPropTagName, ulPropTagType, ulPropTagEmailAddress, strName,
  1043. strType, strEmailAddress);
  1044. }
  1045. /**
  1046. * Get SMTP emailaddress strings in a IMessage object
  1047. *
  1048. * @param[in] lpAdrBook Addressbook object to use for lookup
  1049. * @param[in] lpMessage IMessage object to get address from
  1050. * @param[in] ulPropTagEntryID Property tag fo the entryid part of the recipient (eg PR_ENTRYID)
  1051. * @param[in] ulPropTagName Property tag of the display name part of the recipeint (eg PR_DISPLAY_NAME)
  1052. * @param[in] ulPropTagType Property tag of the address type of the recipient (eg PR_ADDRTYPE)
  1053. * @param[in] ulPropTagEmailAddress Property tag of the email address part of the recipient (eg PR_EMAIL_ADDRESS)
  1054. * @param[out] strName Return string for display name
  1055. * @param[out] strType Return string for address type
  1056. * @param[out] strEmailAddress Return string for email address
  1057. *
  1058. * This function is a utility function to retrieve the name/type/address information for a recipient. The recipient
  1059. * may be a direct entry in a recipient table or point to an addressbook item.
  1060. *
  1061. * Data is retrieved from the following places (in order)
  1062. * 1. Addressbook (if ulPropTagEntryID is available)
  1063. * 2. Passed properties
  1064. *
  1065. * Also, the address will be resolved to SMTP if steps 1 and 2 did not provide one.
  1066. */
  1067. HRESULT HrGetAddress(LPADRBOOK lpAdrBook, IMessage *lpMessage, ULONG ulPropTagEntryID, ULONG ulPropTagName, ULONG ulPropTagType, ULONG ulPropTagEmailAddress,
  1068. std::wstring &strName, std::wstring &strType, std::wstring &strEmailAddress)
  1069. {
  1070. HRESULT hr = hrSuccess;
  1071. SizedSPropTagArray(4, sptaProps) = { 4, { ulPropTagEntryID, ulPropTagName, ulPropTagType, ulPropTagEmailAddress } };
  1072. ULONG cValues = 0;
  1073. memory_ptr<SPropValue> lpProps;
  1074. if (lpAdrBook == nullptr || lpMessage == nullptr)
  1075. return MAPI_E_INVALID_PARAMETER;
  1076. hr = lpMessage->GetProps(sptaProps, 0, &cValues, &~lpProps);
  1077. if (FAILED(hr))
  1078. return hr;
  1079. return HrGetAddress(lpAdrBook, lpProps, cValues, ulPropTagEntryID,
  1080. ulPropTagName, ulPropTagType, ulPropTagEmailAddress, strName,
  1081. strType, strEmailAddress);
  1082. }
  1083. /*
  1084. * Attempts to get the SMTP email address for an addressbook entity.
  1085. *
  1086. * @param[in] lpAdrBook Addressbook object to use to lookup the address
  1087. * @param[in] strResolve String to resolve
  1088. * @param[in] ulFlags 0 or EMS_AB_ADDRESS_LOOKUP for exact-match only
  1089. * @param[out] strSMTPAddress Resolved SMTP address
  1090. *
  1091. * This function will attempt to resolve the string strReolve to an SMTP address. This can be either a group or user
  1092. * SMTP address, and will only be returned if the match is unambiguous. You may also pass the flags EMS_AB_ADDRESS_LOOKUP
  1093. * to ensure only exact (full-string) matches will be returned.
  1094. *
  1095. * The match is done against various strings including display name and email address.
  1096. */
  1097. static HRESULT HrResolveToSMTP(LPADRBOOK lpAdrBook,
  1098. const std::wstring &strResolve, unsigned int ulFlags,
  1099. std::wstring &strSMTPAddress)
  1100. {
  1101. HRESULT hr = hrSuccess;
  1102. adrlist_ptr lpAdrList;
  1103. const SPropValue *lpEntryID = NULL;
  1104. ULONG ulType = 0;
  1105. object_ptr<IMAPIProp> lpMailUser;
  1106. memory_ptr<SPropValue> lpSMTPAddress, lpEmailAddress;
  1107. hr = MAPIAllocateBuffer(CbNewADRLIST(1), &~lpAdrList);
  1108. if (hr != hrSuccess)
  1109. return hr;
  1110. lpAdrList->cEntries = 1;
  1111. lpAdrList->aEntries[0].cValues = 1;
  1112. hr = MAPIAllocateBuffer(sizeof(SPropValue), (void **)&lpAdrList->aEntries[0].rgPropVals);
  1113. if(hr != hrSuccess)
  1114. return hr;
  1115. lpAdrList->aEntries[0].rgPropVals[0].ulPropTag = PR_DISPLAY_NAME_W;
  1116. lpAdrList->aEntries[0].rgPropVals[0].Value.lpszW = (WCHAR *)strResolve.c_str();
  1117. hr = lpAdrBook->ResolveName(0, ulFlags | MAPI_UNICODE, NULL, lpAdrList);
  1118. if(hr != hrSuccess)
  1119. return hr;
  1120. if (lpAdrList->cEntries != 1)
  1121. return MAPI_E_NOT_FOUND;
  1122. lpEntryID = PCpropFindProp(lpAdrList->aEntries[0].rgPropVals, lpAdrList->aEntries[0].cValues, PR_ENTRYID);
  1123. if (lpEntryID == nullptr)
  1124. return MAPI_E_NOT_FOUND;
  1125. hr = lpAdrBook->OpenEntry(lpEntryID->Value.bin.cb, reinterpret_cast<ENTRYID *>(lpEntryID->Value.bin.lpb), &IID_IMAPIProp, 0, &ulType, &~lpMailUser);
  1126. if (hr != hrSuccess)
  1127. return hr;
  1128. hr = HrGetOneProp(lpMailUser, PR_SMTP_ADDRESS_W, &~lpSMTPAddress);
  1129. if(hr != hrSuccess) {
  1130. // Not always an error
  1131. lpSMTPAddress = NULL;
  1132. hr = hrSuccess;
  1133. }
  1134. if (ulType == MAPI_DISTLIST && (lpSMTPAddress == NULL || wcslen(lpSMTPAddress->Value.lpszW) == 0)) {
  1135. // For a group, we define the SMTP Address to be the same as the name of the group, unless
  1136. // an explicit email address has been set for the group. This sounds unlogical, but it isn't
  1137. // really that strings since whenever we convert to SMTP for a group, we just put the group
  1138. // name as if it were an SMTP address.
  1139. // (Eg. 'To: Everyone; user@domain.com')
  1140. hr = HrGetOneProp(lpMailUser, PR_EMAIL_ADDRESS_W, &~lpEmailAddress);
  1141. if(hr != hrSuccess)
  1142. return hr;
  1143. strSMTPAddress = lpEmailAddress->Value.lpszW;
  1144. } else {
  1145. if (lpSMTPAddress == nullptr)
  1146. return MAPI_E_NOT_FOUND;
  1147. strSMTPAddress = lpSMTPAddress->Value.lpszW;
  1148. }
  1149. return hrSuccess;
  1150. }
  1151. /**
  1152. * Get SMTP emailaddress strings in a set of properties
  1153. *
  1154. * @param[in] lpAdrBook Addressbook object to use to lookup the address
  1155. * @param[in] lpProps Properties to use to lookup email address strings
  1156. * @param[in] cValues Number of properties pointed to by lpProps
  1157. * @param[in] ulPropTagEntryID Property tag fo the entryid part of the recipient (eg PR_ENTRYID)
  1158. * @param[in] ulPropTagName Property tag of the display name part of the recipeint (eg PR_DISPLAY_NAME)
  1159. * @param[in] ulPropTagType Property tag of the address type of the recipient (eg PR_ADDRTYPE)
  1160. * @param[in] ulPropTagEmailAddress Property tag of the email address part of the recipient (eg PR_EMAIL_ADDRESS)
  1161. * @param[out] strName Return string for display name
  1162. * @param[out] strType Return string for address type
  1163. * @param[out] strEmailAddress Return string for email address
  1164. *
  1165. * This function is a utility function to retrieve the name/type/address information for a recipient. The recipient
  1166. * may be a direct entry in a recipient table or point to an addressbook item.
  1167. *
  1168. * Data is retrieved from the following places (in order)
  1169. * 1. Addressbook (if ulPropTagEntryID is available)
  1170. * 2. Passed properties
  1171. *
  1172. * Also, the address will be resolved to SMTP if steps 1 and 2 did not provide one.
  1173. */
  1174. HRESULT HrGetAddress(LPADRBOOK lpAdrBook, LPSPropValue lpProps, ULONG cValues, ULONG ulPropTagEntryID, ULONG ulPropTagName, ULONG ulPropTagType, ULONG ulPropTagEmailAddress,
  1175. std::wstring &strName, std::wstring &strType, std::wstring &strEmailAddress)
  1176. {
  1177. HRESULT hr = hrSuccess;
  1178. const SPropValue *lpEntryID = NULL;
  1179. const SPropValue *lpName = NULL;
  1180. const SPropValue *lpType = NULL;
  1181. const SPropValue *lpAddress = NULL;
  1182. std::wstring strSMTPAddress;
  1183. convert_context converter;
  1184. strName.clear();
  1185. strType.clear();
  1186. strEmailAddress.clear();
  1187. if (lpProps && cValues) {
  1188. lpEntryID = PCpropFindProp(lpProps, cValues, ulPropTagEntryID);
  1189. lpName = PCpropFindProp(lpProps, cValues, ulPropTagName);
  1190. lpType = PCpropFindProp(lpProps, cValues, ulPropTagType);
  1191. lpAddress = PCpropFindProp(lpProps, cValues, ulPropTagEmailAddress);
  1192. if (lpEntryID && PROP_TYPE(lpEntryID->ulPropTag) != PT_BINARY)
  1193. lpEntryID = NULL;
  1194. if (lpName && PROP_TYPE(lpName->ulPropTag) != PT_STRING8 && PROP_TYPE(lpName->ulPropTag) != PT_UNICODE)
  1195. lpName = NULL;
  1196. if (lpType && PROP_TYPE(lpType->ulPropTag) != PT_STRING8 && PROP_TYPE(lpType->ulPropTag) != PT_UNICODE)
  1197. lpType = NULL;
  1198. if (lpAddress && PROP_TYPE(lpAddress->ulPropTag) != PT_STRING8 && PROP_TYPE(lpAddress->ulPropTag) != PT_UNICODE)
  1199. lpAddress = NULL;
  1200. }
  1201. if (lpEntryID == NULL || lpAdrBook == NULL ||
  1202. HrGetAddress(lpAdrBook, (LPENTRYID)lpEntryID->Value.bin.lpb, lpEntryID->Value.bin.cb, strName, strType, strEmailAddress) != hrSuccess)
  1203. {
  1204. // EntryID failed, try fallback
  1205. if (lpName) {
  1206. if (PROP_TYPE(lpName->ulPropTag) == PT_UNICODE)
  1207. strName = lpName->Value.lpszW;
  1208. else
  1209. strName = converter.convert_to<wstring>(lpName->Value.lpszA);
  1210. }
  1211. if (lpType) {
  1212. if (PROP_TYPE(lpType->ulPropTag) == PT_UNICODE)
  1213. strType = lpType->Value.lpszW;
  1214. else
  1215. strType = converter.convert_to<wstring>(lpType->Value.lpszA);
  1216. }
  1217. if (lpAddress) {
  1218. if (PROP_TYPE(lpAddress->ulPropTag) == PT_UNICODE)
  1219. strEmailAddress = lpAddress->Value.lpszW;
  1220. else
  1221. strEmailAddress = converter.convert_to<wstring>(lpAddress->Value.lpszA);
  1222. }
  1223. }
  1224. // If we don't have an SMTP address yet, try to resolve the item to get the SMTP address
  1225. if (lpAdrBook != nullptr && lpType != nullptr &&
  1226. lpAddress != nullptr && wcscasecmp(strType.c_str(), L"SMTP") != 0 &&
  1227. HrResolveToSMTP(lpAdrBook, strEmailAddress, EMS_AB_ADDRESS_LOOKUP, strSMTPAddress) == hrSuccess)
  1228. strEmailAddress = strSMTPAddress;
  1229. return hr;
  1230. }
  1231. /**
  1232. *
  1233. * Gets address from addressbook for specified GAB entryid
  1234. *
  1235. * @param[in] lpAdrBook Addressbook object to use for the lookup
  1236. * @param[in] lpEntryID EntryID of the object to lookup in the addressbook
  1237. * @param[in] cbEntryID Number of bytes pointed to by lpEntryID
  1238. * @param[out] strName Return for display name
  1239. * @param[out] strType Return for address type (ZARAFA)
  1240. * @param[out] strEmailAddress Return for email address
  1241. *
  1242. * This function opens the passed entryid on the passed addressbook and retrieves the recipient
  1243. * address parts to be returned to the caller. If an SMTP address is available, returns the SMTP
  1244. * address for the user, otherwise the ZARAFA addresstype and address is returned.
  1245. */
  1246. HRESULT HrGetAddress(LPADRBOOK lpAdrBook, LPENTRYID lpEntryID, ULONG cbEntryID, std::wstring &strName, std::wstring &strType, std::wstring &strEmailAddress)
  1247. {
  1248. HRESULT hr = hrSuccess;
  1249. object_ptr<IMailUser> lpMailUser;
  1250. ULONG ulType = 0;
  1251. ULONG cMailUserValues = 0;
  1252. memory_ptr<SPropValue> lpMailUserProps;
  1253. static constexpr const SizedSPropTagArray(4, sptaAddressProps) =
  1254. {4, {PR_DISPLAY_NAME_W, PR_ADDRTYPE_W, PR_EMAIL_ADDRESS_W,
  1255. PR_SMTP_ADDRESS_W}};
  1256. if (lpAdrBook == nullptr || lpEntryID == nullptr)
  1257. return MAPI_E_INVALID_PARAMETER;
  1258. hr = lpAdrBook->OpenEntry(cbEntryID, lpEntryID, &IID_IMailUser, 0, &ulType, &~lpMailUser);
  1259. if (hr != hrSuccess)
  1260. return hr;
  1261. hr = lpMailUser->GetProps(sptaAddressProps, 0, &cMailUserValues, &~lpMailUserProps);
  1262. if (FAILED(hr))
  1263. return hr;
  1264. if(lpMailUserProps[0].ulPropTag == PR_DISPLAY_NAME_W)
  1265. strName = lpMailUserProps[0].Value.lpszW;
  1266. if(lpMailUserProps[1].ulPropTag == PR_ADDRTYPE_W)
  1267. strType = lpMailUserProps[1].Value.lpszW;
  1268. if(lpMailUserProps[3].ulPropTag == PR_SMTP_ADDRESS_W) {
  1269. strEmailAddress = lpMailUserProps[3].Value.lpszW;
  1270. strType = L"SMTP";
  1271. }
  1272. else if(lpMailUserProps[2].ulPropTag == PR_EMAIL_ADDRESS_W)
  1273. strEmailAddress = lpMailUserProps[2].Value.lpszW;
  1274. return hrSuccess;
  1275. }
  1276. HRESULT DoSentMail(IMAPISession *lpSession, IMsgStore *lpMDBParam, ULONG ulFlags, LPMESSAGE lpMessage) {
  1277. HRESULT hr = hrSuccess;
  1278. object_ptr<IMsgStore> lpMDB;
  1279. object_ptr<IMAPIFolder> lpFolder;
  1280. ENTRYLIST sMsgList;
  1281. SBinary sEntryID;
  1282. memory_ptr<SPropValue> lpPropValue;
  1283. ULONG cValues = 0;
  1284. ULONG ulType = 0;
  1285. enum esPropDoSentMail{ DSM_ENTRYID, DSM_PARENT_ENTRYID, DSM_SENTMAIL_ENTRYID, DSM_DELETE_AFTER_SUBMIT, DSM_STORE_ENTRYID};
  1286. static constexpr const SizedSPropTagArray(5, sPropDoSentMail) =
  1287. {5, {PR_ENTRYID, PR_PARENT_ENTRYID, PR_SENTMAIL_ENTRYID,
  1288. PR_DELETE_AFTER_SUBMIT, PR_STORE_ENTRYID}};
  1289. assert(lpSession != NULL || lpMDBParam != NULL);
  1290. // Check incomming parameter
  1291. if (lpMessage == nullptr)
  1292. return MAPI_E_INVALID_OBJECT;
  1293. // Get Sentmail properties
  1294. hr = lpMessage->GetProps(sPropDoSentMail, 0, &cValues, &~lpPropValue);
  1295. if(FAILED(hr) ||
  1296. (lpPropValue[DSM_SENTMAIL_ENTRYID].ulPropTag != PR_SENTMAIL_ENTRYID &&
  1297. lpPropValue[DSM_DELETE_AFTER_SUBMIT].ulPropTag != PR_DELETE_AFTER_SUBMIT)
  1298. )
  1299. {
  1300. lpMessage->Release();
  1301. // Ignore error, leave the mail where it is
  1302. return hrSuccess;
  1303. }else if(lpPropValue[DSM_ENTRYID].ulPropTag != PR_ENTRYID ||
  1304. lpPropValue[DSM_PARENT_ENTRYID].ulPropTag != PR_PARENT_ENTRYID ||
  1305. lpPropValue[DSM_STORE_ENTRYID].ulPropTag != PR_STORE_ENTRYID)
  1306. {
  1307. // Those properties always needed
  1308. lpMessage->Release();
  1309. return MAPI_E_NOT_FOUND;
  1310. }
  1311. lpMessage->Release(); // Yes, we release the message for the caller
  1312. if (lpMDBParam == NULL)
  1313. hr = lpSession->OpenMsgStore(0, lpPropValue[DSM_STORE_ENTRYID].Value.bin.cb, reinterpret_cast<ENTRYID *>(lpPropValue[DSM_STORE_ENTRYID].Value.bin.lpb), nullptr, MDB_WRITE | MDB_NO_DIALOG | MDB_NO_MAIL |MDB_TEMPORARY, &~lpMDB);
  1314. else
  1315. hr = lpMDBParam->QueryInterface(IID_IMsgStore, &~lpMDB);
  1316. if(hr != hrSuccess)
  1317. return hr;
  1318. sEntryID.cb = lpPropValue[DSM_ENTRYID].Value.bin.cb;
  1319. sEntryID.lpb = lpPropValue[DSM_ENTRYID].Value.bin.lpb;
  1320. sMsgList.cValues = 1;
  1321. sMsgList.lpbin = &sEntryID;
  1322. // Handle PR_SENTMAIL_ENTRYID
  1323. if(lpPropValue[DSM_SENTMAIL_ENTRYID].ulPropTag == PR_SENTMAIL_ENTRYID)
  1324. {
  1325. //Open Sentmail Folder
  1326. hr = lpMDB->OpenEntry(lpPropValue[DSM_SENTMAIL_ENTRYID].Value.bin.cb, reinterpret_cast<ENTRYID *>(lpPropValue[DSM_SENTMAIL_ENTRYID].Value.bin.lpb), nullptr, MAPI_MODIFY, &ulType, &~lpFolder);
  1327. if(hr != hrSuccess)
  1328. return hr;
  1329. // Move Message
  1330. hr = lpFolder->CopyMessages(&sMsgList, &IID_IMAPIFolder, lpFolder, 0, NULL, MESSAGE_MOVE);
  1331. }
  1332. // Handle PR_DELETE_AFTER_SUBMIT
  1333. if(lpPropValue[DSM_DELETE_AFTER_SUBMIT].ulPropTag == PR_DELETE_AFTER_SUBMIT && lpPropValue[DSM_DELETE_AFTER_SUBMIT].Value.b == TRUE)
  1334. {
  1335. if(lpFolder == NULL)
  1336. {
  1337. // Open parent folder of the sent message
  1338. hr = lpMDB->OpenEntry(lpPropValue[DSM_PARENT_ENTRYID].Value.bin.cb, reinterpret_cast<ENTRYID *>(lpPropValue[DSM_PARENT_ENTRYID].Value.bin.lpb), nullptr, MAPI_MODIFY, &ulType, &~lpFolder);
  1339. if(hr != hrSuccess)
  1340. return hr;
  1341. }
  1342. // Delete Message
  1343. hr = lpFolder->DeleteMessages(&sMsgList, 0, NULL, 0);
  1344. }
  1345. return hr;
  1346. }
  1347. // This is a class that implements IMAPIProp's GetProps(), and nothing else. Its data
  1348. // is retrieved from the passed lpProps/cValues property array
  1349. class ECRowWrapper _kc_final : public IMAPIProp {
  1350. public:
  1351. ECRowWrapper(LPSPropValue lpProps, ULONG cValues) : m_cValues(cValues), m_lpProps(lpProps) {};
  1352. ULONG __stdcall AddRef(void) _kc_override { return 1; } // no ref. counting
  1353. ULONG __stdcall Release(void) _kc_override { return 1; }
  1354. HRESULT __stdcall QueryInterface(const IID &iid, LPVOID *lpvoid) _kc_override { return MAPI_E_INTERFACE_NOT_SUPPORTED; }
  1355. HRESULT __stdcall GetLastError(HRESULT hResult, ULONG ulFlags, LPMAPIERROR *lppMAPIError) _kc_override { return MAPI_E_NOT_FOUND; }
  1356. HRESULT __stdcall SaveChanges(ULONG ulFlags) _kc_override { return MAPI_E_NO_SUPPORT; }
  1357. HRESULT __stdcall GetProps(const SPropTagArray *lpTags, ULONG ulFlags, ULONG *lpcValues, SPropValue **lppProps) _kc_override
  1358. {
  1359. HRESULT hr = hrSuccess;
  1360. BOOL bError;
  1361. ULONG i = 0;
  1362. LPSPropValue lpProps = NULL;
  1363. convert_context converter;
  1364. if ((hr = MAPIAllocateBuffer(sizeof(SPropValue) * lpTags->cValues, (void **) &lpProps)) != hrSuccess)
  1365. return hr;
  1366. for (i = 0; i<lpTags->cValues; ++i) {
  1367. bError = FALSE;
  1368. auto lpFind = PCpropFindProp(m_lpProps, m_cValues, CHANGE_PROP_TYPE(lpTags->aulPropTag[i], PT_UNSPECIFIED));
  1369. if (lpFind == nullptr || PROP_TYPE(lpFind->ulPropTag) == PT_ERROR) {
  1370. bError = true;
  1371. } else if (PROP_TYPE(lpFind->ulPropTag) == PT_STRING8 && PROP_TYPE(lpTags->aulPropTag[i]) == PT_UNICODE) {
  1372. lpProps[i].ulPropTag = lpTags->aulPropTag[i];
  1373. std::wstring wstrTmp = converter.convert_to<std::wstring>(lpFind->Value.lpszA);
  1374. hr = MAPIAllocateMore((wstrTmp.length() + 1) * sizeof *lpProps[i].Value.lpszW, lpProps, reinterpret_cast<void **>(&lpProps[i].Value.lpszW));
  1375. if (hr != hrSuccess)
  1376. return hr;
  1377. wcscpy(lpProps[i].Value.lpszW, wstrTmp.c_str());
  1378. } else if (PROP_TYPE(lpFind->ulPropTag) == PT_UNICODE && PROP_TYPE(lpTags->aulPropTag[i]) == PT_STRING8) {
  1379. lpProps[i].ulPropTag = lpTags->aulPropTag[i];
  1380. std::string strTmp = converter.convert_to<std::string>(lpFind->Value.lpszW);
  1381. hr = MAPIAllocateMore(strTmp.length() + 1, lpProps, reinterpret_cast<void **>(&lpProps[i].Value.lpszA));
  1382. if (hr != hrSuccess)
  1383. return hr;
  1384. strcpy(lpProps[i].Value.lpszA, strTmp.c_str());
  1385. } else if (PROP_TYPE(lpFind->ulPropTag) != PROP_TYPE(lpTags->aulPropTag[i])) {
  1386. bError = TRUE;
  1387. } else if (Util::HrCopyProperty(&lpProps[i], lpFind, lpProps) != hrSuccess) {
  1388. bError = TRUE;
  1389. }
  1390. if(bError) {
  1391. lpProps[i].ulPropTag = PROP_TAG(PT_ERROR, PROP_ID(lpTags->aulPropTag[i]));
  1392. lpProps[i].Value.err = MAPI_E_NOT_FOUND;
  1393. hr = MAPI_W_ERRORS_RETURNED;
  1394. }
  1395. }
  1396. *lppProps = lpProps;
  1397. *lpcValues = lpTags->cValues;
  1398. return hr;
  1399. };
  1400. HRESULT __stdcall GetPropList(ULONG ulFlags, LPSPropTagArray *lppTags) _kc_override { return MAPI_E_NO_SUPPORT; }
  1401. HRESULT __stdcall OpenProperty(ULONG ulPropTag, LPCIID lpiid, ULONG ulInterfaceOptions, ULONG ulFlags, LPUNKNOWN *lppUnk) _kc_override { return MAPI_E_NO_SUPPORT; }
  1402. HRESULT __stdcall SetProps(ULONG cValues, const SPropValue *lpProps, SPropProblemArray **lppProblems) _kc_override { return MAPI_E_NO_SUPPORT; }
  1403. HRESULT __stdcall DeleteProps(const SPropTagArray *, SPropProblemArray **) _kc_override { return MAPI_E_NO_SUPPORT; }
  1404. HRESULT __stdcall CopyTo(ULONG ciidExclude, LPCIID rgiidExclude, const SPropTagArray *lpExcludeProps, ULONG ulUIParam, LPMAPIPROGRESS lpProgress, LPCIID lpInterface, void *lpDestObj, ULONG ulFlags, SPropProblemArray **lppProblems) _kc_override { return MAPI_E_NO_SUPPORT; }
  1405. HRESULT __stdcall CopyProps(const SPropTagArray *lpIncludeProps, ULONG ulUIParam, LPMAPIPROGRESS lpProgress, LPCIID lpInterface, LPVOID lpDestObj, ULONG ulFlags, LPSPropProblemArray *lppProblems) _kc_override { return MAPI_E_NO_SUPPORT; }
  1406. HRESULT __stdcall GetNamesFromIDs( LPSPropTagArray *lppPropTags, LPGUID lpPropSetGuid, ULONG ulFlags, ULONG *lpcPropNames, LPMAPINAMEID **lpppPropNames) _kc_override { return MAPI_E_NO_SUPPORT; }
  1407. HRESULT __stdcall GetIDsFromNames( ULONG cPropNames, LPMAPINAMEID *lppPropNames, ULONG ulFlags, LPSPropTagArray *lppPropTags) _kc_override { return MAPI_E_NO_SUPPORT; }
  1408. private:
  1409. ULONG m_cValues;
  1410. LPSPropValue m_lpProps;
  1411. };
  1412. static HRESULT TestRelop(ULONG relop, int result, bool* fMatch)
  1413. {
  1414. HRESULT hr = hrSuccess;
  1415. switch (relop) {
  1416. case RELOP_LT:
  1417. *fMatch = result < 0;
  1418. break;
  1419. case RELOP_LE:
  1420. *fMatch = result <= 0;
  1421. break;
  1422. case RELOP_GT:
  1423. *fMatch = result > 0;
  1424. break;
  1425. case RELOP_GE:
  1426. *fMatch = result >= 0;
  1427. break;
  1428. case RELOP_EQ:
  1429. *fMatch = result == 0;
  1430. break;
  1431. case RELOP_NE:
  1432. *fMatch = result != 0;
  1433. break;
  1434. case RELOP_RE:
  1435. default:
  1436. *fMatch = false;
  1437. hr = MAPI_E_TOO_COMPLEX;
  1438. break;
  1439. };
  1440. return hr;
  1441. }
  1442. #define RESTRICT_MAX_RECURSE_LEVEL 16
  1443. static HRESULT GetRestrictTagsRecursive(const SRestriction *lpRestriction,
  1444. std::list<unsigned int> *lpList, ULONG ulLevel)
  1445. {
  1446. HRESULT hr = hrSuccess;
  1447. ULONG i;
  1448. if(ulLevel > RESTRICT_MAX_RECURSE_LEVEL)
  1449. return MAPI_E_TOO_COMPLEX;
  1450. switch(lpRestriction->rt) {
  1451. case RES_AND:
  1452. for (i = 0; i < lpRestriction->res.resAnd.cRes; ++i) {
  1453. hr = GetRestrictTagsRecursive(&lpRestriction->res.resAnd.lpRes[i], lpList, ulLevel+1);
  1454. if (hr != hrSuccess)
  1455. return hr;
  1456. }
  1457. break;
  1458. case RES_OR:
  1459. for (i = 0; i < lpRestriction->res.resOr.cRes; ++i) {
  1460. hr = GetRestrictTagsRecursive(&lpRestriction->res.resOr.lpRes[i], lpList, ulLevel+1);
  1461. if (hr != hrSuccess)
  1462. return hr;
  1463. }
  1464. break;
  1465. case RES_NOT:
  1466. hr = GetRestrictTagsRecursive(lpRestriction->res.resNot.lpRes, lpList, ulLevel+1);
  1467. break;
  1468. case RES_CONTENT:
  1469. lpList->push_back(lpRestriction->res.resContent.ulPropTag);
  1470. lpList->push_back(lpRestriction->res.resContent.lpProp->ulPropTag);
  1471. break;
  1472. case RES_PROPERTY:
  1473. lpList->push_back(lpRestriction->res.resProperty.ulPropTag);
  1474. lpList->push_back(lpRestriction->res.resProperty.lpProp->ulPropTag);
  1475. break;
  1476. case RES_COMPAREPROPS:
  1477. lpList->push_back(lpRestriction->res.resCompareProps.ulPropTag1);
  1478. lpList->push_back(lpRestriction->res.resCompareProps.ulPropTag2);
  1479. break;
  1480. case RES_BITMASK:
  1481. lpList->push_back(lpRestriction->res.resBitMask.ulPropTag);
  1482. break;
  1483. case RES_SIZE:
  1484. lpList->push_back(lpRestriction->res.resSize.ulPropTag);
  1485. break;
  1486. case RES_EXIST:
  1487. lpList->push_back(lpRestriction->res.resExist.ulPropTag);
  1488. break;
  1489. case RES_SUBRESTRICTION:
  1490. lpList->push_back(lpRestriction->res.resSub.ulSubObject);
  1491. break;
  1492. case RES_COMMENT:
  1493. hr = GetRestrictTagsRecursive(lpRestriction->res.resComment.lpRes, lpList, ulLevel+1);
  1494. break;
  1495. }
  1496. return hr;
  1497. }
  1498. static HRESULT GetRestrictTags(const SRestriction *lpRestriction,
  1499. LPSPropTagArray *lppTags)
  1500. {
  1501. std::list<unsigned int> lstTags;
  1502. ULONG n = 0;
  1503. LPSPropTagArray lpTags = NULL;
  1504. HRESULT hr = GetRestrictTagsRecursive(lpRestriction, &lstTags, 0);
  1505. if(hr != hrSuccess)
  1506. return hr;
  1507. if ((hr = MAPIAllocateBuffer(CbNewSPropTagArray(lstTags.size()), (void **) &lpTags)) != hrSuccess)
  1508. return hr;
  1509. lpTags->cValues = lstTags.size();
  1510. lstTags.sort();
  1511. lstTags.unique();
  1512. for (auto tag : lstTags)
  1513. lpTags->aulPropTag[n++] = tag;
  1514. lpTags->cValues = n;
  1515. *lppTags = lpTags;
  1516. return hrSuccess;
  1517. }
  1518. HRESULT TestRestriction(LPSRestriction lpCondition, ULONG cValues, LPSPropValue lpPropVals, const ECLocale &locale, ULONG ulLevel) {
  1519. ECRowWrapper lpRowWrapper(lpPropVals, cValues);
  1520. return TestRestriction(lpCondition, static_cast<IMAPIProp *>(&lpRowWrapper), locale, ulLevel);
  1521. }
  1522. HRESULT TestRestriction(LPSRestriction lpCondition, IMAPIProp *lpMessage, const ECLocale &locale, ULONG ulLevel) {
  1523. HRESULT hr = hrSuccess;
  1524. ULONG c;
  1525. bool fMatch = false;
  1526. memory_ptr<SPropValue> lpProp, lpProp2;
  1527. ULONG ulPropType;
  1528. int result;
  1529. unsigned int ulSize;
  1530. object_ptr<IMAPITable> lpTable;
  1531. memory_ptr<SPropTagArray> lpTags;
  1532. if (ulLevel > RESTRICT_MAX_RECURSE_LEVEL)
  1533. return MAPI_E_TOO_COMPLEX;
  1534. if (!lpCondition)
  1535. return MAPI_E_INVALID_PARAMETER;
  1536. switch (lpCondition->rt) {
  1537. // loops
  1538. case RES_AND:
  1539. for (c = 0; c < lpCondition->res.resAnd.cRes; ++c) {
  1540. hr = TestRestriction(&lpCondition->res.resAnd.lpRes[c], lpMessage, locale, ulLevel+1);
  1541. if (hr != hrSuccess) {
  1542. fMatch = false;
  1543. break;
  1544. }
  1545. fMatch = true;
  1546. }
  1547. break;
  1548. case RES_OR:
  1549. for (c = 0; c < lpCondition->res.resAnd.cRes; ++c) {
  1550. hr = TestRestriction(&lpCondition->res.resOr.lpRes[c], lpMessage, locale, ulLevel+1);
  1551. if (hr == hrSuccess) {
  1552. fMatch = true;
  1553. break;
  1554. } else if (hr == MAPI_E_TOO_COMPLEX)
  1555. break;
  1556. }
  1557. break;
  1558. case RES_NOT:
  1559. hr = TestRestriction(lpCondition->res.resNot.lpRes, lpMessage, locale, ulLevel+1);
  1560. if (hr != MAPI_E_TOO_COMPLEX) {
  1561. fMatch = hr != hrSuccess;
  1562. hr = fMatch ? hrSuccess : MAPI_E_NOT_FOUND;
  1563. }
  1564. break;
  1565. // Prop compares
  1566. case RES_CONTENT: {
  1567. // @todo: support PT_MV_STRING8, PT_MV_UNICODE and PT_MV_BINARY
  1568. // fuzzy string compare
  1569. if (PROP_TYPE(lpCondition->res.resContent.ulPropTag) != PT_STRING8 &&
  1570. PROP_TYPE(lpCondition->res.resContent.ulPropTag) != PT_UNICODE &&
  1571. PROP_TYPE(lpCondition->res.resContent.ulPropTag) != PT_BINARY) {
  1572. hr = MAPI_E_TOO_COMPLEX;
  1573. break;
  1574. }
  1575. ulPropType = PROP_TYPE(lpCondition->res.resContent.ulPropTag);
  1576. hr = HrGetOneProp(lpMessage, lpCondition->res.resContent.ulPropTag, &~lpProp);
  1577. if (hr != hrSuccess)
  1578. break;
  1579. char *lpSearchString = NULL, *lpSearchData = NULL;
  1580. wchar_t *lpwSearchString = NULL, *lpwSearchData = NULL;
  1581. unsigned int ulSearchStringSize = 0, ulSearchDataSize = 0;
  1582. ULONG ulFuzzyLevel;
  1583. if (ulPropType == PT_STRING8) {
  1584. lpSearchString = lpCondition->res.resContent.lpProp->Value.lpszA;
  1585. lpSearchData = lpProp->Value.lpszA;
  1586. ulSearchStringSize = lpSearchString?strlen(lpSearchString):0;
  1587. ulSearchDataSize = lpSearchData?strlen(lpSearchData):0;
  1588. } else if (ulPropType == PT_UNICODE) {
  1589. lpwSearchString = lpCondition->res.resContent.lpProp->Value.lpszW;
  1590. lpwSearchData = lpProp->Value.lpszW;
  1591. ulSearchStringSize = lpwSearchString?wcslen(lpwSearchString):0;
  1592. ulSearchDataSize = lpwSearchData?wcslen(lpwSearchData):0;
  1593. } else {
  1594. // PT_BINARY
  1595. lpSearchString = (char*)lpCondition->res.resContent.lpProp->Value.bin.lpb;
  1596. lpSearchData = (char*)lpProp->Value.bin.lpb;
  1597. ulSearchStringSize = lpCondition->res.resContent.lpProp->Value.bin.cb;
  1598. ulSearchDataSize = lpProp->Value.bin.cb;
  1599. }
  1600. ulFuzzyLevel = lpCondition->res.resContent.ulFuzzyLevel;
  1601. switch (ulFuzzyLevel & 0xFFFF) {
  1602. case FL_FULLSTRING:
  1603. if (ulSearchDataSize != ulSearchStringSize)
  1604. break;
  1605. if ((ulPropType == PT_STRING8 && (ulFuzzyLevel & FL_IGNORECASE) && lpSearchData != NULL && lpSearchString != NULL && str_iequals(lpSearchData, lpSearchString, locale)) ||
  1606. (ulPropType == PT_STRING8 && (ulFuzzyLevel & FL_IGNORECASE) == 0 && lpSearchData != NULL && lpSearchString != NULL && str_equals(lpSearchData, lpSearchString, locale)) ||
  1607. (ulPropType == PT_UNICODE && (ulFuzzyLevel & FL_IGNORECASE) && lpwSearchData != NULL && lpwSearchString != NULL && wcs_iequals(lpwSearchData, lpwSearchString, locale)) ||
  1608. (ulPropType == PT_UNICODE && (ulFuzzyLevel & FL_IGNORECASE) == 0 && lpwSearchData != NULL && lpwSearchString != NULL && wcs_equals(lpwSearchData, lpwSearchString, locale)) ||
  1609. (ulPropType == PT_BINARY && lpSearchData != NULL && lpwSearchString != NULL && memcmp(lpSearchData, lpSearchString, ulSearchDataSize) == 0))
  1610. fMatch = true;
  1611. break;
  1612. case FL_PREFIX:
  1613. if (ulSearchDataSize < ulSearchStringSize)
  1614. break;
  1615. if ((ulPropType == PT_STRING8 && (ulFuzzyLevel & FL_IGNORECASE) && lpSearchData != NULL && lpSearchString != NULL && str_istartswith(lpSearchData, lpSearchString, locale)) ||
  1616. (ulPropType == PT_STRING8 && (ulFuzzyLevel & FL_IGNORECASE) == 0 && lpSearchData != NULL && lpSearchString != NULL && str_startswith(lpSearchData, lpSearchString, locale)) ||
  1617. (ulPropType == PT_UNICODE && (ulFuzzyLevel & FL_IGNORECASE) && lpwSearchData != NULL && lpwSearchString != NULL && wcs_istartswith(lpwSearchData, lpwSearchString, locale)) ||
  1618. (ulPropType == PT_UNICODE && (ulFuzzyLevel & FL_IGNORECASE) == 0 && lpwSearchData != NULL && lpwSearchString != NULL && wcs_startswith(lpwSearchData, lpwSearchString, locale)) ||
  1619. (ulPropType == PT_BINARY && lpSearchData != NULL && lpSearchString != NULL && memcmp(lpSearchData, lpSearchString, ulSearchDataSize) == 0))
  1620. fMatch = true;
  1621. break;
  1622. case FL_SUBSTRING:
  1623. if ((ulPropType == PT_STRING8 && (ulFuzzyLevel & FL_IGNORECASE) && lpSearchData != NULL && lpSearchString != NULL && str_icontains(lpSearchData, lpSearchString, locale)) ||
  1624. (ulPropType == PT_STRING8 && (ulFuzzyLevel & FL_IGNORECASE) == 0 && lpSearchData != NULL && lpSearchString != NULL && str_contains(lpSearchData, lpSearchString, locale)) ||
  1625. (ulPropType == PT_UNICODE && (ulFuzzyLevel & FL_IGNORECASE) && lpwSearchData != NULL && lpwSearchString != NULL && wcs_icontains(lpwSearchData, lpwSearchString, locale)) ||
  1626. (ulPropType == PT_UNICODE && (ulFuzzyLevel & FL_IGNORECASE) == 0 && lpwSearchData != NULL && lpwSearchString != NULL && wcs_contains(lpwSearchData, lpwSearchString, locale)) ||
  1627. (ulPropType == PT_BINARY && lpSearchData != NULL && lpSearchString != NULL && memsubstr(lpSearchData, ulSearchDataSize, lpSearchString, ulSearchStringSize) == 0))
  1628. fMatch = true;
  1629. break;
  1630. }
  1631. break;
  1632. }
  1633. case RES_PROPERTY:
  1634. if(PROP_TYPE(lpCondition->res.resProperty.ulPropTag) != PROP_TYPE(lpCondition->res.resProperty.lpProp->ulPropTag)) {
  1635. // cannot compare two different types
  1636. hr = MAPI_E_TOO_COMPLEX;
  1637. break;
  1638. }
  1639. hr = HrGetOneProp(lpMessage, lpCondition->res.resProperty.ulPropTag, &~lpProp);
  1640. if (hr != hrSuccess)
  1641. break;
  1642. Util::CompareProp(lpProp, lpCondition->res.resProperty.lpProp, locale, &result);
  1643. hr = TestRelop(lpCondition->res.resProperty.relop, result, &fMatch);
  1644. break;
  1645. case RES_COMPAREPROPS:
  1646. if(PROP_TYPE(lpCondition->res.resCompareProps.ulPropTag1) != PROP_TYPE(lpCondition->res.resCompareProps.ulPropTag2)) {
  1647. // cannot compare two different types
  1648. hr = MAPI_E_TOO_COMPLEX;
  1649. break;
  1650. }
  1651. hr = HrGetOneProp(lpMessage, lpCondition->res.resCompareProps.ulPropTag1, &~lpProp);
  1652. if (hr != hrSuccess)
  1653. break;
  1654. hr = HrGetOneProp(lpMessage, lpCondition->res.resCompareProps.ulPropTag2, &~lpProp2);
  1655. if (hr != hrSuccess)
  1656. break;
  1657. Util::CompareProp(lpProp, lpProp2, locale, &result);
  1658. hr = TestRelop(lpCondition->res.resProperty.relop, result, &fMatch);
  1659. break;
  1660. case RES_BITMASK:
  1661. if (PROP_TYPE(lpCondition->res.resBitMask.ulPropTag) != PT_LONG) {
  1662. hr = MAPI_E_TOO_COMPLEX;
  1663. break;
  1664. }
  1665. hr = HrGetOneProp(lpMessage, lpCondition->res.resBitMask.ulPropTag, &~lpProp);
  1666. if (hr != hrSuccess)
  1667. break;
  1668. fMatch = (lpProp->Value.ul & lpCondition->res.resBitMask.ulMask) == 0;
  1669. if (lpCondition->res.resBitMask.relBMR == BMR_NEZ)
  1670. fMatch = !fMatch;
  1671. break;
  1672. case RES_SIZE:
  1673. hr = HrGetOneProp(lpMessage, lpCondition->res.resSize.ulPropTag, &~lpProp);
  1674. if (hr != hrSuccess)
  1675. break;
  1676. ulSize = Util::PropSize(lpProp);
  1677. result = ulSize - lpCondition->res.resSize.cb;
  1678. hr = TestRelop(lpCondition->res.resSize.relop, result, &fMatch);
  1679. break;
  1680. case RES_EXIST:
  1681. hr = HrGetOneProp(lpMessage, lpCondition->res.resExist.ulPropTag, &~lpProp);
  1682. if (hr != hrSuccess)
  1683. break;
  1684. fMatch = true;
  1685. break;
  1686. case RES_SUBRESTRICTION:
  1687. // A subrestriction is basically an OR restriction over all the rows in a specific
  1688. // table. We currently support the attachment table (PR_MESSAGE_ATTACHMENTS) and the
  1689. // recipient table (PR_MESSAGE_RECIPIENTS) here.
  1690. hr = lpMessage->OpenProperty(lpCondition->res.resSub.ulSubObject, &IID_IMAPITable, 0, 0, &~lpTable);
  1691. if(hr != hrSuccess) {
  1692. hr = MAPI_E_TOO_COMPLEX;
  1693. goto exit;
  1694. }
  1695. // Get a list of properties we may be needing
  1696. hr = GetRestrictTags(lpCondition->res.resSub.lpRes, &~ lpTags);
  1697. if(hr != hrSuccess)
  1698. goto exit;
  1699. hr = lpTable->SetColumns(lpTags, 0);
  1700. if(hr != hrSuccess)
  1701. goto exit;
  1702. while(1) {
  1703. rowset_ptr lpRowSet;
  1704. hr = lpTable->QueryRows(1, 0, &~lpRowSet);
  1705. if(hr != hrSuccess)
  1706. goto exit;
  1707. if(lpRowSet->cRows != 1)
  1708. break;
  1709. // Wrap the row into an IMAPIProp compatible object so we can recursively call
  1710. // this function (which obviously itself doesn't support RES_SUBRESTRICTION as
  1711. // there aren't any subobjects under the subobjects .. unless we count
  1712. // messages in PR_ATTACH_DATA_OBJ under attachments... Well we don't support
  1713. // that in any case ...)
  1714. hr = TestRestriction(lpCondition->res.resSub.lpRes, lpRowSet->aRow[0].cValues, lpRowSet->aRow[0].lpProps, locale, ulLevel+1);
  1715. if(hr == hrSuccess) {
  1716. fMatch = true;
  1717. break;
  1718. }
  1719. }
  1720. break;
  1721. case RES_COMMENT:
  1722. hr = TestRestriction(lpCondition->res.resComment.lpRes, lpMessage, locale, ulLevel+1);
  1723. if(hr == hrSuccess)
  1724. fMatch = true;
  1725. else
  1726. fMatch = false;
  1727. break;
  1728. default:
  1729. break;
  1730. };
  1731. exit:
  1732. if (fMatch)
  1733. return hrSuccess;
  1734. else if (hr == hrSuccess)
  1735. return MAPI_E_NOT_FOUND;
  1736. return hr;
  1737. }
  1738. HRESULT GetClientVersion(unsigned int* ulVersion)
  1739. {
  1740. HRESULT hr = hrSuccess;
  1741. *ulVersion = CLIENT_VERSION_LATEST;
  1742. return hr;
  1743. }
  1744. /**
  1745. * Find a folder name in a table (hierarchy)
  1746. *
  1747. * Given a hierarchy table, the function searches for a foldername in
  1748. * the PR_DISPLAY_NAME_A property. The EntryID will be returned in the
  1749. * out parameter. The table pointer will be left where the entry was
  1750. * found.
  1751. *
  1752. * @todo make unicode compatible
  1753. *
  1754. * @param[in] lpTable IMAPITable interface, pointing to a hierarchy table
  1755. * @param[in] folder foldername to find in the list
  1756. * @param[out] lppFolderProp Property containing the EntryID of the found folder
  1757. * @return HRESULT Mapi error code
  1758. * @retval MAPI_E_NOT_FOUND if folder not found.
  1759. */
  1760. HRESULT FindFolder(LPMAPITABLE lpTable, const WCHAR *folder, LPSPropValue *lppFolderProp) {
  1761. HRESULT hr;
  1762. ULONG nValues;
  1763. static constexpr const SizedSPropTagArray(2, sptaName) =
  1764. {2, {PR_DISPLAY_NAME_W, PR_ENTRYID}};
  1765. hr = lpTable->SetColumns(sptaName, 0);
  1766. if (hr != hrSuccess)
  1767. return hr;
  1768. while (TRUE) {
  1769. rowset_ptr lpRowSet;
  1770. hr = lpTable->QueryRows(1, 0, &~lpRowSet);
  1771. if (hr != hrSuccess)
  1772. break;
  1773. if (lpRowSet->cRows == 0)
  1774. return MAPI_E_NOT_FOUND;
  1775. if (wcscasecmp(lpRowSet->aRow[0].lpProps[0].Value.lpszW, folder) == 0)
  1776. // found the folder
  1777. return Util::HrCopyPropertyArray(&lpRowSet->aRow[0].lpProps[1], 1, lppFolderProp, &nValues);
  1778. }
  1779. return hr;
  1780. }
  1781. /**
  1782. * Opens any subfolder from the name at any depth. The folder
  1783. * separator character is also passed. You can also open the IPM
  1784. * subtree not by passing the foldername.
  1785. *
  1786. * @param[in] lpMDB A store to open the folder in. If you pass the public store, set the matching bool to true.
  1787. * @param[in] folder The name of the folder you want to open. Can be at any depth, eg. INBOX/folder name1/folder name2. Pass / as separator.
  1788. * Pass NULL to open the IPM subtree of the passed store.
  1789. * @param[in] psep The foldername separator in the folder parameter.
  1790. * @param[in] bIsPublic The lpMDB parameter is the public store if true, otherwise false.
  1791. * @param[in] bCreateFolder Create the subfolders if they are not found, otherwise returns MAPI_E_NOT_FOUND if a folder is not present.
  1792. * @param[out] lppSubFolder The final opened subfolder.
  1793. * @return MAPI error code
  1794. * @retval MAPI_E_NOT_FOUND, MAPI_E_NO_ACCESS, other.
  1795. */
  1796. HRESULT OpenSubFolder(LPMDB lpMDB, const wchar_t *folder, wchar_t psep,
  1797. bool bIsPublic, bool bCreateFolder, LPMAPIFOLDER *lppSubFolder)
  1798. {
  1799. HRESULT hr = hrSuccess;
  1800. memory_ptr<SPropValue> lpPropIPMSubtree, lpPropFolder;
  1801. ULONG ulObjType;
  1802. object_ptr<IMAPIFolder> lpFoundFolder;
  1803. LPMAPIFOLDER lpNewFolder = NULL;
  1804. const WCHAR* ptr = NULL;
  1805. if(bIsPublic)
  1806. {
  1807. hr = HrGetOneProp(lpMDB, PR_IPM_PUBLIC_FOLDERS_ENTRYID, &~lpPropIPMSubtree);
  1808. if (hr != hrSuccess) {
  1809. ec_log_crit("Unable to find PR_IPM_PUBLIC_FOLDERS_ENTRYID object, error code: 0x%08X", hr);
  1810. return hr;
  1811. }
  1812. }
  1813. else
  1814. {
  1815. hr = HrGetOneProp(lpMDB, PR_IPM_SUBTREE_ENTRYID, &~lpPropIPMSubtree);
  1816. if (hr != hrSuccess) {
  1817. ec_log_crit("Unable to find IPM_SUBTREE object, error code: 0x%08X", hr);
  1818. return hr;
  1819. }
  1820. }
  1821. hr = lpMDB->OpenEntry(lpPropIPMSubtree->Value.bin.cb, reinterpret_cast<ENTRYID *>(lpPropIPMSubtree->Value.bin.lpb),
  1822. &IID_IMAPIFolder, 0, &ulObjType, &~lpFoundFolder);
  1823. if (hr != hrSuccess || ulObjType != MAPI_FOLDER) {
  1824. ec_log_crit("Unable to open IPM_SUBTREE object, error code: 0x%08X", hr);
  1825. return hr;
  1826. }
  1827. // correctly return IPM subtree as found folder
  1828. if (!folder)
  1829. goto found;
  1830. // Loop through the folder string to find the wanted folder in the store
  1831. do {
  1832. object_ptr<IMAPITable> lpTable;
  1833. wstring subfld;
  1834. ptr = wcschr(folder, psep);
  1835. if (ptr)
  1836. subfld = wstring(folder, ptr-folder);
  1837. else
  1838. subfld = wstring(folder);
  1839. folder = ptr ? ptr+1 : NULL;
  1840. hr = lpFoundFolder->GetHierarchyTable(0, &~lpTable);
  1841. if (hr != hrSuccess) {
  1842. ec_log_crit("Unable to view folder, error code: 0x%08X", hr);
  1843. return hr;
  1844. }
  1845. hr = FindFolder(lpTable, subfld.c_str(), &~lpPropFolder);
  1846. if (hr == MAPI_E_NOT_FOUND && bCreateFolder) {
  1847. hr = lpFoundFolder->CreateFolder(FOLDER_GENERIC, (LPTSTR)subfld.c_str(), (LPTSTR)L"Auto-created by Kopano", &IID_IMAPIFolder, MAPI_UNICODE | OPEN_IF_EXISTS, &lpNewFolder);
  1848. if (hr != hrSuccess) {
  1849. ec_log_crit("Unable to create folder \"%ls\", error code: 0x%08X", subfld.c_str(), hr);
  1850. return hr;
  1851. }
  1852. } else if (hr != hrSuccess)
  1853. return hr;
  1854. if (lpNewFolder) {
  1855. lpFoundFolder.reset(lpNewFolder, false);
  1856. lpNewFolder = NULL;
  1857. } else {
  1858. hr = lpMDB->OpenEntry(lpPropFolder->Value.bin.cb, reinterpret_cast<ENTRYID *>(lpPropFolder->Value.bin.lpb),
  1859. &IID_IMAPIFolder, MAPI_MODIFY, &ulObjType, &~lpFoundFolder);
  1860. if (hr != hrSuccess) {
  1861. ec_log_crit("Unable to open folder \"%ls\", error code: 0x%08X", subfld.c_str(), hr);
  1862. return hr;
  1863. }
  1864. }
  1865. } while (ptr);
  1866. found:
  1867. if (lpFoundFolder) {
  1868. lpFoundFolder->AddRef();
  1869. *lppSubFolder = lpFoundFolder;
  1870. }
  1871. return hr;
  1872. }
  1873. HRESULT HrOpenUserMsgStore(LPMAPISESSION lpSession, WCHAR *lpszUser, LPMDB *lppStore)
  1874. {
  1875. return HrOpenUserMsgStore(lpSession, NULL, lpszUser, lppStore);
  1876. }
  1877. /**
  1878. * Opens the default store of a given user using a MAPISession.
  1879. *
  1880. * Use this to open any user store a user is allowed to open.
  1881. *
  1882. * @param[in] lpSession The IMAPISession object you received from the logon procedure.
  1883. * @param[in] lpStore Optional store
  1884. * @param[in] lpszUser Login name of the user's store you want to open.
  1885. * @param[out] lppStore Pointer to the store of the given user.
  1886. *
  1887. * @return HRESULT Mapi error code.
  1888. */
  1889. HRESULT HrOpenUserMsgStore(LPMAPISESSION lpSession, LPMDB lpStore, WCHAR *lpszUser, LPMDB *lppStore)
  1890. {
  1891. HRESULT hr = hrSuccess;
  1892. object_ptr<IMsgStore> lpDefaultStore, lpMsgStore;
  1893. object_ptr<IExchangeManageStore> lpExchManageStore;
  1894. ULONG cbStoreEntryID = 0;
  1895. memory_ptr<ENTRYID> lpStoreEntryID;
  1896. if (lpStore == NULL) {
  1897. hr = HrOpenDefaultStore(lpSession, &~lpDefaultStore);
  1898. if (hr != hrSuccess)
  1899. return hr;
  1900. lpStore = lpDefaultStore;
  1901. }
  1902. // Find and open the store for lpszUser.
  1903. hr = lpStore->QueryInterface(IID_IExchangeManageStore, &~lpExchManageStore);
  1904. if (hr != hrSuccess)
  1905. return hr;
  1906. hr = lpExchManageStore->CreateStoreEntryID(NULL, (LPTSTR)lpszUser, MAPI_UNICODE, &cbStoreEntryID, &~lpStoreEntryID);
  1907. if (hr != hrSuccess)
  1908. return hr;
  1909. hr = lpSession->OpenMsgStore(0, cbStoreEntryID, lpStoreEntryID, &IID_IMsgStore, MDB_WRITE, &~lpMsgStore);
  1910. if (hr != hrSuccess)
  1911. return hr;
  1912. return lpMsgStore->QueryInterface(IID_IMsgStore, reinterpret_cast<void **>(lppStore));
  1913. }
  1914. /*
  1915. * NAMED PROPERTY util functions (used with PROPMAP_* macro's)
  1916. */
  1917. ECPropMapEntry::ECPropMapEntry(GUID guid, ULONG ulId) :
  1918. m_sGuid(guid)
  1919. {
  1920. m_sMAPINameId.ulKind = MNID_ID;
  1921. m_sMAPINameId.lpguid = &m_sGuid;
  1922. m_sMAPINameId.Kind.lID = ulId;
  1923. }
  1924. ECPropMapEntry::ECPropMapEntry(GUID guid, const char *strId) :
  1925. m_sGuid(guid)
  1926. {
  1927. m_sMAPINameId.ulKind = MNID_STRING;
  1928. m_sMAPINameId.lpguid = &m_sGuid;
  1929. m_sMAPINameId.Kind.lpwstrName = new WCHAR[strlen(strId)+1];
  1930. mbstowcs(m_sMAPINameId.Kind.lpwstrName, strId, strlen(strId)+1);
  1931. }
  1932. ECPropMapEntry::ECPropMapEntry(const ECPropMapEntry &other) :
  1933. m_sGuid(other.m_sGuid)
  1934. {
  1935. m_sMAPINameId.ulKind = other.m_sMAPINameId.ulKind;
  1936. m_sGuid = other.m_sGuid;
  1937. m_sMAPINameId.lpguid = &m_sGuid;
  1938. if(other.m_sMAPINameId.ulKind == MNID_ID) {
  1939. m_sMAPINameId.Kind.lID = other.m_sMAPINameId.Kind.lID;
  1940. return;
  1941. }
  1942. m_sMAPINameId.Kind.lpwstrName = new WCHAR[wcslen( other.m_sMAPINameId.Kind.lpwstrName )+1];
  1943. wcscpy(m_sMAPINameId.Kind.lpwstrName, other.m_sMAPINameId.Kind.lpwstrName);
  1944. }
  1945. ECPropMapEntry::ECPropMapEntry(ECPropMapEntry &&other) :
  1946. m_sGuid(other.m_sGuid)
  1947. {
  1948. m_sMAPINameId.ulKind = other.m_sMAPINameId.ulKind;
  1949. m_sMAPINameId.lpguid = &m_sGuid;
  1950. if (other.m_sMAPINameId.ulKind == MNID_ID) {
  1951. m_sMAPINameId.Kind.lID = other.m_sMAPINameId.Kind.lID;
  1952. return;
  1953. }
  1954. m_sMAPINameId.Kind.lpwstrName = other.m_sMAPINameId.Kind.lpwstrName;
  1955. other.m_sMAPINameId.Kind.lpwstrName = nullptr;
  1956. }
  1957. ECPropMapEntry::~ECPropMapEntry()
  1958. {
  1959. if (m_sMAPINameId.ulKind == MNID_STRING)
  1960. delete[] m_sMAPINameId.Kind.lpwstrName;
  1961. }
  1962. MAPINAMEID* ECPropMapEntry::GetMAPINameId() {
  1963. return &m_sMAPINameId;
  1964. }
  1965. ECPropMap::ECPropMap(size_t hint)
  1966. {
  1967. lstNames.reserve(hint);
  1968. lstVars.reserve(hint);
  1969. lstTypes.reserve(hint);
  1970. }
  1971. void ECPropMap::AddProp(ULONG *lpId, ULONG ulType, const ECPropMapEntry &entry)
  1972. {
  1973. // Add reference to proptag for later Resolve();
  1974. lstNames.push_back(entry);
  1975. lstVars.push_back(lpId);
  1976. lstTypes.push_back(ulType);
  1977. }
  1978. HRESULT ECPropMap::Resolve(IMAPIProp *lpMAPIProp) {
  1979. HRESULT hr = hrSuccess;
  1980. std::unique_ptr<MAPINAMEID *[]> lppNames;
  1981. std::vector<ULONG *>::const_iterator j;
  1982. std::vector<ULONG>::const_iterator k;
  1983. int n = 0;
  1984. memory_ptr<SPropTagArray> lpPropTags;
  1985. if (lpMAPIProp == nullptr)
  1986. return MAPI_E_INVALID_PARAMETER;
  1987. // Do GetIDsFromNames() and store result in correct places
  1988. lppNames.reset(new MAPINAMEID *[lstNames.size()]);
  1989. for (auto &mapent : lstNames)
  1990. lppNames[n++] = mapent.GetMAPINameId();
  1991. hr = lpMAPIProp->GetIDsFromNames(n, lppNames.get(), MAPI_CREATE, &~lpPropTags);
  1992. if(hr != hrSuccess)
  1993. return hr;
  1994. n = 0;
  1995. k = lstTypes.begin();
  1996. for (j = lstVars.begin(); j != lstVars.end(); ++j, ++k)
  1997. *(*j) = CHANGE_PROP_TYPE(lpPropTags->aulPropTag[n++], *k);
  1998. return hrSuccess;
  1999. }
  2000. /**
  2001. * Opens the Default Calendar folder of the store.
  2002. *
  2003. * @param[in] lpMsgStore Users Store.
  2004. * @param[out] lppFolder Default Calendar Folder of the store.
  2005. * @return HRESULT
  2006. * @retval MAPI_E_NOT_FOUND Default Folder not found.
  2007. * @retval MAPI_E_NO_ACCESS Insufficient permissions to open the folder.
  2008. */
  2009. HRESULT HrOpenDefaultCalendar(LPMDB lpMsgStore, LPMAPIFOLDER *lppFolder)
  2010. {
  2011. HRESULT hr = hrSuccess;
  2012. memory_ptr<SPropValue> lpPropDefFld;
  2013. object_ptr<IMAPIFolder> lpRootFld, lpDefaultFolder;
  2014. ULONG ulType = 0;
  2015. //open Root Container.
  2016. hr = lpMsgStore->OpenEntry(0, nullptr, nullptr, 0, &ulType, &~lpRootFld);
  2017. if (hr != hrSuccess || ulType != MAPI_FOLDER)
  2018. {
  2019. ec_log_crit("Unable to open Root Container, error code: 0x%08X", hr);
  2020. return hr;
  2021. }
  2022. //retrive Entryid of Default Calendar Folder.
  2023. hr = HrGetOneProp(lpRootFld, PR_IPM_APPOINTMENT_ENTRYID, &~lpPropDefFld);
  2024. if (hr != hrSuccess)
  2025. {
  2026. ec_log_crit("Unable to find PR_IPM_APPOINTMENT_ENTRYID, error code: 0x%08X", hr);
  2027. return hr;
  2028. }
  2029. hr = lpMsgStore->OpenEntry(lpPropDefFld->Value.bin.cb, reinterpret_cast<ENTRYID *>(lpPropDefFld->Value.bin.lpb), nullptr, MAPI_MODIFY, &ulType, &~lpDefaultFolder);
  2030. if (hr != hrSuccess || ulType != MAPI_FOLDER)
  2031. {
  2032. ec_log_crit("Unable to open IPM_SUBTREE object, error code: 0x%08X", hr);
  2033. return hr;
  2034. }
  2035. *lppFolder = lpDefaultFolder.release();
  2036. return hrSuccess;
  2037. }
  2038. /**
  2039. * Gets all properties for passed object
  2040. *
  2041. * This includes properties that are normally returned from GetProps() as MAPI_E_NOT_ENOUGH_MEMORY. The
  2042. * rest of the semantics of this call are equal to those of calling IMAPIProp::GetProps() with NULL as the
  2043. * property tag array.
  2044. *
  2045. * @param[in] lpProp IMAPIProp object to get properties from
  2046. * @param[in] ulFlags MAPI_UNICODE or 0
  2047. * @param[out] lpcValues Number of properties saved in lppProps
  2048. * @param[out] lppProps Output properties
  2049. */
  2050. HRESULT HrGetAllProps(IMAPIProp *lpProp, ULONG ulFlags, ULONG *lpcValues, LPSPropValue *lppProps)
  2051. {
  2052. SPropTagArrayPtr lpTags;
  2053. SPropArrayPtr lpProps;
  2054. ULONG cValues = 0;
  2055. StreamPtr lpStream;
  2056. std::string strData;
  2057. void *lpData = NULL;
  2058. HRESULT hr = lpProp->GetPropList(ulFlags, &~lpTags);
  2059. if(hr != hrSuccess)
  2060. return hr;
  2061. hr = lpProp->GetProps(lpTags, ulFlags, &cValues, &~lpProps);
  2062. if(FAILED(hr))
  2063. return hr;
  2064. for (unsigned int i = 0; i < cValues; ++i) {
  2065. if(PROP_TYPE(lpProps[i].ulPropTag) == PT_ERROR && lpProps[i].Value.err == MAPI_E_NOT_ENOUGH_MEMORY) {
  2066. if(PROP_TYPE(lpTags->aulPropTag[i]) != PT_STRING8 && PROP_TYPE(lpTags->aulPropTag[i]) != PT_UNICODE && PROP_TYPE(lpTags->aulPropTag[i]) != PT_BINARY)
  2067. continue;
  2068. if(lpProp->OpenProperty(lpTags->aulPropTag[i], &IID_IStream, 0, 0, &~lpStream) != hrSuccess)
  2069. continue;
  2070. strData.clear();
  2071. if(Util::HrStreamToString(lpStream.get(), strData) != hrSuccess)
  2072. continue;
  2073. if ((hr = MAPIAllocateMore(strData.size() + sizeof(WCHAR), lpProps, (void **)&lpData)) != hrSuccess)
  2074. return hr;
  2075. memcpy(lpData, strData.data(), strData.size());
  2076. lpProps[i].ulPropTag = lpTags->aulPropTag[i];
  2077. switch PROP_TYPE(lpTags->aulPropTag[i]) {
  2078. case PT_STRING8:
  2079. lpProps[i].Value.lpszA = (char *)lpData;
  2080. lpProps[i].Value.lpszA[strData.size()] = 0;
  2081. break;
  2082. case PT_UNICODE:
  2083. lpProps[i].Value.lpszW = (wchar_t *)lpData;
  2084. lpProps[i].Value.lpszW[strData.size() / sizeof(WCHAR)] = 0;
  2085. break;
  2086. case PT_BINARY:
  2087. lpProps[i].Value.bin.lpb = (LPBYTE)lpData;
  2088. lpProps[i].Value.bin.cb = strData.size();
  2089. break;
  2090. default:
  2091. assert(false);
  2092. }
  2093. }
  2094. }
  2095. *lppProps = lpProps.release();
  2096. *lpcValues = cValues;
  2097. return hrSuccess;
  2098. }
  2099. /**
  2100. * Converts a wrapped message store's entry identifier to a message store entry identifier.
  2101. *
  2102. * MAPI supplies a wrapped version of a store entryid which indentified a specific service provider.
  2103. * A MAPI client can use IMAPISupport::WrapStoreEntryID to generate a wrapped entryid. The PR_ENTRYID and
  2104. * PR_STORE_ENTRYID are wrapped entries which can be unwrapped by using this function.
  2105. *
  2106. * @param[in] cbOrigEntry
  2107. * Size, in bytes, of the original entry identifier for the wrapped message store.
  2108. * @param[in] lpOrigEntry
  2109. * Pointer to an ENTRYID structure that contains the original wrapped entry identifier.
  2110. * @param[out] lpcbUnWrappedEntry
  2111. * Pointer to the size, in bytes, of the new unwrapped entry identifier.
  2112. * @param[out] lppUnWrappedEntry
  2113. * Pointer to a pointer to an ENTRYID structure that contains the new unwrapped entry identifier
  2114. *
  2115. * @retval MAPI_E_INVALID_PARAMETER
  2116. * One or more values are NULL.
  2117. * @retval MAPI_E_INVALID_ENTRYID
  2118. * The entry ID is not valid. It shouyld be a wrapped entry identifier
  2119. */
  2120. HRESULT __stdcall UnWrapStoreEntryID(ULONG cbOrigEntry, LPENTRYID lpOrigEntry, ULONG *lpcbUnWrappedEntry, LPENTRYID *lppUnWrappedEntry)
  2121. {
  2122. HRESULT hr = hrSuccess;
  2123. ULONG cbRemove = 0;
  2124. ULONG cbDLLName = 0;
  2125. memory_ptr<ENTRYID> lpEntryID;
  2126. if (lpOrigEntry == nullptr || lpcbUnWrappedEntry == nullptr ||
  2127. lppUnWrappedEntry == nullptr)
  2128. return MAPI_E_INVALID_PARAMETER;
  2129. // Check if this a wrapped store entryid
  2130. if (cbOrigEntry < (4 + sizeof(GUID) + 3) ||
  2131. memcmp(lpOrigEntry->ab, &muidStoreWrap, sizeof(GUID)) != 0)
  2132. return MAPI_E_INVALID_ENTRYID;
  2133. cbRemove = 4; // Flags
  2134. cbRemove+= sizeof(GUID); //Wrapped identifier
  2135. cbRemove+= 2; // Unknown, Unicode flag?
  2136. // Dllname size
  2137. cbDLLName = (ULONG)strlen((LPCSTR)lpOrigEntry+cbRemove) + 1;
  2138. cbRemove+= cbDLLName;
  2139. cbRemove += (4 - (cbRemove & 0x03)) & 0x03;; // padding to 4byte block
  2140. if (cbOrigEntry <= cbRemove)
  2141. return MAPI_E_INVALID_ENTRYID;
  2142. // Create Unwrap entryid
  2143. hr = MAPIAllocateBuffer(cbOrigEntry - cbRemove, &~lpEntryID);
  2144. if (hr != hrSuccess)
  2145. return hr;
  2146. memcpy(lpEntryID, ((LPBYTE)lpOrigEntry)+cbRemove, cbOrigEntry - cbRemove);
  2147. *lpcbUnWrappedEntry = cbOrigEntry - cbRemove;
  2148. *lppUnWrappedEntry = lpEntryID.release();
  2149. return hrSuccess;
  2150. }
  2151. /**
  2152. * Call IAddrBook::Address with correct flags
  2153. *
  2154. * Various versions of outlook support different flags in IAddrBook::Address. This function
  2155. * removes the unsupported flags and converts strings to non-unicode strings if needed.
  2156. *
  2157. * NOTE: since you cannot rely on the MAPI_UNICODE flag being passed, the data in lpResult may
  2158. * contain either PT_STRING8 or PT_UNICODE. The calling code should handle both types.
  2159. */
  2160. HRESULT DoAddress(IAddrBook *lpAdrBook, ULONG* hWnd, LPADRPARM lpAdrParam, LPADRLIST *lpAdrList)
  2161. {
  2162. ULONG ulUnCapabilities = 0; // All the UNSUPPORTED features for this outlook version
  2163. std::string strCaption;
  2164. std::string strNewEntryTitle;
  2165. std::string strDestWellsTitle;
  2166. std::string strHelpFileName;
  2167. unsigned int ulVersion = 0;
  2168. LPADRLIST lpResult = *lpAdrList;
  2169. ADRPARM sAdrParam = *lpAdrParam;
  2170. std::vector<std::string> vDestFields;
  2171. HRESULT hr = GetClientVersion(&ulVersion);
  2172. if(hr != hrSuccess)
  2173. return hr;
  2174. // Various versions of outlook support some flags and dont support others
  2175. if (ulVersion <= CLIENT_VERSION_OLK2000)
  2176. ulUnCapabilities |= MAPI_UNICODE;
  2177. if (ulVersion <= CLIENT_VERSION_OLK2002)
  2178. ulUnCapabilities |= AB_UNICODEUI;
  2179. if (ulVersion <= CLIENT_VERSION_OLK2003)
  2180. ulUnCapabilities |= AB_LOCK_NON_ACL;
  2181. if((sAdrParam.ulFlags & AB_UNICODEUI) && (ulUnCapabilities & AB_UNICODEUI)) {
  2182. // Addressbook doesn't support AB_UNICODEUI, convert data
  2183. if(sAdrParam.lpszCaption) {
  2184. strCaption = convert_to<string>((LPWSTR)sAdrParam.lpszCaption);
  2185. sAdrParam.lpszCaption = (LPTSTR)strCaption.c_str();
  2186. }
  2187. if(sAdrParam.lpszNewEntryTitle) {
  2188. strNewEntryTitle = convert_to<string>((LPWSTR)sAdrParam.lpszNewEntryTitle);
  2189. sAdrParam.lpszCaption = (LPTSTR)strNewEntryTitle.c_str();
  2190. }
  2191. if(sAdrParam.lpszDestWellsTitle) {
  2192. strDestWellsTitle = convert_to<string>((LPWSTR)sAdrParam.lpszDestWellsTitle);
  2193. sAdrParam.lpszDestWellsTitle = (LPTSTR)strDestWellsTitle.c_str();
  2194. }
  2195. if(sAdrParam.lpszHelpFileName) {
  2196. strHelpFileName = convert_to<string>((LPWSTR)sAdrParam.lpszHelpFileName);
  2197. sAdrParam.lpszHelpFileName = (LPTSTR)strHelpFileName.c_str();
  2198. }
  2199. // Same for lpAdrParam
  2200. for (unsigned int i = 0; i < sAdrParam.cDestFields; ++i) {
  2201. std::string strField = convert_to<string>((LPWSTR)sAdrParam.lppszDestTitles[i]);
  2202. vDestFields.push_back(std::move(strField));
  2203. sAdrParam.lppszDestTitles[i] = (LPTSTR)vDestFields.back().c_str();
  2204. }
  2205. }
  2206. // Remove unsupported flags
  2207. sAdrParam.ulFlags &= ~ulUnCapabilities;
  2208. hr = lpAdrBook->Address(hWnd, &sAdrParam, &lpResult);
  2209. if(hr != hrSuccess)
  2210. return hr;
  2211. if ((ulUnCapabilities & MAPI_UNICODE) && (lpAdrParam->ulFlags & MAPI_UNICODE)) {
  2212. // MAPI_UNICODE was requested, but the addressbook did not support it. This means we have to convert all the PT_STRING8 data
  2213. // back to PT_UNICODE.
  2214. for (unsigned int i = 0; i < lpResult->cEntries; ++i) {
  2215. for (unsigned int j = 0; j < lpResult->aEntries[i].cValues; ++j) {
  2216. if(PROP_TYPE(lpResult->aEntries[i].rgPropVals[j].ulPropTag) == PT_STRING8) {
  2217. std::wstring wstrData = convert_to<wstring>(lpResult->aEntries[i].rgPropVals[j].Value.lpszA);
  2218. hr = MAPIAllocateMore((wstrData.size() + 1) * sizeof(WCHAR), lpResult->aEntries[i].rgPropVals, (void **)&lpResult->aEntries[i].rgPropVals[j].Value.lpszW);
  2219. if(hr != hrSuccess)
  2220. return hr;
  2221. memcpy(lpResult->aEntries[i].rgPropVals[j].Value.lpszW, wstrData.c_str(), (wstrData.size() + 1) * sizeof(WCHAR));
  2222. lpResult->aEntries[i].rgPropVals[j].ulPropTag = CHANGE_PROP_TYPE(lpResult->aEntries[i].rgPropVals[j].ulPropTag, PT_UNICODE);
  2223. }
  2224. }
  2225. }
  2226. }
  2227. *lpAdrList = lpResult;
  2228. return hrSuccess;
  2229. }
  2230. /**
  2231. * An enumeration for getting the localfreebusy from the calendar or from the free/busy data folder.
  2232. *
  2233. * @note it's also the array position of property PR_FREEBUSY_ENTRYIDS
  2234. */
  2235. enum DGMessageType {
  2236. dgAssociated = 0, /**< Localfreebusy message in default associated calendar folder */
  2237. dgFreebusydata = 1 /**< Localfreebusy message in Free/busy data folder */
  2238. };
  2239. // Default freebusy publish months
  2240. #define ECFREEBUSY_DEFAULT_PUBLISH_MONTHS 6
  2241. /**
  2242. * Create a local free/busy message
  2243. *
  2244. * @param[in] lpFolder
  2245. * Destenation folder for creating the local free/busy message
  2246. * @param[in] ulFlags
  2247. * MAPI_ASSOCIATED for Localfreebusy message in default associated calendar folder
  2248. * @param[out] lppMessage
  2249. * The localfreebusy message with the free/busy settings
  2250. *
  2251. * @todo move the code to a common file
  2252. *
  2253. */
  2254. static HRESULT CreateLocalFreeBusyMessage(LPMAPIFOLDER lpFolder, ULONG ulFlags,
  2255. LPMESSAGE *lppMessage)
  2256. {
  2257. HRESULT hr = hrSuccess;
  2258. object_ptr<IMessage> lpMessage;
  2259. SPropValue sPropValMessage[6];
  2260. memset(sPropValMessage, 0, sizeof(SPropValue) * 6);
  2261. if (lpFolder == nullptr || lppMessage == nullptr || (ulFlags & ~MAPI_ASSOCIATED) != 0)
  2262. return MAPI_E_INVALID_PARAMETER;
  2263. hr = lpFolder->CreateMessage(&IID_IMessage, (ulFlags&MAPI_ASSOCIATED), &~lpMessage);
  2264. if(hr != hrSuccess)
  2265. return hr;
  2266. sPropValMessage[0].ulPropTag = PR_MESSAGE_CLASS_W;
  2267. sPropValMessage[0].Value.lpszW = const_cast<wchar_t *>(L"IPM.Microsoft.ScheduleData.FreeBusy");
  2268. sPropValMessage[1].ulPropTag = PR_SUBJECT_W;
  2269. sPropValMessage[1].Value.lpszW = const_cast<wchar_t *>(L"LocalFreebusy");
  2270. sPropValMessage[2].ulPropTag = PR_FREEBUSY_NUM_MONTHS;
  2271. sPropValMessage[2].Value.ul = ECFREEBUSY_DEFAULT_PUBLISH_MONTHS;
  2272. sPropValMessage[3].ulPropTag = PR_DECLINE_RECURRING_MEETING_REQUESTS;
  2273. sPropValMessage[3].Value.b = false;
  2274. sPropValMessage[4].ulPropTag = PR_DECLINE_CONFLICTING_MEETING_REQUESTS;
  2275. sPropValMessage[4].Value.b = false;
  2276. sPropValMessage[5].ulPropTag = PR_PROCESS_MEETING_REQUESTS;
  2277. sPropValMessage[5].Value.b = false;
  2278. hr = lpMessage->SetProps(6, sPropValMessage, NULL);
  2279. if(hr != hrSuccess)
  2280. return hr;
  2281. hr = lpMessage->SaveChanges(KEEP_OPEN_READWRITE);
  2282. if(hr != hrSuccess)
  2283. return hr;
  2284. return lpMessage->QueryInterface(IID_IMessage, reinterpret_cast<void **>(lppMessage));
  2285. }
  2286. /*
  2287. * Get local free/busy message to get delegate information
  2288. *
  2289. * @note There is a differents between Outlook before 2003 and from 2003.
  2290. * The free/busy settings are written on another place.
  2291. * Outlook 2000 and 2002, a message in the calendar associated folder
  2292. * Outlook 2003 and higher, a message in the freebusydata folder
  2293. * exchange uses always the same?
  2294. *
  2295. * @param[in] ulSettingsLocation
  2296. * Type message to get information
  2297. * @param[in] lpMsgStore
  2298. * the delegate message store for getting delegate information.
  2299. * @param[in] bCreateIfMissing
  2300. * If not exist create the localfreebusy message
  2301. * @param[out] lppFBMessage
  2302. * The localfreebusy message with the free/busy settings
  2303. *
  2304. * @return MAPI_E_NOT_FOUND
  2305. * Local free/busy message is not exist
  2306. *
  2307. */
  2308. static HRESULT OpenLocalFBMessage(DGMessageType eDGMsgType,
  2309. IMsgStore *lpMsgStore, bool bCreateIfMissing, IMessage **lppFBMessage)
  2310. {
  2311. HRESULT hr = hrSuccess;
  2312. object_ptr<IMAPIFolder> lpRoot, lpInbox;
  2313. IMAPIFolder *lpFolder = NULL;
  2314. IMessage *lpMessage = NULL;
  2315. ULONG ulType = 0;
  2316. memory_ptr<SPropValue> lpPropFB, lpPropFBNew;
  2317. LPSPropValue lpPVFBFolder = NULL;
  2318. memory_ptr<SPropValue> lpEntryID, lpAppEntryID;
  2319. LPSPropValue lpPropFBRef = NULL; // Non-free
  2320. ULONG cbEntryIDInbox = 0;
  2321. memory_ptr<ENTRYID> lpEntryIDInbox;
  2322. memory_ptr<TCHAR> lpszExplicitClass;
  2323. hr = lpMsgStore->OpenEntry(0, nullptr, &IID_IMAPIFolder, MAPI_MODIFY, &ulType, &~lpRoot);
  2324. if(hr != hrSuccess)
  2325. return hr;
  2326. // Check if the freebusydata folder and LocalFreeBusy is exist. Create the folder and message if it is request.
  2327. if((HrGetOneProp(lpRoot, PR_FREEBUSY_ENTRYIDS, &~lpPropFB) != hrSuccess ||
  2328. lpPropFB->Value.MVbin.cValues < 2 ||
  2329. lpPropFB->Value.MVbin.lpbin[eDGMsgType].lpb == NULL ||
  2330. lpMsgStore->OpenEntry(lpPropFB->Value.MVbin.lpbin[eDGMsgType].cb, (LPENTRYID)lpPropFB->Value.MVbin.lpbin[eDGMsgType].lpb, &IID_IMessage, MAPI_MODIFY, &ulType, (IUnknown **) &lpMessage) != hrSuccess)
  2331. && bCreateIfMissing) {
  2332. // Open the inbox
  2333. hr = lpMsgStore->GetReceiveFolder((LPTSTR)"", 0, &cbEntryIDInbox, &~lpEntryIDInbox, &~lpszExplicitClass);
  2334. if(hr != hrSuccess)
  2335. return hr;
  2336. hr = lpMsgStore->OpenEntry(cbEntryIDInbox, lpEntryIDInbox, &IID_IMAPIFolder, MAPI_MODIFY, &ulType, &~lpInbox);
  2337. if(hr != hrSuccess)
  2338. return hr;
  2339. if (eDGMsgType == dgFreebusydata) {
  2340. // Create freebusydata Folder
  2341. hr = lpRoot->CreateFolder(FOLDER_GENERIC, (LPTSTR)"Freebusy Data", (LPTSTR)"", &IID_IMAPIFolder, OPEN_IF_EXISTS, &lpFolder);
  2342. if(hr != hrSuccess)
  2343. return hr;
  2344. // Get entryid of freebusydata
  2345. hr = HrGetOneProp(lpFolder, PR_ENTRYID, &lpPVFBFolder);
  2346. if(hr != hrSuccess)
  2347. return hr;
  2348. } else if (eDGMsgType == dgAssociated) {
  2349. //Open default calendar
  2350. hr = HrGetOneProp(lpInbox, PR_IPM_APPOINTMENT_ENTRYID, &~lpAppEntryID);
  2351. if(hr != hrSuccess)
  2352. return hr;
  2353. hr = lpMsgStore->OpenEntry(lpAppEntryID->Value.bin.cb, (LPENTRYID)lpAppEntryID->Value.bin.lpb, &IID_IMAPIFolder, MAPI_MODIFY, &ulType, (IUnknown **) &lpFolder);
  2354. if(hr != hrSuccess)
  2355. return hr;
  2356. }
  2357. hr = CreateLocalFreeBusyMessage(lpFolder, (eDGMsgType == dgAssociated)?MAPI_ASSOCIATED : 0, &lpMessage);
  2358. if(hr != hrSuccess)
  2359. return hr;
  2360. hr = HrGetOneProp(lpMessage, PR_ENTRYID, &~lpEntryID);
  2361. if(hr != hrSuccess)
  2362. return hr;
  2363. // Update Free/Busy entryid
  2364. if(lpPropFB == NULL || lpPropFB->Value.MVbin.cValues < 2) {
  2365. hr = MAPIAllocateBuffer(sizeof(SPropValue), &~lpPropFBNew);
  2366. if(hr != hrSuccess)
  2367. return hr;
  2368. lpPropFBNew->ulPropTag = PR_FREEBUSY_ENTRYIDS;
  2369. hr = MAPIAllocateMore(sizeof(SBinary) * 4, lpPropFB, (void **)&lpPropFBNew->Value.MVbin.lpbin);
  2370. if(hr != hrSuccess)
  2371. return hr;
  2372. memset(lpPropFBNew->Value.MVbin.lpbin, 0, sizeof(SBinary) * 4);
  2373. if (eDGMsgType == dgFreebusydata) {
  2374. if(lpPropFB && lpPropFB->Value.MVbin.cValues > 0) {
  2375. lpPropFBNew->Value.MVbin.lpbin[0] = lpPropFB->Value.MVbin.lpbin[0];
  2376. if (lpPropFB->Value.MVbin.cValues > 2)
  2377. lpPropFBNew->Value.MVbin.lpbin[2] = lpPropFB->Value.MVbin.lpbin[2];
  2378. }
  2379. lpPropFBNew->Value.MVbin.lpbin[1].cb = lpEntryID->Value.bin.cb;
  2380. lpPropFBNew->Value.MVbin.lpbin[1].lpb = lpEntryID->Value.bin.lpb;
  2381. lpPropFBNew->Value.MVbin.lpbin[3].cb = lpPVFBFolder->Value.bin.cb;
  2382. lpPropFBNew->Value.MVbin.lpbin[3].lpb = lpPVFBFolder->Value.bin.lpb;
  2383. } else if(eDGMsgType == dgAssociated) {
  2384. lpPropFBNew->Value.MVbin.lpbin[0].cb = lpEntryID->Value.bin.cb;
  2385. lpPropFBNew->Value.MVbin.lpbin[0].lpb = lpEntryID->Value.bin.lpb;
  2386. }
  2387. lpPropFBNew->Value.MVbin.cValues = 4; // no problem if the data is NULL
  2388. lpPropFBRef = lpPropFBNew; // use this one later on
  2389. } else {
  2390. lpPropFB->Value.MVbin.lpbin[eDGMsgType].cb = lpEntryID->Value.bin.cb;
  2391. lpPropFB->Value.MVbin.lpbin[eDGMsgType].lpb = lpEntryID->Value.bin.lpb;
  2392. lpPropFBRef = lpPropFB; // use this one later on
  2393. }
  2394. // Put the MV property in the root folder
  2395. hr = lpRoot->SetProps(1, lpPropFBRef, NULL);
  2396. if(hr != hrSuccess)
  2397. return hr;
  2398. // Put the MV property in the inbox folder
  2399. hr = lpInbox->SetProps(1, lpPropFBRef, NULL);
  2400. if(hr != hrSuccess)
  2401. return hr;
  2402. }
  2403. if (lpMessage == nullptr)
  2404. return MAPI_E_NOT_FOUND;
  2405. // We now have a message lpMessage which is the LocalFreeBusy message.
  2406. *lppFBMessage = lpMessage;
  2407. return hrSuccess;
  2408. }
  2409. /**
  2410. * Set proccessing meeting request options of a user
  2411. *
  2412. * Use these options if you are responsible for coordinating resources, such as conference rooms.
  2413. *
  2414. * @param[in] lpMsgStore user store to get the options
  2415. * @param[out] bAutoAccept Automatically accept meeting requests and proccess cancellations
  2416. * @param[out] bDeclineConflict Automatically decline conflicting meeting requests
  2417. * @param[out] bDeclineRecurring Automatically decline recurring meeting requests
  2418. *
  2419. * @note because a unknown issue it will update two different free/busy messages, one for
  2420. * outlook 2000/xp and one for outlook 2003/2007.
  2421. *
  2422. * @todo find out why outlook 2000/xp opened the wrong local free/busy message
  2423. * @todo check, should the properties PR_SCHDINFO_BOSS_WANTS_COPY, PR_SCHDINFO_DONT_MAIL_DELEGATES,
  2424. * PR_SCHDINFO_BOSS_WANTS_INFO on TRUE?
  2425. */
  2426. HRESULT SetAutoAcceptSettings(IMsgStore *lpMsgStore, bool bAutoAccept, bool bDeclineConflict, bool bDeclineRecurring)
  2427. {
  2428. HRESULT hr = hrSuccess;
  2429. object_ptr<IMessage> lpLocalFBMessage;
  2430. SPropValue FBProps[6];
  2431. // Meaning of these values are unknown, but are always TRUE in cases seen until now
  2432. FBProps[0].ulPropTag = PROP_TAG(PT_BOOLEAN, 0x6842); // PR_SCHDINFO_BOSS_WANTS_COPY
  2433. FBProps[0].Value.b = TRUE;
  2434. FBProps[1].ulPropTag = PROP_TAG(PT_BOOLEAN, 0x6843); //PR_SCHDINFO_DONT_MAIL_DELEGATES
  2435. FBProps[1].Value.b = TRUE;
  2436. FBProps[2].ulPropTag = PROP_TAG(PT_BOOLEAN, 0x684B); // PR_SCHDINFO_BOSS_WANTS_INFO
  2437. FBProps[2].Value.b = TRUE;
  2438. FBProps[3].ulPropTag = PR_PROCESS_MEETING_REQUESTS;
  2439. FBProps[3].Value.b = bAutoAccept ? TRUE : FALSE;
  2440. FBProps[4].ulPropTag = PR_DECLINE_CONFLICTING_MEETING_REQUESTS;
  2441. FBProps[4].Value.b = bDeclineConflict ? TRUE : FALSE;
  2442. FBProps[5].ulPropTag = PR_DECLINE_RECURRING_MEETING_REQUESTS;
  2443. FBProps[5].Value.b = bDeclineRecurring ? TRUE : FALSE;
  2444. // Save localfreebusy settings
  2445. hr = OpenLocalFBMessage(dgFreebusydata, lpMsgStore, true, &~lpLocalFBMessage);
  2446. if(hr != hrSuccess)
  2447. return hr;
  2448. hr = lpLocalFBMessage->SetProps(6, FBProps, NULL);
  2449. if(hr != hrSuccess)
  2450. return hr;
  2451. hr = lpLocalFBMessage->SaveChanges(0);
  2452. if(hr != hrSuccess)
  2453. return hr;
  2454. // Hack to support outlook 2000/2002 with resources
  2455. hr = OpenLocalFBMessage(dgAssociated, lpMsgStore, true, &~lpLocalFBMessage);
  2456. if(hr != hrSuccess)
  2457. return hr;
  2458. hr = lpLocalFBMessage->SetProps(6, FBProps, NULL);
  2459. if(hr != hrSuccess)
  2460. return hr;
  2461. return lpLocalFBMessage->SaveChanges(0);
  2462. }
  2463. /**
  2464. * Get the proccessing meeting request options of a user
  2465. *
  2466. * Use these options if you are responsible for coordinating resources, such as conference rooms.
  2467. *
  2468. * @param[in] lpMsgStore user store to get the options
  2469. * @param[out] lpbAutoAccept Automatically accept meeting requests and proccess cancellations
  2470. * @param[out] lpbDeclineConflict Automatically decline conflicting meeting requests
  2471. * @param[out] lpbDeclineRecurring Automatically decline recurring meeting requests
  2472. *
  2473. * @note you get the outlook 2003/2007 settings
  2474. */
  2475. HRESULT GetAutoAcceptSettings(IMsgStore *lpMsgStore, bool *lpbAutoAccept, bool *lpbDeclineConflict, bool *lpbDeclineRecurring)
  2476. {
  2477. HRESULT hr = hrSuccess;
  2478. object_ptr<IMessage> lpLocalFBMessage;
  2479. memory_ptr<SPropValue> lpProps;
  2480. static constexpr const SizedSPropTagArray(3, sptaFBProps) =
  2481. {3, {PR_PROCESS_MEETING_REQUESTS,
  2482. PR_DECLINE_CONFLICTING_MEETING_REQUESTS,
  2483. PR_DECLINE_RECURRING_MEETING_REQUESTS}};
  2484. ULONG cValues = 0;
  2485. bool bAutoAccept = false;
  2486. bool bDeclineConflict = false;
  2487. bool bDeclineRecurring = false;
  2488. hr = OpenLocalFBMessage(dgFreebusydata, lpMsgStore, false, &~lpLocalFBMessage);
  2489. if(hr == hrSuccess) {
  2490. hr = lpLocalFBMessage->GetProps(sptaFBProps, 0, &cValues, &~lpProps);
  2491. if(FAILED(hr))
  2492. return hr;
  2493. if(lpProps[0].ulPropTag == PR_PROCESS_MEETING_REQUESTS)
  2494. bAutoAccept = lpProps[0].Value.b;
  2495. if(lpProps[1].ulPropTag == PR_DECLINE_CONFLICTING_MEETING_REQUESTS)
  2496. bDeclineConflict = lpProps[1].Value.b;
  2497. if(lpProps[2].ulPropTag == PR_DECLINE_RECURRING_MEETING_REQUESTS)
  2498. bDeclineRecurring = lpProps[2].Value.b;
  2499. }
  2500. // else, hr != hrSuccess: no FB -> all settings are FALSE
  2501. *lpbAutoAccept = bAutoAccept;
  2502. *lpbDeclineConflict = bDeclineConflict;
  2503. *lpbDeclineRecurring = bDeclineRecurring;
  2504. return hrSuccess;
  2505. }
  2506. HRESULT HrGetRemoteAdminStore(IMAPISession *lpMAPISession, IMsgStore *lpMsgStore, LPCTSTR lpszServerName, ULONG ulFlags, IMsgStore **lppMsgStore)
  2507. {
  2508. ExchangeManageStorePtr ptrEMS;
  2509. ULONG cbStoreId;
  2510. EntryIdPtr ptrStoreId;
  2511. MsgStorePtr ptrMsgStore;
  2512. if (lpMAPISession == NULL || lpMsgStore == NULL ||
  2513. lpszServerName == NULL || (ulFlags & ~(MAPI_UNICODE | MDB_WRITE)) ||
  2514. lppMsgStore == NULL)
  2515. return MAPI_E_INVALID_PARAMETER;
  2516. HRESULT hr = lpMsgStore->QueryInterface(ptrEMS.iid(), &~ptrEMS);
  2517. if (hr != hrSuccess)
  2518. return hr;
  2519. if (ulFlags & MAPI_UNICODE) {
  2520. std::wstring strMsgStoreDN = std::wstring(L"cn=") + (LPCWSTR)lpszServerName + L"/cn=Microsoft Private MDB";
  2521. hr = ptrEMS->CreateStoreEntryID((LPTSTR)strMsgStoreDN.c_str(), (LPTSTR)L"SYSTEM", MAPI_UNICODE|OPENSTORE_OVERRIDE_HOME_MDB, &cbStoreId, &~ptrStoreId);
  2522. } else {
  2523. std::string strMsgStoreDN = std::string("cn=") + (LPCSTR)lpszServerName + "/cn=Microsoft Private MDB";
  2524. hr = ptrEMS->CreateStoreEntryID((LPTSTR)strMsgStoreDN.c_str(), (LPTSTR)"SYSTEM", OPENSTORE_OVERRIDE_HOME_MDB, &cbStoreId, &~ptrStoreId);
  2525. }
  2526. if (hr != hrSuccess)
  2527. return hr;
  2528. hr = lpMAPISession->OpenMsgStore(0, cbStoreId, ptrStoreId, &ptrMsgStore.iid(), ulFlags & MDB_WRITE, &~ptrMsgStore);
  2529. if (hr != hrSuccess)
  2530. return hr;
  2531. return ptrMsgStore->QueryInterface(IID_IMsgStore,
  2532. reinterpret_cast<LPVOID *>(lppMsgStore));
  2533. }
  2534. HRESULT HrGetGAB(LPMAPISESSION lpSession, LPABCONT *lppGAB)
  2535. {
  2536. AddrBookPtr ptrAddrBook;
  2537. if (lpSession == NULL || lppGAB == NULL)
  2538. return MAPI_E_INVALID_PARAMETER;
  2539. HRESULT hr = lpSession->OpenAddressBook(0, 0, 0, &~ptrAddrBook);
  2540. if (hr != hrSuccess)
  2541. return hr;
  2542. return HrGetGAB(ptrAddrBook, lppGAB);
  2543. }
  2544. HRESULT HrGetGAB(LPADRBOOK lpAddrBook, LPABCONT *lppGAB)
  2545. {
  2546. ULONG ulType = 0;
  2547. ABContainerPtr ptrRoot;
  2548. MAPITablePtr ptrTable;
  2549. SRowSetPtr ptrRows;
  2550. ABContainerPtr ptrGAB;
  2551. SPropValue propDisplayType;
  2552. SPropValue propEmsAbContainerid;
  2553. static constexpr const SizedSPropTagArray(1, sptaTableProps) = {1, {PR_ENTRYID}};
  2554. if (lpAddrBook == NULL || lppGAB == NULL)
  2555. return MAPI_E_INVALID_PARAMETER;
  2556. HRESULT hr = lpAddrBook->OpenEntry(0, NULL, &ptrRoot.iid(), MAPI_DEFERRED_ERRORS, &ulType, &~ptrRoot);
  2557. if (hr != hrSuccess)
  2558. return hr;
  2559. hr = ptrRoot->GetHierarchyTable(MAPI_DEFERRED_ERRORS, &~ptrTable);
  2560. if (hr != hrSuccess)
  2561. return hr;
  2562. hr = ptrTable->SetColumns(sptaTableProps, TBL_BATCH);
  2563. if (hr != hrSuccess)
  2564. return hr;
  2565. propDisplayType.ulPropTag = PR_DISPLAY_TYPE;
  2566. propDisplayType.Value.l = DT_GLOBAL;
  2567. propEmsAbContainerid.ulPropTag = PR_EMS_AB_CONTAINERID;
  2568. propEmsAbContainerid.Value.l = 0;
  2569. hr = ECOrRestriction(
  2570. ECPropertyRestriction(RELOP_EQ, PR_DISPLAY_TYPE, &propDisplayType, ECRestriction::Cheap) +
  2571. ECAndRestriction(
  2572. ECExistRestriction(PR_EMS_AB_CONTAINERID) +
  2573. ECPropertyRestriction(RELOP_EQ, PR_EMS_AB_CONTAINERID, &propEmsAbContainerid, ECRestriction::Cheap)
  2574. )
  2575. ).FindRowIn(ptrTable, BOOKMARK_BEGINNING, 0);
  2576. if (hr != hrSuccess)
  2577. return hr;
  2578. hr = ptrTable->QueryRows(1, 0, &ptrRows);
  2579. if (hr != hrSuccess)
  2580. return hr;
  2581. hr = lpAddrBook->OpenEntry(ptrRows[0].lpProps[0].Value.bin.cb, reinterpret_cast<ENTRYID *>(ptrRows[0].lpProps[0].Value.bin.lpb), &ptrGAB.iid(), 0, &ulType, &~ptrGAB);
  2582. if (hr != hrSuccess)
  2583. return hr;
  2584. return ptrGAB->QueryInterface(IID_IABContainer, reinterpret_cast<LPVOID *>(lppGAB));
  2585. }
  2586. /**
  2587. * Opens or creates an associated message in the non-ipm subtree of
  2588. * the given store.
  2589. *
  2590. * @param[in] lpStore User or public store to find message in
  2591. * @param[in] szMessageName Name of the configuration message
  2592. * @param[out] lppMessage Message to load/save your custom data from/to
  2593. *
  2594. * @return MAPI Error code
  2595. */
  2596. HRESULT GetConfigMessage(LPMDB lpStore, const char* szMessageName, IMessage **lppMessage)
  2597. {
  2598. ULONG cValues;
  2599. SPropArrayPtr ptrEntryIDs;
  2600. MAPIFolderPtr ptrFolder;
  2601. ULONG ulType;
  2602. MAPITablePtr ptrTable;
  2603. SPropValue propSubject;
  2604. SRowSetPtr ptrRows;
  2605. MessagePtr ptrMessage;
  2606. static constexpr const SizedSPropTagArray(2, sptaTreeProps) =
  2607. {2, {PR_NON_IPM_SUBTREE_ENTRYID, PR_IPM_SUBTREE_ENTRYID}};
  2608. HRESULT hr = lpStore->GetProps(sptaTreeProps, 0, &cValues, &~ptrEntryIDs);
  2609. if (FAILED(hr))
  2610. return hr;
  2611. // NON_IPM on a public store, IPM on a normal store
  2612. if (ptrEntryIDs[0].ulPropTag == sptaTreeProps.aulPropTag[0])
  2613. hr = lpStore->OpenEntry(ptrEntryIDs[0].Value.bin.cb, reinterpret_cast<ENTRYID *>(ptrEntryIDs[0].Value.bin.lpb), NULL, MAPI_MODIFY, &ulType, &~ptrFolder);
  2614. else if (ptrEntryIDs[1].ulPropTag == sptaTreeProps.aulPropTag[1])
  2615. hr = lpStore->OpenEntry(ptrEntryIDs[1].Value.bin.cb, reinterpret_cast<ENTRYID *>(ptrEntryIDs[1].Value.bin.lpb), NULL, MAPI_MODIFY, &ulType, &~ptrFolder);
  2616. else
  2617. hr = MAPI_E_INVALID_PARAMETER;
  2618. if (hr != hrSuccess)
  2619. return hr;
  2620. hr = ptrFolder->GetContentsTable(MAPI_DEFERRED_ERRORS | MAPI_ASSOCIATED, &~ptrTable);
  2621. if (hr != hrSuccess)
  2622. return hr;
  2623. propSubject.ulPropTag = PR_SUBJECT_A;
  2624. propSubject.Value.lpszA = (char*)szMessageName;
  2625. hr = ECPropertyRestriction(RELOP_EQ, PR_SUBJECT_A, &propSubject, ECRestriction::Cheap)
  2626. .FindRowIn(ptrTable, BOOKMARK_BEGINNING, 0);
  2627. if (hr == hrSuccess) {
  2628. hr = ptrTable->QueryRows(1, 0, &ptrRows);
  2629. if (hr != hrSuccess)
  2630. return hr;
  2631. }
  2632. if (!ptrRows.empty()) {
  2633. // message found, open it
  2634. auto lpEntryID = PCpropFindProp(ptrRows[0].lpProps, ptrRows[0].cValues, PR_ENTRYID);
  2635. if (lpEntryID == NULL)
  2636. return MAPI_E_INVALID_ENTRYID;
  2637. hr = ptrFolder->OpenEntry(lpEntryID->Value.bin.cb, reinterpret_cast<ENTRYID *>(lpEntryID->Value.bin.lpb), NULL, MAPI_MODIFY, &ulType, &~ptrMessage);
  2638. if (hr != hrSuccess)
  2639. return hr;
  2640. } else {
  2641. // not found in folder, create new message
  2642. hr = ptrFolder->CreateMessage(&IID_IMessage, MAPI_ASSOCIATED, &~ptrMessage);
  2643. if (hr != hrSuccess)
  2644. return hr;
  2645. hr = ptrMessage->SetProps(1, &propSubject, NULL);
  2646. if (hr != hrSuccess)
  2647. return hr;
  2648. // set mandatory message property
  2649. propSubject.ulPropTag = PR_MESSAGE_CLASS_A;
  2650. propSubject.Value.lpszA = const_cast<char *>("IPM.Zarafa.Configuration");
  2651. hr = ptrMessage->SetProps(1, &propSubject, NULL);
  2652. if (hr != hrSuccess)
  2653. return hr;
  2654. }
  2655. *lppMessage = ptrMessage.release();
  2656. return hrSuccess;
  2657. }
  2658. } /* namespace */