2 * Consolidate mail from remote POP3 accounts.
4 * Copyright (c) 2007-2009 by the citadel.org team
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * 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.
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
25 #if TIME_WITH_SYS_TIME
26 # include <sys/time.h>
30 # include <sys/time.h>
39 #include <sys/types.h>
41 #include <libcitadel.h>
44 #include "citserver.h"
47 #include "ctdl_module.h"
48 #include "clientsocket.h"
50 #include "internet_addressing.h"
52 #include "citadel_dirs.h"
53 #include "event_client.h"
57 citthread_mutex_t POP3QueueMutex; /* locks the access to the following vars: */
58 HashList *POP3QueueRooms = NULL; /* rss_room_counter */
59 HashList *POP3FetchUrls = NULL; /* -> rss_aggregator; ->RefCount access to be locked too. */
61 typedef struct __pop3_room_counter {
66 typedef enum ePOP3_C_States {
72 ReadMessageBodyFollowing,
79 typedef struct _FetchItem {
86 void HfreeFetchItem(void *vItem)
88 FetchItem *Item = (FetchItem*) vItem;
89 FreeStrBuf(&Item->MsgUIDL);
90 FreeStrBuf(&Item->MsgUID);
94 typedef struct __pop3aggr {
100 DNSQueryParts HostLookup;
104 HashList *OtherQRnumbers;
107 /// StrBuf *pop3host; -> URL
110 StrBuf *RoomName; // TODO: fill me
113 ePOP3_C_States State;
114 HashList *MsgNumbers;
119 void DeletePOP3Aggregator(void *vptr)
121 pop3aggr *ptr = vptr;
122 DeleteHashPos(&ptr->Pos);
123 DeleteHash(&ptr->MsgNumbers);
124 FreeStrBuf(&ptr->rooms);
125 FreeStrBuf(&ptr->pop3user);
126 FreeStrBuf(&ptr->pop3pass);
127 FreeStrBuf(&ptr->RoomName);
131 typedef eNextState(*Pop3ClientHandler)(pop3aggr* RecvMsg);
133 eNextState POP3_C_Shutdown(AsyncIO *IO);
134 eNextState POP3_C_Timeout(AsyncIO *IO);
135 eNextState POP3_C_ConnFail(AsyncIO *IO);
136 eNextState POP3_C_DispatchReadDone(AsyncIO *IO);
137 eNextState POP3_C_DispatchWriteDone(AsyncIO *IO);
138 eNextState POP3_C_Terminate(AsyncIO *IO);
139 eReadState POP3_C_ReadServerStatus(AsyncIO *IO);
141 eNextState FinalizePOP3AggrRun(AsyncIO *IO)
147 eNextState FailAggregationRun(AsyncIO *IO)
152 #define POP3C_DBG_SEND() CtdlLogPrintf(CTDL_DEBUG, "POP3 client[%ld]: > %s\n", RecvMsg->n, ChrPtr(RecvMsg->IO.SendBuf.Buf))
153 #define POP3C_DBG_READ() CtdlLogPrintf(CTDL_DEBUG, "POP3 client[%ld]: < %s\n", RecvMsg->n, ChrPtr(RecvMsg->IO.IOBuf))
154 #define POP3C_OK (strncasecmp(ChrPtr(RecvMsg->IO.IOBuf), "+OK", 3) == 0)
156 eNextState POP3C_ReadGreeting(pop3aggr *RecvMsg)
159 /* Read the server greeting */
160 if (!POP3C_OK) return eTerminateConnection;
161 else return eSendReply;
165 eNextState POP3C_SendUser(pop3aggr *RecvMsg)
167 /* Identify ourselves. NOTE: we have to append a CR to each command. The LF will
168 * automatically be appended by sock_puts(). Believe it or not, leaving out the CR
169 * will cause problems if the server happens to be Exchange, which is so b0rken it
170 * actually barfs on LF-terminated newlines.
172 StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
173 "USER %s\r\n", ChrPtr(RecvMsg->pop3user));
178 eNextState POP3C_GetUserState(pop3aggr *RecvMsg)
181 if (!POP3C_OK) return eTerminateConnection;
182 else return eSendReply;
185 eNextState POP3C_SendPassword(pop3aggr *RecvMsg)
188 StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
189 "PASS %s\r\n", ChrPtr(RecvMsg->pop3pass));
190 CtdlLogPrintf(CTDL_DEBUG, "<PASS <password>\n");
195 eNextState POP3C_GetPassState(pop3aggr *RecvMsg)
198 if (!POP3C_OK) return eTerminateConnection;
199 else return eSendReply;
202 eNextState POP3C_SendListCommand(pop3aggr *RecvMsg)
204 /* Get the list of messages */
205 StrBufPlain(RecvMsg->IO.SendBuf.Buf, HKEY("LIST\r\n"));
210 eNextState POP3C_GetListCommandState(pop3aggr *RecvMsg)
213 if (!POP3C_OK) return eTerminateConnection;
214 RecvMsg->MsgNumbers = NewHash(1, NULL);
219 eNextState POP3C_GetListOneLine(pop3aggr *RecvMsg)
221 FetchItem *OneMsg = NULL;
224 if ((StrLength(RecvMsg->IO.IOBuf) == 1) &&
225 (ChrPtr(RecvMsg->IO.IOBuf)[0] == '.'))
227 if (GetCount(RecvMsg->MsgNumbers) == 0)
229 //// RecvMsg->Sate = ReadQuitState;
233 RecvMsg->Pos = GetNewHashPos(RecvMsg->MsgNumbers, 0);
238 OneMsg = (FetchItem*) malloc(sizeof(FetchItem));
239 memset(OneMsg, 0, sizeof(FetchItem));
240 OneMsg->MSGID = atoi(ChrPtr(RecvMsg->IO.IOBuf));
241 Put(RecvMsg->MsgNumbers, LKEY(OneMsg->MSGID), OneMsg, HfreeFetchItem);
243 //RecvMsg->State --; /* read next Line */
247 eNextState POP3C_GetOneMessagID(pop3aggr *RecvMsg)
253 if(GetNextHashPos(RecvMsg->MsgNumbers, RecvMsg->Pos, &HKLen, &HKey, &vData))
255 RecvMsg->CurrMsg = (FetchItem*) vData;
256 /* Find out the UIDL of the message, to determine whether we've already downloaded it */
257 StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
258 "UIDL %ld\r\n", RecvMsg->CurrMsg->MSGID);
263 DeleteHashPos(&RecvMsg->Pos);
264 /// done receiving uidls.. start looking them up now.
265 RecvMsg->Pos = GetNewHashPos(RecvMsg->MsgNumbers, 0);
268 return eReadMore; /* TODO */
272 eNextState FetchNetworkUsetableEntry(AsyncIO *IO)
274 struct cdbdata *cdbut;
275 networker_save_message *Ctx = (networker_save_message *) IO->Data;
277 if(GetNextHashPos(RecvMsg->MsgNumbers, RecvMsg->Pos, &HKLen, &HKey, &vData))
280 /* Find out if we've already seen this item */
281 strcpy(Ctx->ut.ut_msgid, ChrPtr(Ctx->MsgGUID)); /// TODO
282 Ctx->ut.ut_timestamp = time(NULL);
284 cdbut = cdb_fetch(CDB_USETABLE, SKEY(Ctx->MsgGUID));
286 /* Item has already been seen */
287 CtdlLogPrintf(CTDL_DEBUG, "%s has already been seen\n", ChrPtr(Ctx->MsgGUID));
290 /* rewrite the record anyway, to update the timestamp */
291 cdb_store(CDB_USETABLE,
293 &Ctx->ut, sizeof(struct UseTable) );
298 NextDBOperation(IO, RSSSaveMessage);
307 eNextState POP3C_GetOneMessageIDState(pop3aggr *RecvMsg)
310 if (!POP3C_OK) return eTerminateConnection;
311 RecvMsg->CurrMsg->MsgUIDL = NewStrBufPlain(NULL, StrLength(RecvMsg->IO.IOBuf));
312 RecvMsg->CurrMsg->MsgUID = NewStrBufPlain(NULL, StrLength(RecvMsg->IO.IOBuf) * 2);
314 StrBufExtract_token(RecvMsg->CurrMsg->MsgUIDL, RecvMsg->IO.IOBuf, 2, ' ');
315 StrBufPrintf(RecvMsg->CurrMsg->MsgUID,
317 ChrPtr(RecvMsg->RoomName),
318 ChrPtr(RecvMsg->CurrMsg->MsgUIDL),
319 RecvMsg->Pop3Host.Host);
323 eNextState POP3C_GetOneMessageIDFromUseTable(pop3aggr *RecvMsg)
326 struct cdbdata *cdbut;
329 cdbut = cdb_fetch(CDB_USETABLE, SKEY(RecvMsg->CurrMsg->MsgUID));
331 /* message has already been seen */
332 CtdlLogPrintf(CTDL_DEBUG, "%s has already been seen\n", ChrPtr(RecvMsg->CurrMsg->MsgUID));
335 /* rewrite the record anyway, to update the timestamp */
336 strcpy(ut.ut_msgid, ChrPtr(RecvMsg->CurrMsg->MsgUID));
337 ut.ut_timestamp = time(NULL);
338 cdb_store(CDB_USETABLE, SKEY(RecvMsg->CurrMsg->MsgUID), &ut, sizeof(struct UseTable) );
344 eNextState POP3C_SendGetOneMsg(pop3aggr *RecvMsg)
346 /* Message has not been seen. Tell the server to fetch the message... */
347 StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
348 "RETR %ld\r\n", RecvMsg->CurrMsg->MSGID);
354 eNextState POP3C_ReadMessageBodyFollowing(pop3aggr *RecvMsg)
357 if (!POP3C_OK) return eTerminateConnection;
358 else return eSendReply;
363 eNextState POP3C_ReadMessageBody(pop3aggr *RecvMsg)
367 /* If we get to this point, the message is on its way. Read it. */
368 body = CtdlReadMessageBody(HKEY("."), config.c_maxmsglen, NULL, 1, &sock);
369 if (body == NULL) goto bail;
371 CtdlLogPrintf(CTDL_DEBUG, "Converting message...\n");
372 msg = convert_internet_message(body);
373 body = NULL; /* yes, this should be dereferenced, NOT freed */
375 /* Do Something With It (tm) */
376 msgnum = CtdlSubmitMsg(msg, NULL, roomname, 0);
378 /* Message has been committed to the store */
379 /* write the uidl to the use table so we don't fetch this message again */
381 CtdlFreeMessage(msg);
386 eNextState POP3C_StoreMsgRead(pop3aggr *RecvMsg)
389 strcpy(ut.ut_msgid, utmsgid);
390 ut.ut_timestamp = time(NULL);
391 cdb_store(CDB_USETABLE, utmsgid, strlen(utmsgid),
392 &ut, sizeof(struct UseTable) );
394 return eReadMessage;/// TODO
396 eNextState POP3C_SendDelete(pop3aggr *RecvMsg)
398 if (!RecvMsg->keep) {
399 StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
400 "DELE %ld\r\n", RecvMsg->CurrMsg->MSGID);
405 eNextState POP3C_ReadDeleteState(pop3aggr *RecvMsg)
411 eNextState POP3C_SendQuit(pop3aggr *RecvMsg)
414 StrBufPlain(RecvMsg->IO.SendBuf.Buf,
421 eNextState POP3C_ReadQuitState(pop3aggr *RecvMsg)
427 const long POP3_C_ConnTimeout = 1000;
428 const long DefaultPOP3Port = 110;
430 Pop3ClientHandler POP3C_ReadHandlers[] = {
434 POP3C_GetListCommandState,
435 POP3C_GetOneMessageIDState,
436 POP3C_ReadMessageBodyFollowing,
437 POP3C_ReadMessageBody,
441 const long POP3_C_SendTimeouts[POP3C_MaxRead] = {
451 const ConstStr POP3C_ReadErrors[POP3C_MaxRead] = {
452 {HKEY("Connection broken during ")},
453 {HKEY("Connection broken during ")},
454 {HKEY("Connection broken during ")},
455 {HKEY("Connection broken during ")},
456 {HKEY("Connection broken during ")},
457 {HKEY("Connection broken during ")},
458 {HKEY("Connection broken during ")},
459 {HKEY("Connection broken during ")}
462 Pop3ClientHandler POP3C_SendHandlers[] = {
463 NULL, /* we don't send a greeting */
466 POP3C_SendListCommand,
467 POP3C_GetListOneLine,
468 POP3C_GetOneMessagID,
474 const long POP3_C_ReadTimeouts[] = {
486 /*****************************************************************************/
487 /* POP3 CLIENT DISPATCHER */
488 /*****************************************************************************/
490 void POP3SetTimeout(eNextState NextTCPState, pop3aggr *pMsg)
492 double Timeout = 0.0;
494 CtdlLogPrintf(CTDL_DEBUG, "POP3: %s\n", __FUNCTION__);
496 switch (NextTCPState) {
499 Timeout = POP3_C_SendTimeouts[pMsg->State];
501 if (pMsg->State == eDATABody) {
502 / * if we're sending a huge message, we need more time. * /
503 Timeout += StrLength(pMsg->msgtext) / 1024;
508 Timeout = POP3_C_ReadTimeouts[pMsg->State];
510 if (pMsg->State == eDATATerminateBody) {
512 * some mailservers take a nap before accepting the message
513 * content inspection and such.
515 Timeout += StrLength(pMsg->msgtext) / 1024;
522 case eTerminateConnection:
524 case eReadMore://// TODO
527 SetNextTimeout(&pMsg->IO, Timeout);
529 eNextState POP3_C_DispatchReadDone(AsyncIO *IO)
531 CtdlLogPrintf(CTDL_DEBUG, "POP3: %s\n", __FUNCTION__);
532 pop3aggr *pMsg = IO->Data;
535 rc = POP3C_ReadHandlers[pMsg->State](pMsg);
537 POP3SetTimeout(rc, pMsg);
540 eNextState POP3_C_DispatchWriteDone(AsyncIO *IO)
542 CtdlLogPrintf(CTDL_DEBUG, "POP3: %s\n", __FUNCTION__);
543 pop3aggr *pMsg = IO->Data;
546 rc = POP3C_SendHandlers[pMsg->State](pMsg);
547 POP3SetTimeout(rc, pMsg);
552 /*****************************************************************************/
553 /* POP3 CLIENT ERROR CATCHERS */
554 /*****************************************************************************/
555 eNextState POP3_C_Terminate(AsyncIO *IO)
557 /// pop3aggr *pMsg = (pop3aggr *)IO->Data;
559 CtdlLogPrintf(CTDL_DEBUG, "POP3: %s\n", __FUNCTION__);
560 FinalizePOP3AggrRun(IO);
563 eNextState POP3_C_Timeout(AsyncIO *IO)
565 pop3aggr *pMsg = IO->Data;
567 CtdlLogPrintf(CTDL_DEBUG, "POP3: %s\n", __FUNCTION__);
568 StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State]));
569 return FailAggregationRun(IO);
571 eNextState POP3_C_ConnFail(AsyncIO *IO)
573 pop3aggr *pMsg = (pop3aggr *)IO->Data;
575 CtdlLogPrintf(CTDL_DEBUG, "POP3: %s\n", __FUNCTION__);
576 StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State]));
577 return FailAggregationRun(IO);
579 eNextState POP3_C_Shutdown(AsyncIO *IO)
581 CtdlLogPrintf(CTDL_DEBUG, "POP3: %s\n", __FUNCTION__);
582 //// pop3aggr *pMsg = IO->Data;
584 ////pMsg->MyQEntry->Status = 3;
585 ///StrBufPlain(pMsg->MyQEntry->StatusMessage, HKEY("server shutdown during message retrieval."));
586 FinalizePOP3AggrRun(IO);
592 * @brief lineread Handler; understands when to read more POP3 lines, and when this is a one-lined reply.
594 eReadState POP3_C_ReadServerStatus(AsyncIO *IO)
596 eReadState Finished = eBufferNotEmpty;
598 while (Finished == eBufferNotEmpty) {
599 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
602 case eMustReadMore: /// read new from socket...
605 case eBufferNotEmpty: /* shouldn't happen... */
606 case eReadSuccess: /// done for now...
607 if (StrLength(IO->IOBuf) < 4)
609 if (ChrPtr(IO->IOBuf)[3] == '-')
610 Finished = eBufferNotEmpty;
614 case eReadFail: /// WHUT?
622 /*****************************************************************************
623 * So we connect our Server IP here. *
624 *****************************************************************************/
625 eNextState connect_ip(AsyncIO *IO)
627 pop3aggr *cpptr = IO->Data;
629 CtdlLogPrintf(CTDL_DEBUG, "POP3: %s\n", __FUNCTION__);
631 //// IO->ConnectMe = &cpptr->Pop3Host;
632 /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
634 /////// SetConnectStatus(IO);
636 return InitEventIO(IO, cpptr,
638 POP3_C_ReadTimeouts[0],
642 eNextState get_one_host_ip_done(AsyncIO *IO)
644 pop3aggr *cpptr = IO->Data;
645 struct hostent *hostent;
649 hostent = cpptr->HostLookup.VParsedDNSReply;
650 if ((cpptr->HostLookup.DNSStatus == ARES_SUCCESS) &&
651 (hostent != NULL) ) {
652 memset(&cpptr->Pop3Host.Addr, 0, sizeof(struct in6_addr));
653 if (cpptr->Pop3Host.IPv6) {
654 memcpy(&cpptr->Pop3Host.Addr.sin6_addr.s6_addr,
655 &hostent->h_addr_list[0],
656 sizeof(struct in6_addr));
658 cpptr->Pop3Host.Addr.sin6_family = hostent->h_addrtype;
659 cpptr->Pop3Host.Addr.sin6_port = htons(DefaultPOP3Port);
662 struct sockaddr_in *addr = (struct sockaddr_in*) &cpptr->Pop3Host.Addr;
663 /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
664 // addr->sin_addr.s_addr = htonl((uint32_t)&hostent->h_addr_list[0]);
665 memcpy(&addr->sin_addr.s_addr,
666 hostent->h_addr_list[0],
669 addr->sin_family = hostent->h_addrtype;
670 addr->sin_port = htons(DefaultPOP3Port);
673 return connect_ip(IO);
679 eNextState get_one_host_ip(AsyncIO *IO)
681 pop3aggr *cpptr = IO->Data;
683 * here we start with the lookup of one host. it might be...
684 * - the relay host *sigh*
685 * - the direct hostname if there was no mx record
691 CtdlLogPrintf(CTDL_DEBUG, "POP3: %s\n", __FUNCTION__);
693 CtdlLogPrintf(CTDL_DEBUG,
694 "POP3 client[%ld]: looking up %s-Record %s : %d ...\n",
696 (cpptr->Pop3Host.IPv6)? "aaaa": "a",
697 cpptr->Pop3Host.Host,
698 cpptr->Pop3Host.Port);
700 if (!QueueQuery((cpptr->Pop3Host.IPv6)? ns_t_aaaa : ns_t_a,
701 cpptr->Pop3Host.Host,
704 get_one_host_ip_done))
706 // cpptr->MyQEntry->Status = 5;
707 // StrBufPrintf(SendMsg->MyQEntry->StatusMessage,
708 // "No MX hosts found for <%s>", SendMsg->node);
709 cpptr->IO.NextState = eTerminateConnection;
710 return IO->NextState;
712 IO->NextState = eReadDNSReply;
713 return IO->NextState;
718 int pop3_do_fetching(pop3aggr *cpptr)
722 cpptr->IO.Data = cpptr;
724 cpptr->IO.SendDone = POP3_C_DispatchWriteDone;
725 cpptr->IO.ReadDone = POP3_C_DispatchReadDone;
726 cpptr->IO.Terminate = POP3_C_Terminate;
727 cpptr->IO.LineReader = POP3_C_ReadServerStatus;
728 cpptr->IO.ConnFail = POP3_C_ConnFail;
729 cpptr->IO.Timeout = POP3_C_Timeout;
730 cpptr->IO.ShutdownAbort = POP3_C_Shutdown;
732 cpptr->IO.SendBuf.Buf = NewStrBufPlain(NULL, 1024);
733 cpptr->IO.RecvBuf.Buf = NewStrBufPlain(NULL, 1024);
734 cpptr->IO.IOBuf = NewStrBuf();
736 cpptr->IO.NextState = eReadMessage;
738 CtdlLogPrintf(CTDL_DEBUG, "POP3: %s %s %s <password>\n", roomname, pop3host, pop3user);
739 CtdlLogPrintf(CTDL_NOTICE, "Connecting to <%s>\n", pop3host);
742 SubC = CloneContext (CC);
743 SubC->session_specific_data = (char*) cpptr;
744 cpptr->IO.CitContext = SubC;
746 if (cpptr->IO.ConnectMe->IsIP) {
747 QueueEventContext(&cpptr->IO,
750 else { /* uneducated admin has chosen to add DNS to the equation... */
751 QueueEventContext(&cpptr->IO,
758 * Scan a room's netconfig to determine whether it requires POP3 aggregation
760 void pop3client_scan_room(struct ctdlroom *qrbuf, void *data)
767 char filename[PATH_MAX];
771 const char *CfgPtr, *lPtr;
774 pop3_room_counter *Count = NULL;
777 citthread_mutex_lock(&POP3QueueMutex);
778 if (GetHash(POP3QueueRooms, LKEY(qrbuf->QRnumber), &vptr))
780 CtdlLogPrintf(CTDL_DEBUG,
781 "pop3client: [%ld] %s already in progress.\n",
784 citthread_mutex_unlock(&POP3QueueMutex);
787 citthread_mutex_unlock(&POP3QueueMutex);
789 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
791 if (CtdlThreadCheckStop())
794 /* Only do net processing for rooms that have netconfigs */
795 fd = open(filename, 0);
797 //CtdlLogPrintf(CTDL_DEBUG, "rssclient: %s no config.\n", qrbuf->QRname);
800 if (CtdlThreadCheckStop())
802 if (fstat(fd, &statbuf) == -1) {
803 CtdlLogPrintf(CTDL_DEBUG, "ERROR: could not stat configfile '%s' - %s\n",
804 filename, strerror(errno));
807 if (CtdlThreadCheckStop())
809 CfgData = NewStrBufPlain(NULL, statbuf.st_size + 1);
810 if (StrBufReadBLOB(CfgData, &fd, 1, statbuf.st_size, &Err) < 0) {
812 FreeStrBuf(&CfgData);
813 CtdlLogPrintf(CTDL_DEBUG, "ERROR: reading config '%s' - %s<br>\n",
814 filename, strerror(errno));
818 if (CtdlThreadCheckStop())
822 CfgType = NewStrBuf();
823 Line = NewStrBufPlain(NULL, StrLength(CfgData));
828 Done = StrBufSipLine(Line, CfgData, &CfgPtr) == 0;
829 if (StrLength(Line) > 0)
832 StrBufExtract_NextToken(CfgType, Line, &lPtr, '|');
833 if (!strcasecmp("pop3client", ChrPtr(CfgType)))
839 Count = malloc(sizeof(pop3_room_counter));
843 cptr = (pop3aggr *) malloc(sizeof(pop3aggr));
844 memset(cptr, 0, sizeof(pop3aggr));
845 /// TODO do we need this? cptr->roomlist_parts = 1;
846 cptr->rooms = NewStrBufPlain(qrbuf->QRname, -1);
847 cptr->pop3user = NewStrBufPlain(NULL, StrLength(Line));
848 cptr->pop3pass = NewStrBufPlain(NULL, StrLength(Line));
849 cptr->Url = NewStrBuf();
851 StrBufExtract_NextToken(cptr->Url, Line, &lPtr, '|');
852 StrBufExtract_NextToken(cptr->pop3user, Line, &lPtr, '|');
853 StrBufExtract_NextToken(cptr->pop3pass, Line, &lPtr, '|');
854 cptr->keep = StrBufExtractNext_long(Line, &lPtr, '|');
855 cptr->interval = StrBufExtractNext_long(Line, &lPtr, '|');
857 ParseURL(&cptr->IO.ConnectMe, cptr->Url, 110);
859 cptr->IO.ConnectMe->CurlCreds = cptr->pop3user;
860 cptr->IO.ConnectMe->User = ChrPtr(cptr->IO.ConnectMe->CurlCreds);
861 cptr->IO.ConnectMe->UrlWithoutCred = cptr->pop3pass;
862 cptr->IO.ConnectMe->Pass = ChrPtr(cptr->IO.ConnectMe->UrlWithoutCred);
867 /* todo: we need to reunite the url to be shure. */
869 citthread_mutex_lock(&POP3ueueMutex);
870 GetHash(POP3FetchUrls, SKEY(ptr->Url), &vptr);
871 use_this_cptr = (pop3aggr *)vptr;
873 if (use_this_rncptr != NULL)
875 /* mustn't attach to an active session */
876 if (use_this_cptr->RefCount > 0)
884 StrBufAppendBufPlain(use_this_cptr->rooms,
887 if (use_this_cptr->roomlist_parts == 1)
889 use_this_cptr->OtherQRnumbers = NewHash(1, lFlathash);
891 QRnumber = (long*)malloc(sizeof(long));
892 *QRnumber = qrbuf->QRnumber;
893 Put(use_this_cptr->OtherQRnumbers, LKEY(qrbuf->QRnumber), QRnumber, NULL);
894 use_this_cptr->roomlist_parts++;
896 citthread_mutex_unlock(&POP3QueueMutex);
899 citthread_mutex_unlock(&RSSQueueMutex);
902 citthread_mutex_lock(&POP3QueueMutex);
903 Put(POP3FetchUrls, SKEY(cptr->Url), cptr, DeletePOP3Aggregator);
904 citthread_mutex_unlock(&POP3QueueMutex);
916 void pop3client_scan(void) {
917 static time_t last_run = 0L;
918 static int doing_pop3client = 0;
919 /// struct pop3aggr *pptr;
927 if (config.c_pop3_fastest < config.c_pop3_fetch)
928 fastest_scan = config.c_pop3_fastest;
930 fastest_scan = config.c_pop3_fetch;
933 * Run POP3 aggregation no more frequently than once every n seconds
935 if ( (time(NULL) - last_run) < fastest_scan ) {
940 * This is a simple concurrency check to make sure only one pop3client run
941 * is done at a time. We could do this with a mutex, but since we
942 * don't really require extremely fine granularity here, we'll do it
943 * with a static variable instead.
945 if (doing_pop3client) return;
946 doing_pop3client = 1;
948 CtdlLogPrintf(CTDL_DEBUG, "pop3client started\n");
949 CtdlForEachRoom(pop3client_scan_room, NULL);
952 citthread_mutex_lock(&POP3QueueMutex);
953 it = GetNewHashPos(POP3FetchUrls, 0);
954 while (GetNextHashPos(POP3FetchUrls, it, &len, &Key, &vrptr) &&
956 cptr = (pop3aggr *)vrptr;
957 if (cptr->RefCount == 0)
958 if (!pop3_do_fetching(cptr))
959 DeletePOP3Aggregator(cptr);////TODO
962 citthread_mutex_unlock(&POP3QueueMutex);
964 CtdlLogPrintf(CTDL_DEBUG, "pop3client ended\n");
965 last_run = time(NULL);
966 doing_pop3client = 0;
970 void pop3_cleanup(void)
972 citthread_mutex_destroy(&POP3QueueMutex);
973 DeleteHash(&POP3FetchUrls);
974 DeleteHash(&POP3QueueRooms);
977 CTDL_MODULE_INIT(pop3client)
981 citthread_mutex_init(&POP3QueueMutex, NULL);
982 POP3QueueRooms = NewHash(1, lFlathash);
983 POP3FetchUrls = NewHash(1, NULL);
984 CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER);
985 CtdlRegisterCleanupHook(pop3_cleanup);
987 /* return our Subversion id for the Log */