cmd: DIR command outputs free space for the path.
[wine.git] / dlls / inetcomm / pop3transport.c
blob6dbbab1fa3feb39a7d5092ffb6d0a1d81dfd7f9f
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
23 #include <stdarg.h>
24 #include <stdio.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winnt.h"
29 #include "winuser.h"
30 #include "objbase.h"
31 #include "mimeole.h"
32 #include "wine/debug.h"
34 #include "inetcomm_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(inetcomm);
38 enum parse_state
40 STATE_NONE,
41 STATE_OK,
42 STATE_MULTILINE,
43 STATE_DONE
46 typedef struct
48 InternetTransport InetTransport;
49 ULONG refs;
50 POP3COMMAND command;
51 POP3CMDTYPE type;
52 char *response;
53 char *ptr;
54 enum parse_state state;
55 BOOL valid_info;
56 DWORD msgid;
57 DWORD preview_lines;
58 } POP3Transport;
60 static HRESULT parse_response(POP3Transport *This)
62 switch (This->state)
64 case STATE_NONE:
65 if (strlen(This->response) < 3)
67 WARN("parse error\n");
68 This->state = STATE_DONE;
69 return S_FALSE;
71 if (!memcmp(This->response, "+OK", 3))
73 This->ptr = This->response + 3;
74 This->state = STATE_OK;
75 return S_OK;
77 This->state = STATE_DONE;
78 return S_FALSE;
80 default: return S_OK;
84 static HRESULT parse_uidl_response(POP3Transport *This, POP3UIDL *uidl)
86 char *p;
88 uidl->dwPopId = 0;
89 uidl->pszUidl = NULL;
90 switch (This->state)
92 case STATE_OK:
93 if (This->type == POP3CMD_GET_POPID)
95 if ((p = strchr(This->ptr, ' ')))
97 while (*p == ' ') p++;
98 sscanf(p, "%lu", &uidl->dwPopId);
99 if ((p = strchr(p, ' ')))
101 while (*p == ' ') p++;
102 uidl->pszUidl = p;
103 This->valid_info = TRUE;
106 This->state = STATE_DONE;
107 return S_OK;
109 This->state = STATE_MULTILINE;
110 return S_OK;
112 case STATE_MULTILINE:
113 if (This->response[0] == '.' && !This->response[1])
115 This->valid_info = FALSE;
116 This->state = STATE_DONE;
117 return S_OK;
119 sscanf(This->response, "%lu", &uidl->dwPopId);
120 if ((p = strchr(This->response, ' ')))
122 while (*p == ' ') p++;
123 uidl->pszUidl = p;
124 This->valid_info = TRUE;
125 return S_OK;
128 default:
129 WARN("parse error\n");
130 This->state = STATE_DONE;
131 return S_FALSE;
135 static HRESULT parse_stat_response(POP3Transport *This, POP3STAT *stat)
137 char *p;
139 stat->cMessages = 0;
140 stat->cbMessages = 0;
141 switch (This->state)
143 case STATE_OK:
144 if ((p = strchr(This->ptr, ' ')))
146 while (*p == ' ') p++;
147 sscanf(p, "%lu %lu", &stat->cMessages, &stat->cbMessages);
148 This->valid_info = TRUE;
149 This->state = STATE_DONE;
150 return S_OK;
153 default:
154 WARN("parse error\n");
155 This->state = STATE_DONE;
156 return S_FALSE;
160 static HRESULT parse_list_response(POP3Transport *This, POP3LIST *list)
162 char *p;
164 list->dwPopId = 0;
165 list->cbSize = 0;
166 switch (This->state)
168 case STATE_OK:
169 if (This->type == POP3CMD_GET_POPID)
171 if ((p = strchr(This->ptr, ' ')))
173 while (*p == ' ') p++;
174 sscanf(p, "%lu %lu", &list->dwPopId, &list->cbSize);
175 This->valid_info = TRUE;
177 This->state = STATE_DONE;
178 return S_OK;
180 This->state = STATE_MULTILINE;
181 return S_OK;
183 case STATE_MULTILINE:
184 if (This->response[0] == '.' && !This->response[1])
186 This->valid_info = FALSE;
187 This->state = STATE_DONE;
188 return S_OK;
190 sscanf(This->response, "%lu", &list->dwPopId);
191 if ((p = strchr(This->response, ' ')))
193 while (*p == ' ') p++;
194 sscanf(p, "%lu", &list->cbSize);
195 This->valid_info = TRUE;
196 return S_OK;
199 default:
200 WARN("parse error\n");
201 This->state = STATE_DONE;
202 return S_FALSE;
206 static HRESULT parse_dele_response(POP3Transport *This, DWORD *dwPopId)
208 switch (This->state)
210 case STATE_OK:
211 *dwPopId = 0; /* FIXME */
212 This->state = STATE_DONE;
213 return S_OK;
215 default:
216 WARN("parse error\n");
217 This->state = STATE_DONE;
218 return S_FALSE;
222 static HRESULT parse_retr_response(POP3Transport *This, POP3RETR *retr)
224 switch (This->state)
226 case STATE_OK:
227 retr->fHeader = FALSE;
228 retr->fBody = FALSE;
229 retr->dwPopId = This->msgid;
230 retr->cbSoFar = 0;
231 retr->pszLines = This->response;
232 retr->cbLines = 0;
234 This->state = STATE_MULTILINE;
235 This->valid_info = FALSE;
236 return S_OK;
238 case STATE_MULTILINE:
240 int len;
242 if (This->response[0] == '.' && !This->response[1])
244 retr->cbLines = retr->cbSoFar;
245 This->state = STATE_DONE;
246 return S_OK;
248 retr->fHeader = TRUE;
249 if (!This->response[0]) retr->fBody = TRUE;
251 len = strlen(This->response);
252 retr->cbSoFar += len;
253 retr->pszLines = This->response;
254 retr->cbLines = len;
256 This->valid_info = TRUE;
257 return S_OK;
260 default:
261 WARN("parse error\n");
262 This->state = STATE_DONE;
263 return S_FALSE;
267 static HRESULT parse_top_response(POP3Transport *This, POP3TOP *top)
269 switch (This->state)
271 case STATE_OK:
272 top->fHeader = FALSE;
273 top->fBody = FALSE;
274 top->dwPopId = This->msgid;
275 top->cPreviewLines = This->preview_lines;
276 top->cbSoFar = 0;
277 top->pszLines = This->response;
278 top->cbLines = 0;
280 This->state = STATE_MULTILINE;
281 This->valid_info = FALSE;
282 return S_OK;
284 case STATE_MULTILINE:
286 int len;
288 if (This->response[0] == '.' && !This->response[1])
290 top->cbLines = top->cbSoFar;
291 This->state = STATE_DONE;
292 return S_OK;
294 top->fHeader = TRUE;
295 if (!This->response[0]) top->fBody = TRUE;
297 len = strlen(This->response);
298 top->cbSoFar += len;
299 top->pszLines = This->response;
300 top->cbLines = len;
302 This->valid_info = TRUE;
303 return S_OK;
306 default:
307 WARN("parse error\n");
308 This->state = STATE_DONE;
309 return S_FALSE;
313 static void init_parser(POP3Transport *This, POP3COMMAND command)
315 This->state = STATE_NONE;
316 This->command = command;
319 static HRESULT POP3Transport_ParseResponse(POP3Transport *This, char *pszResponse, POP3RESPONSE *pResponse)
321 HRESULT hr;
323 TRACE("response: %s\n", debugstr_a(pszResponse));
325 This->response = pszResponse;
326 This->valid_info = FALSE;
327 TRACE("state %u\n", This->state);
329 if (SUCCEEDED((hr = parse_response(This))))
331 switch (This->command)
333 case POP3_UIDL: hr = parse_uidl_response(This, &pResponse->rUidlInfo); break;
334 case POP3_STAT: hr = parse_stat_response(This, &pResponse->rStatInfo); break;
335 case POP3_LIST: hr = parse_list_response(This, &pResponse->rListInfo); break;
336 case POP3_DELE: hr = parse_dele_response(This, &pResponse->dwPopId); break;
337 case POP3_RETR: hr = parse_retr_response(This, &pResponse->rRetrInfo); break;
338 case POP3_TOP: hr = parse_top_response(This, &pResponse->rTopInfo); break;
339 default:
340 This->state = STATE_DONE;
341 break;
344 pResponse->command = This->command;
345 pResponse->fDone = (This->state == STATE_DONE);
346 pResponse->fValidInfo = This->valid_info;
347 pResponse->rIxpResult.hrResult = hr;
348 pResponse->rIxpResult.pszResponse = pszResponse;
349 pResponse->rIxpResult.uiServerError = 0;
350 pResponse->rIxpResult.hrServerError = pResponse->rIxpResult.hrResult;
351 pResponse->rIxpResult.dwSocketError = WSAGetLastError();
352 pResponse->rIxpResult.pszProblem = NULL;
353 pResponse->pTransport = (IPOP3Transport *)&This->InetTransport.u.vtblPOP3;
355 if (This->InetTransport.pCallback && This->InetTransport.fCommandLogging)
357 ITransportCallback_OnCommand(This->InetTransport.pCallback, CMD_RESP,
358 pResponse->rIxpResult.pszResponse, pResponse->rIxpResult.hrServerError,
359 (IInternetTransport *)&This->InetTransport.u.vtbl);
361 return S_OK;
364 static void POP3Transport_CallbackProcessDELEResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
366 POP3Transport *This = (POP3Transport *)iface;
367 POP3RESPONSE response;
368 HRESULT hr;
370 TRACE("\n");
372 hr = POP3Transport_ParseResponse(This, pBuffer, &response);
373 if (FAILED(hr))
375 /* FIXME: handle error */
376 return;
379 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
382 static void POP3Transport_CallbackRecvDELEResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
384 POP3Transport *This = (POP3Transport *)iface;
386 TRACE("\n");
387 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessDELEResp);
390 static void POP3Transport_CallbackProcessNOOPResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
392 POP3Transport *This = (POP3Transport *)iface;
393 POP3RESPONSE response;
394 HRESULT hr;
396 TRACE("\n");
398 hr = POP3Transport_ParseResponse(This, pBuffer, &response);
399 if (FAILED(hr))
401 /* FIXME: handle error */
402 return;
405 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
408 static void POP3Transport_CallbackRecvNOOPResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
410 POP3Transport *This = (POP3Transport *)iface;
412 TRACE("\n");
413 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessNOOPResp);
416 static void POP3Transport_CallbackProcessRSETResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
418 POP3Transport *This = (POP3Transport *)iface;
419 POP3RESPONSE response;
420 HRESULT hr;
422 TRACE("\n");
424 hr = POP3Transport_ParseResponse(This, pBuffer, &response);
425 if (FAILED(hr))
427 /* FIXME: handle error */
428 return;
431 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
434 static void POP3Transport_CallbackRecvRSETResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
436 POP3Transport *This = (POP3Transport *)iface;
438 TRACE("\n");
439 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessRSETResp);
442 static void POP3Transport_CallbackProcessRETRResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
444 POP3Transport *This = (POP3Transport *)iface;
445 POP3RESPONSE response;
446 HRESULT hr;
448 TRACE("\n");
450 hr = POP3Transport_ParseResponse(This, pBuffer, &response);
451 if (FAILED(hr))
453 /* FIXME: handle error */
454 return;
457 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
459 if (!response.fDone)
461 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessRETRResp);
462 return;
465 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
468 static void POP3Transport_CallbackRecvRETRResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
470 POP3Transport *This = (POP3Transport *)iface;
472 TRACE("\n");
473 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessRETRResp);
476 static void POP3Transport_CallbackProcessTOPResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
478 POP3Transport *This = (POP3Transport *)iface;
479 POP3RESPONSE response;
480 HRESULT hr;
482 TRACE("\n");
484 hr = POP3Transport_ParseResponse(This, pBuffer, &response);
485 if (FAILED(hr))
487 /* FIXME: handle error */
488 return;
491 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
493 if (!response.fDone)
495 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessTOPResp);
496 return;
499 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
502 static void POP3Transport_CallbackRecvTOPResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
504 POP3Transport *This = (POP3Transport *)iface;
506 TRACE("\n");
507 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessTOPResp);
510 static void POP3Transport_CallbackProcessLISTResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
512 POP3Transport *This = (POP3Transport *)iface;
513 POP3RESPONSE response;
514 HRESULT hr;
516 TRACE("\n");
518 hr = POP3Transport_ParseResponse(This, pBuffer, &response);
519 if (FAILED(hr))
521 /* FIXME: handle error */
522 return;
525 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
527 if (!response.fDone)
529 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessLISTResp);
530 return;
533 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
536 static void POP3Transport_CallbackRecvLISTResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
538 POP3Transport *This = (POP3Transport *)iface;
540 TRACE("\n");
541 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessLISTResp);
544 static void POP3Transport_CallbackProcessUIDLResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
546 POP3Transport *This = (POP3Transport *)iface;
547 POP3RESPONSE response;
548 HRESULT hr;
550 TRACE("\n");
552 hr = POP3Transport_ParseResponse(This, pBuffer, &response);
553 if (FAILED(hr))
555 /* FIXME: handle error */
556 return;
559 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
561 if (!response.fDone)
563 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessUIDLResp);
564 return;
567 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
570 static void POP3Transport_CallbackRecvUIDLResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
572 POP3Transport *This = (POP3Transport *)iface;
574 TRACE("\n");
575 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessUIDLResp);
578 static void POP3Transport_CallbackProcessSTATResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
580 POP3Transport *This = (POP3Transport *)iface;
581 POP3RESPONSE response;
582 HRESULT hr;
584 TRACE("\n");
586 hr = POP3Transport_ParseResponse(This, pBuffer, &response);
587 if (FAILED(hr))
589 /* FIXME: handle error */
590 return;
593 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
596 static void POP3Transport_CallbackRecvSTATResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
598 POP3Transport *This = (POP3Transport *)iface;
600 TRACE("\n");
601 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessSTATResp);
604 static void POP3Transport_CallbackProcessPASSResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
606 POP3Transport *This = (POP3Transport *)iface;
607 POP3RESPONSE response;
608 HRESULT hr;
610 TRACE("\n");
612 hr = POP3Transport_ParseResponse(This, pBuffer, &response);
613 if (FAILED(hr))
615 /* FIXME: handle error */
616 return;
619 InternetTransport_ChangeStatus(&This->InetTransport, IXP_AUTHORIZED);
620 InternetTransport_ChangeStatus(&This->InetTransport, IXP_CONNECTED);
622 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
625 static void POP3Transport_CallbackRecvPASSResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
627 POP3Transport *This = (POP3Transport *)iface;
629 TRACE("\n");
630 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessPASSResp);
633 static void POP3Transport_CallbackProcessUSERResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
635 static const char pass[] = "PASS ";
636 POP3Transport *This = (POP3Transport *)iface;
637 POP3RESPONSE response;
638 char *command;
639 int len;
640 HRESULT hr;
642 TRACE("\n");
644 hr = POP3Transport_ParseResponse(This, pBuffer, &response);
645 if (FAILED(hr))
647 /* FIXME: handle error */
648 return;
651 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
653 len = sizeof(pass) + strlen(This->InetTransport.ServerInfo.szPassword) + 2; /* "\r\n" */
654 command = HeapAlloc(GetProcessHeap(), 0, len);
656 strcpy(command, pass);
657 strcat(command, This->InetTransport.ServerInfo.szPassword);
658 strcat(command, "\r\n");
660 init_parser(This, POP3_PASS);
662 InternetTransport_DoCommand(&This->InetTransport, command, POP3Transport_CallbackRecvPASSResp);
663 HeapFree(GetProcessHeap(), 0, command);
666 static void POP3Transport_CallbackRecvUSERResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
668 POP3Transport *This = (POP3Transport *)iface;
670 TRACE("\n");
671 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessUSERResp);
674 static void POP3Transport_CallbackSendUSERCmd(IInternetTransport *iface, char *pBuffer, int cbBuffer)
676 static const char user[] = "USER ";
677 POP3Transport *This = (POP3Transport *)iface;
678 char *command;
679 int len;
681 TRACE("\n");
683 len = sizeof(user) + strlen(This->InetTransport.ServerInfo.szUserName) + 2; /* "\r\n" */
684 command = HeapAlloc(GetProcessHeap(), 0, len);
686 strcpy(command, user);
687 strcat(command, This->InetTransport.ServerInfo.szUserName);
688 strcat(command, "\r\n");
689 InternetTransport_DoCommand(&This->InetTransport, command, POP3Transport_CallbackRecvUSERResp);
691 HeapFree(GetProcessHeap(), 0, command);
694 static void POP3Transport_CallbackProcessQUITResponse(IInternetTransport *iface, char *pBuffer, int cbBuffer)
696 POP3Transport *This = (POP3Transport *)iface;
697 POP3RESPONSE response;
698 HRESULT hr;
700 TRACE("%s\n", debugstr_an(pBuffer, cbBuffer));
702 hr = POP3Transport_ParseResponse(This, pBuffer, &response);
703 if (FAILED(hr))
705 /* FIXME: handle error */
706 return;
709 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
710 InternetTransport_DropConnection(&This->InetTransport);
713 static void POP3Transport_CallbackRecvQUITResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
715 POP3Transport *This = (POP3Transport *)iface;
717 TRACE("\n");
718 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessQUITResponse);
721 static HRESULT WINAPI POP3Transport_QueryInterface(IPOP3Transport *iface, REFIID riid, void **ppv)
723 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
725 if (IsEqualIID(riid, &IID_IUnknown) ||
726 IsEqualIID(riid, &IID_IInternetTransport) ||
727 IsEqualIID(riid, &IID_IPOP3Transport))
729 *ppv = iface;
730 IPOP3Transport_AddRef(iface);
731 return S_OK;
733 *ppv = NULL;
734 FIXME("no interface for %s\n", debugstr_guid(riid));
735 return E_NOINTERFACE;
738 static ULONG WINAPI POP3Transport_AddRef(IPOP3Transport *iface)
740 POP3Transport *This = (POP3Transport *)iface;
741 return InterlockedIncrement((LONG *)&This->refs);
744 static ULONG WINAPI POP3Transport_Release(IPOP3Transport *iface)
746 POP3Transport *This = (POP3Transport *)iface;
747 ULONG refs = InterlockedDecrement((LONG *)&This->refs);
748 if (!refs)
750 TRACE("destroying %p\n", This);
751 if (This->InetTransport.Status != IXP_DISCONNECTED)
752 InternetTransport_DropConnection(&This->InetTransport);
753 if (This->InetTransport.pCallback) ITransportCallback_Release(This->InetTransport.pCallback);
754 HeapFree(GetProcessHeap(), 0, This);
756 return refs;
759 static HRESULT WINAPI POP3Transport_GetServerInfo(IPOP3Transport *iface,
760 LPINETSERVER pInetServer)
762 POP3Transport *This = (POP3Transport *)iface;
764 TRACE("(%p)\n", pInetServer);
765 return InternetTransport_GetServerInfo(&This->InetTransport, pInetServer);
768 static IXPTYPE WINAPI POP3Transport_GetIXPType(IPOP3Transport *iface)
770 TRACE("()\n");
771 return IXP_POP3;
774 static HRESULT WINAPI POP3Transport_IsState(IPOP3Transport *iface, IXPISSTATE isstate)
776 FIXME("(%u)\n", isstate);
777 return E_NOTIMPL;
780 static HRESULT WINAPI POP3Transport_InetServerFromAccount(
781 IPOP3Transport *iface, IImnAccount *pAccount, LPINETSERVER pInetServer)
783 POP3Transport *This = (POP3Transport *)iface;
785 TRACE("(%p, %p)\n", pAccount, pInetServer);
786 return InternetTransport_InetServerFromAccount(&This->InetTransport, pAccount, pInetServer);
789 static HRESULT WINAPI POP3Transport_Connect(IPOP3Transport *iface,
790 LPINETSERVER pInetServer, boolean fAuthenticate, boolean fCommandLogging)
792 POP3Transport *This = (POP3Transport *)iface;
793 HRESULT hr;
795 TRACE("(%p, %s, %s)\n", pInetServer, fAuthenticate ? "TRUE" : "FALSE", fCommandLogging ? "TRUE" : "FALSE");
797 hr = InternetTransport_Connect(&This->InetTransport, pInetServer, fAuthenticate, fCommandLogging);
798 if (FAILED(hr))
799 return hr;
801 init_parser(This, POP3_USER);
802 return InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackSendUSERCmd);
805 static HRESULT WINAPI POP3Transport_HandsOffCallback(IPOP3Transport *iface)
807 POP3Transport *This = (POP3Transport *)iface;
809 TRACE("()\n");
810 return InternetTransport_HandsOffCallback(&This->InetTransport);
813 static HRESULT WINAPI POP3Transport_Disconnect(IPOP3Transport *iface)
815 TRACE("()\n");
816 return IPOP3Transport_CommandQUIT(iface);
819 static HRESULT WINAPI POP3Transport_DropConnection(IPOP3Transport *iface)
821 POP3Transport *This = (POP3Transport *)iface;
823 TRACE("()\n");
824 return InternetTransport_DropConnection(&This->InetTransport);
827 static HRESULT WINAPI POP3Transport_GetStatus(IPOP3Transport *iface,
828 IXPSTATUS *pCurrentStatus)
830 POP3Transport *This = (POP3Transport *)iface;
832 TRACE("()\n");
833 return InternetTransport_GetStatus(&This->InetTransport, pCurrentStatus);
836 static HRESULT WINAPI POP3Transport_InitNew(IPOP3Transport *iface,
837 LPSTR pszLogFilePath, IPOP3Callback *pCallback)
839 POP3Transport *This = (POP3Transport *)iface;
841 TRACE("(%s, %p)\n", debugstr_a(pszLogFilePath), pCallback);
843 if (!pCallback)
844 return E_INVALIDARG;
846 if (pszLogFilePath)
847 FIXME("not using log file of %s, use Wine debug logging instead\n",
848 debugstr_a(pszLogFilePath));
850 IPOP3Callback_AddRef(pCallback);
851 This->InetTransport.pCallback = (ITransportCallback *)pCallback;
852 This->InetTransport.fInitialised = TRUE;
854 return S_OK;
857 static HRESULT WINAPI POP3Transport_MarkItem(IPOP3Transport *iface, POP3MARKTYPE marktype,
858 DWORD dwPopId, boolean fMarked)
860 FIXME("(%u, %lu, %d)\n", marktype, dwPopId, fMarked);
861 return E_NOTIMPL;
864 static HRESULT WINAPI POP3Transport_CommandAUTH(IPOP3Transport *iface, LPSTR pszAuthType)
866 FIXME("(%s)\n", pszAuthType);
867 return E_NOTIMPL;
870 static HRESULT WINAPI POP3Transport_CommandUSER(IPOP3Transport *iface, LPSTR username)
872 static const char user[] = "USER ";
873 POP3Transport *This = (POP3Transport *)iface;
874 char *command;
875 int len;
877 TRACE("(%s)\n", username);
879 len = sizeof(user) + strlen(username) + 2; /* "\r\n" */
880 if (!(command = HeapAlloc(GetProcessHeap(), 0, len))) return S_FALSE;
882 strcpy(command, user);
883 strcat(command, username);
884 strcat(command, "\r\n");
886 init_parser(This, POP3_USER);
887 InternetTransport_DoCommand(&This->InetTransport, command, POP3Transport_CallbackRecvUSERResp);
889 HeapFree(GetProcessHeap(), 0, command);
890 return S_OK;
893 static HRESULT WINAPI POP3Transport_CommandPASS(IPOP3Transport *iface, LPSTR password)
895 static const char pass[] = "PASS ";
896 POP3Transport *This = (POP3Transport *)iface;
897 char *command;
898 int len;
900 TRACE("(%p)\n", password);
902 len = sizeof(pass) + strlen(password) + 2; /* "\r\n" */
903 if (!(command = HeapAlloc(GetProcessHeap(), 0, len))) return S_FALSE;
905 strcpy(command, pass);
906 strcat(command, password);
907 strcat(command, "\r\n");
909 init_parser(This, POP3_PASS);
910 InternetTransport_DoCommand(&This->InetTransport, command, POP3Transport_CallbackRecvPASSResp);
912 HeapFree(GetProcessHeap(), 0, command);
913 return S_OK;
916 static HRESULT WINAPI POP3Transport_CommandLIST(
917 IPOP3Transport *iface, POP3CMDTYPE cmdtype, DWORD dwPopId)
919 static const char list[] = "LIST %lu\r\n";
920 static char list_all[] = "LIST\r\n";
921 POP3Transport *This = (POP3Transport *)iface;
922 char *command;
923 int len;
925 TRACE("(%u, %lu)\n", cmdtype, dwPopId);
927 if (dwPopId)
929 len = sizeof(list) + 10 + 2; /* "4294967296" + "\r\n" */
930 if (!(command = HeapAlloc(GetProcessHeap(), 0, len))) return S_FALSE;
931 sprintf(command, list, dwPopId);
933 else command = list_all;
935 init_parser(This, POP3_LIST);
936 This->type = cmdtype;
937 InternetTransport_DoCommand(&This->InetTransport, command, POP3Transport_CallbackRecvLISTResp);
939 if (dwPopId) HeapFree(GetProcessHeap(), 0, command);
940 return S_OK;
943 static HRESULT WINAPI POP3Transport_CommandTOP(
944 IPOP3Transport *iface, POP3CMDTYPE cmdtype, DWORD dwPopId, DWORD cPreviewLines)
946 static const char top[] = "TOP %lu %lu\r\n";
947 POP3Transport *This = (POP3Transport *)iface;
948 char *command;
949 int len;
951 TRACE("(%u, %lu, %lu)\n", cmdtype, dwPopId, cPreviewLines);
953 len = sizeof(top) + 20 + 2; /* 2 * "4294967296" + "\r\n" */
954 if (!(command = HeapAlloc(GetProcessHeap(), 0, len))) return S_FALSE;
955 sprintf(command, top, dwPopId, cPreviewLines);
957 This->preview_lines = cPreviewLines;
958 init_parser(This, POP3_TOP);
959 This->type = cmdtype;
960 InternetTransport_DoCommand(&This->InetTransport, command, POP3Transport_CallbackRecvTOPResp);
962 HeapFree(GetProcessHeap(), 0, command);
963 return S_OK;
966 static HRESULT WINAPI POP3Transport_CommandQUIT(IPOP3Transport *iface)
968 static const char command[] = "QUIT\r\n";
969 POP3Transport *This = (POP3Transport *)iface;
971 TRACE("()\n");
973 InternetTransport_ChangeStatus(&This->InetTransport, IXP_DISCONNECTING);
975 init_parser(This, POP3_QUIT);
976 return InternetTransport_DoCommand(&This->InetTransport, command, POP3Transport_CallbackRecvQUITResp);
979 static HRESULT WINAPI POP3Transport_CommandSTAT(IPOP3Transport *iface)
981 static const char stat[] = "STAT\r\n";
982 POP3Transport *This = (POP3Transport *)iface;
984 TRACE("\n");
986 init_parser(This, POP3_STAT);
987 InternetTransport_DoCommand(&This->InetTransport, stat, POP3Transport_CallbackRecvSTATResp);
988 return S_OK;
991 static HRESULT WINAPI POP3Transport_CommandNOOP(IPOP3Transport *iface)
993 static const char noop[] = "NOOP\r\n";
994 POP3Transport *This = (POP3Transport *)iface;
996 TRACE("\n");
998 init_parser(This, POP3_NOOP);
999 InternetTransport_DoCommand(&This->InetTransport, noop, POP3Transport_CallbackRecvNOOPResp);
1000 return S_OK;
1003 static HRESULT WINAPI POP3Transport_CommandRSET(IPOP3Transport *iface)
1005 static const char rset[] = "RSET\r\n";
1006 POP3Transport *This = (POP3Transport *)iface;
1008 TRACE("\n");
1010 init_parser(This, POP3_RSET);
1011 InternetTransport_DoCommand(&This->InetTransport, rset, POP3Transport_CallbackRecvRSETResp);
1012 return S_OK;
1015 static HRESULT WINAPI POP3Transport_CommandUIDL(
1016 IPOP3Transport *iface, POP3CMDTYPE cmdtype, DWORD dwPopId)
1018 static const char uidl[] = "UIDL %lu\r\n";
1019 static char uidl_all[] = "UIDL\r\n";
1020 POP3Transport *This = (POP3Transport *)iface;
1021 char *command;
1022 int len;
1024 TRACE("(%u, %lu)\n", cmdtype, dwPopId);
1026 if (dwPopId)
1028 len = sizeof(uidl) + 10 + 2; /* "4294967296" + "\r\n" */
1029 if (!(command = HeapAlloc(GetProcessHeap(), 0, len))) return S_FALSE;
1030 sprintf(command, uidl, dwPopId);
1032 else command = uidl_all;
1034 init_parser(This, POP3_UIDL);
1035 This->type = cmdtype;
1036 InternetTransport_DoCommand(&This->InetTransport, command, POP3Transport_CallbackRecvUIDLResp);
1038 if (dwPopId) HeapFree(GetProcessHeap(), 0, command);
1039 return S_OK;
1042 static HRESULT WINAPI POP3Transport_CommandDELE(
1043 IPOP3Transport *iface, POP3CMDTYPE cmdtype, DWORD dwPopId)
1045 static const char dele[] = "DELE %lu\r\n";
1046 POP3Transport *This = (POP3Transport *)iface;
1047 char *command;
1048 int len;
1050 TRACE("(%u, %lu)\n", cmdtype, dwPopId);
1052 len = sizeof(dele) + 10 + 2; /* "4294967296" + "\r\n" */
1053 if (!(command = HeapAlloc(GetProcessHeap(), 0, len))) return S_FALSE;
1054 sprintf(command, dele, dwPopId);
1056 init_parser(This, POP3_DELE);
1057 This->type = cmdtype;
1058 InternetTransport_DoCommand(&This->InetTransport, command, POP3Transport_CallbackRecvDELEResp);
1060 HeapFree(GetProcessHeap(), 0, command);
1061 return S_OK;
1064 static HRESULT WINAPI POP3Transport_CommandRETR(
1065 IPOP3Transport *iface, POP3CMDTYPE cmdtype, DWORD dwPopId)
1067 static const char retr[] = "RETR %lu\r\n";
1068 POP3Transport *This = (POP3Transport *)iface;
1069 char *command;
1070 int len;
1072 TRACE("(%u, %lu)\n", cmdtype, dwPopId);
1074 len = sizeof(retr) + 10 + 2; /* "4294967296" + "\r\n" */
1075 if (!(command = HeapAlloc(GetProcessHeap(), 0, len))) return S_FALSE;
1076 sprintf(command, retr, dwPopId);
1078 init_parser(This, POP3_RETR);
1079 This->type = cmdtype;
1080 InternetTransport_DoCommand(&This->InetTransport, command, POP3Transport_CallbackRecvRETRResp);
1082 HeapFree(GetProcessHeap(), 0, command);
1083 return S_OK;
1086 static const IPOP3TransportVtbl POP3TransportVtbl =
1088 POP3Transport_QueryInterface,
1089 POP3Transport_AddRef,
1090 POP3Transport_Release,
1091 POP3Transport_GetServerInfo,
1092 POP3Transport_GetIXPType,
1093 POP3Transport_IsState,
1094 POP3Transport_InetServerFromAccount,
1095 POP3Transport_Connect,
1096 POP3Transport_HandsOffCallback,
1097 POP3Transport_Disconnect,
1098 POP3Transport_DropConnection,
1099 POP3Transport_GetStatus,
1100 POP3Transport_InitNew,
1101 POP3Transport_MarkItem,
1102 POP3Transport_CommandAUTH,
1103 POP3Transport_CommandUSER,
1104 POP3Transport_CommandPASS,
1105 POP3Transport_CommandLIST,
1106 POP3Transport_CommandTOP,
1107 POP3Transport_CommandQUIT,
1108 POP3Transport_CommandSTAT,
1109 POP3Transport_CommandNOOP,
1110 POP3Transport_CommandRSET,
1111 POP3Transport_CommandUIDL,
1112 POP3Transport_CommandDELE,
1113 POP3Transport_CommandRETR
1116 HRESULT WINAPI CreatePOP3Transport(IPOP3Transport **ppTransport)
1118 HRESULT hr;
1119 POP3Transport *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1120 if (!This)
1121 return E_OUTOFMEMORY;
1123 This->InetTransport.u.vtblPOP3 = &POP3TransportVtbl;
1124 This->refs = 0;
1125 hr = InternetTransport_Init(&This->InetTransport);
1126 if (FAILED(hr))
1128 HeapFree(GetProcessHeap(), 0, This);
1129 return hr;
1132 *ppTransport = (IPOP3Transport *)&This->InetTransport.u.vtblPOP3;
1133 IPOP3Transport_AddRef(*ppTransport);
1135 return S_OK;
1138 static HRESULT WINAPI POP3TransportCF_QueryInterface(LPCLASSFACTORY iface,
1139 REFIID riid, LPVOID *ppv)
1141 *ppv = NULL;
1142 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
1144 *ppv = iface;
1145 IClassFactory_AddRef(iface);
1146 return S_OK;
1148 return E_NOINTERFACE;
1151 static ULONG WINAPI POP3TransportCF_AddRef(LPCLASSFACTORY iface)
1153 return 2; /* non-heap based object */
1156 static ULONG WINAPI POP3TransportCF_Release(LPCLASSFACTORY iface)
1158 return 1; /* non-heap based object */
1161 static HRESULT WINAPI POP3TransportCF_CreateInstance(LPCLASSFACTORY iface,
1162 LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
1164 HRESULT hr;
1165 IPOP3Transport *pPop3Transport;
1167 TRACE("(%p, %s, %p)\n", pUnk, debugstr_guid(riid), ppv);
1169 *ppv = NULL;
1171 if (pUnk)
1172 return CLASS_E_NOAGGREGATION;
1174 hr = CreatePOP3Transport(&pPop3Transport);
1175 if (FAILED(hr))
1176 return hr;
1178 hr = IPOP3Transport_QueryInterface(pPop3Transport, riid, ppv);
1179 IPOP3Transport_Release(pPop3Transport);
1181 return hr;
1184 static HRESULT WINAPI POP3TransportCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
1186 FIXME("(%d)\n",fLock);
1187 return S_OK;
1190 static const IClassFactoryVtbl POP3TransportCFVtbl =
1192 POP3TransportCF_QueryInterface,
1193 POP3TransportCF_AddRef,
1194 POP3TransportCF_Release,
1195 POP3TransportCF_CreateInstance,
1196 POP3TransportCF_LockServer
1198 static const IClassFactoryVtbl *POP3TransportCF = &POP3TransportCFVtbl;
1200 HRESULT POP3TransportCF_Create(REFIID riid, LPVOID *ppv)
1202 return IClassFactory_QueryInterface((IClassFactory *)&POP3TransportCF, riid, ppv);