Mark our session as deleteable when we exit.
[citadel.git] / citadel / modules / pop3client / serv_pop3client.c
1 /*
2  * Consolidate mail from remote POP3 accounts.
3  *
4  * Copyright (c) 2007-2011 by the citadel.org team
5  *
6  * This program is open source software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as published
8  * by the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <stdio.h>
24
25 #if TIME_WITH_SYS_TIME
26 # include <sys/time.h>
27 # include <time.h>
28 #else
29 # if HAVE_SYS_TIME_H
30 #  include <sys/time.h>
31 # else
32 #  include <time.h>
33 # endif
34 #endif
35
36 #include <ctype.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <libcitadel.h>
42 #include "citadel.h"
43 #include "server.h"
44 #include "citserver.h"
45 #include "support.h"
46 #include "config.h"
47 #include "ctdl_module.h"
48 #include "clientsocket.h"
49 #include "msgbase.h"
50 #include "internet_addressing.h"
51 #include "database.h"
52 #include "citadel_dirs.h"
53 #include "event_client.h"
54
55
56 struct CitContext pop3_client_CC;
57
58 pthread_mutex_t POP3QueueMutex; /* locks the access to the following vars: */
59 HashList *POP3QueueRooms = NULL; /* rss_room_counter */
60 HashList *POP3FetchUrls = NULL; /* -> rss_aggregator; ->RefCount access to be locked too. */
61
62 typedef struct __pop3_room_counter {
63         int count;
64         long QRnumber;
65 }pop3_room_counter;
66
67 typedef enum ePOP3_C_States {
68         ReadGreeting,
69         GetUserState,
70         GetPassState,
71         GetListCommandState,
72         GetListOneLine,
73         GetOneMessageIDState,
74         ReadMessageBodyFollowing,
75         ReadMessageBody,
76         GetDeleteState,
77         ReadQuitState,
78         POP3C_MaxRead
79 }ePOP3_C_States;
80
81
82 typedef struct _FetchItem {
83         long MSGID;
84         long MSGSize;
85         StrBuf *MsgUIDL;
86         StrBuf *MsgUID;
87         int NeedFetch;
88         struct CtdlMessage *Msg;
89 } FetchItem;
90
91 void HfreeFetchItem(void *vItem)
92 {
93         FetchItem *Item = (FetchItem*) vItem;
94         FreeStrBuf(&Item->MsgUIDL);
95         FreeStrBuf(&Item->MsgUID);
96         free(Item);
97 }
98
99 typedef struct __pop3aggr {
100         AsyncIO          IO;
101
102         long n;
103         long RefCount;
104 ///     ParsedURL *Pop3Host;
105         DNSQueryParts HostLookup;
106
107 //      StrBuf          *rooms;
108         long             QRnumber;
109         HashList        *OtherQRnumbers;
110
111         StrBuf          *Url;
112 ///     StrBuf *pop3host; -> URL
113         StrBuf *pop3user;
114         StrBuf *pop3pass;
115         StrBuf *RoomName; // TODO: fill me
116         int keep;
117         time_t interval;
118         ePOP3_C_States State;
119         HashList *MsgNumbers;
120         HashPos *Pos;
121         FetchItem *CurrMsg;
122 } pop3aggr;
123
124 void DeletePOP3Aggregator(void *vptr)
125 {
126         pop3aggr *ptr = vptr;
127         DeleteHashPos(&ptr->Pos);
128         DeleteHash(&ptr->MsgNumbers);
129 //      FreeStrBuf(&ptr->rooms);
130         FreeStrBuf(&ptr->pop3user);
131         FreeStrBuf(&ptr->pop3pass);
132         FreeStrBuf(&ptr->RoomName);
133         FreeURL(&ptr->IO.ConnectMe);
134         FreeStrBuf(&ptr->Url);
135         FreeStrBuf(&ptr->IO.IOBuf);
136         FreeStrBuf(&ptr->IO.SendBuf.Buf);
137         FreeStrBuf(&ptr->IO.RecvBuf.Buf);
138         DeleteAsyncMsg(&ptr->IO.ReadMsg);
139         ((struct CitContext*)ptr->IO.CitContext)->state = CON_IDLE;
140         ((struct CitContext*)ptr->IO.CitContext)->kill_me = 1;
141         FreeAsyncIOContents(&ptr->IO);
142         free(ptr);
143 }
144
145
146 typedef eNextState(*Pop3ClientHandler)(pop3aggr* RecvMsg);
147
148 eNextState POP3_C_Shutdown(AsyncIO *IO);
149 eNextState POP3_C_Timeout(AsyncIO *IO);
150 eNextState POP3_C_ConnFail(AsyncIO *IO);
151 eNextState POP3_C_DNSFail(AsyncIO *IO);
152 eNextState POP3_C_DispatchReadDone(AsyncIO *IO);
153 eNextState POP3_C_DispatchWriteDone(AsyncIO *IO);
154 eNextState POP3_C_Terminate(AsyncIO *IO);
155 eReadState POP3_C_ReadServerStatus(AsyncIO *IO);
156 eNextState POP3_C_ReAttachToFetchMessages(AsyncIO *IO);
157
158 eNextState FinalizePOP3AggrRun(AsyncIO *IO)
159 {
160         HashPos  *It;
161         pop3aggr *cptr = (pop3aggr *)IO->Data;
162
163         syslog(LOG_DEBUG, "Terminating Aggregator; bye.\n");
164
165         It = GetNewHashPos(POP3FetchUrls, 0);
166         pthread_mutex_lock(&POP3QueueMutex);
167         {
168                 GetHashPosFromKey(POP3FetchUrls, SKEY(cptr->Url), It);
169                 DeleteEntryFromHash(POP3FetchUrls, It);
170         }
171         pthread_mutex_unlock(&POP3QueueMutex);
172         DeleteHashPos(&It);
173         return eAbort;
174 }
175
176 eNextState FailAggregationRun(AsyncIO *IO)
177 {
178         return eAbort;
179 }
180
181
182 #define POP3C_DBG_SEND() syslog(LOG_DEBUG, "POP3 client[%ld]: > %s\n", RecvMsg->n, ChrPtr(RecvMsg->IO.SendBuf.Buf))
183 #define POP3C_DBG_READ() syslog(LOG_DEBUG, "POP3 client[%ld]: < %s\n", RecvMsg->n, ChrPtr(RecvMsg->IO.IOBuf))
184 #define POP3C_OK (strncasecmp(ChrPtr(RecvMsg->IO.IOBuf), "+OK", 3) == 0)
185
186 eNextState POP3C_ReadGreeting(pop3aggr *RecvMsg)
187 {
188         POP3C_DBG_READ();
189         /* Read the server greeting */
190         if (!POP3C_OK) return eTerminateConnection;
191         else return eSendReply;
192 }
193
194 eNextState POP3C_SendUser(pop3aggr *RecvMsg)
195 {
196         /* Identify ourselves.  NOTE: we have to append a CR to each command.  The LF will
197          * automatically be appended by sock_puts().  Believe it or not, leaving out the CR
198          * will cause problems if the server happens to be Exchange, which is so b0rken it
199          * actually barfs on LF-terminated newlines.
200          */
201         StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
202                      "USER %s\r\n", ChrPtr(RecvMsg->pop3user));
203         POP3C_DBG_SEND();
204         return eReadMessage;
205 }
206
207 eNextState POP3C_GetUserState(pop3aggr *RecvMsg)
208 {
209         POP3C_DBG_READ();
210         if (!POP3C_OK) return eTerminateConnection;
211         else return eSendReply;
212 }
213
214 eNextState POP3C_SendPassword(pop3aggr *RecvMsg)
215 {
216         /* Password */
217         StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
218                      "PASS %s\r\n", ChrPtr(RecvMsg->pop3pass));
219         syslog(LOG_DEBUG, "<PASS <password>\n");
220 //      POP3C_DBG_SEND();
221         return eReadMessage;
222 }
223
224 eNextState POP3C_GetPassState(pop3aggr *RecvMsg)
225 {
226         POP3C_DBG_READ();
227         if (!POP3C_OK) return eTerminateConnection;
228         else return eSendReply;
229 }
230
231 eNextState POP3C_SendListCommand(pop3aggr *RecvMsg)
232 {
233         /* Get the list of messages */
234         StrBufPlain(RecvMsg->IO.SendBuf.Buf, HKEY("LIST\r\n"));
235         POP3C_DBG_SEND();
236         return eReadMessage;
237 }
238
239 eNextState POP3C_GetListCommandState(pop3aggr *RecvMsg)
240 {
241         POP3C_DBG_READ();
242         if (!POP3C_OK) return eTerminateConnection;
243         RecvMsg->MsgNumbers = NewHash(1, NULL);
244         RecvMsg->State++;       
245         return eReadMore;
246 }
247
248
249 eNextState POP3C_GetListOneLine(pop3aggr *RecvMsg)
250 {
251 #if 0
252         int rc;
253 #endif
254         const char *pch;
255         FetchItem *OneMsg = NULL;
256         POP3C_DBG_READ();
257
258         if ((StrLength(RecvMsg->IO.IOBuf) == 1) && 
259             (ChrPtr(RecvMsg->IO.IOBuf)[0] == '.'))
260         {
261                 if (GetCount(RecvMsg->MsgNumbers) == 0)
262                 {
263                         ////    RecvMsg->Sate = ReadQuitState;
264                 }
265                 else
266                 {
267                         RecvMsg->Pos = GetNewHashPos(RecvMsg->MsgNumbers, 0);
268                 }
269                 return eSendReply;
270
271         }
272         OneMsg = (FetchItem*) malloc(sizeof(FetchItem));
273         memset(OneMsg, 0, sizeof(FetchItem));
274         OneMsg->MSGID = atol(ChrPtr(RecvMsg->IO.IOBuf));
275
276         pch = strchr(ChrPtr(RecvMsg->IO.IOBuf), ' ');
277         if (pch != NULL)
278         {
279                 OneMsg->MSGSize = atol(pch + 1);
280         }
281 #if 0
282         rc = TestValidateHash(RecvMsg->MsgNumbers);
283         if (rc != 0) 
284                 syslog(LOG_DEBUG, "Hash Invalid: %d\n", rc);
285 #endif
286                 
287         Put(RecvMsg->MsgNumbers, LKEY(OneMsg->MSGID), OneMsg, HfreeFetchItem);
288 #if 0
289         rc = TestValidateHash(RecvMsg->MsgNumbers);
290         if (rc != 0) 
291                 syslog(LOG_DEBUG, "Hash Invalid: %d\n", rc);
292 #endif
293         //RecvMsg->State --; /* read next Line */
294         return eReadMore;
295 }
296
297 eNextState POP3_FetchNetworkUsetableEntry(AsyncIO *IO)
298 {
299         long HKLen;
300         const char *HKey;
301         void *vData;
302         struct cdbdata *cdbut;
303         pop3aggr *RecvMsg = (pop3aggr *) IO->Data;
304
305         if(GetNextHashPos(RecvMsg->MsgNumbers, RecvMsg->Pos, &HKLen, &HKey, &vData))
306         {
307                 struct UseTable ut;
308                 if (server_shutting_down)
309                         return eAbort;
310                         
311                 RecvMsg->CurrMsg = (FetchItem*) vData;
312                 syslog(LOG_DEBUG, "CHECKING: whether %s has already been seen: ", ChrPtr(RecvMsg->CurrMsg->MsgUID));
313                 /* Find out if we've already seen this item */
314                 safestrncpy(ut.ut_msgid, 
315                             ChrPtr(RecvMsg->CurrMsg->MsgUID),
316                             sizeof(ut.ut_msgid));
317                 ut.ut_timestamp = time(NULL);/// TODO: libev timestamp!
318                 
319                 cdbut = cdb_fetch(CDB_USETABLE, SKEY(RecvMsg->CurrMsg->MsgUID));
320                 if (cdbut != NULL) {
321                         /* Item has already been seen */
322                         syslog(LOG_DEBUG, "YES\n");
323                         cdb_free(cdbut);
324                 
325                         /* rewrite the record anyway, to update the timestamp */
326                         cdb_store(CDB_USETABLE, 
327                                   SKEY(RecvMsg->CurrMsg->MsgUID), 
328                                   &ut, sizeof(struct UseTable) );
329                         RecvMsg->CurrMsg->NeedFetch = 0; ////TODO0;
330                 }
331                 else
332                 {
333                         syslog(LOG_DEBUG, "NO\n");
334                         RecvMsg->CurrMsg->NeedFetch = 1;
335                 }
336                 return NextDBOperation(&RecvMsg->IO, POP3_FetchNetworkUsetableEntry);
337         }
338         else
339         {
340                 /* ok, now we know them all, continue with reading the actual messages. */
341                 DeleteHashPos(&RecvMsg->Pos);
342
343                 return QueueEventContext(IO, POP3_C_ReAttachToFetchMessages);
344         }
345 }
346
347 eNextState POP3C_GetOneMessagID(pop3aggr *RecvMsg)
348 {
349         long HKLen;
350         const char *HKey;
351         void *vData;
352
353 #if 0
354         int rc;
355         rc = TestValidateHash(RecvMsg->MsgNumbers);
356         if (rc != 0) 
357                 syslog(LOG_DEBUG, "Hash Invalid: %d\n", rc);
358 #endif
359         if(GetNextHashPos(RecvMsg->MsgNumbers, RecvMsg->Pos, &HKLen, &HKey, &vData))
360         {
361                 RecvMsg->CurrMsg = (FetchItem*) vData;
362                 /* Find out the UIDL of the message, to determine whether we've already downloaded it */
363                 StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
364                              "UIDL %ld\r\n", RecvMsg->CurrMsg->MSGID);
365                 POP3C_DBG_SEND();
366         }
367         else
368         {
369             RecvMsg->State++;
370                 DeleteHashPos(&RecvMsg->Pos);
371                 /// done receiving uidls.. start looking them up now.
372                 RecvMsg->Pos = GetNewHashPos(RecvMsg->MsgNumbers, 0);
373                 return QueueDBOperation(&RecvMsg->IO, POP3_FetchNetworkUsetableEntry);
374         }
375         return eReadMore; /* TODO */
376 }
377
378 eNextState POP3C_GetOneMessageIDState(pop3aggr *RecvMsg)
379 {
380 #if 0
381         int rc;
382         rc = TestValidateHash(RecvMsg->MsgNumbers);
383         if (rc != 0) 
384                 syslog(LOG_DEBUG, "Hash Invalid: %d\n", rc);
385 #endif
386
387         POP3C_DBG_READ();
388         if (!POP3C_OK) return eTerminateConnection;
389         RecvMsg->CurrMsg->MsgUIDL = NewStrBufPlain(NULL, StrLength(RecvMsg->IO.IOBuf));
390         RecvMsg->CurrMsg->MsgUID = NewStrBufPlain(NULL, StrLength(RecvMsg->IO.IOBuf) * 2);
391
392         StrBufExtract_token(RecvMsg->CurrMsg->MsgUIDL, RecvMsg->IO.IOBuf, 2, ' ');
393         StrBufPrintf(RecvMsg->CurrMsg->MsgUID, 
394                      "pop3/%s/%s:%s@%s", 
395                      ChrPtr(RecvMsg->RoomName), 
396                      ChrPtr(RecvMsg->CurrMsg->MsgUIDL),
397                      RecvMsg->IO.ConnectMe->User,
398                      RecvMsg->IO.ConnectMe->Host);
399         RecvMsg->State --;
400         return eSendReply;
401 }
402
403
404 eNextState POP3C_SendGetOneMsg(pop3aggr *RecvMsg)
405 {
406         long HKLen;
407         const char *HKey;
408         void *vData;
409
410         RecvMsg->CurrMsg = NULL;
411         while (GetNextHashPos(RecvMsg->MsgNumbers, RecvMsg->Pos, &HKLen, &HKey, &vData) && 
412                (RecvMsg->CurrMsg = (FetchItem*) vData, RecvMsg->CurrMsg->NeedFetch == 0))
413         {}
414
415         if ((RecvMsg->CurrMsg != NULL ) && (RecvMsg->CurrMsg->NeedFetch == 1))
416         {
417                 /* Message has not been seen. Tell the server to fetch the message... */
418                 StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
419                              "RETR %ld\r\n", RecvMsg->CurrMsg->MSGID);
420                 POP3C_DBG_SEND();
421                 return eReadMessage;
422         }
423         else {
424                 RecvMsg->State = ReadQuitState;
425                 return POP3_C_DispatchWriteDone(&RecvMsg->IO);
426         }
427 }
428
429
430 eNextState POP3C_ReadMessageBodyFollowing(pop3aggr *RecvMsg)
431 {
432         POP3C_DBG_READ();
433         if (!POP3C_OK) return eTerminateConnection;
434         RecvMsg->IO.ReadMsg = NewAsyncMsg(HKEY("."), 
435                                           RecvMsg->CurrMsg->MSGSize,
436                                           config.c_maxmsglen, 
437                                           NULL, -1,
438                                           1);
439
440         return eReadPayload;
441 }       
442
443
444 eNextState POP3C_StoreMsgRead(AsyncIO *IO)
445 {
446         pop3aggr *RecvMsg = (pop3aggr *) IO->Data;
447         struct UseTable ut;
448
449         syslog(LOG_DEBUG, "MARKING: %s as seen: ", ChrPtr(RecvMsg->CurrMsg->MsgUID));
450
451         safestrncpy(ut.ut_msgid, 
452                     ChrPtr(RecvMsg->CurrMsg->MsgUID),
453                     sizeof(ut.ut_msgid));
454         ut.ut_timestamp = time(NULL); /* TODO: use libev time */
455         cdb_store(CDB_USETABLE, 
456                   ChrPtr(RecvMsg->CurrMsg->MsgUID), 
457                   StrLength(RecvMsg->CurrMsg->MsgUID),
458                   &ut, 
459                   sizeof(struct UseTable) );
460
461         return QueueEventContext(&RecvMsg->IO, POP3_C_ReAttachToFetchMessages);
462 }
463 eNextState POP3C_SaveMsg(AsyncIO *IO)
464 {
465         long msgnum;
466         pop3aggr *RecvMsg = (pop3aggr *) IO->Data;
467
468         /* Do Something With It (tm) */
469         msgnum = CtdlSubmitMsg(RecvMsg->CurrMsg->Msg, 
470                                NULL, 
471                                ChrPtr(RecvMsg->RoomName), 
472                                0);
473         if (msgnum > 0L) {
474                 /* Message has been committed to the store */
475                 /* write the uidl to the use table so we don't fetch this message again */
476         }
477         CtdlFreeMessage(RecvMsg->CurrMsg->Msg);
478
479         return NextDBOperation(&RecvMsg->IO, POP3C_StoreMsgRead);
480 }
481
482 eNextState POP3C_ReadMessageBody(pop3aggr *RecvMsg)
483 {
484         syslog(LOG_DEBUG, "Converting message...\n");
485         RecvMsg->CurrMsg->Msg = convert_internet_message_buf(&RecvMsg->IO.ReadMsg->MsgBuf);
486
487         return QueueDBOperation(&RecvMsg->IO, POP3C_SaveMsg);
488 }
489
490 eNextState POP3C_SendDelete(pop3aggr *RecvMsg)
491 {
492         if (!RecvMsg->keep) {
493                 StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
494                              "DELE %ld\r\n", RecvMsg->CurrMsg->MSGID);
495                 POP3C_DBG_SEND();
496                 return eReadMessage;
497         }
498         else {
499                 RecvMsg->State = ReadMessageBodyFollowing;
500                 return POP3_C_DispatchWriteDone(&RecvMsg->IO);
501         }
502 }
503 eNextState POP3C_ReadDeleteState(pop3aggr *RecvMsg)
504 {
505         POP3C_DBG_READ();
506         RecvMsg->State = GetOneMessageIDState;
507         return eReadMessage;
508 }
509
510 eNextState POP3C_SendQuit(pop3aggr *RecvMsg)
511 {
512         /* Log out */
513         StrBufPlain(RecvMsg->IO.SendBuf.Buf,
514                     HKEY("QUIT\r\n3)"));
515         POP3C_DBG_SEND();
516         return eReadMessage;
517 }
518
519
520 eNextState POP3C_ReadQuitState(pop3aggr *RecvMsg)
521 {
522         POP3C_DBG_READ();
523         return eTerminateConnection;
524 }
525
526 const long POP3_C_ConnTimeout = 1000;
527 const long DefaultPOP3Port = 110;
528
529 Pop3ClientHandler POP3C_ReadHandlers[] = {
530         POP3C_ReadGreeting,
531         POP3C_GetUserState,
532         POP3C_GetPassState,
533         POP3C_GetListCommandState,
534         POP3C_GetListOneLine,
535         POP3C_GetOneMessageIDState,
536         POP3C_ReadMessageBodyFollowing,
537         POP3C_ReadMessageBody,
538         POP3C_ReadDeleteState,
539         POP3C_ReadQuitState,
540 };
541
542 const long POP3_C_SendTimeouts[POP3C_MaxRead] = {
543         100,
544         100,
545         100,
546         100,
547         100,
548         100,
549         100,
550         100
551 };
552 const ConstStr POP3C_ReadErrors[POP3C_MaxRead] = {
553         {HKEY("Connection broken during ")},
554         {HKEY("Connection broken during ")},
555         {HKEY("Connection broken during ")},
556         {HKEY("Connection broken during ")},
557         {HKEY("Connection broken during ")},
558         {HKEY("Connection broken during ")},
559         {HKEY("Connection broken during ")},
560         {HKEY("Connection broken during ")}
561 };
562
563 Pop3ClientHandler POP3C_SendHandlers[] = {
564         NULL, /* we don't send a greeting */
565         POP3C_SendUser,
566         POP3C_SendPassword,
567         POP3C_SendListCommand,
568         NULL,
569         POP3C_GetOneMessagID,
570         POP3C_SendGetOneMsg,
571         NULL,
572         POP3C_SendDelete,
573         POP3C_SendQuit
574 };
575
576 const long POP3_C_ReadTimeouts[] = {
577         100,
578         100,
579         100,
580         100,
581         100,
582         100,
583         100,
584         100,
585         100,
586         100
587 };
588 /*****************************************************************************/
589 /*                     POP3 CLIENT DISPATCHER                                */
590 /*****************************************************************************/
591
592 void POP3SetTimeout(eNextState NextTCPState, pop3aggr *pMsg)
593 {
594         double Timeout = 0.0;
595
596         syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
597
598         switch (NextTCPState) {
599         case eSendReply:
600         case eSendMore:
601                 Timeout = POP3_C_SendTimeouts[pMsg->State];
602 /*
603   if (pMsg->State == eDATABody) {
604   / * if we're sending a huge message, we need more time. * /
605   Timeout += StrLength(pMsg->msgtext) / 1024;
606   }
607 */
608                 break;
609         case eReadMessage:
610                 Timeout = POP3_C_ReadTimeouts[pMsg->State];
611 /*
612   if (pMsg->State == eDATATerminateBody) {
613   / * 
614   * some mailservers take a nap before accepting the message
615   * content inspection and such.
616   * /
617   Timeout += StrLength(pMsg->msgtext) / 1024;
618   }
619 */
620                 break;
621         case eReadPayload:
622                 Timeout = 100000;
623                 /* TODO!!! */
624                 break;
625         case eSendDNSQuery:
626         case eReadDNSReply:
627         case eConnect:
628         case eTerminateConnection:
629         case eDBQuery:
630         case eAbort:
631         case eReadMore://// TODO
632                 return;
633         }
634         SetNextTimeout(&pMsg->IO, Timeout);
635 }
636 eNextState POP3_C_DispatchReadDone(AsyncIO *IO)
637 {
638         syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
639         pop3aggr *pMsg = IO->Data;
640         eNextState rc;
641
642         rc = POP3C_ReadHandlers[pMsg->State](pMsg);
643         if (rc != eReadMore)
644             pMsg->State++;
645         POP3SetTimeout(rc, pMsg);
646         return rc;
647 }
648 eNextState POP3_C_DispatchWriteDone(AsyncIO *IO)
649 {
650         syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
651         pop3aggr *pMsg = IO->Data;
652         eNextState rc;
653
654         rc = POP3C_SendHandlers[pMsg->State](pMsg);
655         POP3SetTimeout(rc, pMsg);
656         return rc;
657 }
658
659
660 /*****************************************************************************/
661 /*                     POP3 CLIENT ERROR CATCHERS                            */
662 /*****************************************************************************/
663 eNextState POP3_C_Terminate(AsyncIO *IO)
664 {
665 ///     pop3aggr *pMsg = (pop3aggr *)IO->Data;
666
667         syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
668         FinalizePOP3AggrRun(IO);
669         return eAbort;
670 }
671 eNextState POP3_C_Timeout(AsyncIO *IO)
672 {
673         pop3aggr *pMsg = IO->Data;
674
675         syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
676         StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State]));
677         return FailAggregationRun(IO);
678 }
679 eNextState POP3_C_ConnFail(AsyncIO *IO)
680 {
681         pop3aggr *pMsg = (pop3aggr *)IO->Data;
682
683         syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
684         StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State]));
685         return FailAggregationRun(IO);
686 }
687 eNextState POP3_C_DNSFail(AsyncIO *IO)
688 {
689         pop3aggr *pMsg = (pop3aggr *)IO->Data;
690
691         syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
692         StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State]));
693         return FailAggregationRun(IO);
694 }
695 eNextState POP3_C_Shutdown(AsyncIO *IO)
696 {
697         syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
698 ////    pop3aggr *pMsg = IO->Data;
699
700         ////pMsg->MyQEntry->Status = 3;
701         ///StrBufPlain(pMsg->MyQEntry->StatusMessage, HKEY("server shutdown during message retrieval."));
702         FinalizePOP3AggrRun(IO);
703         return eAbort;
704 }
705
706
707 /**
708  * @brief lineread Handler; understands when to read more POP3 lines, and when this is a one-lined reply.
709  */
710 eReadState POP3_C_ReadServerStatus(AsyncIO *IO)
711 {
712         eReadState Finished = eBufferNotEmpty; 
713
714         switch (IO->NextState) {
715         case eSendDNSQuery:
716         case eReadDNSReply:
717         case eDBQuery:
718         case eConnect:
719         case eTerminateConnection:
720         case eAbort:
721                 Finished = eReadFail;
722                 break;
723         case eSendReply: 
724         case eSendMore:
725         case eReadMore:
726         case eReadMessage: 
727                 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
728                 break;
729         case eReadPayload:
730                 Finished = CtdlReadMessageBodyAsync(IO);
731                 break;
732         }
733         return Finished;
734 }
735
736 /*****************************************************************************
737  * So we connect our Server IP here.                                         *
738  *****************************************************************************/
739 eNextState POP3_C_ReAttachToFetchMessages(AsyncIO *IO)
740 {
741         pop3aggr *cpptr = IO->Data;
742
743         syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
744 ////??? cpptr->State ++;
745         if (cpptr->Pos == NULL)
746                 cpptr->Pos = GetNewHashPos(cpptr->MsgNumbers, 0);
747
748         POP3_C_DispatchWriteDone(IO);
749         ReAttachIO(IO, cpptr, 0);
750         IO->NextState = eReadMessage;
751         return IO->NextState;
752 }
753
754 eNextState pop3_connect_ip(AsyncIO *IO)
755 {
756         pop3aggr *cpptr = IO->Data;
757
758         syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
759         
760 ////    IO->ConnectMe = &cpptr->Pop3Host;
761         /*  Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
762
763         /////// SetConnectStatus(IO);
764
765         return InitEventIO(IO, cpptr, 
766                            POP3_C_ConnTimeout, 
767                            POP3_C_ReadTimeouts[0],
768                            1);
769 }
770
771 eNextState pop3_get_one_host_ip_done(AsyncIO *IO)
772 {
773         pop3aggr *cpptr = IO->Data;
774         struct hostent *hostent;
775
776         QueryCbDone(IO);
777
778         hostent = cpptr->HostLookup.VParsedDNSReply;
779         if ((cpptr->HostLookup.DNSStatus == ARES_SUCCESS) && 
780             (hostent != NULL) ) {
781                 memset(&cpptr->IO.ConnectMe->Addr, 0, sizeof(struct in6_addr));
782                 if (cpptr->IO.ConnectMe->IPv6) {
783                         memcpy(&cpptr->IO.ConnectMe->Addr.sin6_addr.s6_addr, 
784                                &hostent->h_addr_list[0],
785                                sizeof(struct in6_addr));
786                         
787                         cpptr->IO.ConnectMe->Addr.sin6_family = hostent->h_addrtype;
788                         cpptr->IO.ConnectMe->Addr.sin6_port   = htons(DefaultPOP3Port);
789                 }
790                 else {
791                         struct sockaddr_in *addr = (struct sockaddr_in*) &cpptr->IO.ConnectMe->Addr;
792                         /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
793 //                      addr->sin_addr.s_addr = htonl((uint32_t)&hostent->h_addr_list[0]);
794                         memcpy(&addr->sin_addr.s_addr, 
795                                hostent->h_addr_list[0], 
796                                sizeof(uint32_t));
797                         
798                         addr->sin_family = hostent->h_addrtype;
799                         addr->sin_port   = htons(DefaultPOP3Port);
800                         
801                 }
802                 return pop3_connect_ip(IO);
803         }
804         else
805                 return eAbort;
806 }
807
808 eNextState pop3_get_one_host_ip(AsyncIO *IO)
809 {
810         pop3aggr *cpptr = IO->Data;
811         /* 
812          * here we start with the lookup of one host. it might be...
813          * - the relay host *sigh*
814          * - the direct hostname if there was no mx record
815          * - one of the mx'es
816          */ 
817
818         InitC_ares_dns(IO);
819
820         syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
821
822         syslog(LOG_DEBUG, 
823                       "POP3 client[%ld]: looking up %s-Record %s : %d ...\n", 
824                       cpptr->n, 
825                       (cpptr->IO.ConnectMe->IPv6)? "aaaa": "a",
826                       cpptr->IO.ConnectMe->Host, 
827                       cpptr->IO.ConnectMe->Port);
828
829         QueueQuery((cpptr->IO.ConnectMe->IPv6)? ns_t_aaaa : ns_t_a, 
830                    cpptr->IO.ConnectMe->Host, 
831                    &cpptr->IO, 
832                    &cpptr->HostLookup, 
833                    pop3_get_one_host_ip_done);
834         IO->NextState = eReadDNSReply;
835         return IO->NextState;
836 }
837
838
839
840 int pop3_do_fetching(pop3aggr *cpptr)
841 {
842         CitContext *SubC;
843
844         cpptr->IO.Data          = cpptr;
845
846         cpptr->IO.SendDone      = POP3_C_DispatchWriteDone;
847         cpptr->IO.ReadDone      = POP3_C_DispatchReadDone;
848         cpptr->IO.Terminate     = POP3_C_Terminate;
849         cpptr->IO.LineReader    = POP3_C_ReadServerStatus;
850         cpptr->IO.ConnFail      = POP3_C_ConnFail;
851         cpptr->IO.DNSFail       = POP3_C_DNSFail;
852         cpptr->IO.Timeout       = POP3_C_Timeout;
853         cpptr->IO.ShutdownAbort = POP3_C_Shutdown;
854         
855         cpptr->IO.SendBuf.Buf   = NewStrBufPlain(NULL, 1024);
856         cpptr->IO.RecvBuf.Buf   = NewStrBufPlain(NULL, 1024);
857         cpptr->IO.IOBuf         = NewStrBuf();
858         
859         cpptr->IO.NextState     = eReadMessage;
860 /* TODO
861    syslog(LOG_DEBUG, "POP3: %s %s %s <password>\n", roomname, pop3host, pop3user);
862    syslog(LOG_DEBUG, "Connecting to <%s>\n", pop3host);
863 */
864         
865         SubC = CloneContext (&pop3_client_CC);
866         SubC->session_specific_data = (char*) cpptr;
867         cpptr->IO.CitContext = SubC;
868
869         if (cpptr->IO.ConnectMe->IsIP) {
870                 QueueEventContext(&cpptr->IO,
871                                   pop3_connect_ip);
872         }
873         else { /* uneducated admin has chosen to add DNS to the equation... */
874                 QueueEventContext(&cpptr->IO,
875                                   pop3_get_one_host_ip);
876         }
877         return 1;
878 }
879
880 /*
881  * Scan a room's netconfig to determine whether it requires POP3 aggregation
882  */
883 void pop3client_scan_room(struct ctdlroom *qrbuf, void *data)
884 {
885         StrBuf *CfgData;
886         StrBuf *CfgType;
887         StrBuf *Line;
888
889         struct stat statbuf;
890         char filename[PATH_MAX];
891         int  fd;
892         int Done;
893         void *vptr;
894         const char *CfgPtr, *lPtr;
895         const char *Err;
896
897 //      pop3_room_counter *Count = NULL;
898 //      pop3aggr *cpptr;
899
900         pthread_mutex_lock(&POP3QueueMutex);
901         if (GetHash(POP3QueueRooms, LKEY(qrbuf->QRnumber), &vptr))
902         {
903                 syslog(LOG_DEBUG, 
904                               "pop3client: [%ld] %s already in progress.\n", 
905                               qrbuf->QRnumber, 
906                               qrbuf->QRname);
907                 pthread_mutex_unlock(&POP3QueueMutex);
908         }
909         pthread_mutex_unlock(&POP3QueueMutex);
910
911         if (server_shutting_down) return;
912
913         assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
914
915         if (server_shutting_down)
916                 return;
917                 
918         /* Only do net processing for rooms that have netconfigs */
919         fd = open(filename, 0);
920         if (fd <= 0) {
921                 //syslog(LOG_DEBUG, "rssclient: %s no config.\n", qrbuf->QRname);
922                 return;
923         }
924         if (server_shutting_down)
925                 return;
926         if (fstat(fd, &statbuf) == -1) {
927                 syslog(LOG_DEBUG, "ERROR: could not stat configfile '%s' - %s\n",
928                               filename, strerror(errno));
929                 return;
930         }
931         if (server_shutting_down)
932                 return;
933         CfgData = NewStrBufPlain(NULL, statbuf.st_size + 1);
934         if (StrBufReadBLOB(CfgData, &fd, 1, statbuf.st_size, &Err) < 0) {
935                 close(fd);
936                 FreeStrBuf(&CfgData);
937                 syslog(LOG_DEBUG, "ERROR: reading config '%s' - %s<br>\n",
938                               filename, strerror(errno));
939                 return;
940         }
941         close(fd);
942         if (server_shutting_down)
943                 return;
944         
945         CfgPtr = NULL;
946         CfgType = NewStrBuf();
947         Line = NewStrBufPlain(NULL, StrLength(CfgData));
948         Done = 0;
949
950         while (!Done)
951         {
952                 Done = StrBufSipLine(Line, CfgData, &CfgPtr) == 0;
953                 if (StrLength(Line) > 0)
954                 {
955                         lPtr = NULL;
956                         StrBufExtract_NextToken(CfgType, Line, &lPtr, '|');
957                         if (!strcasecmp("pop3client", ChrPtr(CfgType)))
958                         {
959                                 pop3aggr *cptr;
960                                 StrBuf *Tmp;
961 /*
962                                 if (Count == NULL)
963                                 {
964                                         Count = malloc(sizeof(pop3_room_counter));
965                                         Count->count = 0;
966                                 }
967                                 Count->count ++;
968 */
969                                 cptr = (pop3aggr *) malloc(sizeof(pop3aggr));
970                                 memset(cptr, 0, sizeof(pop3aggr));
971                                 /// TODO do we need this? cptr->roomlist_parts = 1;
972                                 cptr->RoomName = NewStrBufPlain(qrbuf->QRname, -1);
973                                 cptr->pop3user = NewStrBufPlain(NULL, StrLength(Line));
974                                 cptr->pop3pass = NewStrBufPlain(NULL, StrLength(Line));
975                                 cptr->Url = NewStrBuf();
976                                 Tmp = NewStrBuf();
977
978                                 StrBufExtract_NextToken(Tmp, Line, &lPtr, '|');
979                                 StrBufExtract_NextToken(cptr->pop3user, Line, &lPtr, '|');
980                                 StrBufExtract_NextToken(cptr->pop3pass, Line, &lPtr, '|');
981                                 cptr->keep = StrBufExtractNext_long(Line, &lPtr, '|');
982                                 cptr->interval = StrBufExtractNext_long(Line, &lPtr, '|');
983                     
984                                 StrBufPrintf(cptr->Url, "pop3://%s:%s@%s/%s", 
985                                              ChrPtr(cptr->pop3user),
986                                              ChrPtr(cptr->pop3pass), 
987                                              ChrPtr(Tmp), 
988                                              ChrPtr(cptr->RoomName));
989                                 FreeStrBuf(&Tmp);
990                                 ParseURL(&cptr->IO.ConnectMe, cptr->Url, 110);
991
992
993 #if 0 
994 /* todo: we need to reunite the url to be shure. */
995                                 
996                                 pthread_mutex_lock(&POP3ueueMutex);
997                                 GetHash(POP3FetchUrls, SKEY(ptr->Url), &vptr);
998                                 use_this_cptr = (pop3aggr *)vptr;
999                                 
1000                                 if (use_this_rncptr != NULL)
1001                                 {
1002                                         /* mustn't attach to an active session */
1003                                         if (use_this_cptr->RefCount > 0)
1004                                         {
1005                                                 DeletePOP3Cfg(cptr);
1006 ///                                             Count->count--;
1007                                         }
1008                                         else 
1009                                         {
1010                                                 long *QRnumber;
1011                                                 StrBufAppendBufPlain(use_this_cptr->rooms, 
1012                                                                      qrbuf->QRname, 
1013                                                                      -1, 0);
1014                                                 if (use_this_cptr->roomlist_parts == 1)
1015                                                 {
1016                                                         use_this_cptr->OtherQRnumbers = NewHash(1, lFlathash);
1017                                                 }
1018                                                 QRnumber = (long*)malloc(sizeof(long));
1019                                                 *QRnumber = qrbuf->QRnumber;
1020                                                 Put(use_this_cptr->OtherQRnumbers, LKEY(qrbuf->QRnumber), QRnumber, NULL);
1021                                                 use_this_cptr->roomlist_parts++;
1022                                         }
1023                                         pthread_mutex_unlock(&POP3QueueMutex);
1024                                         continue;
1025                                 }
1026                                 pthread_mutex_unlock(&RSSQueueMutex);
1027 #endif
1028
1029                                 pthread_mutex_lock(&POP3QueueMutex);
1030                                 Put(POP3FetchUrls, SKEY(cptr->Url), cptr, DeletePOP3Aggregator);
1031                                 pthread_mutex_unlock(&POP3QueueMutex);
1032
1033                         }
1034
1035                 }
1036
1037                 ///fclose(fp);
1038
1039         }
1040         FreeStrBuf(&Line);
1041         FreeStrBuf(&CfgType);
1042         FreeStrBuf(&CfgData);
1043 }
1044
1045
1046 void pop3client_scan(void) {
1047         static time_t last_run = 0L;
1048         static int doing_pop3client = 0;
1049 ///     struct pop3aggr *pptr;
1050         time_t fastest_scan;
1051         HashPos *it;
1052         long len;
1053         const char *Key;
1054         void *vrptr;
1055         pop3aggr *cptr;
1056
1057         if (config.c_pop3_fastest < config.c_pop3_fetch)
1058                 fastest_scan = config.c_pop3_fastest;
1059         else
1060                 fastest_scan = config.c_pop3_fetch;
1061
1062         /*
1063          * Run POP3 aggregation no more frequently than once every n seconds
1064          */
1065         if ( (time(NULL) - last_run) < fastest_scan ) {
1066                 return;
1067         }
1068
1069         /*
1070          * This is a simple concurrency check to make sure only one pop3client run
1071          * is done at a time.  We could do this with a mutex, but since we
1072          * don't really require extremely fine granularity here, we'll do it
1073          * with a static variable instead.
1074          */
1075         if (doing_pop3client) return;
1076         doing_pop3client = 1;
1077
1078         syslog(LOG_DEBUG, "pop3client started");
1079         CtdlForEachRoom(pop3client_scan_room, NULL);
1080
1081         pthread_mutex_lock(&POP3QueueMutex);
1082         it = GetNewHashPos(POP3FetchUrls, 0);
1083         while (!server_shutting_down && 
1084                GetNextHashPos(POP3FetchUrls, it, &len, &Key, &vrptr) && 
1085                (vrptr != NULL)) {
1086                 cptr = (pop3aggr *)vrptr;
1087                 if (cptr->RefCount == 0) 
1088                         if (!pop3_do_fetching(cptr))
1089                                 DeletePOP3Aggregator(cptr);////TODO
1090
1091 /*
1092                 if ((palist->interval && time(NULL) > (last_run + palist->interval))
1093                         || (time(NULL) > last_run + config.c_pop3_fetch))
1094                                 pop3_do_fetching(palist->roomname, palist->pop3host,
1095                                         palist->pop3user, palist->pop3pass, palist->keep);
1096                 pptr = palist;
1097                 palist = palist->next;
1098                 free(pptr);
1099 */
1100         }
1101         DeleteHashPos(&it);
1102         pthread_mutex_unlock(&POP3QueueMutex);
1103
1104         syslog(LOG_DEBUG, "pop3client ended");
1105         last_run = time(NULL);
1106         doing_pop3client = 0;
1107 }
1108
1109
1110 void pop3_cleanup(void)
1111 {
1112         /* citthread_mutex_destroy(&POP3QueueMutex); TODO */
1113 //      DeleteHash(&POP3FetchUrls);
1114         DeleteHash(&POP3QueueRooms);
1115 }
1116
1117 CTDL_MODULE_INIT(pop3client)
1118 {
1119         if (!threading)
1120         {
1121                 CtdlFillSystemContext(&pop3_client_CC, "POP3aggr");
1122                 pthread_mutex_init(&POP3QueueMutex, NULL);
1123                 POP3QueueRooms = NewHash(1, lFlathash);
1124                 POP3FetchUrls = NewHash(1, NULL);
1125                 CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER);
1126                 CtdlRegisterCleanupHook(pop3_cleanup);
1127         }
1128
1129         /* return our module id for the log */
1130         return "pop3client";
1131 }