winepulse.drv: add a pulseaudio driver
[wine/hacks.git] / dlls / inetcomm / pop3transport.c
blobb0e31df6449999e9c9028302b5cc732fd932a0c4
1 /*
2 * POP3 Transport
4 * Copyright 2008 Hans Leidekker for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #define COBJMACROS
22 #define NONAMELESSUNION
24 #include <stdarg.h>
25 #include <stdio.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winnt.h"
30 #include "winuser.h"
31 #include "objbase.h"
32 #include "mimeole.h"
33 #include "wine/debug.h"
35 #include "inetcomm_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(inetcomm);
39 enum parse_state
41 STATE_NONE,
42 STATE_OK,
43 STATE_MULTILINE,
44 STATE_DONE
47 typedef struct
49 InternetTransport InetTransport;
50 ULONG refs;
51 POP3COMMAND command;
52 POP3CMDTYPE type;
53 char *response;
54 char *ptr;
55 enum parse_state state;
56 BOOL valid_info;
57 DWORD msgid;
58 DWORD preview_lines;
59 } POP3Transport;
61 static HRESULT parse_response(POP3Transport *This)
63 switch (This->state)
65 case STATE_NONE:
67 if (strlen(This->response) < 3)
69 WARN("parse error\n");
70 This->state = STATE_DONE;
71 return S_FALSE;
73 if (!memcmp(This->response, "+OK", 3))
75 This->ptr = This->response + 3;
76 This->state = STATE_OK;
77 return S_OK;
79 This->state = STATE_DONE;
80 return S_FALSE;
82 default: return S_OK;
86 static HRESULT parse_uidl_response(POP3Transport *This, POP3UIDL *uidl)
88 char *p;
90 uidl->dwPopId = 0;
91 uidl->pszUidl = NULL;
92 switch (This->state)
94 case STATE_OK:
96 if (This->type == POP3CMD_GET_POPID)
98 if ((p = strchr(This->ptr, ' ')))
100 while (*p == ' ') p++;
101 sscanf(p, "%u", &uidl->dwPopId);
102 if ((p = strchr(p, ' ')))
104 while (*p == ' ') p++;
105 uidl->pszUidl = p;
106 This->valid_info = TRUE;
109 This->state = STATE_DONE;
110 return S_OK;
112 This->state = STATE_MULTILINE;
113 return S_OK;
115 case STATE_MULTILINE:
117 if (This->response[0] == '.' && !This->response[1])
119 This->valid_info = FALSE;
120 This->state = STATE_DONE;
121 return S_OK;
123 sscanf(This->response, "%u", &uidl->dwPopId);
124 if ((p = strchr(This->response, ' ')))
126 while (*p == ' ') p++;
127 uidl->pszUidl = p;
128 This->valid_info = TRUE;
129 return S_OK;
132 default:
134 WARN("parse error\n");
135 This->state = STATE_DONE;
136 return S_FALSE;
141 static HRESULT parse_stat_response(POP3Transport *This, POP3STAT *stat)
143 char *p;
145 stat->cMessages = 0;
146 stat->cbMessages = 0;
147 switch (This->state)
149 case STATE_OK:
151 if ((p = strchr(This->ptr, ' ')))
153 while (*p == ' ') p++;
154 sscanf(p, "%u %u", &stat->cMessages, &stat->cbMessages);
155 This->valid_info = TRUE;
156 This->state = STATE_DONE;
157 return S_OK;
160 default:
162 WARN("parse error\n");
163 This->state = STATE_DONE;
164 return S_FALSE;
169 static HRESULT parse_list_response(POP3Transport *This, POP3LIST *list)
171 char *p;
173 list->dwPopId = 0;
174 list->cbSize = 0;
175 switch (This->state)
177 case STATE_OK:
179 if (This->type == POP3CMD_GET_POPID)
181 if ((p = strchr(This->ptr, ' ')))
183 while (*p == ' ') p++;
184 sscanf(p, "%u %u", &list->dwPopId, &list->cbSize);
185 This->valid_info = TRUE;
187 This->state = STATE_DONE;
188 return S_OK;
190 This->state = STATE_MULTILINE;
191 return S_OK;
193 case STATE_MULTILINE:
195 if (This->response[0] == '.' && !This->response[1])
197 This->valid_info = FALSE;
198 This->state = STATE_DONE;
199 return S_OK;
201 sscanf(This->response, "%u", &list->dwPopId);
202 if ((p = strchr(This->response, ' ')))
204 while (*p == ' ') p++;
205 sscanf(p, "%u", &list->cbSize);
206 This->valid_info = TRUE;
207 return S_OK;
210 default:
212 WARN("parse error\n");
213 This->state = STATE_DONE;
214 return S_FALSE;
219 static HRESULT parse_dele_response(POP3Transport *This, DWORD *dwPopId)
221 switch (This->state)
223 case STATE_OK:
225 *dwPopId = 0; /* FIXME */
226 This->state = STATE_DONE;
227 return S_OK;
229 default:
231 WARN("parse error\n");
232 This->state = STATE_DONE;
233 return S_FALSE;
238 static HRESULT parse_retr_response(POP3Transport *This, POP3RETR *retr)
240 switch (This->state)
242 case STATE_OK:
244 retr->fHeader = FALSE;
245 retr->fBody = FALSE;
246 retr->dwPopId = This->msgid;
247 retr->cbSoFar = 0;
248 retr->pszLines = This->response;
249 retr->cbLines = 0;
251 This->state = STATE_MULTILINE;
252 This->valid_info = FALSE;
253 return S_OK;
255 case STATE_MULTILINE:
257 int len;
259 if (This->response[0] == '.' && !This->response[1])
261 retr->cbLines = retr->cbSoFar;
262 This->state = STATE_DONE;
263 return S_OK;
265 retr->fHeader = TRUE;
266 if (!This->response[0]) retr->fBody = TRUE;
268 len = strlen(This->response);
269 retr->cbSoFar += len;
270 retr->pszLines = This->response;
271 retr->cbLines = len;
273 This->valid_info = TRUE;
274 return S_OK;
276 default:
278 WARN("parse error\n");
279 This->state = STATE_DONE;
280 return S_FALSE;
285 static HRESULT parse_top_response(POP3Transport *This, POP3TOP *top)
287 switch (This->state)
289 case STATE_OK:
291 top->fHeader = FALSE;
292 top->fBody = FALSE;
293 top->dwPopId = This->msgid;
294 top->cPreviewLines = This->preview_lines;
295 top->cbSoFar = 0;
296 top->pszLines = This->response;
297 top->cbLines = 0;
299 This->state = STATE_MULTILINE;
300 This->valid_info = FALSE;
301 return S_OK;
303 case STATE_MULTILINE:
305 int len;
307 if (This->response[0] == '.' && !This->response[1])
309 top->cbLines = top->cbSoFar;
310 This->state = STATE_DONE;
311 return S_OK;
313 top->fHeader = TRUE;
314 if (!This->response[0]) top->fBody = TRUE;
316 len = strlen(This->response);
317 top->cbSoFar += len;
318 top->pszLines = This->response;
319 top->cbLines = len;
321 This->valid_info = TRUE;
322 return S_OK;
324 default:
326 WARN("parse error\n");
327 This->state = STATE_DONE;
328 return S_FALSE;
333 static void init_parser(POP3Transport *This, POP3COMMAND command, POP3CMDTYPE type)
335 This->state = STATE_NONE;
336 This->command = command;
337 This->type = type;
340 static HRESULT POP3Transport_ParseResponse(POP3Transport *This, char *pszResponse, POP3RESPONSE *pResponse)
342 HRESULT hr;
344 TRACE("response: %s\n", debugstr_a(pszResponse));
346 This->response = pszResponse;
347 This->valid_info = FALSE;
348 TRACE("state %u\n", This->state);
350 if (SUCCEEDED((hr = parse_response(This))))
352 switch (This->command)
354 case POP3_UIDL: hr = parse_uidl_response(This, &pResponse->u.rUidlInfo); break;
355 case POP3_STAT: hr = parse_stat_response(This, &pResponse->u.rStatInfo); break;
356 case POP3_LIST: hr = parse_list_response(This, &pResponse->u.rListInfo); break;
357 case POP3_DELE: hr = parse_dele_response(This, &pResponse->u.dwPopId); break;
358 case POP3_RETR: hr = parse_retr_response(This, &pResponse->u.rRetrInfo); break;
359 case POP3_TOP: hr = parse_top_response(This, &pResponse->u.rTopInfo); break;
360 default:
361 This->state = STATE_DONE;
362 break;
365 pResponse->command = This->command;
366 pResponse->fDone = (This->state == STATE_DONE);
367 pResponse->fValidInfo = This->valid_info;
368 pResponse->rIxpResult.hrResult = hr;
369 pResponse->rIxpResult.pszResponse = pszResponse;
370 pResponse->rIxpResult.uiServerError = 0;
371 pResponse->rIxpResult.hrServerError = pResponse->rIxpResult.hrResult;
372 pResponse->rIxpResult.dwSocketError = WSAGetLastError();
373 pResponse->rIxpResult.pszProblem = NULL;
374 pResponse->pTransport = (IPOP3Transport *)&This->InetTransport.u.vtblPOP3;
376 if (This->InetTransport.pCallback && This->InetTransport.fCommandLogging)
378 ITransportCallback_OnCommand(This->InetTransport.pCallback, CMD_RESP,
379 pResponse->rIxpResult.pszResponse, pResponse->rIxpResult.hrServerError,
380 (IInternetTransport *)&This->InetTransport.u.vtbl);
382 return S_OK;
385 static void POP3Transport_CallbackProcessDELEResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
387 POP3Transport *This = (POP3Transport *)iface;
388 POP3RESPONSE response;
389 HRESULT hr;
391 TRACE("\n");
393 hr = POP3Transport_ParseResponse(This, pBuffer, &response);
394 if (FAILED(hr))
396 /* FIXME: handle error */
397 return;
400 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
403 static void POP3Transport_CallbackRecvDELEResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
405 POP3Transport *This = (POP3Transport *)iface;
407 TRACE("\n");
408 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessDELEResp);
411 static void POP3Transport_CallbackProcessNOOPResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
413 POP3Transport *This = (POP3Transport *)iface;
414 POP3RESPONSE response;
415 HRESULT hr;
417 TRACE("\n");
419 hr = POP3Transport_ParseResponse(This, pBuffer, &response);
420 if (FAILED(hr))
422 /* FIXME: handle error */
423 return;
426 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
429 static void POP3Transport_CallbackRecvNOOPResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
431 POP3Transport *This = (POP3Transport *)iface;
433 TRACE("\n");
434 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessNOOPResp);
437 static void POP3Transport_CallbackProcessRSETResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
439 POP3Transport *This = (POP3Transport *)iface;
440 POP3RESPONSE response;
441 HRESULT hr;
443 TRACE("\n");
445 hr = POP3Transport_ParseResponse(This, pBuffer, &response);
446 if (FAILED(hr))
448 /* FIXME: handle error */
449 return;
452 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
455 static void POP3Transport_CallbackRecvRSETResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
457 POP3Transport *This = (POP3Transport *)iface;
459 TRACE("\n");
460 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessRSETResp);
463 static void POP3Transport_CallbackProcessRETRResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
465 POP3Transport *This = (POP3Transport *)iface;
466 POP3RESPONSE response;
467 HRESULT hr;
469 TRACE("\n");
471 hr = POP3Transport_ParseResponse(This, pBuffer, &response);
472 if (FAILED(hr))
474 /* FIXME: handle error */
475 return;
478 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
480 if (!response.fDone)
482 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessRETRResp);
483 return;
486 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
489 static void POP3Transport_CallbackRecvRETRResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
491 POP3Transport *This = (POP3Transport *)iface;
493 TRACE("\n");
494 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessRETRResp);
497 static void POP3Transport_CallbackProcessTOPResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
499 POP3Transport *This = (POP3Transport *)iface;
500 POP3RESPONSE response;
501 HRESULT hr;
503 TRACE("\n");
505 hr = POP3Transport_ParseResponse(This, pBuffer, &response);
506 if (FAILED(hr))
508 /* FIXME: handle error */
509 return;
512 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
514 if (!response.fDone)
516 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessTOPResp);
517 return;
520 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
523 static void POP3Transport_CallbackRecvTOPResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
525 POP3Transport *This = (POP3Transport *)iface;
527 TRACE("\n");
528 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessTOPResp);
531 static void POP3Transport_CallbackProcessLISTResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
533 POP3Transport *This = (POP3Transport *)iface;
534 POP3RESPONSE response;
535 HRESULT hr;
537 TRACE("\n");
539 hr = POP3Transport_ParseResponse(This, pBuffer, &response);
540 if (FAILED(hr))
542 /* FIXME: handle error */
543 return;
546 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
548 if (!response.fDone)
550 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessLISTResp);
551 return;
554 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
557 static void POP3Transport_CallbackRecvLISTResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
559 POP3Transport *This = (POP3Transport *)iface;
561 TRACE("\n");
562 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessLISTResp);
565 static void POP3Transport_CallbackProcessUIDLResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
567 POP3Transport *This = (POP3Transport *)iface;
568 POP3RESPONSE response;
569 HRESULT hr;
571 TRACE("\n");
573 hr = POP3Transport_ParseResponse(This, pBuffer, &response);
574 if (FAILED(hr))
576 /* FIXME: handle error */
577 return;
580 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
582 if (!response.fDone)
584 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessUIDLResp);
585 return;
588 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
591 static void POP3Transport_CallbackRecvUIDLResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
593 POP3Transport *This = (POP3Transport *)iface;
595 TRACE("\n");
596 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessUIDLResp);
599 static void POP3Transport_CallbackProcessSTATResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
601 POP3Transport *This = (POP3Transport *)iface;
602 POP3RESPONSE response;
603 HRESULT hr;
605 TRACE("\n");
607 hr = POP3Transport_ParseResponse(This, pBuffer, &response);
608 if (FAILED(hr))
610 /* FIXME: handle error */
611 return;
614 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
617 static void POP3Transport_CallbackRecvSTATResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
619 POP3Transport *This = (POP3Transport *)iface;
621 TRACE("\n");
622 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessSTATResp);
625 static void POP3Transport_CallbackProcessPASSResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
627 POP3Transport *This = (POP3Transport *)iface;
628 POP3RESPONSE response;
629 HRESULT hr;
631 TRACE("\n");
633 hr = POP3Transport_ParseResponse(This, pBuffer, &response);
634 if (FAILED(hr))
636 /* FIXME: handle error */
637 return;
640 InternetTransport_ChangeStatus(&This->InetTransport, IXP_AUTHORIZED);
641 InternetTransport_ChangeStatus(&This->InetTransport, IXP_CONNECTED);
643 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
646 static void POP3Transport_CallbackRecvPASSResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
648 POP3Transport *This = (POP3Transport *)iface;
650 TRACE("\n");
651 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessPASSResp);
654 static void POP3Transport_CallbackProcessUSERResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
656 static char pass[] = "PASS ";
657 POP3Transport *This = (POP3Transport *)iface;
658 POP3RESPONSE response;
659 char *command;
660 int len;
661 HRESULT hr;
663 TRACE("\n");
665 hr = POP3Transport_ParseResponse(This, pBuffer, &response);
666 if (FAILED(hr))
668 /* FIXME: handle error */
669 return;
672 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
674 len = sizeof(pass) + strlen(This->InetTransport.ServerInfo.szPassword) + 2; /* "\r\n" */
675 command = HeapAlloc(GetProcessHeap(), 0, len);
677 strcpy(command, pass);
678 strcat(command, This->InetTransport.ServerInfo.szPassword);
679 strcat(command, "\r\n");
681 init_parser(This, POP3_PASS, POP3_NONE);
683 InternetTransport_DoCommand(&This->InetTransport, command, POP3Transport_CallbackRecvPASSResp);
684 HeapFree(GetProcessHeap(), 0, command);
687 static void POP3Transport_CallbackRecvUSERResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
689 POP3Transport *This = (POP3Transport *)iface;
691 TRACE("\n");
692 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessUSERResp);
695 static void POP3Transport_CallbackSendUSERCmd(IInternetTransport *iface, char *pBuffer, int cbBuffer)
697 static char user[] = "USER ";
698 POP3Transport *This = (POP3Transport *)iface;
699 char *command;
700 int len;
702 TRACE("\n");
704 len = sizeof(user) + strlen(This->InetTransport.ServerInfo.szUserName) + 2; /* "\r\n" */
705 command = HeapAlloc(GetProcessHeap(), 0, len);
707 strcpy(command, user);
708 strcat(command, This->InetTransport.ServerInfo.szUserName);
709 strcat(command, "\r\n");
710 InternetTransport_DoCommand(&This->InetTransport, command, POP3Transport_CallbackRecvUSERResp);
712 HeapFree(GetProcessHeap(), 0, command);
715 static void POP3Transport_CallbackProcessQUITResponse(IInternetTransport *iface, char *pBuffer, int cbBuffer)
717 POP3Transport *This = (POP3Transport *)iface;
718 POP3RESPONSE response;
719 HRESULT hr;
721 TRACE("%s\n", debugstr_an(pBuffer, cbBuffer));
723 hr = POP3Transport_ParseResponse(This, pBuffer, &response);
724 if (FAILED(hr))
726 /* FIXME: handle error */
727 return;
730 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
731 InternetTransport_DropConnection(&This->InetTransport);
734 static void POP3Transport_CallbackRecvQUITResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
736 POP3Transport *This = (POP3Transport *)iface;
738 TRACE("\n");
739 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessQUITResponse);
742 static HRESULT WINAPI POP3Transport_QueryInterface(IPOP3Transport *iface, REFIID riid, void **ppv)
744 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
746 if (IsEqualIID(riid, &IID_IUnknown) ||
747 IsEqualIID(riid, &IID_IInternetTransport) ||
748 IsEqualIID(riid, &IID_IPOP3Transport))
750 *ppv = iface;
751 IUnknown_AddRef(iface);
752 return S_OK;
754 *ppv = NULL;
755 FIXME("no interface for %s\n", debugstr_guid(riid));
756 return E_NOINTERFACE;
759 static ULONG WINAPI POP3Transport_AddRef(IPOP3Transport *iface)
761 POP3Transport *This = (POP3Transport *)iface;
762 return InterlockedIncrement((LONG *)&This->refs);
765 static ULONG WINAPI POP3Transport_Release(IPOP3Transport *iface)
767 POP3Transport *This = (POP3Transport *)iface;
768 ULONG refs = InterlockedDecrement((LONG *)&This->refs);
769 if (!refs)
771 TRACE("destroying %p\n", This);
772 if (This->InetTransport.Status != IXP_DISCONNECTED)
773 InternetTransport_DropConnection(&This->InetTransport);
774 if (This->InetTransport.pCallback) ITransportCallback_Release(This->InetTransport.pCallback);
775 HeapFree(GetProcessHeap(), 0, This);
777 return refs;
780 static HRESULT WINAPI POP3Transport_GetServerInfo(IPOP3Transport *iface,
781 LPINETSERVER pInetServer)
783 POP3Transport *This = (POP3Transport *)iface;
785 TRACE("(%p)\n", pInetServer);
786 return InternetTransport_GetServerInfo(&This->InetTransport, pInetServer);
789 static IXPTYPE WINAPI POP3Transport_GetIXPType(IPOP3Transport *iface)
791 TRACE("()\n");
792 return IXP_POP3;
795 static HRESULT WINAPI POP3Transport_IsState(IPOP3Transport *iface, IXPISSTATE isstate)
797 FIXME("(%u)\n", isstate);
798 return E_NOTIMPL;
801 static HRESULT WINAPI POP3Transport_InetServerFromAccount(
802 IPOP3Transport *iface, IImnAccount *pAccount, LPINETSERVER pInetServer)
804 POP3Transport *This = (POP3Transport *)iface;
806 TRACE("(%p, %p)\n", pAccount, pInetServer);
807 return InternetTransport_InetServerFromAccount(&This->InetTransport, pAccount, pInetServer);
810 static HRESULT WINAPI POP3Transport_Connect(IPOP3Transport *iface,
811 LPINETSERVER pInetServer, boolean fAuthenticate, boolean fCommandLogging)
813 POP3Transport *This = (POP3Transport *)iface;
814 HRESULT hr;
816 TRACE("(%p, %s, %s)\n", pInetServer, fAuthenticate ? "TRUE" : "FALSE", fCommandLogging ? "TRUE" : "FALSE");
818 hr = InternetTransport_Connect(&This->InetTransport, pInetServer, fAuthenticate, fCommandLogging);
819 if (FAILED(hr))
820 return hr;
822 init_parser(This, POP3_USER, POP3_NONE);
823 return InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackSendUSERCmd);
826 static HRESULT WINAPI POP3Transport_HandsOffCallback(IPOP3Transport *iface)
828 POP3Transport *This = (POP3Transport *)iface;
830 TRACE("()\n");
831 return InternetTransport_HandsOffCallback(&This->InetTransport);
834 static HRESULT WINAPI POP3Transport_Disconnect(IPOP3Transport *iface)
836 TRACE("()\n");
837 return IPOP3Transport_CommandQUIT(iface);
840 static HRESULT WINAPI POP3Transport_DropConnection(IPOP3Transport *iface)
842 POP3Transport *This = (POP3Transport *)iface;
844 TRACE("()\n");
845 return InternetTransport_DropConnection(&This->InetTransport);
848 static HRESULT WINAPI POP3Transport_GetStatus(IPOP3Transport *iface,
849 IXPSTATUS *pCurrentStatus)
851 POP3Transport *This = (POP3Transport *)iface;
853 TRACE("()\n");
854 return InternetTransport_GetStatus(&This->InetTransport, pCurrentStatus);
857 static HRESULT WINAPI POP3Transport_InitNew(IPOP3Transport *iface,
858 LPSTR pszLogFilePath, IPOP3Callback *pCallback)
860 POP3Transport *This = (POP3Transport *)iface;
862 TRACE("(%s, %p)\n", debugstr_a(pszLogFilePath), pCallback);
864 if (!pCallback)
865 return E_INVALIDARG;
867 if (pszLogFilePath)
868 FIXME("not using log file of %s, use Wine debug logging instead\n",
869 debugstr_a(pszLogFilePath));
871 IPOP3Callback_AddRef(pCallback);
872 This->InetTransport.pCallback = (ITransportCallback *)pCallback;
873 This->InetTransport.fInitialised = TRUE;
875 return S_OK;
878 static HRESULT WINAPI POP3Transport_MarkItem(IPOP3Transport *iface, POP3MARKTYPE marktype,
879 DWORD dwPopId, boolean fMarked)
881 FIXME("(%u, %u, %d)\n", marktype, dwPopId, fMarked);
882 return E_NOTIMPL;
885 static HRESULT WINAPI POP3Transport_CommandAUTH(IPOP3Transport *iface, LPSTR pszAuthType)
887 FIXME("(%s)\n", pszAuthType);
888 return E_NOTIMPL;
891 static HRESULT WINAPI POP3Transport_CommandUSER(IPOP3Transport *iface, LPSTR username)
893 static char user[] = "USER ";
894 POP3Transport *This = (POP3Transport *)iface;
895 char *command;
896 int len;
898 TRACE("(%s)\n", username);
900 len = sizeof(user) + strlen(username) + 2; /* "\r\n" */
901 if (!(command = HeapAlloc(GetProcessHeap(), 0, len))) return S_FALSE;
903 strcpy(command, user);
904 strcat(command, username);
905 strcat(command, "\r\n");
907 init_parser(This, POP3_USER, POP3_NONE);
908 InternetTransport_DoCommand(&This->InetTransport, command, POP3Transport_CallbackRecvUSERResp);
910 HeapFree(GetProcessHeap(), 0, command);
911 return S_OK;
914 static HRESULT WINAPI POP3Transport_CommandPASS(IPOP3Transport *iface, LPSTR password)
916 static char pass[] = "PASS ";
917 POP3Transport *This = (POP3Transport *)iface;
918 char *command;
919 int len;
921 TRACE("(%p)\n", password);
923 len = sizeof(pass) + strlen(password) + 2; /* "\r\n" */
924 if (!(command = HeapAlloc(GetProcessHeap(), 0, len))) return S_FALSE;
926 strcpy(command, pass);
927 strcat(command, password);
928 strcat(command, "\r\n");
930 init_parser(This, POP3_PASS, POP3_NONE);
931 InternetTransport_DoCommand(&This->InetTransport, command, POP3Transport_CallbackRecvPASSResp);
933 HeapFree(GetProcessHeap(), 0, command);
934 return S_OK;
937 static HRESULT WINAPI POP3Transport_CommandLIST(
938 IPOP3Transport *iface, POP3CMDTYPE cmdtype, DWORD dwPopId)
940 static char list[] = "LIST %u\r\n";
941 static char list_all[] = "LIST\r\n";
942 POP3Transport *This = (POP3Transport *)iface;
943 char *command;
944 int len;
946 TRACE("(%u, %u)\n", cmdtype, dwPopId);
948 if (dwPopId)
950 len = sizeof(list) + 10 + 2; /* "4294967296" + "\r\n" */
951 if (!(command = HeapAlloc(GetProcessHeap(), 0, len))) return S_FALSE;
952 sprintf(command, list, dwPopId);
954 else command = list_all;
956 init_parser(This, POP3_LIST, cmdtype);
957 InternetTransport_DoCommand(&This->InetTransport, command, POP3Transport_CallbackRecvLISTResp);
959 if (dwPopId) HeapFree(GetProcessHeap(), 0, command);
960 return S_OK;
963 static HRESULT WINAPI POP3Transport_CommandTOP(
964 IPOP3Transport *iface, POP3CMDTYPE cmdtype, DWORD dwPopId, DWORD cPreviewLines)
966 static char top[] = "TOP %u %u\r\n";
967 POP3Transport *This = (POP3Transport *)iface;
968 char *command;
969 int len;
971 TRACE("(%u, %u, %u)\n", cmdtype, dwPopId, cPreviewLines);
973 len = sizeof(top) + 20 + 2; /* 2 * "4294967296" + "\r\n" */
974 if (!(command = HeapAlloc(GetProcessHeap(), 0, len))) return S_FALSE;
975 sprintf(command, top, dwPopId, cPreviewLines);
977 This->preview_lines = cPreviewLines;
978 init_parser(This, POP3_TOP, cmdtype);
979 InternetTransport_DoCommand(&This->InetTransport, command, POP3Transport_CallbackRecvTOPResp);
981 HeapFree(GetProcessHeap(), 0, command);
982 return S_OK;
985 static HRESULT WINAPI POP3Transport_CommandQUIT(IPOP3Transport *iface)
987 static char command[] = "QUIT\r\n";
988 POP3Transport *This = (POP3Transport *)iface;
990 TRACE("()\n");
992 InternetTransport_ChangeStatus(&This->InetTransport, IXP_DISCONNECTING);
994 init_parser(This, POP3_QUIT, POP3_NONE);
995 return InternetTransport_DoCommand(&This->InetTransport, command, POP3Transport_CallbackRecvQUITResp);
998 static HRESULT WINAPI POP3Transport_CommandSTAT(IPOP3Transport *iface)
1000 static char stat[] = "STAT\r\n";
1001 POP3Transport *This = (POP3Transport *)iface;
1003 TRACE("\n");
1005 init_parser(This, POP3_STAT, POP3_NONE);
1006 InternetTransport_DoCommand(&This->InetTransport, stat, POP3Transport_CallbackRecvSTATResp);
1007 return S_OK;
1010 static HRESULT WINAPI POP3Transport_CommandNOOP(IPOP3Transport *iface)
1012 static char noop[] = "NOOP\r\n";
1013 POP3Transport *This = (POP3Transport *)iface;
1015 TRACE("\n");
1017 init_parser(This, POP3_NOOP, POP3_NONE);
1018 InternetTransport_DoCommand(&This->InetTransport, noop, POP3Transport_CallbackRecvNOOPResp);
1019 return S_OK;
1022 static HRESULT WINAPI POP3Transport_CommandRSET(IPOP3Transport *iface)
1024 static char rset[] = "RSET\r\n";
1025 POP3Transport *This = (POP3Transport *)iface;
1027 TRACE("\n");
1029 init_parser(This, POP3_RSET, POP3_NONE);
1030 InternetTransport_DoCommand(&This->InetTransport, rset, POP3Transport_CallbackRecvRSETResp);
1031 return S_OK;
1034 static HRESULT WINAPI POP3Transport_CommandUIDL(
1035 IPOP3Transport *iface, POP3CMDTYPE cmdtype, DWORD dwPopId)
1037 static char uidl[] = "UIDL %u\r\n";
1038 static char uidl_all[] = "UIDL\r\n";
1039 POP3Transport *This = (POP3Transport *)iface;
1040 char *command;
1041 int len;
1043 TRACE("(%u, %u)\n", cmdtype, dwPopId);
1045 if (dwPopId)
1047 len = sizeof(uidl) + 10 + 2; /* "4294967296" + "\r\n" */
1048 if (!(command = HeapAlloc(GetProcessHeap(), 0, len))) return S_FALSE;
1049 sprintf(command, uidl, dwPopId);
1051 else command = uidl_all;
1053 init_parser(This, POP3_UIDL, cmdtype);
1054 InternetTransport_DoCommand(&This->InetTransport, command, POP3Transport_CallbackRecvUIDLResp);
1056 if (dwPopId) HeapFree(GetProcessHeap(), 0, command);
1057 return S_OK;
1060 static HRESULT WINAPI POP3Transport_CommandDELE(
1061 IPOP3Transport *iface, POP3CMDTYPE cmdtype, DWORD dwPopId)
1063 static char dele[] = "DELE %u\r\n";
1064 POP3Transport *This = (POP3Transport *)iface;
1065 char *command;
1066 int len;
1068 TRACE("(%u, %u)\n", cmdtype, dwPopId);
1070 len = sizeof(dele) + 10 + 2; /* "4294967296" + "\r\n" */
1071 if (!(command = HeapAlloc(GetProcessHeap(), 0, len))) return S_FALSE;
1072 sprintf(command, dele, dwPopId);
1074 init_parser(This, POP3_DELE, cmdtype);
1075 InternetTransport_DoCommand(&This->InetTransport, command, POP3Transport_CallbackRecvDELEResp);
1077 HeapFree(GetProcessHeap(), 0, command);
1078 return S_OK;
1081 static HRESULT WINAPI POP3Transport_CommandRETR(
1082 IPOP3Transport *iface, POP3CMDTYPE cmdtype, DWORD dwPopId)
1084 static char retr[] = "RETR %u\r\n";
1085 POP3Transport *This = (POP3Transport *)iface;
1086 char *command;
1087 int len;
1089 TRACE("(%u, %u)\n", cmdtype, dwPopId);
1091 len = sizeof(retr) + 10 + 2; /* "4294967296" + "\r\n" */
1092 if (!(command = HeapAlloc(GetProcessHeap(), 0, len))) return S_FALSE;
1093 sprintf(command, retr, dwPopId);
1095 init_parser(This, POP3_RETR, cmdtype);
1096 InternetTransport_DoCommand(&This->InetTransport, command, POP3Transport_CallbackRecvRETRResp);
1098 HeapFree(GetProcessHeap(), 0, command);
1099 return S_OK;
1102 static const IPOP3TransportVtbl POP3TransportVtbl =
1104 POP3Transport_QueryInterface,
1105 POP3Transport_AddRef,
1106 POP3Transport_Release,
1107 POP3Transport_GetServerInfo,
1108 POP3Transport_GetIXPType,
1109 POP3Transport_IsState,
1110 POP3Transport_InetServerFromAccount,
1111 POP3Transport_Connect,
1112 POP3Transport_HandsOffCallback,
1113 POP3Transport_Disconnect,
1114 POP3Transport_DropConnection,
1115 POP3Transport_GetStatus,
1116 POP3Transport_InitNew,
1117 POP3Transport_MarkItem,
1118 POP3Transport_CommandAUTH,
1119 POP3Transport_CommandUSER,
1120 POP3Transport_CommandPASS,
1121 POP3Transport_CommandLIST,
1122 POP3Transport_CommandTOP,
1123 POP3Transport_CommandQUIT,
1124 POP3Transport_CommandSTAT,
1125 POP3Transport_CommandNOOP,
1126 POP3Transport_CommandRSET,
1127 POP3Transport_CommandUIDL,
1128 POP3Transport_CommandDELE,
1129 POP3Transport_CommandRETR
1132 HRESULT WINAPI CreatePOP3Transport(IPOP3Transport **ppTransport)
1134 HRESULT hr;
1135 POP3Transport *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1136 if (!This)
1137 return E_OUTOFMEMORY;
1139 This->InetTransport.u.vtblPOP3 = &POP3TransportVtbl;
1140 This->refs = 0;
1141 hr = InternetTransport_Init(&This->InetTransport);
1142 if (FAILED(hr))
1144 HeapFree(GetProcessHeap(), 0, This);
1145 return hr;
1148 *ppTransport = (IPOP3Transport *)&This->InetTransport.u.vtblPOP3;
1149 IPOP3Transport_AddRef(*ppTransport);
1151 return S_OK;
1154 static HRESULT WINAPI POP3TransportCF_QueryInterface(LPCLASSFACTORY iface,
1155 REFIID riid, LPVOID *ppv)
1157 *ppv = NULL;
1158 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
1160 *ppv = iface;
1161 IUnknown_AddRef(iface);
1162 return S_OK;
1164 return E_NOINTERFACE;
1167 static ULONG WINAPI POP3TransportCF_AddRef(LPCLASSFACTORY iface)
1169 return 2; /* non-heap based object */
1172 static ULONG WINAPI POP3TransportCF_Release(LPCLASSFACTORY iface)
1174 return 1; /* non-heap based object */
1177 static HRESULT WINAPI POP3TransportCF_CreateInstance(LPCLASSFACTORY iface,
1178 LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
1180 HRESULT hr;
1181 IPOP3Transport *pPop3Transport;
1183 TRACE("(%p, %s, %p)\n", pUnk, debugstr_guid(riid), ppv);
1185 *ppv = NULL;
1187 if (pUnk)
1188 return CLASS_E_NOAGGREGATION;
1190 hr = CreatePOP3Transport(&pPop3Transport);
1191 if (FAILED(hr))
1192 return hr;
1194 hr = IPOP3Transport_QueryInterface(pPop3Transport, riid, ppv);
1195 IPOP3Transport_Release(pPop3Transport);
1197 return hr;
1200 static HRESULT WINAPI POP3TransportCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
1202 FIXME("(%d)\n",fLock);
1203 return S_OK;
1206 static const IClassFactoryVtbl POP3TransportCFVtbl =
1208 POP3TransportCF_QueryInterface,
1209 POP3TransportCF_AddRef,
1210 POP3TransportCF_Release,
1211 POP3TransportCF_CreateInstance,
1212 POP3TransportCF_LockServer
1214 static const IClassFactoryVtbl *POP3TransportCF = &POP3TransportCFVtbl;
1216 HRESULT POP3TransportCF_Create(REFIID riid, LPVOID *ppv)
1218 return IClassFactory_QueryInterface((IClassFactory *)&POP3TransportCF, riid, ppv);