Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

370 lines
9.9 KiB

  1. // oe.cpp
  2. //
  3. #include "stdpch.h"
  4. #pragma hdrstop
  5. #include "host.h"
  6. #include "oe.h"
  7. #include <initguid.h>
  8. #include <oaidl.h>
  9. #include <imnact.h> // account manager stuff
  10. #include <imnxport.h>
  11. #include <msident.h>
  12. enum MAIL_TYPE
  13. {
  14. SMTP,
  15. SMTP2,
  16. IMAP,
  17. POP3,
  18. HTTP,
  19. };
  20. HRESULT GetServerAndPort(IN INETSERVER & rServer, OUT CHost & host, OUT DWORD & dwPort)
  21. {
  22. #ifdef UNICODE
  23. TCHAR wcsServer[128];
  24. mbstowcs(wcsServer, rServer.szServerName, 128);
  25. host.SetHost(wcsServer);
  26. #else
  27. host.SetHost(rServer.szServerName);
  28. #endif
  29. dwPort = (long)rServer.dwPort;
  30. return S_OK;
  31. }
  32. HRESULT GetDefaultOutBoundMailServer(IN IImnAccount *pIImnAccount,
  33. OUT INETSERVER & rServer,
  34. OUT WCHAR *pszMailType)
  35. {
  36. ISMTPTransport *pSMTPTransport;
  37. ISMTPTransport2 *pSMTPTransport2;
  38. HRESULT hr;
  39. WCHAR wsz[1000];
  40. // Create the SMTP transport object
  41. //
  42. hr = CoCreateInstance(CLSID_ISMTPTransport, NULL, CLSCTX_INPROC_SERVER, IID_ISMTPTransport, (LPVOID *)&pSMTPTransport);
  43. if( SUCCEEDED(hr) )
  44. {
  45. // Get the SMTP server information
  46. //
  47. hr = pSMTPTransport->InetServerFromAccount(pIImnAccount, &rServer);
  48. lstrcpy(pszMailType,L"SMTP");
  49. pSMTPTransport->Release();
  50. }
  51. if( FAILED(hr) )
  52. {
  53. // Unable to get SMTP server info, lets try and get SMTP transport 2 server information
  54. //
  55. hr = CoCreateInstance(CLSID_ISMTPTransport, NULL, CLSCTX_INPROC_SERVER, IID_ISMTPTransport2, (LPVOID *)&pSMTPTransport2);
  56. if( SUCCEEDED(hr) )
  57. {
  58. // Get SMTP2 server info
  59. //
  60. hr = pSMTPTransport2->InetServerFromAccount(pIImnAccount, &rServer);
  61. lstrcpy(pszMailType,L"SMTP2");
  62. pSMTPTransport->Release();
  63. }
  64. }
  65. if( FAILED(hr) )
  66. {
  67. wsprintf(wsz,L"Unable to get Outbound Mail server %X\n",hr);
  68. OutputDebugString(wsz);
  69. }
  70. if( FAILED(hr) )
  71. {
  72. // Make sure to clear the struct
  73. //
  74. memset(&rServer,0,sizeof(rServer));
  75. lstrcpy(pszMailType,L"");
  76. }
  77. return hr;
  78. }
  79. HRESULT GetDefaultInBoundMailServer(IN IImnAccount *pIImnAccount,
  80. OUT INETSERVER & rServer,
  81. OUT WCHAR *pszMailType)
  82. {
  83. IPOP3Transport *pPOP3Transport;
  84. IIMAPTransport *pIMAPTransport;
  85. IHTTPMailTransport *pHTTPTransport;
  86. HRESULT hr;
  87. WCHAR wsz[1000];
  88. // Create the HTTP transport object
  89. //
  90. hr = CoCreateInstance(CLSID_IHTTPMailTransport, NULL, CLSCTX_INPROC_SERVER, IID_IHTTPMailTransport, (LPVOID *)&pHTTPTransport);
  91. if( SUCCEEDED(hr) )
  92. {
  93. // Get the HTTP server information
  94. //
  95. hr = pHTTPTransport->InetServerFromAccount(pIImnAccount, &rServer);
  96. lstrcpy(pszMailType,L"HTTP");
  97. pHTTPTransport->Release();
  98. }
  99. if( FAILED(hr) )
  100. {
  101. // Create the POP3 transport object
  102. //
  103. hr = CoCreateInstance(CLSID_IPOP3Transport, NULL, CLSCTX_INPROC_SERVER, IID_IPOP3Transport, (LPVOID *)&pPOP3Transport);
  104. if( SUCCEEDED(hr) )
  105. {
  106. // Get the POP3 server information
  107. //
  108. hr = pPOP3Transport->InetServerFromAccount(pIImnAccount, &rServer);
  109. lstrcpy(pszMailType,L"POP3");
  110. pPOP3Transport->Release();
  111. }
  112. }
  113. if( FAILED(hr) )
  114. {
  115. // Create the SMTP transport object
  116. //
  117. hr = CoCreateInstance(CLSID_IIMAPTransport, NULL, CLSCTX_INPROC_SERVER, IID_IIMAPTransport, (LPVOID *)&pIMAPTransport);
  118. if( SUCCEEDED(hr) )
  119. {
  120. // Get the SMTP server information
  121. //
  122. hr = pIMAPTransport->InetServerFromAccount(pIImnAccount, &rServer);
  123. lstrcpy(pszMailType,L"IMAP");
  124. pIMAPTransport->Release();
  125. }
  126. }
  127. if( FAILED(hr) )
  128. {
  129. wsprintf(wsz,L"Unable to get Inbound Mail server %X\n",hr);
  130. OutputDebugString(wsz);
  131. }
  132. if( FAILED(hr) )
  133. {
  134. memset(&rServer,0,sizeof(rServer));
  135. lstrcpy(pszMailType,L"");
  136. }
  137. return hr;
  138. }
  139. HRESULT GetOEDefaultMailServer(OUT CHost & InBoundMailHost,
  140. OUT DWORD & dwInBoundMailPort,
  141. OUT TCHAR * pszInBoundMailType,
  142. OUT CHost & OutBoundMailHost,
  143. OUT DWORD & dwOutBoundMailPort,
  144. OUT TCHAR * pszOutBoundMailType)
  145. {
  146. IImnAccountManager2 *pIImnAccountManager2 = NULL;
  147. IImnAccountManager *pIImnAccountManager = NULL;
  148. IImnAccount *pIImnAccount = NULL;
  149. HRESULT hr;
  150. WCHAR wsz[1000];
  151. hr = CoCreateInstance(CLSID_ImnAccountManager, NULL, CLSCTX_INPROC_SERVER, IID_IImnAccountManager, (void**)&pIImnAccountManager);
  152. if( SUCCEEDED(hr) )
  153. {
  154. hr = pIImnAccountManager->QueryInterface(__uuidof(IImnAccountManager2), (void**)&pIImnAccountManager2);
  155. if(SUCCEEDED(hr))
  156. {
  157. hr = pIImnAccountManager2->InitUser(NULL, (GUID)UID_GIBC_DEFAULT_USER, 0);
  158. }
  159. //if(FAILED(hr))
  160. if(SUCCEEDED(hr))
  161. {
  162. hr = pIImnAccountManager->Init(NULL);
  163. }
  164. if( SUCCEEDED(hr) )
  165. {
  166. hr = pIImnAccountManager->GetDefaultAccount(ACCT_MAIL, &pIImnAccount);
  167. if( SUCCEEDED(hr) )
  168. {
  169. INETSERVER rServer={0};
  170. HRESULT hr2, hr3;
  171. hr2 = GetDefaultInBoundMailServer(pIImnAccount,rServer,pszInBoundMailType);
  172. GetServerAndPort(rServer,InBoundMailHost,dwInBoundMailPort);
  173. hr3 = GetDefaultOutBoundMailServer(pIImnAccount,rServer,pszOutBoundMailType);
  174. GetServerAndPort(rServer,OutBoundMailHost,dwOutBoundMailPort);
  175. if( SUCCEEDED(hr2) || SUCCEEDED(hr3) )
  176. {
  177. hr = S_OK;
  178. }
  179. else
  180. {
  181. hr = E_FAIL;
  182. }
  183. pIImnAccount->Release();
  184. }
  185. }
  186. else
  187. {
  188. wsprintf(wsz,L"Unable to get IImnAccountManager2 %X\n",hr);
  189. OutputDebugString(wsz);
  190. }
  191. pIImnAccountManager->Release();
  192. if( pIImnAccountManager2 )
  193. {
  194. pIImnAccountManager2->Release();
  195. }
  196. }
  197. return hr;
  198. }
  199. HRESULT GetOEDefaultMailServer(CHost& host, DWORD& dwPort)
  200. {
  201. HRESULT hr;
  202. WCHAR wsz[700];
  203. auto_rel<IImnAccountManager> pIImnAccountManager;
  204. auto_rel<IImnAccountManager2> pIImnAccountManager2;
  205. auto_rel<IImnAccount> pIImnAccount;
  206. auto_rel<ISMTPTransport> pSMTPTransport;
  207. auto_rel<INNTPTransport> pNNTPTransport;
  208. INETSERVER rServer={0};
  209. hr = CoCreateInstance(CLSID_ImnAccountManager, NULL, CLSCTX_INPROC_SERVER, IID_IImnAccountManager, (void**)&pIImnAccountManager);
  210. if(FAILED(hr))
  211. {
  212. swprintf(wsz,L"Unable to CoCreateInstance %X",hr);
  213. OutputDebugString(wsz);
  214. return hr;
  215. }
  216. hr = pIImnAccountManager->QueryInterface(__uuidof(IImnAccountManager2), (void**)&pIImnAccountManager2);
  217. if(SUCCEEDED(hr))
  218. {
  219. hr = pIImnAccountManager2->InitUser(NULL, (GUID)UID_GIBC_DEFAULT_USER, 0);
  220. }
  221. if(FAILED(hr))
  222. {
  223. hr = pIImnAccountManager->Init(NULL);
  224. if(FAILED(hr))
  225. {
  226. swprintf(wsz,L"Unable to pIImnAccountManager->Init %X",hr);
  227. OutputDebugString(wsz);
  228. return hr;
  229. }
  230. }
  231. hr = pIImnAccountManager->GetDefaultAccount(ACCT_MAIL, &pIImnAccount);
  232. if(SUCCEEDED(hr))
  233. {
  234. hr = CoCreateInstance(CLSID_ISMTPTransport, NULL, CLSCTX_INPROC_SERVER, IID_ISMTPTransport, (LPVOID *)&pSMTPTransport);
  235. if(SUCCEEDED(hr))
  236. {
  237. hr = pSMTPTransport->InetServerFromAccount(pIImnAccount, &rServer);
  238. if(SUCCEEDED(hr))
  239. {
  240. #ifdef UNICODE
  241. TCHAR wcsServer[128];
  242. mbstowcs(wcsServer, rServer.szServerName, 128);
  243. host.SetHost(wcsServer);
  244. #else
  245. host.SetHost(rServer.szServerName);
  246. #endif
  247. dwPort = (long)rServer.dwPort;
  248. }
  249. else
  250. {
  251. swprintf(wsz,L"Unable to pSMTPTransport->InetServerFromAccount %X",hr);
  252. OutputDebugString(wsz);
  253. }
  254. }
  255. else
  256. {
  257. swprintf(wsz,L"Unable to oCreateInstance(CLSID_ISMTPTransport %X",hr);
  258. OutputDebugString(wsz);
  259. }
  260. }
  261. else
  262. {
  263. swprintf(wsz,L"Unable to pIImnAccountManager->GetDefaultAccount %X",hr);
  264. OutputDebugString(wsz);
  265. }
  266. swprintf(wsz,L"HR Value %X",hr);
  267. OutputDebugString(wsz);
  268. return hr;
  269. }
  270. HRESULT GetOEDefaultNewsServer(CHost& host, DWORD& dwPort)
  271. {
  272. HRESULT hr;
  273. auto_rel<IImnAccountManager> pIImnAccountManager;
  274. auto_rel<IImnAccountManager2> pIImnAccountManager2;
  275. auto_rel<IImnAccount> pIImnAccount;
  276. auto_rel<ISMTPTransport> pSMTPTransport;
  277. auto_rel<INNTPTransport> pNNTPTransport;
  278. INETSERVER rServer={0};
  279. host.SetHost(TEXT(""));
  280. hr = CoCreateInstance(CLSID_ImnAccountManager, NULL, CLSCTX_INPROC_SERVER, IID_IImnAccountManager, (void**)&pIImnAccountManager);
  281. if(FAILED(hr))
  282. return hr;
  283. hr = pIImnAccountManager->QueryInterface(__uuidof(IImnAccountManager2), (void**)&pIImnAccountManager2);
  284. if(SUCCEEDED(hr))
  285. {
  286. hr = pIImnAccountManager2->InitUser(NULL, (GUID)UID_GIBC_DEFAULT_USER, 0);
  287. }
  288. if(FAILED(hr))
  289. {
  290. hr = pIImnAccountManager->Init(NULL);
  291. if(FAILED(hr))
  292. return hr;
  293. }
  294. hr = pIImnAccountManager->GetDefaultAccount(ACCT_NEWS, &pIImnAccount);
  295. if(SUCCEEDED(hr))
  296. {
  297. hr = CoCreateInstance(CLSID_INNTPTransport, NULL, CLSCTX_INPROC_SERVER, IID_INNTPTransport, (LPVOID *)&pNNTPTransport);
  298. if(SUCCEEDED(hr))
  299. {
  300. hr = pNNTPTransport->InetServerFromAccount(pIImnAccount, &rServer);
  301. if(SUCCEEDED(hr))
  302. {
  303. #ifdef UNICODE
  304. TCHAR wcsServer[128];
  305. mbstowcs(wcsServer, rServer.szServerName, 128);
  306. host.SetHost(wcsServer);
  307. #else
  308. host.SetHost(rServer.szServerName);
  309. #endif
  310. dwPort = (long)rServer.dwPort;
  311. }
  312. }
  313. }
  314. return S_OK;
  315. }