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.

571 lines
22 KiB

  1. // --------------------------------------------------------------------------------
  2. // Nntpcall.cpp
  3. // --------------------------------------------------------------------------------
  4. #include "pch.h"
  5. #include "iconsole.h"
  6. #include "nntpcall.h"
  7. // --------------------------------------------------------------------------------
  8. // HrCreateNNTPTransport
  9. // --------------------------------------------------------------------------------
  10. HRESULT HrCreateNNTPTransport(INNTPTransport **ppNNTP)
  11. {
  12. HRESULT hr;
  13. CNNTPCallback *pCallback = NULL;
  14. // Create the callback object
  15. pCallback = new CNNTPCallback();
  16. if (NULL == pCallback)
  17. {
  18. printf("Memory allocation failure\n");
  19. return (E_OUTOFMEMORY);
  20. }
  21. // Load the NNTP Transport
  22. hr = CoCreateInstance(CLSID_INNTPTransport, NULL, CLSCTX_INPROC_SERVER,
  23. IID_INNTPTransport, (LPVOID*) ppNNTP);
  24. if (FAILED(hr))
  25. {
  26. pCallback->Release();
  27. printf("Unable to load CLSID_IMNXPORT - IID_INNTPTransport\n");
  28. return (hr);
  29. }
  30. // Initialize the transport
  31. hr = (*ppNNTP)->InitNew(NULL, pCallback);
  32. if (FAILED(hr))
  33. {
  34. pCallback->Release();
  35. printf("Unable to initialize the transport\n");
  36. return (hr);
  37. }
  38. // Release our refcount on the callback since the transport has one now
  39. pCallback->Release();
  40. return (S_OK);
  41. }
  42. // --------------------------------------------------------------------------------
  43. // CNNTPCallback::CNNTPCallback
  44. // --------------------------------------------------------------------------------
  45. CNNTPCallback::CNNTPCallback(void)
  46. {
  47. m_cRef = 1;
  48. }
  49. // --------------------------------------------------------------------------------
  50. // CNNTPCallback::~CNNTPCallback
  51. // --------------------------------------------------------------------------------
  52. CNNTPCallback::~CNNTPCallback(void)
  53. {
  54. }
  55. // --------------------------------------------------------------------------------
  56. // CNNTPCallback::QueryInterface
  57. // --------------------------------------------------------------------------------
  58. STDMETHODIMP CNNTPCallback::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_INNTPCallback
  74. else if (IID_INNTPCallback == riid)
  75. *ppv = (INNTPCallback *)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. // CNNTPCallback::AddRef
  90. // --------------------------------------------------------------------------------
  91. STDMETHODIMP_(ULONG) CNNTPCallback::AddRef(void)
  92. {
  93. return ++m_cRef;
  94. }
  95. // --------------------------------------------------------------------------------
  96. // CNNTPCallback::Release
  97. // --------------------------------------------------------------------------------
  98. STDMETHODIMP_(ULONG) CNNTPCallback::Release(void)
  99. {
  100. if (0 != --m_cRef)
  101. return m_cRef;
  102. delete this;
  103. return 0;
  104. }
  105. // --------------------------------------------------------------------------------
  106. // CNNTPCallback::OnLogonPrompt
  107. // --------------------------------------------------------------------------------
  108. STDMETHODIMP CNNTPCallback::OnLogonPrompt(LPINETSERVER pInetServer,
  109. IInternetTransport *pTransport)
  110. {
  111. printf("Enter User Name ('quit' to abort logon)>");
  112. scanf("%s", pInetServer->szUserName);
  113. fflush(stdin);
  114. if (lstrcmpi(pInetServer->szUserName, "quit") == 0)
  115. return S_FALSE;
  116. printf("Enter Password ('quit' to abort logon)>");
  117. scanf("%s", pInetServer->szPassword);
  118. fflush(stdin);
  119. if (lstrcmpi(pInetServer->szPassword, "quit") == 0)
  120. return S_FALSE;
  121. return S_OK;
  122. }
  123. // --------------------------------------------------------------------------------
  124. // CNNTPCallback::OnPrompt
  125. // --------------------------------------------------------------------------------
  126. STDMETHODIMP_(INT) CNNTPCallback::OnPrompt(HRESULT hrError, LPCTSTR pszText,
  127. LPCTSTR pszCaption, UINT uType,
  128. IInternetTransport *pTransport)
  129. {
  130. return S_OK;
  131. }
  132. // --------------------------------------------------------------------------------
  133. // CNNTPCallback::OnError
  134. // --------------------------------------------------------------------------------
  135. STDMETHODIMP CNNTPCallback::OnError(IXPSTATUS ixpstatus, LPIXPRESULT pIxpResult,
  136. IInternetTransport *pTransport)
  137. {
  138. HANDLE hConsole = INVALID_HANDLE_VALUE;
  139. CONSOLE_SCREEN_BUFFER_INFO csbi;
  140. BOOL fChanged = FALSE;
  141. char szBuffer[256];
  142. DWORD dwWritten = 0;
  143. // Get a handle to the console window
  144. if (INVALID_HANDLE_VALUE != (hConsole = GetStdHandle(STD_OUTPUT_HANDLE)))
  145. {
  146. // Get the current attributes for the console
  147. if (GetConsoleScreenBufferInfo(hConsole, &csbi))
  148. {
  149. // Set the text color to be red on whatever background is currently
  150. // there
  151. fChanged = SetConsoleTextAttribute(hConsole,
  152. (csbi.wAttributes & 0xF0) | FOREGROUND_RED | FOREGROUND_INTENSITY);
  153. }
  154. }
  155. wsprintf(szBuffer, "CNNTPCallback::OnError - Status: %d, hrResult: %08x\n",
  156. ixpstatus, pIxpResult->hrResult);
  157. WriteConsole(hConsole, szBuffer, lstrlen(szBuffer), &dwWritten, NULL);
  158. // If we changed the screen attributes, then change them back
  159. if (fChanged)
  160. SetConsoleTextAttribute(hConsole, csbi.wAttributes);
  161. return S_OK;
  162. }
  163. // --------------------------------------------------------------------------------
  164. // CNNTPCallback::OnStatus
  165. // --------------------------------------------------------------------------------
  166. STDMETHODIMP CNNTPCallback::OnStatus(IXPSTATUS ixpstatus,
  167. IInternetTransport *pTransport)
  168. {
  169. INETSERVER rServer;
  170. HANDLE hConsole = INVALID_HANDLE_VALUE;
  171. CONSOLE_SCREEN_BUFFER_INFO csbi;
  172. BOOL fChanged = FALSE;
  173. char szBuffer[256];
  174. DWORD dwWritten = 0;
  175. // Get a handle to the console window
  176. if (INVALID_HANDLE_VALUE != (hConsole = GetStdHandle(STD_OUTPUT_HANDLE)))
  177. {
  178. // Get the current attributes for the console
  179. if (GetConsoleScreenBufferInfo(hConsole, &csbi))
  180. {
  181. // Set the text color to be red on whatever background is currently
  182. // there
  183. fChanged = SetConsoleTextAttribute(hConsole,
  184. (csbi.wAttributes & 0xF0) | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
  185. }
  186. }
  187. pTransport->GetServerInfo(&rServer);
  188. switch(ixpstatus)
  189. {
  190. case IXP_FINDINGHOST:
  191. wsprintf(szBuffer, "Finding '%s'...\n", rServer.szServerName);
  192. break;
  193. case IXP_CONNECTING:
  194. wsprintf(szBuffer, "Connecting '%s'...\n", rServer.szServerName);
  195. break;
  196. case IXP_SECURING:
  197. wsprintf(szBuffer, "Establishing secure connection to '%s'...\n", rServer.szServerName);
  198. break;
  199. case IXP_CONNECTED:
  200. wsprintf(szBuffer, "Connected '%s'\n", rServer.szServerName);
  201. break;
  202. case IXP_AUTHORIZING:
  203. wsprintf(szBuffer, "Authorizing '%s'...\n", rServer.szServerName);
  204. break;
  205. case IXP_AUTHRETRY:
  206. wsprintf(szBuffer, "Retrying Logon '%s'...\n", rServer.szServerName);
  207. break;
  208. case IXP_DISCONNECTING:
  209. wsprintf(szBuffer, "Disconnecting '%s'...\n", rServer.szServerName);
  210. break;
  211. case IXP_DISCONNECTED:
  212. wsprintf(szBuffer, "Disconnected '%s'\n", rServer.szServerName);
  213. break;
  214. }
  215. WriteConsole(hConsole, szBuffer, lstrlen(szBuffer), &dwWritten, NULL);
  216. // If we changed the screen attributes, then change them back
  217. if (fChanged)
  218. SetConsoleTextAttribute(hConsole, csbi.wAttributes);
  219. return S_OK;
  220. }
  221. // --------------------------------------------------------------------------------
  222. // CNNTPCallback::OnProgress
  223. // --------------------------------------------------------------------------------
  224. STDMETHODIMP CNNTPCallback::OnProgress(DWORD dwIncrement, DWORD dwCurrent,
  225. DWORD dwMaximum, IInternetTransport *pTransport)
  226. {
  227. return S_OK;
  228. }
  229. // --------------------------------------------------------------------------------
  230. // CNNTPCallback::OnCommand
  231. // --------------------------------------------------------------------------------
  232. STDMETHODIMP CNNTPCallback::OnCommand(CMDTYPE cmdtype, LPSTR pszLine,
  233. HRESULT hrResponse,
  234. IInternetTransport *pTransport)
  235. {
  236. INETSERVER rServer;
  237. #if 1
  238. HANDLE hOut;
  239. CONSOLE_SCREEN_BUFFER_INFO csbi;
  240. hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  241. GetConsoleScreenBufferInfo(hOut, &csbi);
  242. SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | (0xf0 & csbi.wAttributes));
  243. #endif
  244. pTransport->GetServerInfo(&rServer);
  245. if (CMD_SEND == cmdtype)
  246. {
  247. if (strstr(pszLine, "pass") || strstr(pszLine, "PASS"))
  248. printf(" %s[TX]: <Secret Password>\n", rServer.szServerName);
  249. else
  250. printf(" %s[TX]: %s", rServer.szServerName, pszLine);
  251. }
  252. else if (CMD_RESP == cmdtype)
  253. printf(" %s[RX]: %s - %08x\n", rServer.szServerName, pszLine, hrResponse);
  254. #if 1
  255. SetConsoleTextAttribute(hOut, csbi.wAttributes);
  256. #endif
  257. return S_OK;
  258. }
  259. // --------------------------------------------------------------------------------
  260. // CNNTPCallback::OnTimeout
  261. // --------------------------------------------------------------------------------
  262. STDMETHODIMP CNNTPCallback::OnTimeout(DWORD *pdwTimeout,
  263. IInternetTransport *pTransport)
  264. {
  265. INETSERVER rServer;
  266. pTransport->GetServerInfo(&rServer);
  267. printf("Timeout '%s' !!!\n", rServer.szServerName);
  268. return S_OK;
  269. }
  270. // --------------------------------------------------------------------------------
  271. // CNNTPCallback::OnResponse
  272. // --------------------------------------------------------------------------------
  273. STDMETHODIMP CNNTPCallback::OnResponse(LPNNTPRESPONSE pResponse)
  274. {
  275. switch(pResponse->state)
  276. {
  277. case NS_DISCONNECTED:
  278. PostThreadMessage(GetCurrentThreadId(), g_msgNNTP, pResponse->state, 0);
  279. break;
  280. case NS_CONNECT:
  281. PostThreadMessage(GetCurrentThreadId(), g_msgNNTP, 0, 0);
  282. break;
  283. case NS_AUTHINFO:
  284. PostThreadMessage(GetCurrentThreadId(), g_msgNNTP, 0, 0);
  285. break;
  286. case NS_GROUP:
  287. if (SUCCEEDED(pResponse->rIxpResult.hrResult))
  288. {
  289. printf("\n"
  290. "NS_GROUP_RESP - rGroup.dwFirst = %d\n"
  291. " rGroup.dwLast = %d\n"
  292. " rGroup.dwCount = %d\n"
  293. " rGroup.pszGroup = %s\n\n",
  294. pResponse->rGroup.dwFirst, pResponse->rGroup.dwLast,
  295. pResponse->rGroup.dwCount, pResponse->rGroup.pszGroup);
  296. }
  297. g_pNNTP->ReleaseResponse(pResponse);
  298. PostThreadMessage(GetCurrentThreadId(), g_msgNNTP, pResponse->state, 0);
  299. break;
  300. case NS_NEXT:
  301. if (SUCCEEDED(pResponse->rIxpResult.hrResult))
  302. {
  303. printf("\n"
  304. "NS_NEXT_RESP - rNext.dwArticleNum = %d\n"
  305. " - rNext.pszMessageId = %s\n\n",
  306. pResponse->rNext.dwArticleNum, pResponse->rNext.pszMessageId);
  307. pResponse->pTransport->ReleaseResponse(pResponse);
  308. }
  309. g_pNNTP->ReleaseResponse(pResponse);
  310. PostThreadMessage(GetCurrentThreadId(), g_msgNNTP, pResponse->state, 0);
  311. break;
  312. case NS_LAST:
  313. if (SUCCEEDED(pResponse->rIxpResult.hrResult))
  314. {
  315. printf("\n"
  316. "NS_LAST_RESP - rNext.dwArticleNum = %d\n"
  317. " - rNext.pszMessageId = %s\n\n",
  318. pResponse->rLast.dwArticleNum, pResponse->rLast.pszMessageId);
  319. pResponse->pTransport->ReleaseResponse(pResponse);
  320. }
  321. g_pNNTP->ReleaseResponse(pResponse);
  322. PostThreadMessage(GetCurrentThreadId(), g_msgNNTP, pResponse->state, 0);
  323. break;
  324. case NS_STAT:
  325. if (SUCCEEDED(pResponse->rIxpResult.hrResult))
  326. {
  327. printf("\n"
  328. "NS_STAT_RESP - rNext.dwArticleNum = %d\n"
  329. " - rNext.pszMessageId = %s\n\n",
  330. pResponse->rLast.dwArticleNum, pResponse->rLast.pszMessageId);
  331. pResponse->pTransport->ReleaseResponse(pResponse);
  332. }
  333. g_pNNTP->ReleaseResponse(pResponse);
  334. PostThreadMessage(GetCurrentThreadId(), g_msgNNTP, pResponse->state, 0);
  335. break;
  336. case NS_LIST:
  337. if (pResponse->rIxpResult.uiServerError != IXP_NNTP_LIST_DATA_FOLLOWS)
  338. PostThreadMessage(GetCurrentThreadId(), g_msgNNTP, 0, 0);
  339. else
  340. {
  341. if (SUCCEEDED(pResponse->rIxpResult.hrResult))
  342. {
  343. for (UINT i = 0; i < pResponse->rList.cLines; i++)
  344. printf("%s\n", pResponse->rList.rgszLines[i]);
  345. }
  346. g_pNNTP->ReleaseResponse(pResponse);
  347. if (pResponse->fDone)
  348. PostThreadMessage(GetCurrentThreadId(), g_msgNNTP, 0, 0);
  349. }
  350. break;
  351. case NS_LISTGROUP:
  352. if (pResponse->rIxpResult.uiServerError != IXP_NNTP_GROUP_SELECTED)
  353. PostThreadMessage(GetCurrentThreadId(), g_msgNNTP, 0, 0);
  354. else
  355. {
  356. if (SUCCEEDED(pResponse->rIxpResult.hrResult))
  357. {
  358. for (UINT i = 0; i < pResponse->rListGroup.cArticles; i++)
  359. printf("%d\n", pResponse->rListGroup.rgArticles[i]);
  360. }
  361. g_pNNTP->ReleaseResponse(pResponse);
  362. if (pResponse->fDone)
  363. PostThreadMessage(GetCurrentThreadId(), g_msgNNTP, 0, 0);
  364. }
  365. break;
  366. case NS_DATE:
  367. if (SUCCEEDED(pResponse->rIxpResult.hrResult))
  368. {
  369. printf("\nNS_DATE - %02d/%02d/%04d %02d:%02d:%02d\n\n",
  370. pResponse->rDate.wMonth, pResponse->rDate.wDay,
  371. pResponse->rDate.wYear, pResponse->rDate.wHour,
  372. pResponse->rDate.wMinute, pResponse->rDate.wSecond);
  373. }
  374. g_pNNTP->ReleaseResponse(pResponse);
  375. PostThreadMessage(GetCurrentThreadId(), g_msgNNTP, pResponse->state, 0);
  376. break;
  377. case NS_MODE:
  378. printf("\nNS_MODE\n\n");
  379. g_pNNTP->ReleaseResponse(pResponse);
  380. PostThreadMessage(GetCurrentThreadId(), g_msgNNTP, pResponse->state, 0);
  381. break;
  382. case NS_NEWGROUPS:
  383. if (pResponse->rIxpResult.uiServerError != IXP_NNTP_NEWNEWSGROUPS_FOLLOWS)
  384. PostThreadMessage(GetCurrentThreadId(), g_msgNNTP, 0, 0);
  385. else
  386. {
  387. if (SUCCEEDED(pResponse->rIxpResult.hrResult))
  388. {
  389. for (UINT i = 0; i < pResponse->rNewgroups.cLines; i++)
  390. printf("%s\n", pResponse->rNewgroups.rgszLines[i]);
  391. }
  392. g_pNNTP->ReleaseResponse(pResponse);
  393. if (pResponse->fDone)
  394. PostThreadMessage(GetCurrentThreadId(), g_msgNNTP, 0, 0);
  395. }
  396. break;
  397. case NS_ARTICLE:
  398. if (pResponse->rIxpResult.uiServerError != IXP_NNTP_ARTICLE_FOLLOWS)
  399. PostThreadMessage(GetCurrentThreadId(), g_msgNNTP, 0, 0);
  400. else
  401. {
  402. if (SUCCEEDED(pResponse->rIxpResult.hrResult))
  403. printf("%s", pResponse->rArticle.pszLines);
  404. g_pNNTP->ReleaseResponse(pResponse);
  405. if (pResponse->fDone)
  406. PostThreadMessage(GetCurrentThreadId(), g_msgNNTP, 0, 0);
  407. }
  408. break;
  409. case NS_HEAD:
  410. if (pResponse->rIxpResult.uiServerError != IXP_NNTP_HEAD_FOLLOWS)
  411. PostThreadMessage(GetCurrentThreadId(), g_msgNNTP, 0, 0);
  412. else
  413. {
  414. if (SUCCEEDED(pResponse->rIxpResult.hrResult))
  415. printf("%s", pResponse->rArticle.pszLines);
  416. g_pNNTP->ReleaseResponse(pResponse);
  417. if (pResponse->fDone)
  418. PostThreadMessage(GetCurrentThreadId(), g_msgNNTP, 0, 0);
  419. }
  420. break;
  421. case NS_BODY:
  422. if (pResponse->rIxpResult.uiServerError != IXP_NNTP_BODY_FOLLOWS)
  423. PostThreadMessage(GetCurrentThreadId(), g_msgNNTP, 0, 0);
  424. else
  425. {
  426. if (SUCCEEDED(pResponse->rIxpResult.hrResult))
  427. {
  428. printf("%s", pResponse->rArticle.pszLines);
  429. }
  430. g_pNNTP->ReleaseResponse(pResponse);
  431. if (pResponse->fDone)
  432. PostThreadMessage(GetCurrentThreadId(), g_msgNNTP, 0, 0);
  433. }
  434. break;
  435. case NS_IDLE:
  436. printf("NS_IDLE\n");
  437. printf("Why would we ever be here?");
  438. g_pNNTP->ReleaseResponse(pResponse);
  439. PostThreadMessage(GetCurrentThreadId(), g_msgNNTP, 0, 0);
  440. break;
  441. case NS_HEADERS:
  442. if ((pResponse->rIxpResult.uiServerError / 100) != 2)
  443. PostThreadMessage(GetCurrentThreadId(), g_msgNNTP, 0, 0);
  444. else
  445. {
  446. if (SUCCEEDED(pResponse->rIxpResult.hrResult))
  447. {
  448. for (UINT i = 0; i < pResponse->rHeaders.cHeaders; i++)
  449. {
  450. printf("%d\n", pResponse->rHeaders.rgHeaders[i].dwArticleNum);
  451. printf("%s\n", pResponse->rHeaders.rgHeaders[i].pszSubject);
  452. printf("%s\n", pResponse->rHeaders.rgHeaders[i].pszFrom);
  453. printf("%s\n", pResponse->rHeaders.rgHeaders[i].pszDate);
  454. printf("%s\n", pResponse->rHeaders.rgHeaders[i].pszMessageId);
  455. printf("%s\n", pResponse->rHeaders.rgHeaders[i].pszReferences);
  456. printf("%d\n", pResponse->rHeaders.rgHeaders[i].dwBytes);
  457. printf("%d\n", pResponse->rHeaders.rgHeaders[i].dwLines);
  458. if (pResponse->rHeaders.rgHeaders[i].pszXref)
  459. printf("%s\n", pResponse->rHeaders.rgHeaders[i].pszXref);
  460. printf("\n\n");
  461. }
  462. }
  463. g_pNNTP->ReleaseResponse(pResponse);
  464. if (pResponse->fDone)
  465. PostThreadMessage(GetCurrentThreadId(), g_msgNNTP, 0, 0);
  466. }
  467. break;
  468. case NS_XHDR:
  469. if ((pResponse->rIxpResult.uiServerError / 100) != 2)
  470. PostThreadMessage(GetCurrentThreadId(), g_msgNNTP, 0, 0);
  471. else
  472. {
  473. if (SUCCEEDED(pResponse->rIxpResult.hrResult))
  474. {
  475. for (UINT i = 0; i < pResponse->rXhdr.cHeaders; i++)
  476. {
  477. printf("%6d %s\n", pResponse->rXhdr.rgHeaders[i].dwArticleNum,
  478. pResponse->rXhdr.rgHeaders[i].pszHeader);
  479. }
  480. }
  481. g_pNNTP->ReleaseResponse(pResponse);
  482. if (pResponse->fDone)
  483. PostThreadMessage(GetCurrentThreadId(), g_msgNNTP, 0, 0);
  484. }
  485. break;
  486. case NS_POST:
  487. printf("%s\n", pResponse->rIxpResult.pszResponse);
  488. g_pNNTP->ReleaseResponse(pResponse);
  489. PostThreadMessage(GetCurrentThreadId(), g_msgNNTP, 0, 0);
  490. break;
  491. case NS_QUIT:
  492. PostThreadMessage(GetCurrentThreadId(), g_msgNNTP,0, 0);
  493. break;
  494. }
  495. return S_OK;
  496. }