2 * Consolidate mail from remote POP3 accounts.
4 * Copyright (c) 2007-2017 by the citadel.org team
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.
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.
20 #include <sysconfig.h>
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
27 # include <sys/time.h>
36 #include <sys/types.h>
38 #include <libcitadel.h>
41 #include "citserver.h"
44 #include "ctdl_module.h"
45 #include "clientsocket.h"
47 #include "internet_addressing.h"
49 #include "citadel_dirs.h"
50 #include "event_client.h"
53 #define POP3C_OK (strncasecmp(ChrPtr(RecvMsg->IO.IOBuf), "+OK", 3) == 0)
56 #define N ((pop3aggr*)IO->Data)->n
58 struct CitContext pop3_client_CC;
60 pthread_mutex_t POP3QueueMutex; /* locks the access to the following vars: */
61 HashList *POP3QueueRooms = NULL;
62 HashList *POP3FetchUrls = NULL;
64 typedef struct pop3aggr pop3aggr;
65 typedef eNextState(*Pop3ClientHandler)(pop3aggr* RecvMsg);
67 eNextState POP3_C_Shutdown(AsyncIO *IO);
68 eNextState POP3_C_Timeout(AsyncIO *IO);
69 eNextState POP3_C_ConnFail(AsyncIO *IO);
70 eNextState POP3_C_DNSFail(AsyncIO *IO);
71 eNextState POP3_C_DispatchReadDone(AsyncIO *IO);
72 eNextState POP3_C_DispatchWriteDone(AsyncIO *IO);
73 eNextState POP3_C_Terminate(AsyncIO *IO);
74 eReadState POP3_C_ReadServerStatus(AsyncIO *IO);
75 eNextState POP3_C_ReAttachToFetchMessages(AsyncIO *IO);
77 typedef struct __pop3_room_counter {
82 typedef enum ePOP3_C_States {
89 ReadMessageBodyFollowing,
97 typedef struct _FetchItem {
103 struct CtdlMessage *Msg;
106 void HfreeFetchItem(void *vItem)
108 FetchItem *Item = (FetchItem*) vItem;
109 FreeStrBuf(&Item->MsgUIDL);
110 FreeStrBuf(&Item->MsgUID);
116 typedef enum _POP3State {
130 ConstStr POP3States[] = {
131 {HKEY("Aggregator created")},
132 {HKEY("Reading Greeting")},
133 {HKEY("Sending User")},
134 {HKEY("Sending Password")},
136 {HKEY("Fetching Usetable")},
137 {HKEY("Get MSG ID")},
138 {HKEY("Get Message")},
140 {HKEY("Delete Upstream")},
144 static void SetPOP3State(AsyncIO *IO, POP3State State)
146 CitContext* CCC = IO->CitContext;
148 memcpy(CCC->cs_clientname, POP3States[State].Key, POP3States[State].len + 1);
159 DNSQueryParts HostLookup;
162 HashList *OtherQRnumbers;
168 StrBuf *RoomName; // TODO: fill me
171 ePOP3_C_States State;
172 HashList *MsgNumbers;
177 void DeletePOP3Aggregator(void *vptr)
179 pop3aggr *ptr = vptr;
180 DeleteHashPos(&ptr->Pos);
181 DeleteHash(&ptr->MsgNumbers);
182 // FreeStrBuf(&ptr->rooms);
183 FreeStrBuf(&ptr->pop3user);
184 FreeStrBuf(&ptr->pop3pass);
185 FreeStrBuf(&ptr->Host);
186 FreeStrBuf(&ptr->RoomName);
187 FreeURL(&ptr->IO.ConnectMe);
188 FreeStrBuf(&ptr->Url);
189 FreeStrBuf(&ptr->IO.IOBuf);
190 FreeStrBuf(&ptr->IO.SendBuf.Buf);
191 FreeStrBuf(&ptr->IO.RecvBuf.Buf);
192 DeleteAsyncMsg(&ptr->IO.ReadMsg);
193 if (((struct CitContext*)ptr->IO.CitContext)) {
194 ((struct CitContext*)ptr->IO.CitContext)->state = CON_IDLE;
195 ((struct CitContext*)ptr->IO.CitContext)->kill_me = 1;
197 FreeAsyncIOContents(&ptr->IO);
201 eNextState FinalizePOP3AggrRun(AsyncIO *IO)
204 pop3aggr *cpptr = (pop3aggr *)IO->Data;
207 "%s@%s: fetched %ld new of %d messages in %fs. bye.",
208 ChrPtr(cpptr->pop3user),
211 GetCount(cpptr->MsgNumbers),
212 IO->Now - cpptr->IOStart
215 It = GetNewHashPos(POP3FetchUrls, 0);
216 pthread_mutex_lock(&POP3QueueMutex);
218 if (GetHashPosFromKey(POP3FetchUrls, SKEY(cpptr->Url), It))
219 DeleteEntryFromHash(POP3FetchUrls, It);
221 pthread_mutex_unlock(&POP3QueueMutex);
226 eNextState FailAggregationRun(AsyncIO *IO)
231 eNextState POP3C_ReadGreeting(pop3aggr *RecvMsg)
233 AsyncIO *IO = &RecvMsg->IO;
234 SetPOP3State(IO, eGreeting);
235 /* Read the server greeting */
236 if (!POP3C_OK) return eTerminateConnection;
237 else return eSendReply;
240 eNextState POP3C_SendUser(pop3aggr *RecvMsg)
242 AsyncIO *IO = &RecvMsg->IO;
243 SetPOP3State(IO, eUser);
244 /* Identify ourselves. NOTE: we have to append a CR to each command.
245 * The LF will automatically be appended by sock_puts(). Believe it
246 * or not, leaving out the CR will cause problems if the server happens
247 * to be Exchange, which is so b0rken it actually barfs on
248 * LF-terminated newlines.
250 StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
251 "USER %s\r\n", ChrPtr(RecvMsg->pop3user));
255 eNextState POP3C_GetUserState(pop3aggr *RecvMsg)
257 if (!POP3C_OK) return eTerminateConnection;
258 else return eSendReply;
261 eNextState POP3C_SendPassword(pop3aggr *RecvMsg)
263 AsyncIO *IO = &RecvMsg->IO;
264 SetPOP3State(IO, ePassword);
266 StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
267 "PASS %s\r\n", ChrPtr(RecvMsg->pop3pass));
268 syslog(LOG_DEBUG, "<PASS <password>\n");
272 eNextState POP3C_GetPassState(pop3aggr *RecvMsg)
274 if (!POP3C_OK) return eTerminateConnection;
275 else return eSendReply;
278 eNextState POP3C_SendListCommand(pop3aggr *RecvMsg)
280 AsyncIO *IO = &RecvMsg->IO;
281 SetPOP3State(IO, eListing);
283 /* Get the list of messages */
284 StrBufPlain(RecvMsg->IO.SendBuf.Buf, HKEY("LIST\r\n"));
288 eNextState POP3C_GetListCommandState(pop3aggr *RecvMsg)
290 if (!POP3C_OK) return eTerminateConnection;
291 RecvMsg->MsgNumbers = NewHash(1, NULL);
297 eNextState POP3C_GetListOneLine(pop3aggr *RecvMsg)
303 FetchItem *OneMsg = NULL;
305 if ((StrLength(RecvMsg->IO.IOBuf) == 1) &&
306 (ChrPtr(RecvMsg->IO.IOBuf)[0] == '.'))
308 if (GetCount(RecvMsg->MsgNumbers) == 0)
310 //// RecvMsg->Sate = ReadQuitState;
314 RecvMsg->Pos = GetNewHashPos(RecvMsg->MsgNumbers, 0);
321 * work around buggy pop3 servers which send
322 * empty lines in their listings.
324 if ((StrLength(RecvMsg->IO.IOBuf) == 0) ||
325 !isdigit(ChrPtr(RecvMsg->IO.IOBuf)[0]))
330 OneMsg = (FetchItem*) malloc(sizeof(FetchItem));
331 memset(OneMsg, 0, sizeof(FetchItem));
332 OneMsg->MSGID = atol(ChrPtr(RecvMsg->IO.IOBuf));
334 pch = strchr(ChrPtr(RecvMsg->IO.IOBuf), ' ');
337 OneMsg->MSGSize = atol(pch + 1);
340 rc = TestValidateHash(RecvMsg->MsgNumbers);
342 syslog(LOG_DEBUG, "Hash Invalid: %d\n", rc);
345 Put(RecvMsg->MsgNumbers, LKEY(OneMsg->MSGID), OneMsg, HfreeFetchItem);
347 rc = TestValidateHash(RecvMsg->MsgNumbers);
349 syslog(LOG_DEBUG, "Hash Invalid: %d\n", rc);
351 //RecvMsg->State --; /* read next Line */
355 eNextState POP3_FetchNetworkUsetableEntry(AsyncIO *IO)
360 pop3aggr *RecvMsg = (pop3aggr *) IO->Data;
361 time_t seenstamp = 0;
363 SetPOP3State(IO, eUseTable);
365 if((RecvMsg->Pos != NULL) &&
366 GetNextHashPos(RecvMsg->MsgNumbers,
372 if (server_shutting_down)
375 RecvMsg->CurrMsg = (FetchItem*)vData;
377 seenstamp = CheckIfAlreadySeen("POP3 Item Seen",
378 RecvMsg->CurrMsg->MsgUID,
380 EvGetNow(IO) - USETABLE_ANTIEXPIRE,
385 /* Item has already been seen */
386 RecvMsg->CurrMsg->NeedFetch = 0;
390 syslog(LOG_DEBUG, "NO\n");
391 RecvMsg->CurrMsg->NeedFetch = 1;
393 return NextDBOperation(&RecvMsg->IO,
394 POP3_FetchNetworkUsetableEntry);
398 /* ok, now we know them all,
399 * continue with reading the actual messages. */
400 DeleteHashPos(&RecvMsg->Pos);
401 return DBQueueEventContext(IO, POP3_C_ReAttachToFetchMessages);
405 eNextState POP3C_GetOneMessagID(pop3aggr *RecvMsg)
407 AsyncIO *IO = &RecvMsg->IO;
412 SetPOP3State(IO, eGetMsgID);
415 rc = TestValidateHash(RecvMsg->MsgNumbers);
417 syslog(LOG_DEBUG, "Hash Invalid: %d\n", rc);
419 if((RecvMsg->Pos != NULL) &&
420 GetNextHashPos(RecvMsg->MsgNumbers,
425 RecvMsg->CurrMsg = (FetchItem*) vData;
426 /* Find out the UIDL of the message,
427 * to determine whether we've already downloaded it */
428 StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
429 "UIDL %ld\r\n", RecvMsg->CurrMsg->MSGID);
434 DeleteHashPos(&RecvMsg->Pos);
435 /// done receiving uidls.. start looking them up now.
436 RecvMsg->Pos = GetNewHashPos(RecvMsg->MsgNumbers, 0);
437 return EventQueueDBOperation(&RecvMsg->IO,
438 POP3_FetchNetworkUsetableEntry,
441 return eReadMore; /* TODO */
444 eNextState POP3C_GetOneMessageIDState(pop3aggr *RecvMsg)
448 rc = TestValidateHash(RecvMsg->MsgNumbers);
450 syslog(LOG_DEBUG, "Hash Invalid: %d\n", rc);
453 if (!POP3C_OK) return eTerminateConnection;
454 RecvMsg->CurrMsg->MsgUIDL = NewStrBufPlain(NULL, StrLength(RecvMsg->IO.IOBuf));
455 RecvMsg->CurrMsg->MsgUID = NewStrBufPlain(NULL, StrLength(RecvMsg->IO.IOBuf) * 2);
457 StrBufExtract_token(RecvMsg->CurrMsg->MsgUIDL, RecvMsg->IO.IOBuf, 2, ' ');
459 StrBufPrintf(RecvMsg->CurrMsg->MsgUID,
461 ChrPtr(RecvMsg->RoomName),
462 ChrPtr(RecvMsg->CurrMsg->MsgUIDL),
463 RecvMsg->IO.ConnectMe->User,
464 RecvMsg->IO.ConnectMe->Host
471 eNextState POP3C_SendGetOneMsg(pop3aggr *RecvMsg)
473 AsyncIO *IO = &RecvMsg->IO;
478 SetPOP3State(IO, eGetMsg);
480 syslog(LOG_DEBUG, "fast forwarding to the next unknown message");
482 RecvMsg->CurrMsg = NULL;
483 while ((RecvMsg->Pos != NULL) &&
484 GetNextHashPos(RecvMsg->MsgNumbers,
488 (RecvMsg->CurrMsg = (FetchItem*) vData,
489 RecvMsg->CurrMsg->NeedFetch == 0))
492 if ((RecvMsg->CurrMsg != NULL ) && (RecvMsg->CurrMsg->NeedFetch == 1))
494 syslog(LOG_DEBUG, "fetching next");
495 /* Message has not been seen.
496 * Tell the server to fetch the message... */
497 StrBufPrintf(RecvMsg->IO.SendBuf.Buf, "RETR %ld\r\n", RecvMsg->CurrMsg->MSGID);
501 syslog(LOG_DEBUG, "no more messages to fetch.");
502 RecvMsg->State = ReadQuitState;
503 return POP3_C_DispatchWriteDone(&RecvMsg->IO);
508 eNextState POP3C_ReadMessageBodyFollowing(pop3aggr *RecvMsg)
510 if (!POP3C_OK) return eTerminateConnection;
511 RecvMsg->IO.ReadMsg = NewAsyncMsg(HKEY("."),
512 RecvMsg->CurrMsg->MSGSize,
513 CtdlGetConfigLong("c_maxmsglen"),
522 eNextState POP3C_StoreMsgRead(AsyncIO *IO)
524 pop3aggr *RecvMsg = (pop3aggr *) IO->Data;
526 SetPOP3State(IO, eStoreMsg);
528 syslog(LOG_DEBUG, "MARKING: %s as seen: ", ChrPtr(RecvMsg->CurrMsg->MsgUID));
529 CheckIfAlreadySeen("POP3 Item Seen",
530 RecvMsg->CurrMsg->MsgUID,
532 EvGetNow(IO) - USETABLE_ANTIEXPIRE,
537 return DBQueueEventContext(&RecvMsg->IO, POP3_C_ReAttachToFetchMessages);
541 eNextState POP3C_SaveMsg(AsyncIO *IO)
544 pop3aggr *RecvMsg = (pop3aggr *) IO->Data;
546 /* Do Something With It (tm) */
547 msgnum = CtdlSubmitMsg(RecvMsg->CurrMsg->Msg,
549 ChrPtr(RecvMsg->RoomName),
553 /* Message has been committed to the store
554 * write the uidl to the use table
555 * so we don't fetch this message again
558 CM_Free(RecvMsg->CurrMsg->Msg);
561 return NextDBOperation(&RecvMsg->IO, POP3C_StoreMsgRead);
565 eNextState POP3C_ReadMessageBody(pop3aggr *RecvMsg)
567 syslog(LOG_DEBUG, "Converting message...");
568 RecvMsg->CurrMsg->Msg = convert_internet_message_buf(&RecvMsg->IO.ReadMsg->MsgBuf);
569 return EventQueueDBOperation(&RecvMsg->IO, POP3C_SaveMsg, 0);
573 eNextState POP3C_SendDelete(pop3aggr *RecvMsg)
575 AsyncIO *IO = &RecvMsg->IO;
577 SetPOP3State(IO, eDelete);
579 if (!RecvMsg->keep) {
580 StrBufPrintf(RecvMsg->IO.SendBuf.Buf, "DELE %ld\r\n", RecvMsg->CurrMsg->MSGID);
584 RecvMsg->State = ReadMessageBodyFollowing;
585 return POP3_C_DispatchWriteDone(&RecvMsg->IO);
590 eNextState POP3C_ReadDeleteState(pop3aggr *RecvMsg)
592 RecvMsg->State = GetOneMessageIDState;
593 return POP3_C_DispatchWriteDone(&RecvMsg->IO);
597 eNextState POP3C_SendQuit(pop3aggr *RecvMsg)
599 AsyncIO *IO = &RecvMsg->IO;
600 SetPOP3State(IO, eQuit);
603 StrBufPlain(RecvMsg->IO.SendBuf.Buf,
609 eNextState POP3C_ReadQuitState(pop3aggr *RecvMsg)
611 return eTerminateConnection;
615 const long POP3_C_ConnTimeout = 1000;
616 const long DefaultPOP3Port = 110;
618 Pop3ClientHandler POP3C_ReadHandlers[] = {
622 POP3C_GetListCommandState,
623 POP3C_GetListOneLine,
624 POP3C_GetOneMessageIDState,
625 POP3C_ReadMessageBodyFollowing,
626 POP3C_ReadMessageBody,
627 POP3C_ReadDeleteState,
631 const long POP3_C_SendTimeouts[POP3C_MaxRead] = {
641 const ConstStr POP3C_ReadErrors[POP3C_MaxRead] = {
642 {HKEY("Connection broken during ")},
643 {HKEY("Connection broken during ")},
644 {HKEY("Connection broken during ")},
645 {HKEY("Connection broken during ")},
646 {HKEY("Connection broken during ")},
647 {HKEY("Connection broken during ")},
648 {HKEY("Connection broken during ")},
649 {HKEY("Connection broken during ")}
652 Pop3ClientHandler POP3C_SendHandlers[] = {
653 NULL, /* we don't send a greeting */
656 POP3C_SendListCommand,
658 POP3C_GetOneMessagID,
665 const long POP3_C_ReadTimeouts[] = {
677 /*****************************************************************************/
678 /* POP3 CLIENT DISPATCHER */
679 /*****************************************************************************/
681 void POP3SetTimeout(eNextState NextTCPState, pop3aggr *pMsg)
683 double Timeout = 0.0;
685 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
687 switch (NextTCPState) {
691 Timeout = POP3_C_SendTimeouts[pMsg->State];
693 if (pMsg->State == eDATABody) {
694 / * if we're sending a huge message, we need more time. * /
695 Timeout += StrLength(pMsg->msgtext) / 1024;
701 Timeout = POP3_C_ReadTimeouts[pMsg->State];
703 if (pMsg->State == eDATATerminateBody) {
705 * some mailservers take a nap before accepting the message
706 * content inspection and such.
708 Timeout += StrLength(pMsg->msgtext) / 1024;
719 case eTerminateConnection:
722 case eReadMore://// TODO
725 SetNextTimeout(&pMsg->IO, Timeout);
727 eNextState POP3_C_DispatchReadDone(AsyncIO *IO)
729 /* syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__); to noisy anyways. */
730 pop3aggr *pMsg = IO->Data;
733 rc = POP3C_ReadHandlers[pMsg->State](pMsg);
736 POP3SetTimeout(rc, pMsg);
739 eNextState POP3_C_DispatchWriteDone(AsyncIO *IO)
741 pop3aggr *pMsg = IO->Data;
744 /* syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__); to noisy anyways. */
745 rc = POP3C_SendHandlers[pMsg->State](pMsg);
746 POP3SetTimeout(rc, pMsg);
751 /*****************************************************************************/
752 /* POP3 CLIENT ERROR CATCHERS */
753 /*****************************************************************************/
754 eNextState POP3_C_Terminate(AsyncIO *IO)
756 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
757 FinalizePOP3AggrRun(IO);
760 eNextState POP3_C_TerminateDB(AsyncIO *IO)
762 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
763 FinalizePOP3AggrRun(IO);
766 eNextState POP3_C_Timeout(AsyncIO *IO)
768 pop3aggr *pMsg = IO->Data;
770 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
771 StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State]));
772 return FailAggregationRun(IO);
774 eNextState POP3_C_ConnFail(AsyncIO *IO)
776 pop3aggr *pMsg = (pop3aggr *)IO->Data;
778 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
779 StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State]));
780 return FailAggregationRun(IO);
782 eNextState POP3_C_DNSFail(AsyncIO *IO)
784 pop3aggr *pMsg = (pop3aggr *)IO->Data;
786 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
787 StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State]));
788 return FailAggregationRun(IO);
790 eNextState POP3_C_Shutdown(AsyncIO *IO)
792 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
793 FinalizePOP3AggrRun(IO);
799 * lineread Handler; understands when to read more POP3 lines, and when this is a one-lined reply.
801 eReadState POP3_C_ReadServerStatus(AsyncIO *IO)
803 eReadState Finished = eBufferNotEmpty;
805 switch (IO->NextState) {
810 case eTerminateConnection:
812 Finished = eReadFail;
819 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
823 Finished = CtdlReadMessageBodyAsync(IO);
829 /*****************************************************************************
830 * So we connect our Server IP here. *
831 *****************************************************************************/
832 eNextState POP3_C_ReAttachToFetchMessages(AsyncIO *IO)
834 pop3aggr *cpptr = IO->Data;
836 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
837 ////??? cpptr->State ++;
838 if (cpptr->Pos == NULL)
839 cpptr->Pos = GetNewHashPos(cpptr->MsgNumbers, 0);
841 POP3_C_DispatchWriteDone(IO);
842 ReAttachIO(IO, cpptr, 0);
843 IO->NextState = eReadMessage;
844 return IO->NextState;
847 eNextState pop3_connect_ip(AsyncIO *IO)
849 pop3aggr *cpptr = IO->Data;
851 if (cpptr->IOStart == 0.0) /* whith or without DNS? */
852 cpptr->IOStart = IO->Now;
854 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
856 return EvConnectSock(IO,
858 POP3_C_ReadTimeouts[0],
862 eNextState pop3_get_one_host_ip_done(AsyncIO *IO)
864 pop3aggr *cpptr = IO->Data;
865 struct hostent *hostent;
869 hostent = cpptr->HostLookup.VParsedDNSReply;
870 if ((cpptr->HostLookup.DNSStatus == ARES_SUCCESS) &&
871 (hostent != NULL) ) {
872 memset(&cpptr->IO.ConnectMe->Addr, 0, sizeof(struct in6_addr));
873 if (cpptr->IO.ConnectMe->IPv6) {
874 memcpy(&cpptr->IO.ConnectMe->Addr.sin6_addr.s6_addr,
875 &hostent->h_addr_list[0],
876 sizeof(struct in6_addr));
878 cpptr->IO.ConnectMe->Addr.sin6_family =
880 cpptr->IO.ConnectMe->Addr.sin6_port =
881 htons(DefaultPOP3Port);
884 struct sockaddr_in *addr =
885 (struct sockaddr_in*)
886 &cpptr->IO.ConnectMe->Addr;
888 memcpy(&addr->sin_addr.s_addr,
889 hostent->h_addr_list[0],
892 addr->sin_family = hostent->h_addrtype;
893 addr->sin_port = htons(DefaultPOP3Port);
895 return pop3_connect_ip(IO);
901 eNextState pop3_get_one_host_ip(AsyncIO *IO)
903 pop3aggr *cpptr = IO->Data;
905 cpptr->IOStart = IO->Now;
907 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
910 "POP3 client[%ld]: looking up %s-Record %s : %d ...\n",
912 (cpptr->IO.ConnectMe->IPv6)? "aaaa": "a",
913 cpptr->IO.ConnectMe->Host,
914 cpptr->IO.ConnectMe->Port);
916 QueueQuery((cpptr->IO.ConnectMe->IPv6)? ns_t_aaaa : ns_t_a,
917 cpptr->IO.ConnectMe->Host,
920 pop3_get_one_host_ip_done);
921 IO->NextState = eReadDNSReply;
922 return IO->NextState;
927 int pop3_do_fetching(pop3aggr *cpptr)
929 AsyncIO *IO = &cpptr->IO;
934 POP3_C_ReadServerStatus,
936 POP3_C_DispatchWriteDone,
937 POP3_C_DispatchReadDone,
944 safestrncpy(((CitContext *)cpptr->IO.CitContext)->cs_host,
946 sizeof(((CitContext *)cpptr->IO.CitContext)->cs_host));
948 if (cpptr->IO.ConnectMe->IsIP) {
949 QueueEventContext(&cpptr->IO,
953 QueueEventContext(&cpptr->IO,
954 pop3_get_one_host_ip);
960 * Scan a room's netconfig to determine whether it requires POP3 aggregation
962 void pop3client_scan_room(struct ctdlroom *qrbuf, void *data, OneRoomNetCfg *OneRNCFG)
964 const RoomNetCfgLine *pLine;
967 pthread_mutex_lock(&POP3QueueMutex);
968 if (GetHash(POP3QueueRooms, LKEY(qrbuf->QRnumber), &vptr))
970 pthread_mutex_unlock(&POP3QueueMutex);
972 "pop3client: [%ld] %s already in progress.",
977 pthread_mutex_unlock(&POP3QueueMutex);
979 if (server_shutting_down) return;
981 pLine = OneRNCFG->NetConfigs[pop3client];
983 while (pLine != NULL)
987 cptr = (pop3aggr *) malloc(sizeof(pop3aggr));
988 memset(cptr, 0, sizeof(pop3aggr));
989 ///TODO do we need this? cptr->roomlist_parts=1;
990 cptr->RoomName = NewStrBufPlain(qrbuf->QRname, -1);
991 cptr->pop3user = NewStrBufDup(pLine->Value[1]);
992 cptr->pop3pass = NewStrBufDup(pLine->Value[2]);
993 cptr->Url = NewStrBuf();
994 cptr->Host = NewStrBufDup(pLine->Value[0]);
996 cptr->keep = atol(ChrPtr(pLine->Value[3]));
997 cptr->interval = atol(ChrPtr(pLine->Value[4]));
999 StrBufAppendBufPlain(cptr->Url, HKEY("pop3://"), 0);
1000 StrBufUrlescUPAppend(cptr->Url, cptr->pop3user, NULL);
1001 StrBufAppendBufPlain(cptr->Url, HKEY(":"), 0);
1002 StrBufUrlescUPAppend(cptr->Url, cptr->pop3pass, NULL);
1003 StrBufAppendBufPlain(cptr->Url, HKEY("@"), 0);
1004 StrBufAppendBuf(cptr->Url, cptr->Host, 0);
1005 StrBufAppendBufPlain(cptr->Url, HKEY("/"), 0);
1006 StrBufUrlescAppend(cptr->Url, cptr->RoomName, NULL);
1008 ParseURL(&cptr->IO.ConnectMe, cptr->Url, 110);
1010 cptr->n = Pop3ClientID++;
1011 pthread_mutex_lock(&POP3QueueMutex);
1015 DeletePOP3Aggregator);
1017 pthread_mutex_unlock(&POP3QueueMutex);
1018 pLine = pLine->next;
1023 static int doing_pop3client = 0;
1025 void pop3client_scan(void) {
1026 static time_t last_run = 0L;
1027 time_t fastest_scan;
1034 become_session(&pop3_client_CC);
1036 if (CtdlGetConfigLong("c_pop3_fastest") < CtdlGetConfigLong("c_pop3_fetch"))
1037 fastest_scan = CtdlGetConfigLong("c_pop3_fastest");
1039 fastest_scan = CtdlGetConfigLong("c_pop3_fetch");
1042 * Run POP3 aggregation no more frequently than once every n seconds
1044 if ( (time(NULL) - last_run) < fastest_scan ) {
1049 * This is a simple concurrency check to make sure only one pop3client
1050 * run is done at a time. We could do this with a mutex, but since we
1051 * don't really require extremely fine granularity here, we'll do it
1052 * with a static variable instead.
1054 if (doing_pop3client) return;
1055 doing_pop3client = 1;
1057 syslog(LOG_DEBUG, "pop3client started");
1058 CtdlForEachNetCfgRoom(pop3client_scan_room, NULL);
1060 pthread_mutex_lock(&POP3QueueMutex);
1061 it = GetNewHashPos(POP3FetchUrls, 0);
1062 while (!server_shutting_down &&
1063 GetNextHashPos(POP3FetchUrls, it, &len, &Key, &vrptr) &&
1065 cptr = (pop3aggr *)vrptr;
1066 if (cptr->RefCount == 0) {
1067 if (!pop3_do_fetching(cptr)) {
1068 DeletePOP3Aggregator(cptr);////TODO
1073 pthread_mutex_unlock(&POP3QueueMutex);
1075 syslog(LOG_DEBUG, "pop3client ended");
1076 last_run = time(NULL);
1077 doing_pop3client = 0;
1081 void pop3_cleanup(void)
1083 /* citthread_mutex_destroy(&POP3QueueMutex); TODO */
1084 while (doing_pop3client != 0) ;
1085 DeleteHash(&POP3FetchUrls);
1086 DeleteHash(&POP3QueueRooms);
1091 CTDL_MODULE_INIT(pop3client)
1095 CtdlFillSystemContext(&pop3_client_CC, "POP3aggr");
1096 CtdlREGISTERRoomCfgType(pop3client, ParseGeneric, 0, 5, SerializeGeneric, DeleteGenericCfgLine);
1097 pthread_mutex_init(&POP3QueueMutex, NULL);
1098 POP3QueueRooms = NewHash(1, lFlathash);
1099 POP3FetchUrls = NewHash(1, NULL);
1100 CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER, PRIO_AGGR + 50);
1101 CtdlRegisterEVCleanupHook(pop3_cleanup);
1104 /* return our module id for the log */
1105 return "pop3client";