2 * Consolidate mail from remote POP3 accounts.
4 * Copyright (c) 2007-2015 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 =
455 NewStrBufPlain(NULL, StrLength(RecvMsg->IO.IOBuf));
456 RecvMsg->CurrMsg->MsgUID =
457 NewStrBufPlain(NULL, StrLength(RecvMsg->IO.IOBuf) * 2);
459 StrBufExtract_token(RecvMsg->CurrMsg->MsgUIDL,
460 RecvMsg->IO.IOBuf, 2, ' ');
462 StrBufPrintf(RecvMsg->CurrMsg->MsgUID,
464 ChrPtr(RecvMsg->RoomName),
465 ChrPtr(RecvMsg->CurrMsg->MsgUIDL),
466 RecvMsg->IO.ConnectMe->User,
467 RecvMsg->IO.ConnectMe->Host);
473 eNextState POP3C_SendGetOneMsg(pop3aggr *RecvMsg)
475 AsyncIO *IO = &RecvMsg->IO;
480 SetPOP3State(IO, eGetMsg);
482 syslog(LOG_DEBUG, "fast forwarding to the next unknown message");
484 RecvMsg->CurrMsg = NULL;
485 while ((RecvMsg->Pos != NULL) &&
486 GetNextHashPos(RecvMsg->MsgNumbers,
490 (RecvMsg->CurrMsg = (FetchItem*) vData,
491 RecvMsg->CurrMsg->NeedFetch == 0))
494 if ((RecvMsg->CurrMsg != NULL ) && (RecvMsg->CurrMsg->NeedFetch == 1))
496 syslog(LOG_DEBUG, "fetching next");
497 /* Message has not been seen.
498 * Tell the server to fetch the message... */
499 StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
500 "RETR %ld\r\n", RecvMsg->CurrMsg->MSGID);
504 syslog(LOG_DEBUG, "no more messages to fetch.");
505 RecvMsg->State = ReadQuitState;
506 return POP3_C_DispatchWriteDone(&RecvMsg->IO);
511 eNextState POP3C_ReadMessageBodyFollowing(pop3aggr *RecvMsg)
513 if (!POP3C_OK) return eTerminateConnection;
514 RecvMsg->IO.ReadMsg = NewAsyncMsg(HKEY("."),
515 RecvMsg->CurrMsg->MSGSize,
516 CtdlGetConfigLong("c_maxmsglen"),
524 eNextState POP3C_StoreMsgRead(AsyncIO *IO)
526 pop3aggr *RecvMsg = (pop3aggr *) IO->Data;
528 SetPOP3State(IO, eStoreMsg);
531 "MARKING: %s as seen: ",
532 ChrPtr(RecvMsg->CurrMsg->MsgUID));
533 CheckIfAlreadySeen("POP3 Item Seen",
534 RecvMsg->CurrMsg->MsgUID,
536 EvGetNow(IO) - USETABLE_ANTIEXPIRE,
540 return DBQueueEventContext(&RecvMsg->IO, POP3_C_ReAttachToFetchMessages);
542 eNextState POP3C_SaveMsg(AsyncIO *IO)
545 pop3aggr *RecvMsg = (pop3aggr *) IO->Data;
547 /* Do Something With It (tm) */
548 msgnum = CtdlSubmitMsg(RecvMsg->CurrMsg->Msg,
550 ChrPtr(RecvMsg->RoomName),
554 /* Message has been committed to the store
555 * write the uidl to the use table
556 * so we don't fetch this message again
559 CM_Free(RecvMsg->CurrMsg->Msg);
562 return NextDBOperation(&RecvMsg->IO, POP3C_StoreMsgRead);
565 eNextState POP3C_ReadMessageBody(pop3aggr *RecvMsg)
567 syslog(LOG_DEBUG, "Converting message...");
568 RecvMsg->CurrMsg->Msg =
569 convert_internet_message_buf(&RecvMsg->IO.ReadMsg->MsgBuf);
570 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,
581 "DELE %ld\r\n", RecvMsg->CurrMsg->MSGID);
585 RecvMsg->State = ReadMessageBodyFollowing;
586 return POP3_C_DispatchWriteDone(&RecvMsg->IO);
589 eNextState POP3C_ReadDeleteState(pop3aggr *RecvMsg)
591 RecvMsg->State = GetOneMessageIDState;
592 return POP3_C_DispatchWriteDone(&RecvMsg->IO);
595 eNextState POP3C_SendQuit(pop3aggr *RecvMsg)
597 AsyncIO *IO = &RecvMsg->IO;
598 SetPOP3State(IO, eQuit);
601 StrBufPlain(RecvMsg->IO.SendBuf.Buf,
607 eNextState POP3C_ReadQuitState(pop3aggr *RecvMsg)
609 return eTerminateConnection;
612 const long POP3_C_ConnTimeout = 1000;
613 const long DefaultPOP3Port = 110;
615 Pop3ClientHandler POP3C_ReadHandlers[] = {
619 POP3C_GetListCommandState,
620 POP3C_GetListOneLine,
621 POP3C_GetOneMessageIDState,
622 POP3C_ReadMessageBodyFollowing,
623 POP3C_ReadMessageBody,
624 POP3C_ReadDeleteState,
628 const long POP3_C_SendTimeouts[POP3C_MaxRead] = {
638 const ConstStr POP3C_ReadErrors[POP3C_MaxRead] = {
639 {HKEY("Connection broken during ")},
640 {HKEY("Connection broken during ")},
641 {HKEY("Connection broken during ")},
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 ")}
649 Pop3ClientHandler POP3C_SendHandlers[] = {
650 NULL, /* we don't send a greeting */
653 POP3C_SendListCommand,
655 POP3C_GetOneMessagID,
662 const long POP3_C_ReadTimeouts[] = {
674 /*****************************************************************************/
675 /* POP3 CLIENT DISPATCHER */
676 /*****************************************************************************/
678 void POP3SetTimeout(eNextState NextTCPState, pop3aggr *pMsg)
680 double Timeout = 0.0;
682 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
684 switch (NextTCPState) {
688 Timeout = POP3_C_SendTimeouts[pMsg->State];
690 if (pMsg->State == eDATABody) {
691 / * if we're sending a huge message, we need more time. * /
692 Timeout += StrLength(pMsg->msgtext) / 1024;
698 Timeout = POP3_C_ReadTimeouts[pMsg->State];
700 if (pMsg->State == eDATATerminateBody) {
702 * some mailservers take a nap before accepting the message
703 * content inspection and such.
705 Timeout += StrLength(pMsg->msgtext) / 1024;
716 case eTerminateConnection:
719 case eReadMore://// TODO
722 SetNextTimeout(&pMsg->IO, Timeout);
724 eNextState POP3_C_DispatchReadDone(AsyncIO *IO)
726 /* syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__); to noisy anyways. */
727 pop3aggr *pMsg = IO->Data;
730 rc = POP3C_ReadHandlers[pMsg->State](pMsg);
733 POP3SetTimeout(rc, pMsg);
736 eNextState POP3_C_DispatchWriteDone(AsyncIO *IO)
738 pop3aggr *pMsg = IO->Data;
741 /* syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__); to noisy anyways. */
742 rc = POP3C_SendHandlers[pMsg->State](pMsg);
743 POP3SetTimeout(rc, pMsg);
748 /*****************************************************************************/
749 /* POP3 CLIENT ERROR CATCHERS */
750 /*****************************************************************************/
751 eNextState POP3_C_Terminate(AsyncIO *IO)
753 /// pop3aggr *pMsg = (pop3aggr *)IO->Data;
755 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
756 FinalizePOP3AggrRun(IO);
759 eNextState POP3_C_TerminateDB(AsyncIO *IO)
761 /// pop3aggr *pMsg = (pop3aggr *)IO->Data;
763 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
764 FinalizePOP3AggrRun(IO);
767 eNextState POP3_C_Timeout(AsyncIO *IO)
769 pop3aggr *pMsg = IO->Data;
771 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
772 StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State]));
773 return FailAggregationRun(IO);
775 eNextState POP3_C_ConnFail(AsyncIO *IO)
777 pop3aggr *pMsg = (pop3aggr *)IO->Data;
779 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
780 StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State]));
781 return FailAggregationRun(IO);
783 eNextState POP3_C_DNSFail(AsyncIO *IO)
785 pop3aggr *pMsg = (pop3aggr *)IO->Data;
787 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
788 StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State]));
789 return FailAggregationRun(IO);
791 eNextState POP3_C_Shutdown(AsyncIO *IO)
793 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
794 //// pop3aggr *pMsg = IO->Data;
796 ////pMsg->MyQEntry->Status = 3;
797 ///StrBufPlain(pMsg->MyQEntry->StatusMessage, HKEY("server shutdown during message retrieval."));
798 FinalizePOP3AggrRun(IO);
804 * @brief lineread Handler; understands when to read more POP3 lines,
805 * and when this is a one-lined reply.
807 eReadState POP3_C_ReadServerStatus(AsyncIO *IO)
809 eReadState Finished = eBufferNotEmpty;
811 switch (IO->NextState) {
816 case eTerminateConnection:
818 Finished = eReadFail;
825 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
829 Finished = CtdlReadMessageBodyAsync(IO);
835 /*****************************************************************************
836 * So we connect our Server IP here. *
837 *****************************************************************************/
838 eNextState POP3_C_ReAttachToFetchMessages(AsyncIO *IO)
840 pop3aggr *cpptr = IO->Data;
842 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
843 ////??? cpptr->State ++;
844 if (cpptr->Pos == NULL)
845 cpptr->Pos = GetNewHashPos(cpptr->MsgNumbers, 0);
847 POP3_C_DispatchWriteDone(IO);
848 ReAttachIO(IO, cpptr, 0);
849 IO->NextState = eReadMessage;
850 return IO->NextState;
853 eNextState pop3_connect_ip(AsyncIO *IO)
855 pop3aggr *cpptr = IO->Data;
857 if (cpptr->IOStart == 0.0) /* whith or without DNS? */
858 cpptr->IOStart = IO->Now;
860 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
862 return EvConnectSock(IO,
864 POP3_C_ReadTimeouts[0],
868 eNextState pop3_get_one_host_ip_done(AsyncIO *IO)
870 pop3aggr *cpptr = IO->Data;
871 struct hostent *hostent;
875 hostent = cpptr->HostLookup.VParsedDNSReply;
876 if ((cpptr->HostLookup.DNSStatus == ARES_SUCCESS) &&
877 (hostent != NULL) ) {
878 memset(&cpptr->IO.ConnectMe->Addr, 0, sizeof(struct in6_addr));
879 if (cpptr->IO.ConnectMe->IPv6) {
880 memcpy(&cpptr->IO.ConnectMe->Addr.sin6_addr.s6_addr,
881 &hostent->h_addr_list[0],
882 sizeof(struct in6_addr));
884 cpptr->IO.ConnectMe->Addr.sin6_family =
886 cpptr->IO.ConnectMe->Addr.sin6_port =
887 htons(DefaultPOP3Port);
890 struct sockaddr_in *addr =
891 (struct sockaddr_in*)
892 &cpptr->IO.ConnectMe->Addr;
894 memcpy(&addr->sin_addr.s_addr,
895 hostent->h_addr_list[0],
898 addr->sin_family = hostent->h_addrtype;
899 addr->sin_port = htons(DefaultPOP3Port);
901 return pop3_connect_ip(IO);
907 eNextState pop3_get_one_host_ip(AsyncIO *IO)
909 pop3aggr *cpptr = IO->Data;
911 cpptr->IOStart = IO->Now;
913 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
916 "POP3 client[%ld]: looking up %s-Record %s : %d ...\n",
918 (cpptr->IO.ConnectMe->IPv6)? "aaaa": "a",
919 cpptr->IO.ConnectMe->Host,
920 cpptr->IO.ConnectMe->Port);
922 QueueQuery((cpptr->IO.ConnectMe->IPv6)? ns_t_aaaa : ns_t_a,
923 cpptr->IO.ConnectMe->Host,
926 pop3_get_one_host_ip_done);
927 IO->NextState = eReadDNSReply;
928 return IO->NextState;
933 int pop3_do_fetching(pop3aggr *cpptr)
935 AsyncIO *IO = &cpptr->IO;
940 POP3_C_ReadServerStatus,
942 POP3_C_DispatchWriteDone,
943 POP3_C_DispatchReadDone,
950 safestrncpy(((CitContext *)cpptr->IO.CitContext)->cs_host,
952 sizeof(((CitContext *)cpptr->IO.CitContext)->cs_host));
954 if (cpptr->IO.ConnectMe->IsIP) {
955 QueueEventContext(&cpptr->IO,
959 QueueEventContext(&cpptr->IO,
960 pop3_get_one_host_ip);
966 * Scan a room's netconfig to determine whether it requires POP3 aggregation
968 void pop3client_scan_room(struct ctdlroom *qrbuf, void *data, OneRoomNetCfg *OneRNCFG)
970 const RoomNetCfgLine *pLine;
973 pthread_mutex_lock(&POP3QueueMutex);
974 if (GetHash(POP3QueueRooms, LKEY(qrbuf->QRnumber), &vptr))
976 pthread_mutex_unlock(&POP3QueueMutex);
978 "pop3client: [%ld] %s already in progress.",
983 pthread_mutex_unlock(&POP3QueueMutex);
985 if (server_shutting_down) return;
987 pLine = OneRNCFG->NetConfigs[pop3client];
989 while (pLine != NULL)
993 cptr = (pop3aggr *) malloc(sizeof(pop3aggr));
994 memset(cptr, 0, sizeof(pop3aggr));
995 ///TODO do we need this? cptr->roomlist_parts=1;
996 cptr->RoomName = NewStrBufPlain(qrbuf->QRname, -1);
997 cptr->pop3user = NewStrBufDup(pLine->Value[1]);
998 cptr->pop3pass = NewStrBufDup(pLine->Value[2]);
999 cptr->Url = NewStrBuf();
1000 cptr->Host = NewStrBufDup(pLine->Value[0]);
1002 cptr->keep = atol(ChrPtr(pLine->Value[3]));
1003 cptr->interval = atol(ChrPtr(pLine->Value[4]));
1005 StrBufAppendBufPlain(cptr->Url, HKEY("pop3://"), 0);
1006 StrBufUrlescUPAppend(cptr->Url, cptr->pop3user, NULL);
1007 StrBufAppendBufPlain(cptr->Url, HKEY(":"), 0);
1008 StrBufUrlescUPAppend(cptr->Url, cptr->pop3pass, NULL);
1009 StrBufAppendBufPlain(cptr->Url, HKEY("@"), 0);
1010 StrBufAppendBuf(cptr->Url, cptr->Host, 0);
1011 StrBufAppendBufPlain(cptr->Url, HKEY("/"), 0);
1012 StrBufUrlescAppend(cptr->Url, cptr->RoomName, NULL);
1014 ParseURL(&cptr->IO.ConnectMe, cptr->Url, 110);
1018 /* todo: we need to reunite the url to be shure. */
1020 pthread_mutex_lock(&POP3ueueMutex);
1021 GetHash(POP3FetchUrls, SKEY(ptr->Url), &vptr);
1022 use_this_cptr = (pop3aggr *)vptr;
1024 if (use_this_rncptr != NULL)
1026 /* mustn't attach to an active session */
1027 if (use_this_cptr->RefCount > 0)
1029 DeletePOP3Cfg(cptr);
1035 StrBufAppendBufPlain(
1036 use_this_cptr->rooms,
1039 if (use_this_cptr->roomlist_parts == 1)
1041 use_this_cptr->OtherQRnumbers
1042 = NewHash(1, lFlathash);
1044 QRnumber = (long*)malloc(sizeof(long));
1045 *QRnumber = qrbuf->QRnumber;
1046 Put(use_this_cptr->OtherQRnumbers,
1047 LKEY(qrbuf->QRnumber),
1051 use_this_cptr->roomlist_parts++;
1053 pthread_mutex_unlock(&POP3QueueMutex);
1056 pthread_mutex_unlock(&RSSQueueMutex);
1058 cptr->n = Pop3ClientID++;
1059 pthread_mutex_lock(&POP3QueueMutex);
1063 DeletePOP3Aggregator);
1065 pthread_mutex_unlock(&POP3QueueMutex);
1066 pLine = pLine->next;
1071 static int doing_pop3client = 0;
1073 void pop3client_scan(void) {
1074 static time_t last_run = 0L;
1075 time_t fastest_scan;
1082 become_session(&pop3_client_CC);
1084 if (CtdlGetConfigLong("c_pop3_fastest") < CtdlGetConfigLong("c_pop3_fetch"))
1085 fastest_scan = CtdlGetConfigLong("c_pop3_fastest");
1087 fastest_scan = CtdlGetConfigLong("c_pop3_fetch");
1090 * Run POP3 aggregation no more frequently than once every n seconds
1092 if ( (time(NULL) - last_run) < fastest_scan ) {
1097 * This is a simple concurrency check to make sure only one pop3client
1098 * run is done at a time. We could do this with a mutex, but since we
1099 * don't really require extremely fine granularity here, we'll do it
1100 * with a static variable instead.
1102 if (doing_pop3client) return;
1103 doing_pop3client = 1;
1105 syslog(LOG_DEBUG, "pop3client started");
1106 CtdlForEachNetCfgRoom(pop3client_scan_room, NULL);
1108 pthread_mutex_lock(&POP3QueueMutex);
1109 it = GetNewHashPos(POP3FetchUrls, 0);
1110 while (!server_shutting_down &&
1111 GetNextHashPos(POP3FetchUrls, it, &len, &Key, &vrptr) &&
1113 cptr = (pop3aggr *)vrptr;
1114 if (cptr->RefCount == 0)
1115 if (!pop3_do_fetching(cptr))
1116 DeletePOP3Aggregator(cptr);////TODO
1119 if ((palist->interval && time(NULL) > (last_run + palist->interval))
1120 || (time(NULL) > last_run + CtdlGetConfigLong("c_pop3_fetch")))
1121 pop3_do_fetching(palist->roomname, palist->pop3host,
1122 palist->pop3user, palist->pop3pass, palist->keep);
1124 palist = palist->next;
1129 pthread_mutex_unlock(&POP3QueueMutex);
1131 syslog(LOG_DEBUG, "pop3client ended");
1132 last_run = time(NULL);
1133 doing_pop3client = 0;
1137 void pop3_cleanup(void)
1139 /* citthread_mutex_destroy(&POP3QueueMutex); TODO */
1140 while (doing_pop3client != 0) ;
1141 DeleteHash(&POP3FetchUrls);
1142 DeleteHash(&POP3QueueRooms);
1147 CTDL_MODULE_INIT(pop3client)
1151 CtdlFillSystemContext(&pop3_client_CC, "POP3aggr");
1152 CtdlREGISTERRoomCfgType(pop3client, ParseGeneric, 0, 5, SerializeGeneric, DeleteGenericCfgLine);
1153 pthread_mutex_init(&POP3QueueMutex, NULL);
1154 POP3QueueRooms = NewHash(1, lFlathash);
1155 POP3FetchUrls = NewHash(1, NULL);
1156 CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER, PRIO_AGGR + 50);
1157 CtdlRegisterEVCleanupHook(pop3_cleanup);
1160 /* return our module id for the log */
1161 return "pop3client";