2 * Consolidate mail from remote POP3 accounts.
4 * Copyright (c) 2007-2012 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 version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
19 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
24 # include <sys/time.h>
33 #include <sys/types.h>
35 #include <libcitadel.h>
38 #include "citserver.h"
41 #include "ctdl_module.h"
42 #include "clientsocket.h"
44 #include "internet_addressing.h"
46 #include "citadel_dirs.h"
47 #include "event_client.h"
49 #define POP3C_OK (strncasecmp(ChrPtr(RecvMsg->IO.IOBuf), "+OK", 3) == 0)
51 #define POP3C_DBG_SEND() \
53 "POP3 client[%ld]: > %s\n", \
54 RecvMsg->n, ChrPtr(RecvMsg->IO.SendBuf.Buf))
56 #define POP3C_DBG_READ() \
58 "POP3 client[%ld]: < %s\n", \
60 ChrPtr(RecvMsg->IO.IOBuf))
62 struct CitContext pop3_client_CC;
64 pthread_mutex_t POP3QueueMutex; /* locks the access to the following vars: */
65 HashList *POP3QueueRooms = NULL;
66 HashList *POP3FetchUrls = NULL;
68 typedef struct pop3aggr pop3aggr;
69 typedef eNextState(*Pop3ClientHandler)(pop3aggr* RecvMsg);
71 eNextState POP3_C_Shutdown(AsyncIO *IO);
72 eNextState POP3_C_Timeout(AsyncIO *IO);
73 eNextState POP3_C_ConnFail(AsyncIO *IO);
74 eNextState POP3_C_DNSFail(AsyncIO *IO);
75 eNextState POP3_C_DispatchReadDone(AsyncIO *IO);
76 eNextState POP3_C_DispatchWriteDone(AsyncIO *IO);
77 eNextState POP3_C_Terminate(AsyncIO *IO);
78 eReadState POP3_C_ReadServerStatus(AsyncIO *IO);
79 eNextState POP3_C_ReAttachToFetchMessages(AsyncIO *IO);
81 typedef struct __pop3_room_counter {
86 typedef enum ePOP3_C_States {
93 ReadMessageBodyFollowing,
101 typedef struct _FetchItem {
107 struct CtdlMessage *Msg;
110 void HfreeFetchItem(void *vItem)
112 FetchItem *Item = (FetchItem*) vItem;
113 FreeStrBuf(&Item->MsgUIDL);
114 FreeStrBuf(&Item->MsgUID);
123 DNSQueryParts HostLookup;
126 HashList *OtherQRnumbers;
131 StrBuf *RoomName; // TODO: fill me
134 ePOP3_C_States State;
135 HashList *MsgNumbers;
140 void DeletePOP3Aggregator(void *vptr)
142 pop3aggr *ptr = vptr;
143 DeleteHashPos(&ptr->Pos);
144 DeleteHash(&ptr->MsgNumbers);
145 // FreeStrBuf(&ptr->rooms);
146 FreeStrBuf(&ptr->pop3user);
147 FreeStrBuf(&ptr->pop3pass);
148 FreeStrBuf(&ptr->RoomName);
149 FreeURL(&ptr->IO.ConnectMe);
150 FreeStrBuf(&ptr->Url);
151 FreeStrBuf(&ptr->IO.IOBuf);
152 FreeStrBuf(&ptr->IO.SendBuf.Buf);
153 FreeStrBuf(&ptr->IO.RecvBuf.Buf);
154 DeleteAsyncMsg(&ptr->IO.ReadMsg);
155 ((struct CitContext*)ptr->IO.CitContext)->state = CON_IDLE;
156 ((struct CitContext*)ptr->IO.CitContext)->kill_me = 1;
157 FreeAsyncIOContents(&ptr->IO);
161 eNextState FinalizePOP3AggrRun(AsyncIO *IO)
164 pop3aggr *cptr = (pop3aggr *)IO->Data;
166 syslog(LOG_DEBUG, "Terminating Aggregator; bye.\n");
168 It = GetNewHashPos(POP3FetchUrls, 0);
169 pthread_mutex_lock(&POP3QueueMutex);
171 if (GetHashPosFromKey(POP3FetchUrls, SKEY(cptr->Url), It))
172 DeleteEntryFromHash(POP3FetchUrls, It);
174 pthread_mutex_unlock(&POP3QueueMutex);
179 eNextState FailAggregationRun(AsyncIO *IO)
184 eNextState POP3C_ReadGreeting(pop3aggr *RecvMsg)
187 /* Read the server greeting */
188 if (!POP3C_OK) return eTerminateConnection;
189 else return eSendReply;
192 eNextState POP3C_SendUser(pop3aggr *RecvMsg)
194 /* Identify ourselves. NOTE: we have to append a CR to each command.
195 * The LF will automatically be appended by sock_puts(). Believe it
196 * or not, leaving out the CR will cause problems if the server happens
197 * to be Exchange, which is so b0rken it actually barfs on
198 * LF-terminated newlines.
200 StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
201 "USER %s\r\n", ChrPtr(RecvMsg->pop3user));
206 eNextState POP3C_GetUserState(pop3aggr *RecvMsg)
209 if (!POP3C_OK) return eTerminateConnection;
210 else return eSendReply;
213 eNextState POP3C_SendPassword(pop3aggr *RecvMsg)
216 StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
217 "PASS %s\r\n", ChrPtr(RecvMsg->pop3pass));
218 syslog(LOG_DEBUG, "<PASS <password>\n");
219 // POP3C_DBG_SEND(); No, we won't write the password to syslog...
223 eNextState POP3C_GetPassState(pop3aggr *RecvMsg)
226 if (!POP3C_OK) return eTerminateConnection;
227 else return eSendReply;
230 eNextState POP3C_SendListCommand(pop3aggr *RecvMsg)
232 /* Get the list of messages */
233 StrBufPlain(RecvMsg->IO.SendBuf.Buf, HKEY("LIST\r\n"));
238 eNextState POP3C_GetListCommandState(pop3aggr *RecvMsg)
241 if (!POP3C_OK) return eTerminateConnection;
242 RecvMsg->MsgNumbers = NewHash(1, NULL);
248 eNextState POP3C_GetListOneLine(pop3aggr *RecvMsg)
254 FetchItem *OneMsg = NULL;
257 if ((StrLength(RecvMsg->IO.IOBuf) == 1) &&
258 (ChrPtr(RecvMsg->IO.IOBuf)[0] == '.'))
260 if (GetCount(RecvMsg->MsgNumbers) == 0)
262 //// RecvMsg->Sate = ReadQuitState;
266 RecvMsg->Pos = GetNewHashPos(RecvMsg->MsgNumbers, 0);
271 OneMsg = (FetchItem*) malloc(sizeof(FetchItem));
272 memset(OneMsg, 0, sizeof(FetchItem));
273 OneMsg->MSGID = atol(ChrPtr(RecvMsg->IO.IOBuf));
275 pch = strchr(ChrPtr(RecvMsg->IO.IOBuf), ' ');
278 OneMsg->MSGSize = atol(pch + 1);
281 rc = TestValidateHash(RecvMsg->MsgNumbers);
283 syslog(LOG_DEBUG, "Hash Invalid: %d\n", rc);
286 Put(RecvMsg->MsgNumbers, LKEY(OneMsg->MSGID), OneMsg, HfreeFetchItem);
288 rc = TestValidateHash(RecvMsg->MsgNumbers);
290 syslog(LOG_DEBUG, "Hash Invalid: %d\n", rc);
292 //RecvMsg->State --; /* read next Line */
296 eNextState POP3_FetchNetworkUsetableEntry(AsyncIO *IO)
301 struct cdbdata *cdbut;
302 pop3aggr *RecvMsg = (pop3aggr *) IO->Data;
304 if(GetNextHashPos(RecvMsg->MsgNumbers,
311 if (server_shutting_down)
314 RecvMsg->CurrMsg = (FetchItem*) vData;
316 "CHECKING: whether %s has already been seen: ",
317 ChrPtr(RecvMsg->CurrMsg->MsgUID));
319 /* Find out if we've already seen this item */
320 safestrncpy(ut.ut_msgid,
321 ChrPtr(RecvMsg->CurrMsg->MsgUID),
322 sizeof(ut.ut_msgid));
323 ut.ut_timestamp = time(NULL);/// TODO: libev timestamp!
325 cdbut = cdb_fetch(CDB_USETABLE, SKEY(RecvMsg->CurrMsg->MsgUID));
327 /* Item has already been seen */
328 syslog(LOG_DEBUG, "YES\n");
331 /* rewrite the record anyway, to update the timestamp */
332 cdb_store(CDB_USETABLE,
333 SKEY(RecvMsg->CurrMsg->MsgUID),
334 &ut, sizeof(struct UseTable) );
335 RecvMsg->CurrMsg->NeedFetch = 0; ////TODO0;
339 syslog(LOG_DEBUG, "NO\n");
340 RecvMsg->CurrMsg->NeedFetch = 1;
342 return NextDBOperation(&RecvMsg->IO,
343 POP3_FetchNetworkUsetableEntry);
347 /* ok, now we know them all,
348 * continue with reading the actual messages. */
349 DeleteHashPos(&RecvMsg->Pos);
351 return QueueEventContext(IO, POP3_C_ReAttachToFetchMessages);
355 eNextState POP3C_GetOneMessagID(pop3aggr *RecvMsg)
363 rc = TestValidateHash(RecvMsg->MsgNumbers);
365 syslog(LOG_DEBUG, "Hash Invalid: %d\n", rc);
367 if(GetNextHashPos(RecvMsg->MsgNumbers,
372 RecvMsg->CurrMsg = (FetchItem*) vData;
373 /* Find out the UIDL of the message,
374 * to determine whether we've already downloaded it */
375 StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
376 "UIDL %ld\r\n", RecvMsg->CurrMsg->MSGID);
382 DeleteHashPos(&RecvMsg->Pos);
383 /// done receiving uidls.. start looking them up now.
384 RecvMsg->Pos = GetNewHashPos(RecvMsg->MsgNumbers, 0);
385 return QueueDBOperation(&RecvMsg->IO,
386 POP3_FetchNetworkUsetableEntry);
388 return eReadMore; /* TODO */
391 eNextState POP3C_GetOneMessageIDState(pop3aggr *RecvMsg)
395 rc = TestValidateHash(RecvMsg->MsgNumbers);
397 syslog(LOG_DEBUG, "Hash Invalid: %d\n", rc);
401 if (!POP3C_OK) return eTerminateConnection;
402 RecvMsg->CurrMsg->MsgUIDL =
403 NewStrBufPlain(NULL, StrLength(RecvMsg->IO.IOBuf));
404 RecvMsg->CurrMsg->MsgUID =
405 NewStrBufPlain(NULL, StrLength(RecvMsg->IO.IOBuf) * 2);
407 StrBufExtract_token(RecvMsg->CurrMsg->MsgUIDL,
408 RecvMsg->IO.IOBuf, 2, ' ');
410 StrBufPrintf(RecvMsg->CurrMsg->MsgUID,
412 ChrPtr(RecvMsg->RoomName),
413 ChrPtr(RecvMsg->CurrMsg->MsgUIDL),
414 RecvMsg->IO.ConnectMe->User,
415 RecvMsg->IO.ConnectMe->Host);
421 eNextState POP3C_SendGetOneMsg(pop3aggr *RecvMsg)
427 RecvMsg->CurrMsg = NULL;
428 while (GetNextHashPos(RecvMsg->MsgNumbers,
432 (RecvMsg->CurrMsg = (FetchItem*) vData,
433 RecvMsg->CurrMsg->NeedFetch == 0))
436 if ((RecvMsg->CurrMsg != NULL ) && (RecvMsg->CurrMsg->NeedFetch == 1))
438 /* Message has not been seen.
439 * Tell the server to fetch the message... */
440 StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
441 "RETR %ld\r\n", RecvMsg->CurrMsg->MSGID);
446 RecvMsg->State = ReadQuitState;
447 return POP3_C_DispatchWriteDone(&RecvMsg->IO);
452 eNextState POP3C_ReadMessageBodyFollowing(pop3aggr *RecvMsg)
455 if (!POP3C_OK) return eTerminateConnection;
456 RecvMsg->IO.ReadMsg = NewAsyncMsg(HKEY("."),
457 RecvMsg->CurrMsg->MSGSize,
466 eNextState POP3C_StoreMsgRead(AsyncIO *IO)
468 pop3aggr *RecvMsg = (pop3aggr *) IO->Data;
472 "MARKING: %s as seen: ",
473 ChrPtr(RecvMsg->CurrMsg->MsgUID));
475 safestrncpy(ut.ut_msgid,
476 ChrPtr(RecvMsg->CurrMsg->MsgUID),
477 sizeof(ut.ut_msgid));
478 ut.ut_timestamp = time(NULL); /* TODO: use libev time */
479 cdb_store(CDB_USETABLE,
480 ChrPtr(RecvMsg->CurrMsg->MsgUID),
481 StrLength(RecvMsg->CurrMsg->MsgUID),
483 sizeof(struct UseTable) );
485 return QueueEventContext(&RecvMsg->IO, POP3_C_ReAttachToFetchMessages);
487 eNextState POP3C_SaveMsg(AsyncIO *IO)
490 pop3aggr *RecvMsg = (pop3aggr *) IO->Data;
492 /* Do Something With It (tm) */
493 msgnum = CtdlSubmitMsg(RecvMsg->CurrMsg->Msg,
495 ChrPtr(RecvMsg->RoomName),
499 /* Message has been committed to the store
500 * write the uidl to the use table
501 * so we don't fetch this message again
504 CtdlFreeMessage(RecvMsg->CurrMsg->Msg);
506 return NextDBOperation(&RecvMsg->IO, POP3C_StoreMsgRead);
509 eNextState POP3C_ReadMessageBody(pop3aggr *RecvMsg)
511 syslog(LOG_DEBUG, "Converting message...\n");
512 RecvMsg->CurrMsg->Msg =
513 convert_internet_message_buf(&RecvMsg->IO.ReadMsg->MsgBuf);
515 return QueueDBOperation(&RecvMsg->IO, POP3C_SaveMsg);
518 eNextState POP3C_SendDelete(pop3aggr *RecvMsg)
520 if (!RecvMsg->keep) {
521 StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
522 "DELE %ld\r\n", RecvMsg->CurrMsg->MSGID);
527 RecvMsg->State = ReadMessageBodyFollowing;
528 return POP3_C_DispatchWriteDone(&RecvMsg->IO);
531 eNextState POP3C_ReadDeleteState(pop3aggr *RecvMsg)
534 RecvMsg->State = GetOneMessageIDState;
538 eNextState POP3C_SendQuit(pop3aggr *RecvMsg)
541 StrBufPlain(RecvMsg->IO.SendBuf.Buf,
548 eNextState POP3C_ReadQuitState(pop3aggr *RecvMsg)
551 return eTerminateConnection;
554 const long POP3_C_ConnTimeout = 1000;
555 const long DefaultPOP3Port = 110;
557 Pop3ClientHandler POP3C_ReadHandlers[] = {
561 POP3C_GetListCommandState,
562 POP3C_GetListOneLine,
563 POP3C_GetOneMessageIDState,
564 POP3C_ReadMessageBodyFollowing,
565 POP3C_ReadMessageBody,
566 POP3C_ReadDeleteState,
570 const long POP3_C_SendTimeouts[POP3C_MaxRead] = {
580 const ConstStr POP3C_ReadErrors[POP3C_MaxRead] = {
581 {HKEY("Connection broken during ")},
582 {HKEY("Connection broken during ")},
583 {HKEY("Connection broken during ")},
584 {HKEY("Connection broken during ")},
585 {HKEY("Connection broken during ")},
586 {HKEY("Connection broken during ")},
587 {HKEY("Connection broken during ")},
588 {HKEY("Connection broken during ")}
591 Pop3ClientHandler POP3C_SendHandlers[] = {
592 NULL, /* we don't send a greeting */
595 POP3C_SendListCommand,
597 POP3C_GetOneMessagID,
604 const long POP3_C_ReadTimeouts[] = {
616 /*****************************************************************************/
617 /* POP3 CLIENT DISPATCHER */
618 /*****************************************************************************/
620 void POP3SetTimeout(eNextState NextTCPState, pop3aggr *pMsg)
622 double Timeout = 0.0;
624 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
626 switch (NextTCPState) {
630 Timeout = POP3_C_SendTimeouts[pMsg->State];
632 if (pMsg->State == eDATABody) {
633 / * if we're sending a huge message, we need more time. * /
634 Timeout += StrLength(pMsg->msgtext) / 1024;
640 Timeout = POP3_C_ReadTimeouts[pMsg->State];
642 if (pMsg->State == eDATATerminateBody) {
644 * some mailservers take a nap before accepting the message
645 * content inspection and such.
647 Timeout += StrLength(pMsg->msgtext) / 1024;
658 case eTerminateConnection:
661 case eReadMore://// TODO
664 SetNextTimeout(&pMsg->IO, Timeout);
666 eNextState POP3_C_DispatchReadDone(AsyncIO *IO)
668 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
669 pop3aggr *pMsg = IO->Data;
672 rc = POP3C_ReadHandlers[pMsg->State](pMsg);
675 POP3SetTimeout(rc, pMsg);
678 eNextState POP3_C_DispatchWriteDone(AsyncIO *IO)
680 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
681 pop3aggr *pMsg = IO->Data;
684 rc = POP3C_SendHandlers[pMsg->State](pMsg);
685 POP3SetTimeout(rc, pMsg);
690 /*****************************************************************************/
691 /* POP3 CLIENT ERROR CATCHERS */
692 /*****************************************************************************/
693 eNextState POP3_C_Terminate(AsyncIO *IO)
695 /// pop3aggr *pMsg = (pop3aggr *)IO->Data;
697 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
698 FinalizePOP3AggrRun(IO);
701 eNextState POP3_C_TerminateDB(AsyncIO *IO)
703 /// pop3aggr *pMsg = (pop3aggr *)IO->Data;
705 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
706 FinalizePOP3AggrRun(IO);
709 eNextState POP3_C_Timeout(AsyncIO *IO)
711 pop3aggr *pMsg = IO->Data;
713 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
714 StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State]));
715 return FailAggregationRun(IO);
717 eNextState POP3_C_ConnFail(AsyncIO *IO)
719 pop3aggr *pMsg = (pop3aggr *)IO->Data;
721 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
722 StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State]));
723 return FailAggregationRun(IO);
725 eNextState POP3_C_DNSFail(AsyncIO *IO)
727 pop3aggr *pMsg = (pop3aggr *)IO->Data;
729 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
730 StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State]));
731 return FailAggregationRun(IO);
733 eNextState POP3_C_Shutdown(AsyncIO *IO)
735 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
736 //// pop3aggr *pMsg = IO->Data;
738 ////pMsg->MyQEntry->Status = 3;
739 ///StrBufPlain(pMsg->MyQEntry->StatusMessage, HKEY("server shutdown during message retrieval."));
740 FinalizePOP3AggrRun(IO);
746 * lineread Handler; understands when to read more POP3 lines,
747 * and when this is a one-lined reply.
749 eReadState POP3_C_ReadServerStatus(AsyncIO *IO)
751 eReadState Finished = eBufferNotEmpty;
753 switch (IO->NextState) {
758 case eTerminateConnection:
760 Finished = eReadFail;
767 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
771 Finished = CtdlReadMessageBodyAsync(IO);
777 /*****************************************************************************
778 * So we connect our Server IP here. *
779 *****************************************************************************
781 eNextState POP3_C_ReAttachToFetchMessages(AsyncIO *IO)
783 pop3aggr *cpptr = IO->Data;
785 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
786 ////??? cpptr->State ++;
787 if (cpptr->Pos == NULL)
788 cpptr->Pos = GetNewHashPos(cpptr->MsgNumbers, 0);
790 POP3_C_DispatchWriteDone(IO);
791 ReAttachIO(IO, cpptr, 0);
792 IO->NextState = eReadMessage;
793 return IO->NextState;
796 eNextState pop3_connect_ip(AsyncIO *IO)
798 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
800 return EvConnectSock(IO,
802 POP3_C_ReadTimeouts[0],
806 eNextState pop3_get_one_host_ip_done(AsyncIO *IO)
808 pop3aggr *cpptr = IO->Data;
809 struct hostent *hostent;
813 hostent = cpptr->HostLookup.VParsedDNSReply;
814 if ((cpptr->HostLookup.DNSStatus == ARES_SUCCESS) &&
815 (hostent != NULL) ) {
816 memset(&cpptr->IO.ConnectMe->Addr, 0, sizeof(struct in6_addr));
817 if (cpptr->IO.ConnectMe->IPv6) {
818 memcpy(&cpptr->IO.ConnectMe->Addr.sin6_addr.s6_addr,
819 &hostent->h_addr_list[0],
820 sizeof(struct in6_addr));
822 cpptr->IO.ConnectMe->Addr.sin6_family =
824 cpptr->IO.ConnectMe->Addr.sin6_port =
825 htons(DefaultPOP3Port);
828 struct sockaddr_in *addr =
829 (struct sockaddr_in*)
830 &cpptr->IO.ConnectMe->Addr;
832 memcpy(&addr->sin_addr.s_addr,
833 hostent->h_addr_list[0],
836 addr->sin_family = hostent->h_addrtype;
837 addr->sin_port = htons(DefaultPOP3Port);
839 return pop3_connect_ip(IO);
845 eNextState pop3_get_one_host_ip(AsyncIO *IO)
847 pop3aggr *cpptr = IO->Data;
849 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
852 "POP3 client[%ld]: looking up %s-Record %s : %d ...\n",
854 (cpptr->IO.ConnectMe->IPv6)? "aaaa": "a",
855 cpptr->IO.ConnectMe->Host,
856 cpptr->IO.ConnectMe->Port);
858 QueueQuery((cpptr->IO.ConnectMe->IPv6)? ns_t_aaaa : ns_t_a,
859 cpptr->IO.ConnectMe->Host,
862 pop3_get_one_host_ip_done);
863 IO->NextState = eReadDNSReply;
864 return IO->NextState;
869 int pop3_do_fetching(pop3aggr *cpptr)
871 InitIOStruct(&cpptr->IO,
874 POP3_C_ReadServerStatus,
876 POP3_C_DispatchWriteDone,
877 POP3_C_DispatchReadDone,
884 safestrncpy(((CitContext *)cpptr->IO.CitContext)->cs_host,
886 sizeof(((CitContext *)cpptr->IO.CitContext)->cs_host));
888 if (cpptr->IO.ConnectMe->IsIP) {
889 QueueEventContext(&cpptr->IO,
893 QueueEventContext(&cpptr->IO,
894 pop3_get_one_host_ip);
900 * Scan a room's netconfig to determine whether it requires POP3 aggregation
902 void pop3client_scan_room(struct ctdlroom *qrbuf, void *data)
909 char filename[PATH_MAX];
913 const char *CfgPtr, *lPtr;
916 // pop3_room_counter *Count = NULL;
919 pthread_mutex_lock(&POP3QueueMutex);
920 if (GetHash(POP3QueueRooms, LKEY(qrbuf->QRnumber), &vptr))
923 "pop3client: [%ld] %s already in progress.\n",
926 pthread_mutex_unlock(&POP3QueueMutex);
928 pthread_mutex_unlock(&POP3QueueMutex);
930 if (server_shutting_down) return;
932 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
934 if (server_shutting_down)
937 /* Only do net processing for rooms that have netconfigs */
938 fd = open(filename, 0);
942 if (server_shutting_down)
944 if (fstat(fd, &statbuf) == -1) {
946 "ERROR: could not stat configfile '%s' - %s\n",
951 if (server_shutting_down)
953 CfgData = NewStrBufPlain(NULL, statbuf.st_size + 1);
954 if (StrBufReadBLOB(CfgData, &fd, 1, statbuf.st_size, &Err) < 0) {
956 FreeStrBuf(&CfgData);
957 syslog(LOG_DEBUG, "ERROR: reading config '%s' - %s<br>\n",
958 filename, strerror(errno));
962 if (server_shutting_down)
966 CfgType = NewStrBuf();
967 Line = NewStrBufPlain(NULL, StrLength(CfgData));
972 Done = StrBufSipLine(Line, CfgData, &CfgPtr) == 0;
973 if (StrLength(Line) > 0)
976 StrBufExtract_NextToken(CfgType, Line, &lPtr, '|');
977 if (!strcasecmp("pop3client", ChrPtr(CfgType)))
984 Count = malloc(sizeof(pop3_room_counter));
989 cptr = (pop3aggr *) malloc(sizeof(pop3aggr));
990 memset(cptr, 0, sizeof(pop3aggr));
991 ///TODO do we need this? cptr->roomlist_parts=1;
993 NewStrBufPlain(qrbuf->QRname, -1);
995 NewStrBufPlain(NULL, StrLength(Line));
997 NewStrBufPlain(NULL, StrLength(Line));
998 cptr->Url = NewStrBuf();
1001 StrBufExtract_NextToken(Tmp, Line, &lPtr, '|');
1002 StrBufExtract_NextToken(cptr->pop3user,
1007 StrBufExtract_NextToken(cptr->pop3pass,
1012 cptr->keep = StrBufExtractNext_long(Line,
1016 cptr->interval = StrBufExtractNext_long(Line,
1020 StrBufPrintf(cptr->Url, "pop3://%s:%s@%s/%s",
1021 ChrPtr(cptr->pop3user),
1022 ChrPtr(cptr->pop3pass),
1024 ChrPtr(cptr->RoomName));
1026 ParseURL(&cptr->IO.ConnectMe, cptr->Url, 110);
1030 /* todo: we need to reunite the url to be shure. */
1032 pthread_mutex_lock(&POP3ueueMutex);
1033 GetHash(POP3FetchUrls, SKEY(ptr->Url), &vptr);
1034 use_this_cptr = (pop3aggr *)vptr;
1036 if (use_this_rncptr != NULL)
1038 /* mustn't attach to an active session */
1039 if (use_this_cptr->RefCount > 0)
1041 DeletePOP3Cfg(cptr);
1047 StrBufAppendBufPlain(
1048 use_this_cptr->rooms,
1051 if (use_this_cptr->roomlist_parts == 1)
1053 use_this_cptr->OtherQRnumbers
1054 = NewHash(1, lFlathash);
1056 QRnumber = (long*)malloc(sizeof(long));
1057 *QRnumber = qrbuf->QRnumber;
1058 Put(use_this_cptr->OtherQRnumbers,
1059 LKEY(qrbuf->QRnumber),
1063 use_this_cptr->roomlist_parts++;
1065 pthread_mutex_unlock(&POP3QueueMutex);
1068 pthread_mutex_unlock(&RSSQueueMutex);
1071 pthread_mutex_lock(&POP3QueueMutex);
1075 DeletePOP3Aggregator);
1077 pthread_mutex_unlock(&POP3QueueMutex);
1087 FreeStrBuf(&CfgType);
1088 FreeStrBuf(&CfgData);
1091 static int doing_pop3client = 0;
1093 void pop3client_scan(void) {
1094 static time_t last_run = 0L;
1095 time_t fastest_scan;
1102 become_session(&pop3_client_CC);
1104 if (config.c_pop3_fastest < config.c_pop3_fetch)
1105 fastest_scan = config.c_pop3_fastest;
1107 fastest_scan = config.c_pop3_fetch;
1110 * Run POP3 aggregation no more frequently than once every n seconds
1112 if ( (time(NULL) - last_run) < fastest_scan ) {
1114 "pop3client: polling interval not yet reached; last run was %ldm%lds ago",
1115 ((time(NULL) - last_run) / 60),
1116 ((time(NULL) - last_run) % 60)
1122 * This is a simple concurrency check to make sure only one pop3client
1123 * run is done at a time. We could do this with a mutex, but since we
1124 * don't really require extremely fine granularity here, we'll do it
1125 * with a static variable instead.
1127 if (doing_pop3client) {
1128 syslog(LOG_DEBUG, "pop3client: concurrency check failed; another poll is already running");
1131 doing_pop3client = 1;
1133 syslog(LOG_DEBUG, "pop3client started");
1134 CtdlForEachRoom(pop3client_scan_room, NULL);
1136 pthread_mutex_lock(&POP3QueueMutex);
1137 it = GetNewHashPos(POP3FetchUrls, 0);
1138 while (!server_shutting_down &&
1139 GetNextHashPos(POP3FetchUrls, it, &len, &Key, &vrptr) &&
1141 cptr = (pop3aggr *)vrptr;
1142 if (cptr->RefCount == 0)
1143 if (!pop3_do_fetching(cptr))
1144 DeletePOP3Aggregator(cptr);////TODO
1147 if ((palist->interval && time(NULL) > (last_run + palist->interval))
1148 || (time(NULL) > last_run + config.c_pop3_fetch))
1149 pop3_do_fetching(palist->roomname, palist->pop3host,
1150 palist->pop3user, palist->pop3pass, palist->keep);
1152 palist = palist->next;
1157 pthread_mutex_unlock(&POP3QueueMutex);
1159 syslog(LOG_DEBUG, "pop3client ended");
1160 last_run = time(NULL);
1161 doing_pop3client = 0;
1165 void pop3_cleanup(void)
1167 /* citthread_mutex_destroy(&POP3QueueMutex); TODO */
1168 while (doing_pop3client != 0) ;
1169 DeleteHash(&POP3FetchUrls);
1170 DeleteHash(&POP3QueueRooms);
1173 CTDL_MODULE_INIT(pop3client)
1177 CtdlFillSystemContext(&pop3_client_CC, "POP3aggr");
1178 pthread_mutex_init(&POP3QueueMutex, NULL);
1179 POP3QueueRooms = NewHash(1, lFlathash);
1180 POP3FetchUrls = NewHash(1, NULL);
1181 CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER);
1182 CtdlRegisterEVCleanupHook(pop3_cleanup);
1185 /* return our module id for the log */
1186 return "pop3client";