kernelbase: Let GetModuleBaseName succeed on 64bit modules in wow64.
[wine.git] / dlls / inetcomm / pop3transport.c
blob7901b831cb5eaa92ef335424213cf5f66972e8af
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:
66 if (strlen(This->response) < 3)
68 WARN("parse error\n");
69 This->state = STATE_DONE;
70 return S_FALSE;
72 if (!memcmp(This->response, "+OK", 3))
74 This->ptr = This->response + 3;
75 This->state = STATE_OK;
76 return S_OK;
78 This->state = STATE_DONE;
79 return S_FALSE;
81 default: return S_OK;
85 static HRESULT parse_uidl_response(POP3Transport *This, POP3UIDL *uidl)
87 char *p;
89 uidl->dwPopId = 0;
90 uidl->pszUidl = NULL;
91 switch (This->state)
93 case STATE_OK:
94 if (This->type == POP3CMD_GET_POPID)
96 if ((p = strchr(This->ptr, ' ')))
98 while (*p == ' ') p++;
99 sscanf(p, "%lu", &uidl->dwPopId);
100 if ((p = strchr(p, ' ')))
102 while (*p == ' ') p++;
103 uidl->pszUidl = p;
104 This->valid_info = TRUE;
107 This->state = STATE_DONE;
108 return S_OK;
110 This->state = STATE_MULTILINE;
111 return S_OK;
113 case STATE_MULTILINE:
114 if (This->response[0] == '.' && !This->response[1])
116 This->valid_info = FALSE;
117 This->state = STATE_DONE;
118 return S_OK;
120 sscanf(This->response, "%lu", &uidl->dwPopId);
121 if ((p = strchr(This->response, ' ')))
123 while (*p == ' ') p++;
124 uidl->pszUidl = p;
125 This->valid_info = TRUE;
126 return S_OK;
129 default:
130 WARN("parse error\n");
131 This->state = STATE_DONE;
132 return S_FALSE;
136 static HRESULT parse_stat_response(POP3Transport *This, POP3STAT *stat)
138 char *p;
140 stat->cMessages = 0;
141 stat->cbMessages = 0;
142 switch (This->state)
144 case STATE_OK:
145 if ((p = strchr(This->ptr, ' ')))
147 while (*p == ' ') p++;
148 sscanf(p, "%lu %lu", &stat->cMessages, &stat->cbMessages);
149 This->valid_info = TRUE;
150 This->state = STATE_DONE;
151 return S_OK;
154 default:
155 WARN("parse error\n");
156 This->state = STATE_DONE;
157 return S_FALSE;
161 static HRESULT parse_list_response(POP3Transport *This, POP3LIST *list)
163 char *p;
165 list->dwPopId = 0;
166 list->cbSize = 0;
167 switch (This->state)
169 case STATE_OK:
170 if (This->type == POP3CMD_GET_POPID)
172 if ((p = strchr(This->ptr, ' ')))
174 while (*p == ' ') p++;
175 sscanf(p, "%lu %lu", &list->dwPopId, &list->cbSize);
176 This->valid_info = TRUE;
178 This->state = STATE_DONE;
179 return S_OK;
181 This->state = STATE_MULTILINE;
182 return S_OK;
184 case STATE_MULTILINE:
185 if (This->response[0] == '.' && !This->response[1])
187 This->valid_info = FALSE;
188 This->state = STATE_DONE;
189 return S_OK;
191 sscanf(This->response, "%lu", &list->dwPopId);
192 if ((p = strchr(This->response, ' ')))
194 while (*p == ' ') p++;
195 sscanf(p, "%lu", &list->cbSize);
196 This->valid_info = TRUE;
197 return S_OK;
200 default:
201 WARN("parse error\n");
202 This->state = STATE_DONE;
203 return S_FALSE;
207 static HRESULT parse_dele_response(POP3Transport *This, DWORD *dwPopId)
209 switch (This->state)
211 case STATE_OK:
212 *dwPopId = 0; /* FIXME */
213 This->state = STATE_DONE;
214 return S_OK;
216 default:
217 WARN("parse error\n");
218 This->state = STATE_DONE;
219 return S_FALSE;
223 static HRESULT parse_retr_response(POP3Transport *This, POP3RETR *retr)
225 switch (This->state)
227 case STATE_OK:
228 retr->fHeader = FALSE;
229 retr->fBody = FALSE;
230 retr->dwPopId = This->msgid;
231 retr->cbSoFar = 0;
232 retr->pszLines = This->response;
233 retr->cbLines = 0;
235 This->state = STATE_MULTILINE;
236 This->valid_info = FALSE;
237 return S_OK;
239 case STATE_MULTILINE:
241 int len;
243 if (This->response[0] == '.' && !This->response[1])
245 retr->cbLines = retr->cbSoFar;
246 This->state = STATE_DONE;
247 return S_OK;
249 retr->fHeader = TRUE;
250 if (!This->response[0]) retr->fBody = TRUE;
252 len = strlen(This->response);
253 retr->cbSoFar += len;
254 retr->pszLines = This->response;
255 retr->cbLines = len;
257 This->valid_info = TRUE;
258 return S_OK;
261 default:
262 WARN("parse error\n");
263 This->state = STATE_DONE;
264 return S_FALSE;
268 static HRESULT parse_top_response(POP3Transport *This, POP3TOP *top)
270 switch (This->state)
272 case STATE_OK:
273 top->fHeader = FALSE;
274 top->fBody = FALSE;
275 top->dwPopId = This->msgid;
276 top->cPreviewLines = This->preview_lines;
277 top->cbSoFar = 0;
278 top->pszLines = This->response;
279 top->cbLines = 0;
281 This->state = STATE_MULTILINE;
282 This->valid_info = FALSE;
283 return S_OK;
285 case STATE_MULTILINE:
287 int len;
289 if (This->response[0] == '.' && !This->response[1])
291 top->cbLines = top->cbSoFar;
292 This->state = STATE_DONE;
293 return S_OK;
295 top->fHeader = TRUE;
296 if (!This->response[0]) top->fBody = TRUE;
298 len = strlen(This->response);
299 top->cbSoFar += len;
300 top->pszLines = This->response;
301 top->cbLines = len;
303 This->valid_info = TRUE;
304 return S_OK;
307 default:
308 WARN("parse error\n");
309 This->state = STATE_DONE;
310 return S_FALSE;
314 static void init_parser(POP3Transport *This, POP3COMMAND command)
316 This->state = STATE_NONE;
317 This->command = command;
320 static HRESULT POP3Transport_ParseResponse(POP3Transport *This, char *pszResponse, POP3RESPONSE *pResponse)
322 HRESULT hr;
324 TRACE("response: %s\n", debugstr_a(pszResponse));
326 This->response = pszResponse;
327 This->valid_info = FALSE;
328 TRACE("state %u\n", This->state);
330 if (SUCCEEDED((hr = parse_response(This))))
332 switch (This->command)
334 case POP3_UIDL: hr = parse_uidl_response(This, &pResponse->rUidlInfo); break;
335 case POP3_STAT: hr = parse_stat_response(This, &pResponse->rStatInfo); break;
336 case POP3_LIST: hr = parse_list_response(This, &pResponse->rListInfo); break;
337 case POP3_DELE: hr = parse_dele_response(This, &pResponse->dwPopId); break;
338 case POP3_RETR: hr = parse_retr_response(This, &pResponse->rRetrInfo); break;
339 case POP3_TOP: hr = parse_top_response(This, &pResponse->rTopInfo); break;
340 default:
341 This->state = STATE_DONE;
342 break;
345 pResponse->command = This->command;
346 pResponse->fDone = (This->state == STATE_DONE);
347 pResponse->fValidInfo = This->valid_info;
348 pResponse->rIxpResult.hrResult = hr;
349 pResponse->rIxpResult.pszResponse = pszResponse;
350 pResponse->rIxpResult.uiServerError = 0;
351 pResponse->rIxpResult.hrServerError = pResponse->rIxpResult.hrResult;
352 pResponse->rIxpResult.dwSocketError = WSAGetLastError();
353 pResponse->rIxpResult.pszProblem = NULL;
354 pResponse->pTransport = (IPOP3Transport *)&This->InetTransport.u.vtblPOP3;
356 if (This->InetTransport.pCallback && This->InetTransport.fCommandLogging)
358 ITransportCallback_OnCommand(This->InetTransport.pCallback, CMD_RESP,
359 pResponse->rIxpResult.pszResponse, pResponse->rIxpResult.hrServerError,
360 (IInternetTransport *)&This->InetTransport.u.vtbl);
362 return S_OK;
365 static void POP3Transport_CallbackProcessDELEResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
367 POP3Transport *This = (POP3Transport *)iface;
368 POP3RESPONSE response;
369 HRESULT hr;
371 TRACE("\n");
373 hr = POP3Transport_ParseResponse(This, pBuffer, &response);
374 if (FAILED(hr))
376 /* FIXME: handle error */
377 return;
380 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
383 static void POP3Transport_CallbackRecvDELEResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
385 POP3Transport *This = (POP3Transport *)iface;
387 TRACE("\n");
388 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessDELEResp);
391 static void POP3Transport_CallbackProcessNOOPResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
393 POP3Transport *This = (POP3Transport *)iface;
394 POP3RESPONSE response;
395 HRESULT hr;
397 TRACE("\n");
399 hr = POP3Transport_ParseResponse(This, pBuffer, &response);
400 if (FAILED(hr))
402 /* FIXME: handle error */
403 return;
406 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
409 static void POP3Transport_CallbackRecvNOOPResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
411 POP3Transport *This = (POP3Transport *)iface;
413 TRACE("\n");
414 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessNOOPResp);
417 static void POP3Transport_CallbackProcessRSETResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
419 POP3Transport *This = (POP3Transport *)iface;
420 POP3RESPONSE response;
421 HRESULT hr;
423 TRACE("\n");
425 hr = POP3Transport_ParseResponse(This, pBuffer, &response);
426 if (FAILED(hr))
428 /* FIXME: handle error */
429 return;
432 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
435 static void POP3Transport_CallbackRecvRSETResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
437 POP3Transport *This = (POP3Transport *)iface;
439 TRACE("\n");
440 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessRSETResp);
443 static void POP3Transport_CallbackProcessRETRResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
445 POP3Transport *This = (POP3Transport *)iface;
446 POP3RESPONSE response;
447 HRESULT hr;
449 TRACE("\n");
451 hr = POP3Transport_ParseResponse(This, pBuffer, &response);
452 if (FAILED(hr))
454 /* FIXME: handle error */
455 return;
458 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
460 if (!response.fDone)
462 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessRETRResp);
463 return;
466 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
469 static void POP3Transport_CallbackRecvRETRResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
471 POP3Transport *This = (POP3Transport *)iface;
473 TRACE("\n");
474 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessRETRResp);
477 static void POP3Transport_CallbackProcessTOPResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
479 POP3Transport *This = (POP3Transport *)iface;
480 POP3RESPONSE response;
481 HRESULT hr;
483 TRACE("\n");
485 hr = POP3Transport_ParseResponse(This, pBuffer, &response);
486 if (FAILED(hr))
488 /* FIXME: handle error */
489 return;
492 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
494 if (!response.fDone)
496 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessTOPResp);
497 return;
500 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
503 static void POP3Transport_CallbackRecvTOPResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
505 POP3Transport *This = (POP3Transport *)iface;
507 TRACE("\n");
508 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessTOPResp);
511 static void POP3Transport_CallbackProcessLISTResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
513 POP3Transport *This = (POP3Transport *)iface;
514 POP3RESPONSE response;
515 HRESULT hr;
517 TRACE("\n");
519 hr = POP3Transport_ParseResponse(This, pBuffer, &response);
520 if (FAILED(hr))
522 /* FIXME: handle error */
523 return;
526 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
528 if (!response.fDone)
530 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessLISTResp);
531 return;
534 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
537 static void POP3Transport_CallbackRecvLISTResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
539 POP3Transport *This = (POP3Transport *)iface;
541 TRACE("\n");
542 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessLISTResp);
545 static void POP3Transport_CallbackProcessUIDLResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
547 POP3Transport *This = (POP3Transport *)iface;
548 POP3RESPONSE response;
549 HRESULT hr;
551 TRACE("\n");
553 hr = POP3Transport_ParseResponse(This, pBuffer, &response);
554 if (FAILED(hr))
556 /* FIXME: handle error */
557 return;
560 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
562 if (!response.fDone)
564 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessUIDLResp);
565 return;
568 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
571 static void POP3Transport_CallbackRecvUIDLResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
573 POP3Transport *This = (POP3Transport *)iface;
575 TRACE("\n");
576 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessUIDLResp);
579 static void POP3Transport_CallbackProcessSTATResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
581 POP3Transport *This = (POP3Transport *)iface;
582 POP3RESPONSE response;
583 HRESULT hr;
585 TRACE("\n");
587 hr = POP3Transport_ParseResponse(This, pBuffer, &response);
588 if (FAILED(hr))
590 /* FIXME: handle error */
591 return;
594 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
597 static void POP3Transport_CallbackRecvSTATResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
599 POP3Transport *This = (POP3Transport *)iface;
601 TRACE("\n");
602 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessSTATResp);
605 static void POP3Transport_CallbackProcessPASSResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
607 POP3Transport *This = (POP3Transport *)iface;
608 POP3RESPONSE response;
609 HRESULT hr;
611 TRACE("\n");
613 hr = POP3Transport_ParseResponse(This, pBuffer, &response);
614 if (FAILED(hr))
616 /* FIXME: handle error */
617 return;
620 InternetTransport_ChangeStatus(&This->InetTransport, IXP_AUTHORIZED);
621 InternetTransport_ChangeStatus(&This->InetTransport, IXP_CONNECTED);
623 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
626 static void POP3Transport_CallbackRecvPASSResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
628 POP3Transport *This = (POP3Transport *)iface;
630 TRACE("\n");
631 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessPASSResp);
634 static void POP3Transport_CallbackProcessUSERResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
636 static const char pass[] = "PASS ";
637 POP3Transport *This = (POP3Transport *)iface;
638 POP3RESPONSE response;
639 char *command;
640 int len;
641 HRESULT hr;
643 TRACE("\n");
645 hr = POP3Transport_ParseResponse(This, pBuffer, &response);
646 if (FAILED(hr))
648 /* FIXME: handle error */
649 return;
652 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
654 len = sizeof(pass) + strlen(This->InetTransport.ServerInfo.szPassword) + 2; /* "\r\n" */
655 command = HeapAlloc(GetProcessHeap(), 0, len);
657 strcpy(command, pass);
658 strcat(command, This->InetTransport.ServerInfo.szPassword);
659 strcat(command, "\r\n");
661 init_parser(This, POP3_PASS);
663 InternetTransport_DoCommand(&This->InetTransport, command, POP3Transport_CallbackRecvPASSResp);
664 HeapFree(GetProcessHeap(), 0, command);
667 static void POP3Transport_CallbackRecvUSERResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
669 POP3Transport *This = (POP3Transport *)iface;
671 TRACE("\n");
672 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessUSERResp);
675 static void POP3Transport_CallbackSendUSERCmd(IInternetTransport *iface, char *pBuffer, int cbBuffer)
677 static const char user[] = "USER ";
678 POP3Transport *This = (POP3Transport *)iface;
679 char *command;
680 int len;
682 TRACE("\n");
684 len = sizeof(user) + strlen(This->InetTransport.ServerInfo.szUserName) + 2; /* "\r\n" */
685 command = HeapAlloc(GetProcessHeap(), 0, len);
687 strcpy(command, user);
688 strcat(command, This->InetTransport.ServerInfo.szUserName);
689 strcat(command, "\r\n");
690 InternetTransport_DoCommand(&This->InetTransport, command, POP3Transport_CallbackRecvUSERResp);
692 HeapFree(GetProcessHeap(), 0, command);
695 static void POP3Transport_CallbackProcessQUITResponse(IInternetTransport *iface, char *pBuffer, int cbBuffer)
697 POP3Transport *This = (POP3Transport *)iface;
698 POP3RESPONSE response;
699 HRESULT hr;
701 TRACE("%s\n", debugstr_an(pBuffer, cbBuffer));
703 hr = POP3Transport_ParseResponse(This, pBuffer, &response);
704 if (FAILED(hr))
706 /* FIXME: handle error */
707 return;
710 IPOP3Callback_OnResponse((IPOP3Callback *)This->InetTransport.pCallback, &response);
711 InternetTransport_DropConnection(&This->InetTransport);
714 static void POP3Transport_CallbackRecvQUITResp(IInternetTransport *iface, char *pBuffer, int cbBuffer)
716 POP3Transport *This = (POP3Transport *)iface;
718 TRACE("\n");
719 InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackProcessQUITResponse);
722 static HRESULT WINAPI POP3Transport_QueryInterface(IPOP3Transport *iface, REFIID riid, void **ppv)
724 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
726 if (IsEqualIID(riid, &IID_IUnknown) ||
727 IsEqualIID(riid, &IID_IInternetTransport) ||
728 IsEqualIID(riid, &IID_IPOP3Transport))
730 *ppv = iface;
731 IPOP3Transport_AddRef(iface);
732 return S_OK;
734 *ppv = NULL;
735 FIXME("no interface for %s\n", debugstr_guid(riid));
736 return E_NOINTERFACE;
739 static ULONG WINAPI POP3Transport_AddRef(IPOP3Transport *iface)
741 POP3Transport *This = (POP3Transport *)iface;
742 return InterlockedIncrement((LONG *)&This->refs);
745 static ULONG WINAPI POP3Transport_Release(IPOP3Transport *iface)
747 POP3Transport *This = (POP3Transport *)iface;
748 ULONG refs = InterlockedDecrement((LONG *)&This->refs);
749 if (!refs)
751 TRACE("destroying %p\n", This);
752 if (This->InetTransport.Status != IXP_DISCONNECTED)
753 InternetTransport_DropConnection(&This->InetTransport);
754 if (This->InetTransport.pCallback) ITransportCallback_Release(This->InetTransport.pCallback);
755 HeapFree(GetProcessHeap(), 0, This);
757 return refs;
760 static HRESULT WINAPI POP3Transport_GetServerInfo(IPOP3Transport *iface,
761 LPINETSERVER pInetServer)
763 POP3Transport *This = (POP3Transport *)iface;
765 TRACE("(%p)\n", pInetServer);
766 return InternetTransport_GetServerInfo(&This->InetTransport, pInetServer);
769 static IXPTYPE WINAPI POP3Transport_GetIXPType(IPOP3Transport *iface)
771 TRACE("()\n");
772 return IXP_POP3;
775 static HRESULT WINAPI POP3Transport_IsState(IPOP3Transport *iface, IXPISSTATE isstate)
777 FIXME("(%u)\n", isstate);
778 return E_NOTIMPL;
781 static HRESULT WINAPI POP3Transport_InetServerFromAccount(
782 IPOP3Transport *iface, IImnAccount *pAccount, LPINETSERVER pInetServer)
784 POP3Transport *This = (POP3Transport *)iface;
786 TRACE("(%p, %p)\n", pAccount, pInetServer);
787 return InternetTransport_InetServerFromAccount(&This->InetTransport, pAccount, pInetServer);
790 static HRESULT WINAPI POP3Transport_Connect(IPOP3Transport *iface,
791 LPINETSERVER pInetServer, boolean fAuthenticate, boolean fCommandLogging)
793 POP3Transport *This = (POP3Transport *)iface;
794 HRESULT hr;
796 TRACE("(%p, %s, %s)\n", pInetServer, fAuthenticate ? "TRUE" : "FALSE", fCommandLogging ? "TRUE" : "FALSE");
798 hr = InternetTransport_Connect(&This->InetTransport, pInetServer, fAuthenticate, fCommandLogging);
799 if (FAILED(hr))
800 return hr;
802 init_parser(This, POP3_USER);
803 return InternetTransport_ReadLine(&This->InetTransport, POP3Transport_CallbackSendUSERCmd);
806 static HRESULT WINAPI POP3Transport_HandsOffCallback(IPOP3Transport *iface)
808 POP3Transport *This = (POP3Transport *)iface;
810 TRACE("()\n");
811 return InternetTransport_HandsOffCallback(&This->InetTransport);
814 static HRESULT WINAPI POP3Transport_Disconnect(IPOP3Transport *iface)
816 TRACE("()\n");
817 return IPOP3Transport_CommandQUIT(iface);
820 static HRESULT WINAPI POP3Transport_DropConnection(IPOP3Transport *iface)
822 POP3Transport *This = (POP3Transport *)iface;
824 TRACE("()\n");
825 return InternetTransport_DropConnection(&This->InetTransport);
828 static HRESULT WINAPI POP3Transport_GetStatus(IPOP3Transport *iface,
829 IXPSTATUS *pCurrentStatus)
831 POP3Transport *This = (POP3Transport *)iface;
833 TRACE("()\n");
834 return InternetTransport_GetStatus(&This->InetTransport, pCurrentStatus);
837 static HRESULT WINAPI POP3Transport_InitNew(IPOP3Transport *iface,
838 LPSTR pszLogFilePath, IPOP3Callback *pCallback)
840 POP3Transport *This = (POP3Transport *)iface;
842 TRACE("(%s, %p)\n", debugstr_a(pszLogFilePath), pCallback);
844 if (!pCallback)
845 return E_INVALIDARG;
847 if (pszLogFilePath)
848 FIXME("not using log file of %s, use Wine debug logging instead\n",
849 debugstr_a(pszLogFilePath));
851 IPOP3Callback_AddRef(pCallback);
852 This->InetTransport.pCallback = (ITransportCallback *)pCallback;
853 This->InetTransport.fInitialised = TRUE;
855 return S_OK;
858 static HRESULT WINAPI POP3Transport_MarkItem(IPOP3Transport *iface, POP3MARKTYPE marktype,
859 DWORD dwPopId, boolean fMarked)
861 FIXME("(%u, %lu, %d)\n", marktype, dwPopId, fMarked);
862 return E_NOTIMPL;
865 static HRESULT WINAPI POP3Transport_CommandAUTH(IPOP3Transport *iface, LPSTR pszAuthType)
867 FIXME("(%s)\n", pszAuthType);
868 return E_NOTIMPL;
871 static HRESULT WINAPI POP3Transport_CommandUSER(IPOP3Transport *iface, LPSTR username)
873 static const char user[] = "USER ";
874 POP3Transport *This = (POP3Transport *)iface;
875 char *command;
876 int len;
878 TRACE("(%s)\n", username);
880 len = sizeof(user) + strlen(username) + 2; /* "\r\n" */
881 if (!(command = HeapAlloc(GetProcessHeap(), 0, len))) return S_FALSE;
883 strcpy(command, user);
884 strcat(command, username);
885 strcat(command, "\r\n");
887 init_parser(This, POP3_USER);
888 InternetTransport_DoCommand(&This->InetTransport, command, POP3Transport_CallbackRecvUSERResp);
890 HeapFree(GetProcessHeap(), 0, command);
891 return S_OK;
894 static HRESULT WINAPI POP3Transport_CommandPASS(IPOP3Transport *iface, LPSTR password)
896 static const char pass[] = "PASS ";
897 POP3Transport *This = (POP3Transport *)iface;
898 char *command;
899 int len;
901 TRACE("(%p)\n", password);
903 len = sizeof(pass) + strlen(password) + 2; /* "\r\n" */
904 if (!(command = HeapAlloc(GetProcessHeap(), 0, len))) return S_FALSE;
906 strcpy(command, pass);
907 strcat(command, password);
908 strcat(command, "\r\n");
910 init_parser(This, POP3_PASS);
911 InternetTransport_DoCommand(&This->InetTransport, command, POP3Transport_CallbackRecvPASSResp);
913 HeapFree(GetProcessHeap(), 0, command);
914 return S_OK;
917 static HRESULT WINAPI POP3Transport_CommandLIST(
918 IPOP3Transport *iface, POP3CMDTYPE cmdtype, DWORD dwPopId)
920 static const char list[] = "LIST %lu\r\n";
921 static char list_all[] = "LIST\r\n";
922 POP3Transport *This = (POP3Transport *)iface;
923 char *command;
924 int len;
926 TRACE("(%u, %lu)\n", cmdtype, dwPopId);
928 if (dwPopId)
930 len = sizeof(list) + 10 + 2; /* "4294967296" + "\r\n" */
931 if (!(command = HeapAlloc(GetProcessHeap(), 0, len))) return S_FALSE;
932 sprintf(command, list, dwPopId);
934 else command = list_all;
936 init_parser(This, POP3_LIST);
937 This->type = cmdtype;
938 InternetTransport_DoCommand(&This->InetTransport, command, POP3Transport_CallbackRecvLISTResp);
940 if (dwPopId) HeapFree(GetProcessHeap(), 0, command);
941 return S_OK;
944 static HRESULT WINAPI POP3Transport_CommandTOP(
945 IPOP3Transport *iface, POP3CMDTYPE cmdtype, DWORD dwPopId, DWORD cPreviewLines)
947 static const char top[] = "TOP %lu %lu\r\n";
948 POP3Transport *This = (POP3Transport *)iface;
949 char *command;
950 int len;
952 TRACE("(%u, %lu, %lu)\n", cmdtype, dwPopId, cPreviewLines);
954 len = sizeof(top) + 20 + 2; /* 2 * "4294967296" + "\r\n" */
955 if (!(command = HeapAlloc(GetProcessHeap(), 0, len))) return S_FALSE;
956 sprintf(command, top, dwPopId, cPreviewLines);
958 This->preview_lines = cPreviewLines;
959 init_parser(This, POP3_TOP);
960 This->type = cmdtype;
961 InternetTransport_DoCommand(&This->InetTransport, command, POP3Transport_CallbackRecvTOPResp);
963 HeapFree(GetProcessHeap(), 0, command);
964 return S_OK;
967 static HRESULT WINAPI POP3Transport_CommandQUIT(IPOP3Transport *iface)
969 static const char command[] = "QUIT\r\n";
970 POP3Transport *This = (POP3Transport *)iface;
972 TRACE("()\n");
974 InternetTransport_ChangeStatus(&This->InetTransport, IXP_DISCONNECTING);
976 init_parser(This, POP3_QUIT);
977 return InternetTransport_DoCommand(&This->InetTransport, command, POP3Transport_CallbackRecvQUITResp);
980 static HRESULT WINAPI POP3Transport_CommandSTAT(IPOP3Transport *iface)
982 static const char stat[] = "STAT\r\n";
983 POP3Transport *This = (POP3Transport *)iface;
985 TRACE("\n");
987 init_parser(This, POP3_STAT);
988 InternetTransport_DoCommand(&This->InetTransport, stat, POP3Transport_CallbackRecvSTATResp);
989 return S_OK;
992 static HRESULT WINAPI POP3Transport_CommandNOOP(IPOP3Transport *iface)
994 static const char noop[] = "NOOP\r\n";
995 POP3Transport *This = (POP3Transport *)iface;
997 TRACE("\n");
999 init_parser(This, POP3_NOOP);
1000 InternetTransport_DoCommand(&This->InetTransport, noop, POP3Transport_CallbackRecvNOOPResp);
1001 return S_OK;
1004 static HRESULT WINAPI POP3Transport_CommandRSET(IPOP3Transport *iface)
1006 static const char rset[] = "RSET\r\n";
1007 POP3Transport *This = (POP3Transport *)iface;
1009 TRACE("\n");
1011 init_parser(This, POP3_RSET);
1012 InternetTransport_DoCommand(&This->InetTransport, rset, POP3Transport_CallbackRecvRSETResp);
1013 return S_OK;
1016 static HRESULT WINAPI POP3Transport_CommandUIDL(
1017 IPOP3Transport *iface, POP3CMDTYPE cmdtype, DWORD dwPopId)
1019 static const char uidl[] = "UIDL %lu\r\n";
1020 static char uidl_all[] = "UIDL\r\n";
1021 POP3Transport *This = (POP3Transport *)iface;
1022 char *command;
1023 int len;
1025 TRACE("(%u, %lu)\n", cmdtype, dwPopId);
1027 if (dwPopId)
1029 len = sizeof(uidl) + 10 + 2; /* "4294967296" + "\r\n" */
1030 if (!(command = HeapAlloc(GetProcessHeap(), 0, len))) return S_FALSE;
1031 sprintf(command, uidl, dwPopId);
1033 else command = uidl_all;
1035 init_parser(This, POP3_UIDL);
1036 This->type = cmdtype;
1037 InternetTransport_DoCommand(&This->InetTransport, command, POP3Transport_CallbackRecvUIDLResp);
1039 if (dwPopId) HeapFree(GetProcessHeap(), 0, command);
1040 return S_OK;
1043 static HRESULT WINAPI POP3Transport_CommandDELE(
1044 IPOP3Transport *iface, POP3CMDTYPE cmdtype, DWORD dwPopId)
1046 static const char dele[] = "DELE %lu\r\n";
1047 POP3Transport *This = (POP3Transport *)iface;
1048 char *command;
1049 int len;
1051 TRACE("(%u, %lu)\n", cmdtype, dwPopId);
1053 len = sizeof(dele) + 10 + 2; /* "4294967296" + "\r\n" */
1054 if (!(command = HeapAlloc(GetProcessHeap(), 0, len))) return S_FALSE;
1055 sprintf(command, dele, dwPopId);
1057 init_parser(This, POP3_DELE);
1058 This->type = cmdtype;
1059 InternetTransport_DoCommand(&This->InetTransport, command, POP3Transport_CallbackRecvDELEResp);
1061 HeapFree(GetProcessHeap(), 0, command);
1062 return S_OK;
1065 static HRESULT WINAPI POP3Transport_CommandRETR(
1066 IPOP3Transport *iface, POP3CMDTYPE cmdtype, DWORD dwPopId)
1068 static const char retr[] = "RETR %lu\r\n";
1069 POP3Transport *This = (POP3Transport *)iface;
1070 char *command;
1071 int len;
1073 TRACE("(%u, %lu)\n", cmdtype, dwPopId);
1075 len = sizeof(retr) + 10 + 2; /* "4294967296" + "\r\n" */
1076 if (!(command = HeapAlloc(GetProcessHeap(), 0, len))) return S_FALSE;
1077 sprintf(command, retr, dwPopId);
1079 init_parser(This, POP3_RETR);
1080 This->type = cmdtype;
1081 InternetTransport_DoCommand(&This->InetTransport, command, POP3Transport_CallbackRecvRETRResp);
1083 HeapFree(GetProcessHeap(), 0, command);
1084 return S_OK;
1087 static const IPOP3TransportVtbl POP3TransportVtbl =
1089 POP3Transport_QueryInterface,
1090 POP3Transport_AddRef,
1091 POP3Transport_Release,
1092 POP3Transport_GetServerInfo,
1093 POP3Transport_GetIXPType,
1094 POP3Transport_IsState,
1095 POP3Transport_InetServerFromAccount,
1096 POP3Transport_Connect,
1097 POP3Transport_HandsOffCallback,
1098 POP3Transport_Disconnect,
1099 POP3Transport_DropConnection,
1100 POP3Transport_GetStatus,
1101 POP3Transport_InitNew,
1102 POP3Transport_MarkItem,
1103 POP3Transport_CommandAUTH,
1104 POP3Transport_CommandUSER,
1105 POP3Transport_CommandPASS,
1106 POP3Transport_CommandLIST,
1107 POP3Transport_CommandTOP,
1108 POP3Transport_CommandQUIT,
1109 POP3Transport_CommandSTAT,
1110 POP3Transport_CommandNOOP,
1111 POP3Transport_CommandRSET,
1112 POP3Transport_CommandUIDL,
1113 POP3Transport_CommandDELE,
1114 POP3Transport_CommandRETR
1117 HRESULT WINAPI CreatePOP3Transport(IPOP3Transport **ppTransport)
1119 HRESULT hr;
1120 POP3Transport *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1121 if (!This)
1122 return E_OUTOFMEMORY;
1124 This->InetTransport.u.vtblPOP3 = &POP3TransportVtbl;
1125 This->refs = 0;
1126 hr = InternetTransport_Init(&This->InetTransport);
1127 if (FAILED(hr))
1129 HeapFree(GetProcessHeap(), 0, This);
1130 return hr;
1133 *ppTransport = (IPOP3Transport *)&This->InetTransport.u.vtblPOP3;
1134 IPOP3Transport_AddRef(*ppTransport);
1136 return S_OK;
1139 static HRESULT WINAPI POP3TransportCF_QueryInterface(LPCLASSFACTORY iface,
1140 REFIID riid, LPVOID *ppv)
1142 *ppv = NULL;
1143 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
1145 *ppv = iface;
1146 IClassFactory_AddRef(iface);
1147 return S_OK;
1149 return E_NOINTERFACE;
1152 static ULONG WINAPI POP3TransportCF_AddRef(LPCLASSFACTORY iface)
1154 return 2; /* non-heap based object */
1157 static ULONG WINAPI POP3TransportCF_Release(LPCLASSFACTORY iface)
1159 return 1; /* non-heap based object */
1162 static HRESULT WINAPI POP3TransportCF_CreateInstance(LPCLASSFACTORY iface,
1163 LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
1165 HRESULT hr;
1166 IPOP3Transport *pPop3Transport;
1168 TRACE("(%p, %s, %p)\n", pUnk, debugstr_guid(riid), ppv);
1170 *ppv = NULL;
1172 if (pUnk)
1173 return CLASS_E_NOAGGREGATION;
1175 hr = CreatePOP3Transport(&pPop3Transport);
1176 if (FAILED(hr))
1177 return hr;
1179 hr = IPOP3Transport_QueryInterface(pPop3Transport, riid, ppv);
1180 IPOP3Transport_Release(pPop3Transport);
1182 return hr;
1185 static HRESULT WINAPI POP3TransportCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
1187 FIXME("(%d)\n",fLock);
1188 return S_OK;
1191 static const IClassFactoryVtbl POP3TransportCFVtbl =
1193 POP3TransportCF_QueryInterface,
1194 POP3TransportCF_AddRef,
1195 POP3TransportCF_Release,
1196 POP3TransportCF_CreateInstance,
1197 POP3TransportCF_LockServer
1199 static const IClassFactoryVtbl *POP3TransportCF = &POP3TransportCFVtbl;
1201 HRESULT POP3TransportCF_Create(REFIID riid, LPVOID *ppv)
1203 return IClassFactory_QueryInterface((IClassFactory *)&POP3TransportCF, riid, ppv);