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 {
73 ReadMessageBodyFollowing,
81 typedef struct _FetchItem {
87 struct CtdlMessage *Msg;
90 void HfreeFetchItem(void *vItem)
92 FetchItem *Item = (FetchItem*) vItem;
93 FreeStrBuf(&Item->MsgUIDL);
94 FreeStrBuf(&Item->MsgUID);
98 typedef struct __pop3aggr {
104 DNSQueryParts HostLookup;
108 HashList *OtherQRnumbers;
111 /// StrBuf *pop3host; -> URL
114 StrBuf *RoomName; // TODO: fill me
117 ePOP3_C_States State;
118 HashList *MsgNumbers;
123 void DeletePOP3Aggregator(void *vptr)
125 pop3aggr *ptr = vptr;
126 DeleteHashPos(&ptr->Pos);
127 DeleteHash(&ptr->MsgNumbers);
128 // FreeStrBuf(&ptr->rooms);
129 FreeStrBuf(&ptr->pop3user);
130 FreeStrBuf(&ptr->pop3pass);
131 FreeStrBuf(&ptr->RoomName);
135 typedef eNextState(*Pop3ClientHandler)(pop3aggr* RecvMsg);
137 eNextState POP3_C_Shutdown(AsyncIO *IO);
138 eNextState POP3_C_Timeout(AsyncIO *IO);
139 eNextState POP3_C_ConnFail(AsyncIO *IO);
140 eNextState POP3_C_DispatchReadDone(AsyncIO *IO);
141 eNextState POP3_C_DispatchWriteDone(AsyncIO *IO);
142 eNextState POP3_C_Terminate(AsyncIO *IO);
143 eReadState POP3_C_ReadServerStatus(AsyncIO *IO);
144 eNextState POP3_C_ReAttachToFetchMessages(AsyncIO *IO);
146 eNextState FinalizePOP3AggrRun(AsyncIO *IO)
149 pop3aggr *cptr = (pop3aggr *)IO->Data;
151 CtdlLogPrintf(CTDL_DEBUG, "Terminating Aggregator; bye.\n");
153 It = GetNewHashPos(POP3FetchUrls, 0);
154 citthread_mutex_lock(&POP3QueueMutex);
156 GetHashPosFromKey(POP3FetchUrls, SKEY(cptr->Url), It);
157 DeleteEntryFromHash(POP3FetchUrls, It);
159 citthread_mutex_unlock(&POP3QueueMutex);
164 eNextState FailAggregationRun(AsyncIO *IO)
169 #define POP3C_DBG_SEND() CtdlLogPrintf(CTDL_DEBUG, "POP3 client[%ld]: > %s\n", RecvMsg->n, ChrPtr(RecvMsg->IO.SendBuf.Buf))
170 #define POP3C_DBG_READ() CtdlLogPrintf(CTDL_DEBUG, "POP3 client[%ld]: < %s\n", RecvMsg->n, ChrPtr(RecvMsg->IO.IOBuf))
171 #define POP3C_OK (strncasecmp(ChrPtr(RecvMsg->IO.IOBuf), "+OK", 3) == 0)
173 eNextState POP3C_ReadGreeting(pop3aggr *RecvMsg)
176 /* Read the server greeting */
177 if (!POP3C_OK) return eTerminateConnection;
178 else return eSendReply;
182 eNextState POP3C_SendUser(pop3aggr *RecvMsg)
184 /* Identify ourselves. NOTE: we have to append a CR to each command. The LF will
185 * automatically be appended by sock_puts(). Believe it or not, leaving out the CR
186 * will cause problems if the server happens to be Exchange, which is so b0rken it
187 * actually barfs on LF-terminated newlines.
189 StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
190 "USER %s\r\n", ChrPtr(RecvMsg->pop3user));
195 eNextState POP3C_GetUserState(pop3aggr *RecvMsg)
198 if (!POP3C_OK) return eTerminateConnection;
199 else return eSendReply;
202 eNextState POP3C_SendPassword(pop3aggr *RecvMsg)
205 StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
206 "PASS %s\r\n", ChrPtr(RecvMsg->pop3pass));
207 CtdlLogPrintf(CTDL_DEBUG, "<PASS <password>\n");
212 eNextState POP3C_GetPassState(pop3aggr *RecvMsg)
215 if (!POP3C_OK) return eTerminateConnection;
216 else return eSendReply;
219 eNextState POP3C_SendListCommand(pop3aggr *RecvMsg)
221 /* Get the list of messages */
222 StrBufPlain(RecvMsg->IO.SendBuf.Buf, HKEY("LIST\r\n"));
227 eNextState POP3C_GetListCommandState(pop3aggr *RecvMsg)
230 if (!POP3C_OK) return eTerminateConnection;
231 RecvMsg->MsgNumbers = NewHash(1, NULL);
237 eNextState POP3C_GetListOneLine(pop3aggr *RecvMsg)
240 FetchItem *OneMsg = NULL;
243 if ((StrLength(RecvMsg->IO.IOBuf) == 1) &&
244 (ChrPtr(RecvMsg->IO.IOBuf)[0] == '.'))
246 if (GetCount(RecvMsg->MsgNumbers) == 0)
248 //// RecvMsg->Sate = ReadQuitState;
252 RecvMsg->Pos = GetNewHashPos(RecvMsg->MsgNumbers, 0);
257 OneMsg = (FetchItem*) malloc(sizeof(FetchItem));
258 memset(OneMsg, 0, sizeof(FetchItem));
259 OneMsg->MSGID = atol(ChrPtr(RecvMsg->IO.IOBuf));
261 pch = strchr(ChrPtr(RecvMsg->IO.IOBuf), ' ');
264 OneMsg->MSGSize = atol(pch + 1);
266 Put(RecvMsg->MsgNumbers, LKEY(OneMsg->MSGID), OneMsg, HfreeFetchItem);
268 //RecvMsg->State --; /* read next Line */
272 eNextState POP3_FetchNetworkUsetableEntry(AsyncIO *IO)
277 struct cdbdata *cdbut;
278 pop3aggr *RecvMsg = (pop3aggr *) IO->Data;
280 if(GetNextHashPos(RecvMsg->MsgNumbers, RecvMsg->Pos, &HKLen, &HKey, &vData))
284 RecvMsg->CurrMsg = (FetchItem*) vData;
285 /* Find out if we've already seen this item */
286 safestrncpy(ut.ut_msgid,
287 ChrPtr(RecvMsg->CurrMsg->MsgUIDL),
288 sizeof(ut.ut_msgid));
289 ut.ut_timestamp = time(NULL);/// TODO: libev timestamp!
291 cdbut = cdb_fetch(CDB_USETABLE, SKEY(RecvMsg->CurrMsg->MsgUIDL));
293 /* Item has already been seen */
294 CtdlLogPrintf(CTDL_DEBUG, "%s has already been seen\n", ChrPtr(RecvMsg->CurrMsg->MsgUIDL));
297 /* rewrite the record anyway, to update the timestamp */
298 cdb_store(CDB_USETABLE,
299 SKEY(RecvMsg->CurrMsg->MsgUIDL),
300 &ut, sizeof(struct UseTable) );
301 RecvMsg->CurrMsg->NeedFetch = 0;
305 RecvMsg->CurrMsg->NeedFetch = 1;
307 return NextDBOperation(&RecvMsg->IO, POP3_FetchNetworkUsetableEntry);
311 /* ok, now we know them all, continue with reading the actual messages. */
312 DeleteHashPos(&RecvMsg->Pos);
314 return QueueEventContext(IO, POP3_C_ReAttachToFetchMessages);
318 eNextState POP3C_GetOneMessagID(pop3aggr *RecvMsg)
324 if(GetNextHashPos(RecvMsg->MsgNumbers, RecvMsg->Pos, &HKLen, &HKey, &vData))
326 RecvMsg->CurrMsg = (FetchItem*) vData;
327 /* Find out the UIDL of the message, to determine whether we've already downloaded it */
328 StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
329 "UIDL %ld\r\n", RecvMsg->CurrMsg->MSGID);
335 DeleteHashPos(&RecvMsg->Pos);
336 /// done receiving uidls.. start looking them up now.
337 RecvMsg->Pos = GetNewHashPos(RecvMsg->MsgNumbers, 0);
338 return QueueDBOperation(&RecvMsg->IO, POP3_FetchNetworkUsetableEntry);
340 return eReadMore; /* TODO */
343 eNextState POP3C_GetOneMessageIDState(pop3aggr *RecvMsg)
346 if (!POP3C_OK) return eTerminateConnection;
347 RecvMsg->CurrMsg->MsgUIDL = NewStrBufPlain(NULL, StrLength(RecvMsg->IO.IOBuf));
348 RecvMsg->CurrMsg->MsgUID = NewStrBufPlain(NULL, StrLength(RecvMsg->IO.IOBuf) * 2);
350 StrBufExtract_token(RecvMsg->CurrMsg->MsgUIDL, RecvMsg->IO.IOBuf, 2, ' ');
351 StrBufPrintf(RecvMsg->CurrMsg->MsgUID,
353 ChrPtr(RecvMsg->RoomName),
354 ChrPtr(RecvMsg->CurrMsg->MsgUIDL),
355 RecvMsg->Pop3Host.Host);
360 eNextState POP3C_GetOneMessageIDFromUseTable(pop3aggr *RecvMsg)
363 struct cdbdata *cdbut;
366 cdbut = cdb_fetch(CDB_USETABLE, SKEY(RecvMsg->CurrMsg->MsgUID));
368 /* message has already been seen */
369 CtdlLogPrintf(CTDL_DEBUG, "%s has already been seen\n", ChrPtr(RecvMsg->CurrMsg->MsgUID));
372 /* rewrite the record anyway, to update the timestamp */
373 strcpy(ut.ut_msgid, ChrPtr(RecvMsg->CurrMsg->MsgUID));
374 ut.ut_timestamp = time(NULL);
375 cdb_store(CDB_USETABLE, SKEY(RecvMsg->CurrMsg->MsgUID), &ut, sizeof(struct UseTable) );
381 eNextState POP3C_SendGetOneMsg(pop3aggr *RecvMsg)
387 RecvMsg->CurrMsg = NULL;
388 while (GetNextHashPos(RecvMsg->MsgNumbers, RecvMsg->Pos, &HKLen, &HKey, &vData) &&
389 (RecvMsg->CurrMsg = (FetchItem*) vData, RecvMsg->CurrMsg->NeedFetch == 0))
392 if ((RecvMsg->CurrMsg != NULL ) && (RecvMsg->CurrMsg->NeedFetch == 1))
394 /* Message has not been seen. Tell the server to fetch the message... */
395 StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
396 "RETR %ld\r\n", RecvMsg->CurrMsg->MSGID);
401 RecvMsg->State = ReadQuitState;
402 return POP3_C_DispatchWriteDone(&RecvMsg->IO);
407 eNextState POP3C_ReadMessageBodyFollowing(pop3aggr *RecvMsg)
410 if (!POP3C_OK) return eTerminateConnection;
411 RecvMsg->IO.ReadMsg = NewAsyncMsg(HKEY("."),
412 RecvMsg->CurrMsg->MSGSize,
421 eNextState POP3C_StoreMsgRead(AsyncIO *IO)
423 pop3aggr *RecvMsg = (pop3aggr *) IO->Data;
426 safestrncpy(ut.ut_msgid,
427 ChrPtr(RecvMsg->CurrMsg->MsgUID),
428 sizeof(ut.ut_msgid));
429 ut.ut_timestamp = time(NULL); /* TODO: use libev time */
430 cdb_store(CDB_USETABLE,
431 ChrPtr(RecvMsg->CurrMsg->MsgUID),
432 StrLength(RecvMsg->CurrMsg->MsgUID),
434 sizeof(struct UseTable) );
436 return QueueEventContext(&RecvMsg->IO, POP3_C_ReAttachToFetchMessages);
438 eNextState POP3C_SaveMsg(AsyncIO *IO)
441 pop3aggr *RecvMsg = (pop3aggr *) IO->Data;
443 /* Do Something With It (tm) */
444 msgnum = CtdlSubmitMsg(RecvMsg->CurrMsg->Msg,
446 ChrPtr(RecvMsg->RoomName),
449 /* Message has been committed to the store */
450 /* write the uidl to the use table so we don't fetch this message again */
452 CtdlFreeMessage(RecvMsg->CurrMsg->Msg);
454 return NextDBOperation(&RecvMsg->IO, POP3C_StoreMsgRead);
458 eNextState POP3C_ReadMessageBody(pop3aggr *RecvMsg)
460 CtdlLogPrintf(CTDL_DEBUG, "Converting message...\n");
461 RecvMsg->CurrMsg->Msg = convert_internet_message_buf(&RecvMsg->IO.ReadMsg->MsgBuf);
463 return QueueDBOperation(&RecvMsg->IO, POP3C_SaveMsg);
466 eNextState POP3C_SendDelete(pop3aggr *RecvMsg)
468 if (!RecvMsg->keep) {
469 StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
470 "DELE %ld\r\n", RecvMsg->CurrMsg->MSGID);
475 RecvMsg->State = ReadMessageBodyFollowing;
476 return POP3_C_DispatchWriteDone(&RecvMsg->IO);
479 eNextState POP3C_ReadDeleteState(pop3aggr *RecvMsg)
482 RecvMsg->State = GetOneMessageIDState;
486 eNextState POP3C_SendQuit(pop3aggr *RecvMsg)
489 StrBufPlain(RecvMsg->IO.SendBuf.Buf,
496 eNextState POP3C_ReadQuitState(pop3aggr *RecvMsg)
499 return eTerminateConnection;
502 const long POP3_C_ConnTimeout = 1000;
503 const long DefaultPOP3Port = 110;
505 Pop3ClientHandler POP3C_ReadHandlers[] = {
509 POP3C_GetListCommandState,
510 POP3C_GetListOneLine,
511 POP3C_GetOneMessageIDState,
512 POP3C_ReadMessageBodyFollowing,
513 POP3C_ReadMessageBody,
514 POP3C_ReadDeleteState,
518 const long POP3_C_SendTimeouts[POP3C_MaxRead] = {
528 const ConstStr POP3C_ReadErrors[POP3C_MaxRead] = {
529 {HKEY("Connection broken during ")},
530 {HKEY("Connection broken during ")},
531 {HKEY("Connection broken during ")},
532 {HKEY("Connection broken during ")},
533 {HKEY("Connection broken during ")},
534 {HKEY("Connection broken during ")},
535 {HKEY("Connection broken during ")},
536 {HKEY("Connection broken during ")}
539 Pop3ClientHandler POP3C_SendHandlers[] = {
540 NULL, /* we don't send a greeting */
543 POP3C_SendListCommand,
545 POP3C_GetOneMessagID,
552 const long POP3_C_ReadTimeouts[] = {
564 /*****************************************************************************/
565 /* POP3 CLIENT DISPATCHER */
566 /*****************************************************************************/
568 void POP3SetTimeout(eNextState NextTCPState, pop3aggr *pMsg)
570 double Timeout = 0.0;
572 CtdlLogPrintf(CTDL_DEBUG, "POP3: %s\n", __FUNCTION__);
574 switch (NextTCPState) {
577 Timeout = POP3_C_SendTimeouts[pMsg->State];
579 if (pMsg->State == eDATABody) {
580 / * if we're sending a huge message, we need more time. * /
581 Timeout += StrLength(pMsg->msgtext) / 1024;
586 Timeout = POP3_C_ReadTimeouts[pMsg->State];
588 if (pMsg->State == eDATATerminateBody) {
590 * some mailservers take a nap before accepting the message
591 * content inspection and such.
593 Timeout += StrLength(pMsg->msgtext) / 1024;
604 case eTerminateConnection:
607 case eReadMore://// TODO
610 SetNextTimeout(&pMsg->IO, Timeout);
612 eNextState POP3_C_DispatchReadDone(AsyncIO *IO)
614 CtdlLogPrintf(CTDL_DEBUG, "POP3: %s\n", __FUNCTION__);
615 pop3aggr *pMsg = IO->Data;
618 rc = POP3C_ReadHandlers[pMsg->State](pMsg);
621 POP3SetTimeout(rc, pMsg);
624 eNextState POP3_C_DispatchWriteDone(AsyncIO *IO)
626 CtdlLogPrintf(CTDL_DEBUG, "POP3: %s\n", __FUNCTION__);
627 pop3aggr *pMsg = IO->Data;
630 rc = POP3C_SendHandlers[pMsg->State](pMsg);
631 POP3SetTimeout(rc, pMsg);
636 /*****************************************************************************/
637 /* POP3 CLIENT ERROR CATCHERS */
638 /*****************************************************************************/
639 eNextState POP3_C_Terminate(AsyncIO *IO)
641 /// pop3aggr *pMsg = (pop3aggr *)IO->Data;
643 CtdlLogPrintf(CTDL_DEBUG, "POP3: %s\n", __FUNCTION__);
644 FinalizePOP3AggrRun(IO);
647 eNextState POP3_C_Timeout(AsyncIO *IO)
649 pop3aggr *pMsg = IO->Data;
651 CtdlLogPrintf(CTDL_DEBUG, "POP3: %s\n", __FUNCTION__);
652 StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State]));
653 return FailAggregationRun(IO);
655 eNextState POP3_C_ConnFail(AsyncIO *IO)
657 pop3aggr *pMsg = (pop3aggr *)IO->Data;
659 CtdlLogPrintf(CTDL_DEBUG, "POP3: %s\n", __FUNCTION__);
660 StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State]));
661 return FailAggregationRun(IO);
663 eNextState POP3_C_Shutdown(AsyncIO *IO)
665 CtdlLogPrintf(CTDL_DEBUG, "POP3: %s\n", __FUNCTION__);
666 //// pop3aggr *pMsg = IO->Data;
668 ////pMsg->MyQEntry->Status = 3;
669 ///StrBufPlain(pMsg->MyQEntry->StatusMessage, HKEY("server shutdown during message retrieval."));
670 FinalizePOP3AggrRun(IO);
676 * @brief lineread Handler; understands when to read more POP3 lines, and when this is a one-lined reply.
678 eReadState POP3_C_ReadServerStatus(AsyncIO *IO)
680 eReadState Finished = eBufferNotEmpty;
682 switch (IO->NextState) {
687 case eTerminateConnection:
689 Finished = eReadFail;
695 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
698 Finished = CtdlReadMessageBodyAsync(IO);
704 /*****************************************************************************
705 * So we connect our Server IP here. *
706 *****************************************************************************/
707 eNextState POP3_C_ReAttachToFetchMessages(AsyncIO *IO)
709 pop3aggr *cpptr = IO->Data;
711 CtdlLogPrintf(CTDL_DEBUG, "POP3: %s\n", __FUNCTION__);
712 ////??? cpptr->State ++;
713 if (cpptr->Pos == NULL)
714 cpptr->Pos = GetNewHashPos(cpptr->MsgNumbers, 0);
716 POP3_C_DispatchWriteDone(IO);
717 ReAttachIO(IO, cpptr, 0);
718 IO->NextState = eReadMessage;
719 return IO->NextState;
722 eNextState connect_ip(AsyncIO *IO)
724 pop3aggr *cpptr = IO->Data;
726 CtdlLogPrintf(CTDL_DEBUG, "POP3: %s\n", __FUNCTION__);
728 //// IO->ConnectMe = &cpptr->Pop3Host;
729 /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
731 /////// SetConnectStatus(IO);
733 return InitEventIO(IO, cpptr,
735 POP3_C_ReadTimeouts[0],
739 eNextState get_one_host_ip_done(AsyncIO *IO)
741 pop3aggr *cpptr = IO->Data;
742 struct hostent *hostent;
746 hostent = cpptr->HostLookup.VParsedDNSReply;
747 if ((cpptr->HostLookup.DNSStatus == ARES_SUCCESS) &&
748 (hostent != NULL) ) {
749 memset(&cpptr->Pop3Host.Addr, 0, sizeof(struct in6_addr));
750 if (cpptr->Pop3Host.IPv6) {
751 memcpy(&cpptr->Pop3Host.Addr.sin6_addr.s6_addr,
752 &hostent->h_addr_list[0],
753 sizeof(struct in6_addr));
755 cpptr->Pop3Host.Addr.sin6_family = hostent->h_addrtype;
756 cpptr->Pop3Host.Addr.sin6_port = htons(DefaultPOP3Port);
759 struct sockaddr_in *addr = (struct sockaddr_in*) &cpptr->Pop3Host.Addr;
760 /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
761 // addr->sin_addr.s_addr = htonl((uint32_t)&hostent->h_addr_list[0]);
762 memcpy(&addr->sin_addr.s_addr,
763 hostent->h_addr_list[0],
766 addr->sin_family = hostent->h_addrtype;
767 addr->sin_port = htons(DefaultPOP3Port);
770 return connect_ip(IO);
776 eNextState get_one_host_ip(AsyncIO *IO)
778 pop3aggr *cpptr = IO->Data;
780 * here we start with the lookup of one host. it might be...
781 * - the relay host *sigh*
782 * - the direct hostname if there was no mx record
788 CtdlLogPrintf(CTDL_DEBUG, "POP3: %s\n", __FUNCTION__);
790 CtdlLogPrintf(CTDL_DEBUG,
791 "POP3 client[%ld]: looking up %s-Record %s : %d ...\n",
793 (cpptr->Pop3Host.IPv6)? "aaaa": "a",
794 cpptr->Pop3Host.Host,
795 cpptr->Pop3Host.Port);
797 if (!QueueQuery((cpptr->Pop3Host.IPv6)? ns_t_aaaa : ns_t_a,
798 cpptr->Pop3Host.Host,
801 get_one_host_ip_done))
803 // cpptr->MyQEntry->Status = 5;
804 // StrBufPrintf(SendMsg->MyQEntry->StatusMessage,
805 // "No MX hosts found for <%s>", SendMsg->node);
806 cpptr->IO.NextState = eTerminateConnection;
807 return IO->NextState;
809 IO->NextState = eReadDNSReply;
810 return IO->NextState;
815 int pop3_do_fetching(pop3aggr *cpptr)
819 cpptr->IO.Data = cpptr;
821 cpptr->IO.SendDone = POP3_C_DispatchWriteDone;
822 cpptr->IO.ReadDone = POP3_C_DispatchReadDone;
823 cpptr->IO.Terminate = POP3_C_Terminate;
824 cpptr->IO.LineReader = POP3_C_ReadServerStatus;
825 cpptr->IO.ConnFail = POP3_C_ConnFail;
826 cpptr->IO.Timeout = POP3_C_Timeout;
827 cpptr->IO.ShutdownAbort = POP3_C_Shutdown;
829 cpptr->IO.SendBuf.Buf = NewStrBufPlain(NULL, 1024);
830 cpptr->IO.RecvBuf.Buf = NewStrBufPlain(NULL, 1024);
831 cpptr->IO.IOBuf = NewStrBuf();
833 cpptr->IO.NextState = eReadMessage;
835 CtdlLogPrintf(CTDL_DEBUG, "POP3: %s %s %s <password>\n", roomname, pop3host, pop3user);
836 CtdlLogPrintf(CTDL_NOTICE, "Connecting to <%s>\n", pop3host);
839 SubC = CloneContext (CC);
840 SubC->session_specific_data = (char*) cpptr;
841 cpptr->IO.CitContext = SubC;
843 if (cpptr->IO.ConnectMe->IsIP) {
844 QueueEventContext(&cpptr->IO,
847 else { /* uneducated admin has chosen to add DNS to the equation... */
848 QueueEventContext(&cpptr->IO,
855 * Scan a room's netconfig to determine whether it requires POP3 aggregation
857 void pop3client_scan_room(struct ctdlroom *qrbuf, void *data)
864 char filename[PATH_MAX];
868 const char *CfgPtr, *lPtr;
871 pop3_room_counter *Count = NULL;
874 citthread_mutex_lock(&POP3QueueMutex);
875 if (GetHash(POP3QueueRooms, LKEY(qrbuf->QRnumber), &vptr))
877 CtdlLogPrintf(CTDL_DEBUG,
878 "pop3client: [%ld] %s already in progress.\n",
881 citthread_mutex_unlock(&POP3QueueMutex);
884 citthread_mutex_unlock(&POP3QueueMutex);
886 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
888 if (CtdlThreadCheckStop())
891 /* Only do net processing for rooms that have netconfigs */
892 fd = open(filename, 0);
894 //CtdlLogPrintf(CTDL_DEBUG, "rssclient: %s no config.\n", qrbuf->QRname);
897 if (CtdlThreadCheckStop())
899 if (fstat(fd, &statbuf) == -1) {
900 CtdlLogPrintf(CTDL_DEBUG, "ERROR: could not stat configfile '%s' - %s\n",
901 filename, strerror(errno));
904 if (CtdlThreadCheckStop())
906 CfgData = NewStrBufPlain(NULL, statbuf.st_size + 1);
907 if (StrBufReadBLOB(CfgData, &fd, 1, statbuf.st_size, &Err) < 0) {
909 FreeStrBuf(&CfgData);
910 CtdlLogPrintf(CTDL_DEBUG, "ERROR: reading config '%s' - %s<br>\n",
911 filename, strerror(errno));
915 if (CtdlThreadCheckStop())
919 CfgType = NewStrBuf();
920 Line = NewStrBufPlain(NULL, StrLength(CfgData));
925 Done = StrBufSipLine(Line, CfgData, &CfgPtr) == 0;
926 if (StrLength(Line) > 0)
929 StrBufExtract_NextToken(CfgType, Line, &lPtr, '|');
930 if (!strcasecmp("pop3client", ChrPtr(CfgType)))
936 Count = malloc(sizeof(pop3_room_counter));
940 cptr = (pop3aggr *) malloc(sizeof(pop3aggr));
941 memset(cptr, 0, sizeof(pop3aggr));
942 /// TODO do we need this? cptr->roomlist_parts = 1;
943 cptr->RoomName = NewStrBufPlain(qrbuf->QRname, -1);
944 cptr->pop3user = NewStrBufPlain(NULL, StrLength(Line));
945 cptr->pop3pass = NewStrBufPlain(NULL, StrLength(Line));
946 cptr->Url = NewStrBuf();
948 StrBufExtract_NextToken(cptr->Url, Line, &lPtr, '|');
949 StrBufExtract_NextToken(cptr->pop3user, Line, &lPtr, '|');
950 StrBufExtract_NextToken(cptr->pop3pass, Line, &lPtr, '|');
951 cptr->keep = StrBufExtractNext_long(Line, &lPtr, '|');
952 cptr->interval = StrBufExtractNext_long(Line, &lPtr, '|');
954 ParseURL(&cptr->IO.ConnectMe, cptr->Url, 110);
956 cptr->IO.ConnectMe->CurlCreds = cptr->pop3user;
957 cptr->IO.ConnectMe->User = ChrPtr(cptr->IO.ConnectMe->CurlCreds);
958 cptr->IO.ConnectMe->UrlWithoutCred = cptr->pop3pass;
959 cptr->IO.ConnectMe->Pass = ChrPtr(cptr->IO.ConnectMe->UrlWithoutCred);
964 /* todo: we need to reunite the url to be shure. */
966 citthread_mutex_lock(&POP3ueueMutex);
967 GetHash(POP3FetchUrls, SKEY(ptr->Url), &vptr);
968 use_this_cptr = (pop3aggr *)vptr;
970 if (use_this_rncptr != NULL)
972 /* mustn't attach to an active session */
973 if (use_this_cptr->RefCount > 0)
981 StrBufAppendBufPlain(use_this_cptr->rooms,
984 if (use_this_cptr->roomlist_parts == 1)
986 use_this_cptr->OtherQRnumbers = NewHash(1, lFlathash);
988 QRnumber = (long*)malloc(sizeof(long));
989 *QRnumber = qrbuf->QRnumber;
990 Put(use_this_cptr->OtherQRnumbers, LKEY(qrbuf->QRnumber), QRnumber, NULL);
991 use_this_cptr->roomlist_parts++;
993 citthread_mutex_unlock(&POP3QueueMutex);
996 citthread_mutex_unlock(&RSSQueueMutex);
999 citthread_mutex_lock(&POP3QueueMutex);
1000 Put(POP3FetchUrls, SKEY(cptr->Url), cptr, DeletePOP3Aggregator);
1001 citthread_mutex_unlock(&POP3QueueMutex);
1013 void pop3client_scan(void) {
1014 static time_t last_run = 0L;
1015 static int doing_pop3client = 0;
1016 /// struct pop3aggr *pptr;
1017 time_t fastest_scan;
1024 if (config.c_pop3_fastest < config.c_pop3_fetch)
1025 fastest_scan = config.c_pop3_fastest;
1027 fastest_scan = config.c_pop3_fetch;
1030 * Run POP3 aggregation no more frequently than once every n seconds
1032 if ( (time(NULL) - last_run) < fastest_scan ) {
1037 * This is a simple concurrency check to make sure only one pop3client run
1038 * is done at a time. We could do this with a mutex, but since we
1039 * don't really require extremely fine granularity here, we'll do it
1040 * with a static variable instead.
1042 if (doing_pop3client) return;
1043 doing_pop3client = 1;
1045 CtdlLogPrintf(CTDL_DEBUG, "pop3client started\n");
1046 CtdlForEachRoom(pop3client_scan_room, NULL);
1049 citthread_mutex_lock(&POP3QueueMutex);
1050 it = GetNewHashPos(POP3FetchUrls, 0);
1051 while (GetNextHashPos(POP3FetchUrls, it, &len, &Key, &vrptr) &&
1053 cptr = (pop3aggr *)vrptr;
1054 if (cptr->RefCount == 0)
1055 if (!pop3_do_fetching(cptr))
1056 DeletePOP3Aggregator(cptr);////TODO
1059 citthread_mutex_unlock(&POP3QueueMutex);
1061 CtdlLogPrintf(CTDL_DEBUG, "pop3client ended\n");
1062 last_run = time(NULL);
1063 doing_pop3client = 0;
1067 void pop3_cleanup(void)
1069 citthread_mutex_destroy(&POP3QueueMutex);
1070 DeleteHash(&POP3FetchUrls);
1071 DeleteHash(&POP3QueueRooms);
1074 CTDL_MODULE_INIT(pop3client)
1078 citthread_mutex_init(&POP3QueueMutex, NULL);
1079 POP3QueueRooms = NewHash(1, lFlathash);
1080 POP3FetchUrls = NewHash(1, NULL);
1081 CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER);
1082 CtdlRegisterCleanupHook(pop3_cleanup);
1084 /* return our Subversion id for the Log */
1085 return "pop3client";