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