2 * Consolidate mail from remote POP3 accounts.
4 * Copyright (c) 2007-2011 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.
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"
56 #define POP3C_OK (strncasecmp(ChrPtr(RecvMsg->IO.IOBuf), "+OK", 3) == 0)
58 int POP3ClientDebugEnabled = 0;
60 #define N ((pop3aggr*)IO->Data)->n
62 #define DBGLOG(LEVEL) if ((LEVEL != LOG_DEBUG) || (POP3ClientDebugEnabled != 0))
64 #define EVP3C_syslog(LEVEL, FORMAT, ...) \
65 DBGLOG(LEVEL) syslog(LEVEL, \
66 "IO[%ld]CC[%d][%ld]" FORMAT, \
67 IO->ID, CCID, N, __VA_ARGS__)
69 #define EVP3CM_syslog(LEVEL, FORMAT) \
70 DBGLOG(LEVEL) syslog(LEVEL, \
71 "IO[%ld]CC[%d][%ld]" FORMAT, \
74 #define EVP3CCS_syslog(LEVEL, FORMAT, ...) \
75 DBGLOG(LEVEL) syslog(LEVEL, "IO[%ld][%ld]" FORMAT, \
76 IO->ID, N, __VA_ARGS__)
78 #define EVP3CCSM_syslog(LEVEL, FORMAT) \
79 DBGLOG(LEVEL) syslog(LEVEL, "IO[%ld][%ld]" FORMAT, \
82 #define POP3C_DBG_SEND() \
83 EVP3C_syslog(LOG_DEBUG, \
85 ChrPtr(RecvMsg->IO.SendBuf.Buf))
87 #define POP3C_DBG_READ() \
88 EVP3C_syslog(LOG_DEBUG, \
90 ChrPtr(RecvMsg->IO.IOBuf))
93 struct CitContext pop3_client_CC;
95 pthread_mutex_t POP3QueueMutex; /* locks the access to the following vars: */
96 HashList *POP3QueueRooms = NULL;
97 HashList *POP3FetchUrls = NULL;
99 typedef struct pop3aggr pop3aggr;
100 typedef eNextState(*Pop3ClientHandler)(pop3aggr* RecvMsg);
102 eNextState POP3_C_Shutdown(AsyncIO *IO);
103 eNextState POP3_C_Timeout(AsyncIO *IO);
104 eNextState POP3_C_ConnFail(AsyncIO *IO);
105 eNextState POP3_C_DNSFail(AsyncIO *IO);
106 eNextState POP3_C_DispatchReadDone(AsyncIO *IO);
107 eNextState POP3_C_DispatchWriteDone(AsyncIO *IO);
108 eNextState POP3_C_Terminate(AsyncIO *IO);
109 eReadState POP3_C_ReadServerStatus(AsyncIO *IO);
110 eNextState POP3_C_ReAttachToFetchMessages(AsyncIO *IO);
112 typedef struct __pop3_room_counter {
117 typedef enum ePOP3_C_States {
123 GetOneMessageIDState,
124 ReadMessageBodyFollowing,
132 typedef struct _FetchItem {
138 struct CtdlMessage *Msg;
141 void HfreeFetchItem(void *vItem)
143 FetchItem *Item = (FetchItem*) vItem;
144 FreeStrBuf(&Item->MsgUIDL);
145 FreeStrBuf(&Item->MsgUID);
154 DNSQueryParts HostLookup;
157 HashList *OtherQRnumbers;
162 StrBuf *RoomName; // TODO: fill me
165 ePOP3_C_States State;
166 HashList *MsgNumbers;
171 void DeletePOP3Aggregator(void *vptr)
173 pop3aggr *ptr = vptr;
174 DeleteHashPos(&ptr->Pos);
175 DeleteHash(&ptr->MsgNumbers);
176 // FreeStrBuf(&ptr->rooms);
177 FreeStrBuf(&ptr->pop3user);
178 FreeStrBuf(&ptr->pop3pass);
179 FreeStrBuf(&ptr->RoomName);
180 FreeURL(&ptr->IO.ConnectMe);
181 FreeStrBuf(&ptr->Url);
182 FreeStrBuf(&ptr->IO.IOBuf);
183 FreeStrBuf(&ptr->IO.SendBuf.Buf);
184 FreeStrBuf(&ptr->IO.RecvBuf.Buf);
185 DeleteAsyncMsg(&ptr->IO.ReadMsg);
186 ((struct CitContext*)ptr->IO.CitContext)->state = CON_IDLE;
187 ((struct CitContext*)ptr->IO.CitContext)->kill_me = 1;
188 FreeAsyncIOContents(&ptr->IO);
192 eNextState FinalizePOP3AggrRun(AsyncIO *IO)
195 pop3aggr *cptr = (pop3aggr *)IO->Data;
197 EVP3CM_syslog(LOG_DEBUG, "Terminating Aggregator; bye.\n");
199 It = GetNewHashPos(POP3FetchUrls, 0);
200 pthread_mutex_lock(&POP3QueueMutex);
202 if (GetHashPosFromKey(POP3FetchUrls, SKEY(cptr->Url), It))
203 DeleteEntryFromHash(POP3FetchUrls, It);
205 pthread_mutex_unlock(&POP3QueueMutex);
210 eNextState FailAggregationRun(AsyncIO *IO)
215 eNextState POP3C_ReadGreeting(pop3aggr *RecvMsg)
217 AsyncIO *IO = &RecvMsg->IO;
219 /* Read the server greeting */
220 if (!POP3C_OK) return eTerminateConnection;
221 else return eSendReply;
224 eNextState POP3C_SendUser(pop3aggr *RecvMsg)
226 AsyncIO *IO = &RecvMsg->IO;
227 /* Identify ourselves. NOTE: we have to append a CR to each command.
228 * The LF will automatically be appended by sock_puts(). Believe it
229 * or not, leaving out the CR will cause problems if the server happens
230 * to be Exchange, which is so b0rken it actually barfs on
231 * LF-terminated newlines.
233 StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
234 "USER %s\r\n", ChrPtr(RecvMsg->pop3user));
239 eNextState POP3C_GetUserState(pop3aggr *RecvMsg)
241 AsyncIO *IO = &RecvMsg->IO;
243 if (!POP3C_OK) return eTerminateConnection;
244 else return eSendReply;
247 eNextState POP3C_SendPassword(pop3aggr *RecvMsg)
249 AsyncIO *IO = &RecvMsg->IO;
251 StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
252 "PASS %s\r\n", ChrPtr(RecvMsg->pop3pass));
253 EVP3CM_syslog(LOG_DEBUG, "<PASS <password>\n");
254 // POP3C_DBG_SEND(); No, we won't write the passvoid to syslog...
258 eNextState POP3C_GetPassState(pop3aggr *RecvMsg)
260 AsyncIO *IO = &RecvMsg->IO;
262 if (!POP3C_OK) return eTerminateConnection;
263 else return eSendReply;
266 eNextState POP3C_SendListCommand(pop3aggr *RecvMsg)
268 AsyncIO *IO = &RecvMsg->IO;
269 /* Get the list of messages */
270 StrBufPlain(RecvMsg->IO.SendBuf.Buf, HKEY("LIST\r\n"));
275 eNextState POP3C_GetListCommandState(pop3aggr *RecvMsg)
277 AsyncIO *IO = &RecvMsg->IO;
279 if (!POP3C_OK) return eTerminateConnection;
280 RecvMsg->MsgNumbers = NewHash(1, NULL);
286 eNextState POP3C_GetListOneLine(pop3aggr *RecvMsg)
288 AsyncIO *IO = &RecvMsg->IO;
293 FetchItem *OneMsg = NULL;
296 if ((StrLength(RecvMsg->IO.IOBuf) == 1) &&
297 (ChrPtr(RecvMsg->IO.IOBuf)[0] == '.'))
299 if (GetCount(RecvMsg->MsgNumbers) == 0)
301 //// RecvMsg->Sate = ReadQuitState;
305 RecvMsg->Pos = GetNewHashPos(RecvMsg->MsgNumbers, 0);
310 OneMsg = (FetchItem*) malloc(sizeof(FetchItem));
311 memset(OneMsg, 0, sizeof(FetchItem));
312 OneMsg->MSGID = atol(ChrPtr(RecvMsg->IO.IOBuf));
314 pch = strchr(ChrPtr(RecvMsg->IO.IOBuf), ' ');
317 OneMsg->MSGSize = atol(pch + 1);
320 rc = TestValidateHash(RecvMsg->MsgNumbers);
322 syslog(LOG_DEBUG, "Hash Invalid: %d\n", rc);
325 Put(RecvMsg->MsgNumbers, LKEY(OneMsg->MSGID), OneMsg, HfreeFetchItem);
327 rc = TestValidateHash(RecvMsg->MsgNumbers);
329 syslog(LOG_DEBUG, "Hash Invalid: %d\n", rc);
331 //RecvMsg->State --; /* read next Line */
335 eNextState POP3_FetchNetworkUsetableEntry(AsyncIO *IO)
340 struct cdbdata *cdbut;
341 pop3aggr *RecvMsg = (pop3aggr *) IO->Data;
343 if(GetNextHashPos(RecvMsg->MsgNumbers,
350 if (server_shutting_down)
353 RecvMsg->CurrMsg = (FetchItem*) vData;
355 "CHECKING: whether %s has already been seen: ",
356 ChrPtr(RecvMsg->CurrMsg->MsgUID));
358 /* Find out if we've already seen this item */
359 safestrncpy(ut.ut_msgid,
360 ChrPtr(RecvMsg->CurrMsg->MsgUID),
361 sizeof(ut.ut_msgid));
362 ut.ut_timestamp = time(NULL);/// TODO: libev timestamp!
364 cdbut = cdb_fetch(CDB_USETABLE, SKEY(RecvMsg->CurrMsg->MsgUID));
366 /* Item has already been seen */
367 syslog(LOG_DEBUG, "YES\n");
370 /* rewrite the record anyway, to update the timestamp */
371 cdb_store(CDB_USETABLE,
372 SKEY(RecvMsg->CurrMsg->MsgUID),
373 &ut, sizeof(struct UseTable) );
374 RecvMsg->CurrMsg->NeedFetch = 0; ////TODO0;
378 syslog(LOG_DEBUG, "NO\n");
379 RecvMsg->CurrMsg->NeedFetch = 1;
381 return NextDBOperation(&RecvMsg->IO,
382 POP3_FetchNetworkUsetableEntry);
386 /* ok, now we know them all,
387 * continue with reading the actual messages. */
388 DeleteHashPos(&RecvMsg->Pos);
390 return QueueEventContext(IO, POP3_C_ReAttachToFetchMessages);
394 eNextState POP3C_GetOneMessagID(pop3aggr *RecvMsg)
396 AsyncIO *IO = &RecvMsg->IO;
403 rc = TestValidateHash(RecvMsg->MsgNumbers);
405 syslog(LOG_DEBUG, "Hash Invalid: %d\n", rc);
407 if(GetNextHashPos(RecvMsg->MsgNumbers,
412 RecvMsg->CurrMsg = (FetchItem*) vData;
413 /* Find out the UIDL of the message,
414 * to determine whether we've already downloaded it */
415 StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
416 "UIDL %ld\r\n", RecvMsg->CurrMsg->MSGID);
422 DeleteHashPos(&RecvMsg->Pos);
423 /// done receiving uidls.. start looking them up now.
424 RecvMsg->Pos = GetNewHashPos(RecvMsg->MsgNumbers, 0);
425 return QueueDBOperation(&RecvMsg->IO,
426 POP3_FetchNetworkUsetableEntry);
428 return eReadMore; /* TODO */
431 eNextState POP3C_GetOneMessageIDState(pop3aggr *RecvMsg)
433 AsyncIO *IO = &RecvMsg->IO;
436 rc = TestValidateHash(RecvMsg->MsgNumbers);
438 syslog(LOG_DEBUG, "Hash Invalid: %d\n", rc);
442 if (!POP3C_OK) return eTerminateConnection;
443 RecvMsg->CurrMsg->MsgUIDL =
444 NewStrBufPlain(NULL, StrLength(RecvMsg->IO.IOBuf));
445 RecvMsg->CurrMsg->MsgUID =
446 NewStrBufPlain(NULL, StrLength(RecvMsg->IO.IOBuf) * 2);
448 StrBufExtract_token(RecvMsg->CurrMsg->MsgUIDL,
449 RecvMsg->IO.IOBuf, 2, ' ');
451 StrBufPrintf(RecvMsg->CurrMsg->MsgUID,
453 ChrPtr(RecvMsg->RoomName),
454 ChrPtr(RecvMsg->CurrMsg->MsgUIDL),
455 RecvMsg->IO.ConnectMe->User,
456 RecvMsg->IO.ConnectMe->Host);
462 eNextState POP3C_SendGetOneMsg(pop3aggr *RecvMsg)
464 AsyncIO *IO = &RecvMsg->IO;
469 RecvMsg->CurrMsg = NULL;
470 while (GetNextHashPos(RecvMsg->MsgNumbers,
474 (RecvMsg->CurrMsg = (FetchItem*) vData,
475 RecvMsg->CurrMsg->NeedFetch == 0))
478 if ((RecvMsg->CurrMsg != NULL ) && (RecvMsg->CurrMsg->NeedFetch == 1))
480 /* Message has not been seen.
481 * Tell the server to fetch the message... */
482 StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
483 "RETR %ld\r\n", RecvMsg->CurrMsg->MSGID);
488 RecvMsg->State = ReadQuitState;
489 return POP3_C_DispatchWriteDone(&RecvMsg->IO);
494 eNextState POP3C_ReadMessageBodyFollowing(pop3aggr *RecvMsg)
496 AsyncIO *IO = &RecvMsg->IO;
498 if (!POP3C_OK) return eTerminateConnection;
499 RecvMsg->IO.ReadMsg = NewAsyncMsg(HKEY("."),
500 RecvMsg->CurrMsg->MSGSize,
509 eNextState POP3C_StoreMsgRead(AsyncIO *IO)
511 pop3aggr *RecvMsg = (pop3aggr *) IO->Data;
515 "MARKING: %s as seen: ",
516 ChrPtr(RecvMsg->CurrMsg->MsgUID));
518 safestrncpy(ut.ut_msgid,
519 ChrPtr(RecvMsg->CurrMsg->MsgUID),
520 sizeof(ut.ut_msgid));
521 ut.ut_timestamp = time(NULL); /* TODO: use libev time */
522 cdb_store(CDB_USETABLE,
523 ChrPtr(RecvMsg->CurrMsg->MsgUID),
524 StrLength(RecvMsg->CurrMsg->MsgUID),
526 sizeof(struct UseTable) );
528 return QueueEventContext(&RecvMsg->IO, POP3_C_ReAttachToFetchMessages);
530 eNextState POP3C_SaveMsg(AsyncIO *IO)
533 pop3aggr *RecvMsg = (pop3aggr *) IO->Data;
535 /* Do Something With It (tm) */
536 msgnum = CtdlSubmitMsg(RecvMsg->CurrMsg->Msg,
538 ChrPtr(RecvMsg->RoomName),
542 /* Message has been committed to the store
543 * write the uidl to the use table
544 * so we don't fetch this message again
547 CtdlFreeMessage(RecvMsg->CurrMsg->Msg);
549 return NextDBOperation(&RecvMsg->IO, POP3C_StoreMsgRead);
552 eNextState POP3C_ReadMessageBody(pop3aggr *RecvMsg)
554 AsyncIO *IO = &RecvMsg->IO;
555 EVP3CM_syslog(LOG_DEBUG, "Converting message...");
556 RecvMsg->CurrMsg->Msg =
557 convert_internet_message_buf(&RecvMsg->IO.ReadMsg->MsgBuf);
558 StopClientWatchers(IO);
559 return QueueDBOperation(&RecvMsg->IO, POP3C_SaveMsg);
562 eNextState POP3C_SendDelete(pop3aggr *RecvMsg)
564 AsyncIO *IO = &RecvMsg->IO;
565 if (!RecvMsg->keep) {
566 StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
567 "DELE %ld\r\n", RecvMsg->CurrMsg->MSGID);
572 RecvMsg->State = ReadMessageBodyFollowing;
573 return POP3_C_DispatchWriteDone(&RecvMsg->IO);
576 eNextState POP3C_ReadDeleteState(pop3aggr *RecvMsg)
578 AsyncIO *IO = &RecvMsg->IO;
580 RecvMsg->State = GetOneMessageIDState;
584 eNextState POP3C_SendQuit(pop3aggr *RecvMsg)
586 AsyncIO *IO = &RecvMsg->IO;
588 StrBufPlain(RecvMsg->IO.SendBuf.Buf,
595 eNextState POP3C_ReadQuitState(pop3aggr *RecvMsg)
597 AsyncIO *IO = &RecvMsg->IO;
599 return eTerminateConnection;
602 const long POP3_C_ConnTimeout = 1000;
603 const long DefaultPOP3Port = 110;
605 Pop3ClientHandler POP3C_ReadHandlers[] = {
609 POP3C_GetListCommandState,
610 POP3C_GetListOneLine,
611 POP3C_GetOneMessageIDState,
612 POP3C_ReadMessageBodyFollowing,
613 POP3C_ReadMessageBody,
614 POP3C_ReadDeleteState,
618 const long POP3_C_SendTimeouts[POP3C_MaxRead] = {
628 const ConstStr POP3C_ReadErrors[POP3C_MaxRead] = {
629 {HKEY("Connection broken during ")},
630 {HKEY("Connection broken during ")},
631 {HKEY("Connection broken during ")},
632 {HKEY("Connection broken during ")},
633 {HKEY("Connection broken during ")},
634 {HKEY("Connection broken during ")},
635 {HKEY("Connection broken during ")},
636 {HKEY("Connection broken during ")}
639 Pop3ClientHandler POP3C_SendHandlers[] = {
640 NULL, /* we don't send a greeting */
643 POP3C_SendListCommand,
645 POP3C_GetOneMessagID,
652 const long POP3_C_ReadTimeouts[] = {
664 /*****************************************************************************/
665 /* POP3 CLIENT DISPATCHER */
666 /*****************************************************************************/
668 void POP3SetTimeout(eNextState NextTCPState, pop3aggr *pMsg)
670 AsyncIO *IO = &pMsg->IO;
671 double Timeout = 0.0;
673 EVP3C_syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
675 switch (NextTCPState) {
679 Timeout = POP3_C_SendTimeouts[pMsg->State];
681 if (pMsg->State == eDATABody) {
682 / * if we're sending a huge message, we need more time. * /
683 Timeout += StrLength(pMsg->msgtext) / 1024;
689 Timeout = POP3_C_ReadTimeouts[pMsg->State];
691 if (pMsg->State == eDATATerminateBody) {
693 * some mailservers take a nap before accepting the message
694 * content inspection and such.
696 Timeout += StrLength(pMsg->msgtext) / 1024;
707 case eTerminateConnection:
710 case eReadMore://// TODO
713 SetNextTimeout(&pMsg->IO, Timeout);
715 eNextState POP3_C_DispatchReadDone(AsyncIO *IO)
717 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
718 pop3aggr *pMsg = IO->Data;
721 rc = POP3C_ReadHandlers[pMsg->State](pMsg);
724 POP3SetTimeout(rc, pMsg);
727 eNextState POP3_C_DispatchWriteDone(AsyncIO *IO)
729 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
730 pop3aggr *pMsg = IO->Data;
733 rc = POP3C_SendHandlers[pMsg->State](pMsg);
734 POP3SetTimeout(rc, pMsg);
739 /*****************************************************************************/
740 /* POP3 CLIENT ERROR CATCHERS */
741 /*****************************************************************************/
742 eNextState POP3_C_Terminate(AsyncIO *IO)
744 /// pop3aggr *pMsg = (pop3aggr *)IO->Data;
746 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
747 FinalizePOP3AggrRun(IO);
750 eNextState POP3_C_TerminateDB(AsyncIO *IO)
752 /// pop3aggr *pMsg = (pop3aggr *)IO->Data;
754 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
755 FinalizePOP3AggrRun(IO);
758 eNextState POP3_C_Timeout(AsyncIO *IO)
760 pop3aggr *pMsg = IO->Data;
762 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
763 StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State]));
764 return FailAggregationRun(IO);
766 eNextState POP3_C_ConnFail(AsyncIO *IO)
768 pop3aggr *pMsg = (pop3aggr *)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_DNSFail(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_Shutdown(AsyncIO *IO)
784 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
785 //// pop3aggr *pMsg = IO->Data;
787 ////pMsg->MyQEntry->Status = 3;
788 ///StrBufPlain(pMsg->MyQEntry->StatusMessage, HKEY("server shutdown during message retrieval."));
789 FinalizePOP3AggrRun(IO);
795 * @brief lineread Handler; understands when to read more POP3 lines,
796 * and when this is a one-lined reply.
798 eReadState POP3_C_ReadServerStatus(AsyncIO *IO)
800 eReadState Finished = eBufferNotEmpty;
802 switch (IO->NextState) {
807 case eTerminateConnection:
809 Finished = eReadFail;
816 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
820 Finished = CtdlReadMessageBodyAsync(IO);
826 /*****************************************************************************
827 * So we connect our Server IP here. *
828 *****************************************************************************/
829 eNextState POP3_C_ReAttachToFetchMessages(AsyncIO *IO)
831 pop3aggr *cpptr = IO->Data;
833 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
834 ////??? cpptr->State ++;
835 if (cpptr->Pos == NULL)
836 cpptr->Pos = GetNewHashPos(cpptr->MsgNumbers, 0);
838 POP3_C_DispatchWriteDone(IO);
839 ReAttachIO(IO, cpptr, 0);
840 IO->NextState = eReadMessage;
841 return IO->NextState;
844 eNextState pop3_connect_ip(AsyncIO *IO)
846 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
848 return EvConnectSock(IO,
850 POP3_C_ReadTimeouts[0],
854 eNextState pop3_get_one_host_ip_done(AsyncIO *IO)
856 pop3aggr *cpptr = IO->Data;
857 struct hostent *hostent;
861 hostent = cpptr->HostLookup.VParsedDNSReply;
862 if ((cpptr->HostLookup.DNSStatus == ARES_SUCCESS) &&
863 (hostent != NULL) ) {
864 memset(&cpptr->IO.ConnectMe->Addr, 0, sizeof(struct in6_addr));
865 if (cpptr->IO.ConnectMe->IPv6) {
866 memcpy(&cpptr->IO.ConnectMe->Addr.sin6_addr.s6_addr,
867 &hostent->h_addr_list[0],
868 sizeof(struct in6_addr));
870 cpptr->IO.ConnectMe->Addr.sin6_family =
872 cpptr->IO.ConnectMe->Addr.sin6_port =
873 htons(DefaultPOP3Port);
876 struct sockaddr_in *addr =
877 (struct sockaddr_in*)
878 &cpptr->IO.ConnectMe->Addr;
880 memcpy(&addr->sin_addr.s_addr,
881 hostent->h_addr_list[0],
884 addr->sin_family = hostent->h_addrtype;
885 addr->sin_port = htons(DefaultPOP3Port);
887 return pop3_connect_ip(IO);
893 eNextState pop3_get_one_host_ip(AsyncIO *IO)
895 pop3aggr *cpptr = IO->Data;
897 syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
900 "POP3 client[%ld]: looking up %s-Record %s : %d ...\n",
902 (cpptr->IO.ConnectMe->IPv6)? "aaaa": "a",
903 cpptr->IO.ConnectMe->Host,
904 cpptr->IO.ConnectMe->Port);
906 QueueQuery((cpptr->IO.ConnectMe->IPv6)? ns_t_aaaa : ns_t_a,
907 cpptr->IO.ConnectMe->Host,
910 pop3_get_one_host_ip_done);
911 IO->NextState = eReadDNSReply;
912 return IO->NextState;
917 int pop3_do_fetching(pop3aggr *cpptr)
919 AsyncIO *IO = &cpptr->IO;
924 POP3_C_ReadServerStatus,
926 POP3_C_DispatchWriteDone,
927 POP3_C_DispatchReadDone,
934 safestrncpy(((CitContext *)cpptr->IO.CitContext)->cs_host,
936 sizeof(((CitContext *)cpptr->IO.CitContext)->cs_host));
938 if (cpptr->IO.ConnectMe->IsIP) {
939 QueueEventContext(&cpptr->IO,
943 QueueEventContext(&cpptr->IO,
944 pop3_get_one_host_ip);
950 * Scan a room's netconfig to determine whether it requires POP3 aggregation
952 void pop3client_scan_room(struct ctdlroom *qrbuf, void *data)
959 char filename[PATH_MAX];
963 const char *CfgPtr, *lPtr;
966 // pop3_room_counter *Count = NULL;
969 pthread_mutex_lock(&POP3QueueMutex);
970 if (GetHash(POP3QueueRooms, LKEY(qrbuf->QRnumber), &vptr))
973 "pop3client: [%ld] %s already in progress.\n",
976 pthread_mutex_unlock(&POP3QueueMutex);
978 pthread_mutex_unlock(&POP3QueueMutex);
980 if (server_shutting_down) return;
982 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
984 if (server_shutting_down)
987 /* Only do net processing for rooms that have netconfigs */
988 fd = open(filename, 0);
992 if (server_shutting_down)
994 if (fstat(fd, &statbuf) == -1) {
996 "ERROR: could not stat configfile '%s' - %s\n",
1001 if (server_shutting_down)
1003 CfgData = NewStrBufPlain(NULL, statbuf.st_size + 1);
1004 if (StrBufReadBLOB(CfgData, &fd, 1, statbuf.st_size, &Err) < 0) {
1006 FreeStrBuf(&CfgData);
1007 syslog(LOG_DEBUG, "ERROR: reading config '%s' - %s<br>\n",
1008 filename, strerror(errno));
1012 if (server_shutting_down)
1016 CfgType = NewStrBuf();
1017 Line = NewStrBufPlain(NULL, StrLength(CfgData));
1022 Done = StrBufSipLine(Line, CfgData, &CfgPtr) == 0;
1023 if (StrLength(Line) > 0)
1026 StrBufExtract_NextToken(CfgType, Line, &lPtr, '|');
1027 if (!strcasecmp("pop3client", ChrPtr(CfgType)))
1034 Count = malloc(sizeof(pop3_room_counter));
1039 cptr = (pop3aggr *) malloc(sizeof(pop3aggr));
1040 memset(cptr, 0, sizeof(pop3aggr));
1041 ///TODO do we need this? cptr->roomlist_parts=1;
1043 NewStrBufPlain(qrbuf->QRname, -1);
1045 NewStrBufPlain(NULL, StrLength(Line));
1047 NewStrBufPlain(NULL, StrLength(Line));
1048 cptr->Url = NewStrBuf();
1051 StrBufExtract_NextToken(Tmp, Line, &lPtr, '|');
1052 StrBufExtract_NextToken(cptr->pop3user,
1057 StrBufExtract_NextToken(cptr->pop3pass,
1062 cptr->keep = StrBufExtractNext_long(Line,
1066 cptr->interval = StrBufExtractNext_long(Line,
1070 StrBufAppendBufPlain(cptr->Url, HKEY("pop3://"), 0);
1071 StrBufUrlescUPAppend(cptr->Url, cptr->pop3user, NULL);
1072 StrBufAppendBufPlain(cptr->Url, HKEY(":"), 0);
1073 StrBufUrlescUPAppend(cptr->Url, cptr->pop3pass, NULL);
1074 StrBufAppendBufPlain(cptr->Url, HKEY("@"), 0);
1075 StrBufAppendBuf(cptr->Url, Tmp, 0);
1076 StrBufAppendBufPlain(cptr->Url, HKEY("/"), 0);
1077 StrBufUrlescAppend(cptr->Url, cptr->RoomName, NULL);
1080 ParseURL(&cptr->IO.ConnectMe, cptr->Url, 110);
1084 /* todo: we need to reunite the url to be shure. */
1086 pthread_mutex_lock(&POP3ueueMutex);
1087 GetHash(POP3FetchUrls, SKEY(ptr->Url), &vptr);
1088 use_this_cptr = (pop3aggr *)vptr;
1090 if (use_this_rncptr != NULL)
1092 /* mustn't attach to an active session */
1093 if (use_this_cptr->RefCount > 0)
1095 DeletePOP3Cfg(cptr);
1101 StrBufAppendBufPlain(
1102 use_this_cptr->rooms,
1105 if (use_this_cptr->roomlist_parts == 1)
1107 use_this_cptr->OtherQRnumbers
1108 = NewHash(1, lFlathash);
1110 QRnumber = (long*)malloc(sizeof(long));
1111 *QRnumber = qrbuf->QRnumber;
1112 Put(use_this_cptr->OtherQRnumbers,
1113 LKEY(qrbuf->QRnumber),
1117 use_this_cptr->roomlist_parts++;
1119 pthread_mutex_unlock(&POP3QueueMutex);
1122 pthread_mutex_unlock(&RSSQueueMutex);
1124 cptr->n = Pop3ClientID++;
1125 pthread_mutex_lock(&POP3QueueMutex);
1129 DeletePOP3Aggregator);
1131 pthread_mutex_unlock(&POP3QueueMutex);
1141 FreeStrBuf(&CfgType);
1142 FreeStrBuf(&CfgData);
1145 static int doing_pop3client = 0;
1147 void pop3client_scan(void) {
1148 static time_t last_run = 0L;
1149 time_t fastest_scan;
1156 become_session(&pop3_client_CC);
1158 if (config.c_pop3_fastest < config.c_pop3_fetch)
1159 fastest_scan = config.c_pop3_fastest;
1161 fastest_scan = config.c_pop3_fetch;
1164 * Run POP3 aggregation no more frequently than once every n seconds
1166 if ( (time(NULL) - last_run) < fastest_scan ) {
1171 * This is a simple concurrency check to make sure only one pop3client
1172 * run is done at a time. We could do this with a mutex, but since we
1173 * don't really require extremely fine granularity here, we'll do it
1174 * with a static variable instead.
1176 if (doing_pop3client) return;
1177 doing_pop3client = 1;
1179 syslog(LOG_DEBUG, "pop3client started");
1180 CtdlForEachRoom(pop3client_scan_room, NULL);
1182 pthread_mutex_lock(&POP3QueueMutex);
1183 it = GetNewHashPos(POP3FetchUrls, 0);
1184 while (!server_shutting_down &&
1185 GetNextHashPos(POP3FetchUrls, it, &len, &Key, &vrptr) &&
1187 cptr = (pop3aggr *)vrptr;
1188 if (cptr->RefCount == 0)
1189 if (!pop3_do_fetching(cptr))
1190 DeletePOP3Aggregator(cptr);////TODO
1193 if ((palist->interval && time(NULL) > (last_run + palist->interval))
1194 || (time(NULL) > last_run + config.c_pop3_fetch))
1195 pop3_do_fetching(palist->roomname, palist->pop3host,
1196 palist->pop3user, palist->pop3pass, palist->keep);
1198 palist = palist->next;
1203 pthread_mutex_unlock(&POP3QueueMutex);
1205 syslog(LOG_DEBUG, "pop3client ended");
1206 last_run = time(NULL);
1207 doing_pop3client = 0;
1211 void pop3_cleanup(void)
1213 /* citthread_mutex_destroy(&POP3QueueMutex); TODO */
1214 while (doing_pop3client != 0) ;
1215 DeleteHash(&POP3FetchUrls);
1216 DeleteHash(&POP3QueueRooms);
1221 void LogDebugEnablePOP3Client(const int n)
1223 POP3ClientDebugEnabled = n;
1226 CTDL_MODULE_INIT(pop3client)
1230 CtdlFillSystemContext(&pop3_client_CC, "POP3aggr");
1231 pthread_mutex_init(&POP3QueueMutex, NULL);
1232 POP3QueueRooms = NewHash(1, lFlathash);
1233 POP3FetchUrls = NewHash(1, NULL);
1234 CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER);
1235 CtdlRegisterEVCleanupHook(pop3_cleanup);
1236 CtdlRegisterDebugFlagHook(HKEY("pop3client"), LogDebugEnablePOP3Client, &POP3ClientDebugEnabled);
1239 /* return our module id for the log */
1240 return "pop3client";