Leaked source code of windows server 2003
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.

356 lines
12 KiB

  1. // --------------------------------------------------------------------------------
  2. // Pop3call.cpp
  3. // --------------------------------------------------------------------------------
  4. #include "pch.h"
  5. #include "iconsole.h"
  6. #include "pop3call.h"
  7. // --------------------------------------------------------------------------------
  8. // HrCreatePOP3Transport
  9. // --------------------------------------------------------------------------------
  10. HRESULT HrCreatePOP3Transport(IPOP3Transport **ppPOP3)
  11. {
  12. // Locals
  13. HRESULT hr;
  14. CPOP3Callback *pCallback=NULL;
  15. // Create callback object
  16. pCallback = new CPOP3Callback();
  17. if (NULL == pCallback)
  18. {
  19. printf("Memory allocation failure\n");
  20. return E_OUTOFMEMORY;
  21. }
  22. // Load SMTP Transport
  23. hr = CoCreateInstance(CLSID_IPOP3Transport, NULL, CLSCTX_INPROC_SERVER, IID_IPOP3Transport, (LPVOID *)ppPOP3);
  24. if (FAILED(hr))
  25. {
  26. pCallback->Release();
  27. printf("Unable to load CLSID_IMNXPORT - IID_IPOP3Transport\n");
  28. return E_FAIL;
  29. }
  30. // InitNew
  31. hr = (*ppPOP3)->InitNew(NULL, pCallback);
  32. if (FAILED(hr))
  33. {
  34. pCallback->Release();
  35. printf("Unable to load CLSID_IMNXPORT - IID_IPOP3Transport\n");
  36. return E_FAIL;
  37. }
  38. // Done
  39. pCallback->Release();
  40. return S_OK;
  41. }
  42. // --------------------------------------------------------------------------------
  43. // CPOP3Callback::CPOP3Callback
  44. // --------------------------------------------------------------------------------
  45. CPOP3Callback::CPOP3Callback(void)
  46. {
  47. m_cRef = 1;
  48. }
  49. // --------------------------------------------------------------------------------
  50. // CPOP3Callback::~CPOP3Callback
  51. // --------------------------------------------------------------------------------
  52. CPOP3Callback::~CPOP3Callback(void)
  53. {
  54. }
  55. // --------------------------------------------------------------------------------
  56. // CPOP3Callback::QueryInterface
  57. // --------------------------------------------------------------------------------
  58. STDMETHODIMP CPOP3Callback::QueryInterface(REFIID riid, LPVOID *ppv)
  59. {
  60. // Locals
  61. HRESULT hr=S_OK;
  62. // Bad param
  63. if (ppv == NULL)
  64. {
  65. hr = E_INVALIDARG;
  66. goto exit;
  67. }
  68. // Init
  69. *ppv=NULL;
  70. // IID_IUnknown
  71. if (IID_IUnknown == riid)
  72. *ppv = (IUnknown *)this;
  73. // IID_IPOP3Callback
  74. else if (IID_IPOP3Callback == riid)
  75. *ppv = (IPOP3Callback *)this;
  76. // If not null, addref it and return
  77. if (NULL != *ppv)
  78. {
  79. ((LPUNKNOWN)*ppv)->AddRef();
  80. goto exit;
  81. }
  82. // No Interface
  83. hr = E_NOINTERFACE;
  84. exit:
  85. // Done
  86. return hr;
  87. }
  88. // --------------------------------------------------------------------------------
  89. // CPOP3Callback::AddRef
  90. // --------------------------------------------------------------------------------
  91. STDMETHODIMP_(ULONG) CPOP3Callback::AddRef(void)
  92. {
  93. return ++m_cRef;
  94. }
  95. // --------------------------------------------------------------------------------
  96. // CPOP3Callback::Release
  97. // --------------------------------------------------------------------------------
  98. STDMETHODIMP_(ULONG) CPOP3Callback::Release(void)
  99. {
  100. if (0 != --m_cRef)
  101. return m_cRef;
  102. delete this;
  103. return 0;
  104. }
  105. // --------------------------------------------------------------------------------
  106. // CPOP3Callback::OnLogonPrompt
  107. // --------------------------------------------------------------------------------
  108. STDMETHODIMP CPOP3Callback::OnLogonPrompt(
  109. LPINETSERVER pInetServer,
  110. IInternetTransport *pTransport)
  111. {
  112. printf("Enter User Name ('quit' to abort logon)>");
  113. scanf("%s", pInetServer->szUserName);
  114. fflush(stdin);
  115. if (lstrcmpi(pInetServer->szUserName, "quit") == 0)
  116. return S_FALSE;
  117. printf("Enter Password ('quit' to abort logon)>");
  118. scanf("%s", pInetServer->szPassword);
  119. fflush(stdin);
  120. if (lstrcmpi(pInetServer->szPassword, "quit") == 0)
  121. return S_FALSE;
  122. return S_OK;
  123. }
  124. // --------------------------------------------------------------------------------
  125. // CPOP3Callback::OnPrompt
  126. // --------------------------------------------------------------------------------
  127. STDMETHODIMP_(INT) CPOP3Callback::OnPrompt(
  128. HRESULT hrError,
  129. LPCTSTR pszText,
  130. LPCTSTR pszCaption,
  131. UINT uType,
  132. IInternetTransport *pTransport)
  133. {
  134. return S_OK;
  135. }
  136. // --------------------------------------------------------------------------------
  137. // CPOP3Callback::OnError
  138. // --------------------------------------------------------------------------------
  139. STDMETHODIMP CPOP3Callback::OnError(
  140. IXPSTATUS ixpstatus,
  141. LPIXPRESULT pIxpResult,
  142. IInternetTransport *pTransport)
  143. {
  144. printf("CPOP3Callback::OnError - Status: %d, hrResult: %08x\n", ixpstatus, pIxpResult->hrResult);
  145. return S_OK;
  146. }
  147. // --------------------------------------------------------------------------------
  148. // CPOP3Callback::OnStatus
  149. // --------------------------------------------------------------------------------
  150. STDMETHODIMP CPOP3Callback::OnStatus(
  151. IXPSTATUS ixpstatus,
  152. IInternetTransport *pTransport)
  153. {
  154. INETSERVER rServer;
  155. pTransport->GetServerInfo(&rServer);
  156. switch(ixpstatus)
  157. {
  158. case IXP_FINDINGHOST:
  159. printf("Finding '%s'...\n", rServer.szServerName);
  160. break;
  161. case IXP_CONNECTING:
  162. printf("Connecting '%s'...\n", rServer.szServerName);
  163. break;
  164. case IXP_SECURING:
  165. printf("Establishing secure connection to '%s'...\n", rServer.szServerName);
  166. break;
  167. case IXP_CONNECTED:
  168. printf("Connected '%s'\n", rServer.szServerName);
  169. break;
  170. case IXP_AUTHORIZING:
  171. printf("Authorizing '%s'...\n", rServer.szServerName);
  172. break;
  173. case IXP_AUTHRETRY:
  174. printf("Retrying Logon '%s'...\n", rServer.szServerName);
  175. break;
  176. case IXP_DISCONNECTING:
  177. printf("Disconnecting '%s'...\n", rServer.szServerName);
  178. break;
  179. case IXP_DISCONNECTED:
  180. printf("Disconnected '%s'\n", rServer.szServerName);
  181. PostThreadMessage(GetCurrentThreadId(), g_msgPOP3, IXP_DISCONNECTED, 0);
  182. PostThreadMessage(GetCurrentThreadId(), g_msgPOP3, POP3_QUIT, 0);
  183. break;
  184. }
  185. return S_OK;
  186. }
  187. // --------------------------------------------------------------------------------
  188. // CPOP3Callback::OnProgress
  189. // --------------------------------------------------------------------------------
  190. STDMETHODIMP CPOP3Callback::OnProgress(
  191. DWORD dwIncrement,
  192. DWORD dwCurrent,
  193. DWORD dwMaximum,
  194. IInternetTransport *pTransport)
  195. {
  196. return S_OK;
  197. }
  198. // --------------------------------------------------------------------------------
  199. // CPOP3Callback::OnCommand
  200. // --------------------------------------------------------------------------------
  201. STDMETHODIMP CPOP3Callback::OnCommand(
  202. CMDTYPE cmdtype,
  203. LPSTR pszLine,
  204. HRESULT hrResponse,
  205. IInternetTransport *pTransport)
  206. {
  207. INETSERVER rServer;
  208. pTransport->GetServerInfo(&rServer);
  209. if (CMD_SEND == cmdtype)
  210. {
  211. if (strstr(pszLine, "pass") || strstr(pszLine, "PASS"))
  212. printf("%s[TX]: <Secret Password>\n", rServer.szServerName);
  213. else
  214. printf("%s[TX]: %s", rServer.szServerName, pszLine);
  215. }
  216. else if (CMD_RESP == cmdtype)
  217. printf("%s[RX]: %s - %08x\n", rServer.szServerName, pszLine, hrResponse);
  218. return S_OK;
  219. }
  220. // --------------------------------------------------------------------------------
  221. // CPOP3Callback::OnTimeout
  222. // --------------------------------------------------------------------------------
  223. STDMETHODIMP CPOP3Callback::OnTimeout(
  224. DWORD *pdwTimeout,
  225. IInternetTransport *pTransport)
  226. {
  227. INETSERVER rServer;
  228. pTransport->GetServerInfo(&rServer);
  229. printf("Timeout '%s' !!!\n", rServer.szServerName);
  230. return S_OK;
  231. }
  232. // --------------------------------------------------------------------------------
  233. // CPOP3Callback::OnResponse
  234. // --------------------------------------------------------------------------------
  235. STDMETHODIMP CPOP3Callback::OnResponse(
  236. LPPOP3RESPONSE pResponse)
  237. {
  238. switch(pResponse->command)
  239. {
  240. case POP3_NONE:
  241. break;
  242. case POP3_BANNER:
  243. break;
  244. case POP3_CONNECTED:
  245. if (pResponse->fDone)
  246. PostThreadMessage(GetCurrentThreadId(), g_msgPOP3, POP3_CONNECTED, 0);
  247. break;
  248. case POP3_RSET:
  249. if (pResponse->fDone)
  250. PostThreadMessage(GetCurrentThreadId(), g_msgPOP3, POP3_RSET, 0);
  251. break;
  252. case POP3_QUIT:
  253. if (pResponse->fDone)
  254. PostThreadMessage(GetCurrentThreadId(), g_msgPOP3, POP3_QUIT, 0);
  255. break;
  256. case POP3_LIST:
  257. // 0 is invalid for dwPopId
  258. if (pResponse->rListInfo.dwPopId)
  259. {
  260. printf("OnResponse::POP3_LIST - dwPopId = %d\n", pResponse->rListInfo.dwPopId);
  261. printf("OnResponse::POP3_LIST - cbSize = %d\n", pResponse->rListInfo.cbSize);
  262. }
  263. if (pResponse->fDone)
  264. PostThreadMessage(GetCurrentThreadId(), g_msgPOP3, POP3_LIST, 0);
  265. break;
  266. case POP3_UIDL:
  267. // 0 is invalid for dwPopId
  268. if (pResponse->fValidInfo)
  269. {
  270. printf("OnResponse::POP3_UIDL - dwPopId = %d\n", pResponse->rUidlInfo.dwPopId);
  271. printf("OnResponse::POP3_UIDL - pszUidl = %s\n", pResponse->rUidlInfo.pszUidl);
  272. }
  273. if (pResponse->fDone)
  274. PostThreadMessage(GetCurrentThreadId(), g_msgPOP3, POP3_UIDL, 0);
  275. break;
  276. case POP3_DELE:
  277. // 0 is invalid for dwPopId
  278. if (pResponse->fValidInfo)
  279. printf("OnResponse::POP3_DELE - dwPopId = %d\n", pResponse->dwPopId);
  280. if (pResponse->fDone)
  281. PostThreadMessage(GetCurrentThreadId(), g_msgPOP3, POP3_DELE, 0);
  282. break;
  283. case POP3_STAT:
  284. if (pResponse->fDone)
  285. {
  286. printf("OnResponse::POP3_STAT - cMessages = %d\n", pResponse->rStatInfo.cMessages);
  287. printf("OnResponse::POP3_STAT - cbMessages = %d\n", pResponse->rStatInfo.cbMessages);
  288. PostThreadMessage(GetCurrentThreadId(), g_msgPOP3, POP3_STAT, 0);
  289. }
  290. break;
  291. case POP3_TOP:
  292. if (pResponse->fValidInfo)
  293. {
  294. if (pResponse->rTopInfo.pszLines)
  295. printf("%s", pResponse->rTopInfo.pszLines);
  296. if (pResponse->rTopInfo.cPreviewLines && pResponse->rTopInfo.fHeader && pResponse->rTopInfo.fBody)
  297. printf("%d Total\n", pResponse->rTopInfo.cbSoFar);
  298. else if (pResponse->rTopInfo.cPreviewLines == 0 && pResponse->rTopInfo.fHeader)
  299. printf("%d Total\n", pResponse->rTopInfo.cbSoFar);
  300. }
  301. if (pResponse->fDone)
  302. PostThreadMessage(GetCurrentThreadId(), g_msgPOP3, POP3_TOP, 0);
  303. break;
  304. case POP3_RETR:
  305. if (pResponse->fValidInfo)
  306. {
  307. if (pResponse->rRetrInfo.pszLines)
  308. printf("%s", pResponse->rRetrInfo.pszLines);
  309. if (pResponse->rRetrInfo.fHeader && pResponse->rRetrInfo.fBody)
  310. printf("%d Total\n", pResponse->rRetrInfo.cbSoFar);
  311. }
  312. if (pResponse->fDone)
  313. PostThreadMessage(GetCurrentThreadId(), g_msgPOP3, POP3_RETR, 0);
  314. break;
  315. }
  316. return S_OK;
  317. }