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.

321 lines
9.9 KiB

  1. // --------------------------------------------------------------------------------
  2. // Smtpcall.cpp
  3. // --------------------------------------------------------------------------------
  4. #include "pch.h"
  5. #include "iconsole.h"
  6. #include "smtpcall.h"
  7. // --------------------------------------------------------------------------------
  8. // HrCreateSMTPTransport
  9. // --------------------------------------------------------------------------------
  10. HRESULT HrCreateSMTPTransport(ISMTPTransport **ppSMTP)
  11. {
  12. // Locals
  13. HRESULT hr;
  14. CSMTPCallback *pCallback=NULL;
  15. // Create callback object
  16. pCallback = new CSMTPCallback();
  17. if (NULL == pCallback)
  18. {
  19. printf("Memory allocation failure\n");
  20. return E_OUTOFMEMORY;
  21. }
  22. // Load SMTP Transport
  23. hr = CoCreateInstance(CLSID_ISMTPTransport, NULL, CLSCTX_INPROC_SERVER, IID_ISMTPTransport, (LPVOID *)ppSMTP);
  24. if (FAILED(hr))
  25. {
  26. pCallback->Release();
  27. printf("Unable to load CLSID_IMNXPORT - IID_ISMTPTransport\n");
  28. return E_FAIL;
  29. }
  30. // InitNew
  31. hr = (*ppSMTP)->InitNew(NULL, pCallback);
  32. if (FAILED(hr))
  33. {
  34. pCallback->Release();
  35. printf("Unable to load CLSID_IMNXPORT - IID_ISMTPTransport\n");
  36. return E_FAIL;
  37. }
  38. // Done
  39. pCallback->Release();
  40. return S_OK;
  41. }
  42. // --------------------------------------------------------------------------------
  43. // CSMTPCallback::CSMTPCallback
  44. // --------------------------------------------------------------------------------
  45. CSMTPCallback::CSMTPCallback(void)
  46. {
  47. m_cRef = 1;
  48. }
  49. // --------------------------------------------------------------------------------
  50. // CSMTPCallback::~CSMTPCallback
  51. // --------------------------------------------------------------------------------
  52. CSMTPCallback::~CSMTPCallback(void)
  53. {
  54. }
  55. // --------------------------------------------------------------------------------
  56. // CSMTPCallback::QueryInterface
  57. // --------------------------------------------------------------------------------
  58. STDMETHODIMP CSMTPCallback::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_ISMTPCallback
  74. else if (IID_ISMTPCallback == riid)
  75. *ppv = (ISMTPCallback *)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. // CSMTPCallback::AddRef
  90. // --------------------------------------------------------------------------------
  91. STDMETHODIMP_(ULONG) CSMTPCallback::AddRef(void)
  92. {
  93. return ++m_cRef;
  94. }
  95. // --------------------------------------------------------------------------------
  96. // CSMTPCallback::Release
  97. // --------------------------------------------------------------------------------
  98. STDMETHODIMP_(ULONG) CSMTPCallback::Release(void)
  99. {
  100. if (0 != --m_cRef)
  101. return m_cRef;
  102. delete this;
  103. return 0;
  104. }
  105. // --------------------------------------------------------------------------------
  106. // CSMTPCallback::OnLogonPrompt
  107. // --------------------------------------------------------------------------------
  108. STDMETHODIMP CSMTPCallback::OnLogonPrompt(
  109. LPINETSERVER pInetServer,
  110. IInternetTransport *pTransport)
  111. {
  112. return S_OK;
  113. }
  114. // --------------------------------------------------------------------------------
  115. // CSMTPCallback::OnPrompt
  116. // --------------------------------------------------------------------------------
  117. STDMETHODIMP_(INT) CSMTPCallback::OnPrompt(
  118. HRESULT hrError,
  119. LPCTSTR pszText,
  120. LPCTSTR pszCaption,
  121. UINT uType,
  122. IInternetTransport *pTransport)
  123. {
  124. return S_OK;
  125. }
  126. // --------------------------------------------------------------------------------
  127. // CSMTPCallback::OnError
  128. // --------------------------------------------------------------------------------
  129. STDMETHODIMP CSMTPCallback::OnError(
  130. IXPSTATUS ixpstatus,
  131. LPIXPRESULT pIxpResult,
  132. IInternetTransport *pTransport)
  133. {
  134. printf("CSMTPCallback::OnError - Status: %d, hrResult: %08x\n", ixpstatus, pIxpResult->hrResult);
  135. return S_OK;
  136. }
  137. // --------------------------------------------------------------------------------
  138. // CSMTPCallback::OnStatus
  139. // --------------------------------------------------------------------------------
  140. STDMETHODIMP CSMTPCallback::OnStatus(
  141. IXPSTATUS ixpstatus,
  142. IInternetTransport *pTransport)
  143. {
  144. INETSERVER rServer;
  145. pTransport->GetServerInfo(&rServer);
  146. switch(ixpstatus)
  147. {
  148. case IXP_FINDINGHOST:
  149. printf("Finding '%s'...\n", rServer.szServerName);
  150. break;
  151. case IXP_CONNECTING:
  152. printf("Connecting '%s'...\n", rServer.szServerName);
  153. break;
  154. case IXP_SECURING:
  155. printf("Establishing secure connection to '%s'...\n", rServer.szServerName);
  156. break;
  157. case IXP_CONNECTED:
  158. printf("Connected '%s'\n", rServer.szServerName);
  159. break;
  160. case IXP_AUTHORIZING:
  161. printf("Authorizing '%s'...\n", rServer.szServerName);
  162. break;
  163. case IXP_AUTHRETRY:
  164. printf("Retrying Logon '%s'...\n", rServer.szServerName);
  165. break;
  166. case IXP_DISCONNECTING:
  167. printf("Disconnecting '%s'...\n", rServer.szServerName);
  168. break;
  169. case IXP_DISCONNECTED:
  170. printf("Disconnected '%s'\n", rServer.szServerName);
  171. PostThreadMessage(GetCurrentThreadId(), g_msgSMTP, IXP_DISCONNECTED, 0);
  172. PostThreadMessage(GetCurrentThreadId(), g_msgSMTP, SMTP_QUIT, 0);
  173. break;
  174. }
  175. return S_OK;
  176. }
  177. // --------------------------------------------------------------------------------
  178. // CSMTPCallback::OnProgress
  179. // --------------------------------------------------------------------------------
  180. STDMETHODIMP CSMTPCallback::OnProgress(
  181. DWORD dwIncrement,
  182. DWORD dwCurrent,
  183. DWORD dwMaximum,
  184. IInternetTransport *pTransport)
  185. {
  186. return S_OK;
  187. }
  188. // --------------------------------------------------------------------------------
  189. // CSMTPCallback::OnCommand
  190. // --------------------------------------------------------------------------------
  191. STDMETHODIMP CSMTPCallback::OnCommand(
  192. CMDTYPE cmdtype,
  193. LPSTR pszLine,
  194. HRESULT hrResponse,
  195. IInternetTransport *pTransport)
  196. {
  197. INETSERVER rServer;
  198. pTransport->GetServerInfo(&rServer);
  199. if (CMD_SEND == cmdtype)
  200. {
  201. printf("%s[TX]: %s", rServer.szServerName, pszLine);
  202. }
  203. else if (CMD_RESP == cmdtype)
  204. printf("%s[RX]: %s - %08x\n", rServer.szServerName, pszLine, hrResponse);
  205. return S_OK;
  206. }
  207. // --------------------------------------------------------------------------------
  208. // CSMTPCallback::OnTimeout
  209. // --------------------------------------------------------------------------------
  210. STDMETHODIMP CSMTPCallback::OnTimeout(
  211. DWORD *pdwTimeout,
  212. IInternetTransport *pTransport)
  213. {
  214. INETSERVER rServer;
  215. pTransport->GetServerInfo(&rServer);
  216. printf("Timeout '%s' !!!\n", rServer.szServerName);
  217. return S_OK;
  218. }
  219. // --------------------------------------------------------------------------------
  220. // CSMTPCallback::OnResponse
  221. // --------------------------------------------------------------------------------
  222. STDMETHODIMP CSMTPCallback::OnResponse(
  223. LPSMTPRESPONSE pResponse)
  224. {
  225. switch(pResponse->command)
  226. {
  227. case SMTP_NONE:
  228. break;
  229. case SMTP_BANNER:
  230. break;
  231. case SMTP_CONNECTED:
  232. if (pResponse->fDone)
  233. PostThreadMessage(GetCurrentThreadId(), g_msgSMTP, SMTP_CONNECTED, 0);
  234. break;
  235. case SMTP_SEND_MESSAGE:
  236. if (pResponse->fDone)
  237. PostThreadMessage(GetCurrentThreadId(), g_msgSMTP, SMTP_SEND_MESSAGE, 0);
  238. break;
  239. case SMTP_EHLO:
  240. if (pResponse->fDone)
  241. PostThreadMessage(GetCurrentThreadId(), g_msgSMTP, SMTP_EHLO, 0);
  242. break;
  243. case SMTP_HELO:
  244. if (pResponse->fDone)
  245. PostThreadMessage(GetCurrentThreadId(), g_msgSMTP, SMTP_HELO, 0);
  246. break;
  247. case SMTP_MAIL:
  248. if (pResponse->fDone)
  249. PostThreadMessage(GetCurrentThreadId(), g_msgSMTP, SMTP_MAIL, 0);
  250. break;
  251. case SMTP_RCPT:
  252. if (pResponse->fDone)
  253. PostThreadMessage(GetCurrentThreadId(), g_msgSMTP, SMTP_RCPT, 0);
  254. break;
  255. case SMTP_RSET:
  256. if (pResponse->fDone)
  257. PostThreadMessage(GetCurrentThreadId(), g_msgSMTP, SMTP_RSET, 0);
  258. break;
  259. case SMTP_QUIT:
  260. if (pResponse->fDone)
  261. PostThreadMessage(GetCurrentThreadId(), g_msgSMTP, SMTP_QUIT, 0);
  262. break;
  263. case SMTP_DATA:
  264. if (pResponse->fDone)
  265. PostThreadMessage(GetCurrentThreadId(), g_msgSMTP, SMTP_DATA, 0);
  266. break;
  267. case SMTP_DOT:
  268. if (pResponse->fDone)
  269. PostThreadMessage(GetCurrentThreadId(), g_msgSMTP, SMTP_DOT, 0);
  270. break;
  271. case SMTP_SEND_STREAM:
  272. if (pResponse->fDone)
  273. PostThreadMessage(GetCurrentThreadId(), g_msgSMTP, SMTP_SEND_STREAM, 0);
  274. break;
  275. case SMTP_CUSTOM:
  276. if (pResponse->fDone)
  277. PostThreadMessage(GetCurrentThreadId(), g_msgSMTP, SMTP_CUSTOM, 0);
  278. break;
  279. }
  280. return S_OK;
  281. }