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]POP3: " 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]POP3: " FORMAT, \
74 #define EVP3CQ_syslog(LEVEL, FORMAT, ...) \
75 DBGLOG(LEVEL) syslog(LEVEL, \
79 #define EVP3CQM_syslog(LEVEL, FORMAT) \
80 DBGLOG(LEVEL) syslog(LEVEL, \
84 #define EVP3CCS_syslog(LEVEL, FORMAT, ...) \
85 DBGLOG(LEVEL) syslog(LEVEL, "IO[%ld][%ld]POP3: " FORMAT, \
86 IO->ID, N, __VA_ARGS__)
88 #define EVP3CCSM_syslog(LEVEL, FORMAT) \
89 DBGLOG(LEVEL) syslog(LEVEL, "IO[%ld][%ld]POP3: " FORMAT, \
92 #define POP3C_DBG_SEND() \
93 EVP3C_syslog(LOG_DEBUG, \
94 "IO[%ld]CC[%d][%ld]POP3: > %s\n", \
96 ChrPtr(RecvMsg->IO.SendBuf.Buf))
98 #define POP3C_DBG_READ() \
99 EVP3C_syslog(LOG_DEBUG, \
100 "IO[%ld]CC[%d][%ld]POP3: < %s\n", \
102 ChrPtr(RecvMsg->IO.IOBuf))
105 struct CitContext pop3_client_CC;
107 pthread_mutex_t POP3QueueMutex; /* locks the access to the following vars: */
108 HashList *POP3QueueRooms = NULL;
109 HashList *POP3FetchUrls = NULL;
111 typedef struct pop3aggr pop3aggr;
112 typedef eNextState(*Pop3ClientHandler)(pop3aggr* RecvMsg);
114 eNextState POP3_C_Shutdown(AsyncIO *IO);
115 eNextState POP3_C_Timeout(AsyncIO *IO);
116 eNextState POP3_C_ConnFail(AsyncIO *IO);
117 eNextState POP3_C_DNSFail(AsyncIO *IO);
118 eNextState POP3_C_DispatchReadDone(AsyncIO *IO);
119 eNextState POP3_C_DispatchWriteDone(AsyncIO *IO);
120 eNextState POP3_C_Terminate(AsyncIO *IO);
121 eReadState POP3_C_ReadServerStatus(AsyncIO *IO);
122 eNextState POP3_C_ReAttachToFetchMessages(AsyncIO *IO);
124 typedef struct __pop3_room_counter {
129 typedef enum ePOP3_C_States {
135 GetOneMessageIDState,
136 ReadMessageBodyFollowing,
144 typedef struct _FetchItem {
150 struct CtdlMessage *Msg;
153 void HfreeFetchItem(void *vItem)
155 FetchItem *Item = (FetchItem*) vItem;
156 FreeStrBuf(&Item->MsgUIDL);
157 FreeStrBuf(&Item->MsgUID);
168 DNSQueryParts HostLookup;
171 HashList *OtherQRnumbers;
177 StrBuf *RoomName; // TODO: fill me
180 ePOP3_C_States State;
181 HashList *MsgNumbers;
186 void DeletePOP3Aggregator(void *vptr)
188 pop3aggr *ptr = vptr;
189 DeleteHashPos(&ptr->Pos);
190 DeleteHash(&ptr->MsgNumbers);
191 // FreeStrBuf(&ptr->rooms);
192 FreeStrBuf(&ptr->pop3user);
193 FreeStrBuf(&ptr->pop3pass);
194 FreeStrBuf(&ptr->Host);
195 FreeStrBuf(&ptr->RoomName);
196 FreeURL(&ptr->IO.ConnectMe);
197 FreeStrBuf(&ptr->Url);
198 FreeStrBuf(&ptr->IO.IOBuf);
199 FreeStrBuf(&ptr->IO.SendBuf.Buf);
200 FreeStrBuf(&ptr->IO.RecvBuf.Buf);
201 DeleteAsyncMsg(&ptr->IO.ReadMsg);
202 if (((struct CitContext*)ptr->IO.CitContext)) {
203 ((struct CitContext*)ptr->IO.CitContext)->state = CON_IDLE;
204 ((struct CitContext*)ptr->IO.CitContext)->kill_me = 1;
206 FreeAsyncIOContents(&ptr->IO);
210 eNextState FinalizePOP3AggrRun(AsyncIO *IO)
213 pop3aggr *cpptr = (pop3aggr *)IO->Data;
215 EVP3C_syslog(LOG_INFO,
216 "%s@%s: fetched %ld new of %d messages in %fs. bye.",
217 ChrPtr(cpptr->pop3user),
220 GetCount(cpptr->MsgNumbers),
221 IO->Now - cpptr->IOStart
224 It = GetNewHashPos(POP3FetchUrls, 0);
225 pthread_mutex_lock(&POP3QueueMutex);
227 if (GetHashPosFromKey(POP3FetchUrls, SKEY(cpptr->Url), It))
228 DeleteEntryFromHash(POP3FetchUrls, It);
230 pthread_mutex_unlock(&POP3QueueMutex);
235 eNextState FailAggregationRun(AsyncIO *IO)
240 eNextState POP3C_ReadGreeting(pop3aggr *RecvMsg)
242 AsyncIO *IO = &RecvMsg->IO;
244 /* Read the server greeting */
245 if (!POP3C_OK) return eTerminateConnection;
246 else return eSendReply;
249 eNextState POP3C_SendUser(pop3aggr *RecvMsg)
251 AsyncIO *IO = &RecvMsg->IO;
252 /* Identify ourselves. NOTE: we have to append a CR to each command.
253 * The LF will automatically be appended by sock_puts(). Believe it
254 * or not, leaving out the CR will cause problems if the server happens
255 * to be Exchange, which is so b0rken it actually barfs on
256 * LF-terminated newlines.
258 StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
259 "USER %s\r\n", ChrPtr(RecvMsg->pop3user));
264 eNextState POP3C_GetUserState(pop3aggr *RecvMsg)
266 AsyncIO *IO = &RecvMsg->IO;
268 if (!POP3C_OK) return eTerminateConnection;
269 else return eSendReply;
272 eNextState POP3C_SendPassword(pop3aggr *RecvMsg)
274 AsyncIO *IO = &RecvMsg->IO;
276 StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
277 "PASS %s\r\n", ChrPtr(RecvMsg->pop3pass));
278 EVP3CM_syslog(LOG_DEBUG, "<PASS <password>\n");
279 // POP3C_DBG_SEND(); No, we won't write the passvoid to syslog...
283 eNextState POP3C_GetPassState(pop3aggr *RecvMsg)
285 AsyncIO *IO = &RecvMsg->IO;
287 if (!POP3C_OK) return eTerminateConnection;
288 else return eSendReply;
291 eNextState POP3C_SendListCommand(pop3aggr *RecvMsg)
293 AsyncIO *IO = &RecvMsg->IO;
294 /* Get the list of messages */
295 StrBufPlain(RecvMsg->IO.SendBuf.Buf, HKEY("LIST\r\n"));
300 eNextState POP3C_GetListCommandState(pop3aggr *RecvMsg)
302 AsyncIO *IO = &RecvMsg->IO;
304 if (!POP3C_OK) return eTerminateConnection;
305 RecvMsg->MsgNumbers = NewHash(1, NULL);
311 eNextState POP3C_GetListOneLine(pop3aggr *RecvMsg)
313 AsyncIO *IO = &RecvMsg->IO;
318 FetchItem *OneMsg = NULL;
321 if ((StrLength(RecvMsg->IO.IOBuf) == 1) &&
322 (ChrPtr(RecvMsg->IO.IOBuf)[0] == '.'))
324 if (GetCount(RecvMsg->MsgNumbers) == 0)
326 //// RecvMsg->Sate = ReadQuitState;
330 RecvMsg->Pos = GetNewHashPos(RecvMsg->MsgNumbers, 0);
337 * work around buggy pop3 servers which send
338 * empty lines in their listings.
340 if ((StrLength(RecvMsg->IO.IOBuf) == 0) ||
341 !isdigit(ChrPtr(RecvMsg->IO.IOBuf)[0]))
346 OneMsg = (FetchItem*) malloc(sizeof(FetchItem));
347 memset(OneMsg, 0, sizeof(FetchItem));
348 OneMsg->MSGID = atol(ChrPtr(RecvMsg->IO.IOBuf));
350 pch = strchr(ChrPtr(RecvMsg->IO.IOBuf), ' ');
353 OneMsg->MSGSize = atol(pch + 1);
356 rc = TestValidateHash(RecvMsg->MsgNumbers);
358 EVP3CCS_syslog(LOG_DEBUG, "Hash Invalid: %d\n", rc);
361 Put(RecvMsg->MsgNumbers, LKEY(OneMsg->MSGID), OneMsg, HfreeFetchItem);
363 rc = TestValidateHash(RecvMsg->MsgNumbers);
365 EVP3CCS_syslog(LOG_DEBUG, "Hash Invalid: %d\n", rc);
367 //RecvMsg->State --; /* read next Line */
371 eNextState POP3_FetchNetworkUsetableEntry(AsyncIO *IO)
376 pop3aggr *RecvMsg = (pop3aggr *) IO->Data;
378 if((RecvMsg->Pos != NULL) &&
379 GetNextHashPos(RecvMsg->MsgNumbers,
385 if (server_shutting_down)
388 if (CheckIfAlreadySeen("POP3 Item Seen",
389 RecvMsg->CurrMsg->MsgUID,
396 /* Item has already been seen */
397 RecvMsg->CurrMsg->NeedFetch = 0;
401 EVP3CCSM_syslog(LOG_DEBUG, "NO\n");
402 RecvMsg->CurrMsg->NeedFetch = 1;
404 return NextDBOperation(&RecvMsg->IO,
405 POP3_FetchNetworkUsetableEntry);
409 /* ok, now we know them all,
410 * continue with reading the actual messages. */
411 DeleteHashPos(&RecvMsg->Pos);
413 return QueueEventContext(IO, POP3_C_ReAttachToFetchMessages);
417 eNextState POP3C_GetOneMessagID(pop3aggr *RecvMsg)
419 AsyncIO *IO = &RecvMsg->IO;
426 rc = TestValidateHash(RecvMsg->MsgNumbers);
428 EVP3CCS_syslog(LOG_DEBUG, "Hash Invalid: %d\n", rc);
430 if((RecvMsg->Pos != NULL) &&
431 GetNextHashPos(RecvMsg->MsgNumbers,
436 RecvMsg->CurrMsg = (FetchItem*) vData;
437 /* Find out the UIDL of the message,
438 * to determine whether we've already downloaded it */
439 StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
440 "UIDL %ld\r\n", RecvMsg->CurrMsg->MSGID);
446 DeleteHashPos(&RecvMsg->Pos);
447 /// done receiving uidls.. start looking them up now.
448 RecvMsg->Pos = GetNewHashPos(RecvMsg->MsgNumbers, 0);
449 return QueueDBOperation(&RecvMsg->IO,
450 POP3_FetchNetworkUsetableEntry);
452 return eReadMore; /* TODO */
455 eNextState POP3C_GetOneMessageIDState(pop3aggr *RecvMsg)
457 AsyncIO *IO = &RecvMsg->IO;
460 rc = TestValidateHash(RecvMsg->MsgNumbers);
462 EVP3CCS_syslog(LOG_DEBUG, "Hash Invalid: %d\n", rc);
466 if (!POP3C_OK) return eTerminateConnection;
467 RecvMsg->CurrMsg->MsgUIDL =
468 NewStrBufPlain(NULL, StrLength(RecvMsg->IO.IOBuf));
469 RecvMsg->CurrMsg->MsgUID =
470 NewStrBufPlain(NULL, StrLength(RecvMsg->IO.IOBuf) * 2);
472 StrBufExtract_token(RecvMsg->CurrMsg->MsgUIDL,
473 RecvMsg->IO.IOBuf, 2, ' ');
475 StrBufPrintf(RecvMsg->CurrMsg->MsgUID,
477 ChrPtr(RecvMsg->RoomName),
478 ChrPtr(RecvMsg->CurrMsg->MsgUIDL),
479 RecvMsg->IO.ConnectMe->User,
480 RecvMsg->IO.ConnectMe->Host);
486 eNextState POP3C_SendGetOneMsg(pop3aggr *RecvMsg)
488 AsyncIO *IO = &RecvMsg->IO;
493 RecvMsg->CurrMsg = NULL;
494 while ((RecvMsg->Pos != NULL) &&
495 GetNextHashPos(RecvMsg->MsgNumbers,
499 (RecvMsg->CurrMsg = (FetchItem*) vData,
500 RecvMsg->CurrMsg->NeedFetch == 0))
503 if ((RecvMsg->CurrMsg != NULL ) && (RecvMsg->CurrMsg->NeedFetch == 1))
505 /* Message has not been seen.
506 * Tell the server to fetch the message... */
507 StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
508 "RETR %ld\r\n", RecvMsg->CurrMsg->MSGID);
513 RecvMsg->State = ReadQuitState;
514 return POP3_C_DispatchWriteDone(&RecvMsg->IO);
519 eNextState POP3C_ReadMessageBodyFollowing(pop3aggr *RecvMsg)
521 AsyncIO *IO = &RecvMsg->IO;
523 if (!POP3C_OK) return eTerminateConnection;
524 RecvMsg->IO.ReadMsg = NewAsyncMsg(HKEY("."),
525 RecvMsg->CurrMsg->MSGSize,
534 eNextState POP3C_StoreMsgRead(AsyncIO *IO)
536 pop3aggr *RecvMsg = (pop3aggr *) IO->Data;
538 EVP3CCS_syslog(LOG_DEBUG,
539 "MARKING: %s as seen: ",
540 ChrPtr(RecvMsg->CurrMsg->MsgUID));
541 CheckIfAlreadySeen("POP3 Item Seen",
542 RecvMsg->CurrMsg->MsgUID,
549 return QueueEventContext(&RecvMsg->IO, POP3_C_ReAttachToFetchMessages);
551 eNextState POP3C_SaveMsg(AsyncIO *IO)
554 pop3aggr *RecvMsg = (pop3aggr *) IO->Data;
556 /* Do Something With It (tm) */
557 msgnum = CtdlSubmitMsg(RecvMsg->CurrMsg->Msg,
559 ChrPtr(RecvMsg->RoomName),
563 /* Message has been committed to the store
564 * write the uidl to the use table
565 * so we don't fetch this message again
568 CtdlFreeMessage(RecvMsg->CurrMsg->Msg);
571 return NextDBOperation(&RecvMsg->IO, POP3C_StoreMsgRead);
574 eNextState POP3C_ReadMessageBody(pop3aggr *RecvMsg)
576 AsyncIO *IO = &RecvMsg->IO;
577 EVP3CM_syslog(LOG_DEBUG, "Converting message...");
578 RecvMsg->CurrMsg->Msg =
579 convert_internet_message_buf(&RecvMsg->IO.ReadMsg->MsgBuf);
580 StopClientWatchers(IO, 0);
581 return QueueDBOperation(&RecvMsg->IO, POP3C_SaveMsg);
584 eNextState POP3C_SendDelete(pop3aggr *RecvMsg)
586 AsyncIO *IO = &RecvMsg->IO;
587 if (!RecvMsg->keep) {
588 StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
589 "DELE %ld\r\n", RecvMsg->CurrMsg->MSGID);
594 RecvMsg->State = ReadMessageBodyFollowing;
595 return POP3_C_DispatchWriteDone(&RecvMsg->IO);
598 eNextState POP3C_ReadDeleteState(pop3aggr *RecvMsg)
600 AsyncIO *IO = &RecvMsg->IO;
602 RecvMsg->State = GetOneMessageIDState;
606 eNextState POP3C_SendQuit(pop3aggr *RecvMsg)
608 AsyncIO *IO = &RecvMsg->IO;
610 StrBufPlain(RecvMsg->IO.SendBuf.Buf,
617 eNextState POP3C_ReadQuitState(pop3aggr *RecvMsg)
619 AsyncIO *IO = &RecvMsg->IO;
621 return eTerminateConnection;
624 const long POP3_C_ConnTimeout = 1000;
625 const long DefaultPOP3Port = 110;
627 Pop3ClientHandler POP3C_ReadHandlers[] = {
631 POP3C_GetListCommandState,
632 POP3C_GetListOneLine,
633 POP3C_GetOneMessageIDState,
634 POP3C_ReadMessageBodyFollowing,
635 POP3C_ReadMessageBody,
636 POP3C_ReadDeleteState,
640 const long POP3_C_SendTimeouts[POP3C_MaxRead] = {
650 const ConstStr POP3C_ReadErrors[POP3C_MaxRead] = {
651 {HKEY("Connection broken during ")},
652 {HKEY("Connection broken during ")},
653 {HKEY("Connection broken during ")},
654 {HKEY("Connection broken during ")},
655 {HKEY("Connection broken during ")},
656 {HKEY("Connection broken during ")},
657 {HKEY("Connection broken during ")},
658 {HKEY("Connection broken during ")}
661 Pop3ClientHandler POP3C_SendHandlers[] = {
662 NULL, /* we don't send a greeting */
665 POP3C_SendListCommand,
667 POP3C_GetOneMessagID,
674 const long POP3_C_ReadTimeouts[] = {
686 /*****************************************************************************/
687 /* POP3 CLIENT DISPATCHER */
688 /*****************************************************************************/
690 void POP3SetTimeout(eNextState NextTCPState, pop3aggr *pMsg)
692 AsyncIO *IO = &pMsg->IO;
693 double Timeout = 0.0;
695 EVP3C_syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
697 switch (NextTCPState) {
701 Timeout = POP3_C_SendTimeouts[pMsg->State];
703 if (pMsg->State == eDATABody) {
704 / * if we're sending a huge message, we need more time. * /
705 Timeout += StrLength(pMsg->msgtext) / 1024;
711 Timeout = POP3_C_ReadTimeouts[pMsg->State];
713 if (pMsg->State == eDATATerminateBody) {
715 * some mailservers take a nap before accepting the message
716 * content inspection and such.
718 Timeout += StrLength(pMsg->msgtext) / 1024;
729 case eTerminateConnection:
732 case eReadMore://// TODO
735 SetNextTimeout(&pMsg->IO, Timeout);
737 eNextState POP3_C_DispatchReadDone(AsyncIO *IO)
739 /* EVP3CCS_syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__); to noisy anyways. */
740 pop3aggr *pMsg = IO->Data;
743 rc = POP3C_ReadHandlers[pMsg->State](pMsg);
746 POP3SetTimeout(rc, pMsg);
749 eNextState POP3_C_DispatchWriteDone(AsyncIO *IO)
751 pop3aggr *pMsg = IO->Data;
754 /* EVP3CCS_syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__); to noisy anyways. */
755 rc = POP3C_SendHandlers[pMsg->State](pMsg);
756 POP3SetTimeout(rc, pMsg);
761 /*****************************************************************************/
762 /* POP3 CLIENT ERROR CATCHERS */
763 /*****************************************************************************/
764 eNextState POP3_C_Terminate(AsyncIO *IO)
766 /// pop3aggr *pMsg = (pop3aggr *)IO->Data;
768 EVP3CCS_syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
769 FinalizePOP3AggrRun(IO);
772 eNextState POP3_C_TerminateDB(AsyncIO *IO)
774 /// pop3aggr *pMsg = (pop3aggr *)IO->Data;
776 EVP3CCS_syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
777 FinalizePOP3AggrRun(IO);
780 eNextState POP3_C_Timeout(AsyncIO *IO)
782 pop3aggr *pMsg = IO->Data;
784 EVP3CCS_syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
785 StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State]));
786 return FailAggregationRun(IO);
788 eNextState POP3_C_ConnFail(AsyncIO *IO)
790 pop3aggr *pMsg = (pop3aggr *)IO->Data;
792 EVP3CCS_syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
793 StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State]));
794 return FailAggregationRun(IO);
796 eNextState POP3_C_DNSFail(AsyncIO *IO)
798 pop3aggr *pMsg = (pop3aggr *)IO->Data;
800 EVP3CCS_syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
801 StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State]));
802 return FailAggregationRun(IO);
804 eNextState POP3_C_Shutdown(AsyncIO *IO)
806 EVP3CCS_syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
807 //// pop3aggr *pMsg = IO->Data;
809 ////pMsg->MyQEntry->Status = 3;
810 ///StrBufPlain(pMsg->MyQEntry->StatusMessage, HKEY("server shutdown during message retrieval."));
811 FinalizePOP3AggrRun(IO);
817 * @brief lineread Handler; understands when to read more POP3 lines,
818 * and when this is a one-lined reply.
820 eReadState POP3_C_ReadServerStatus(AsyncIO *IO)
822 eReadState Finished = eBufferNotEmpty;
824 switch (IO->NextState) {
829 case eTerminateConnection:
831 Finished = eReadFail;
838 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
842 Finished = CtdlReadMessageBodyAsync(IO);
848 /*****************************************************************************
849 * So we connect our Server IP here. *
850 *****************************************************************************/
851 eNextState POP3_C_ReAttachToFetchMessages(AsyncIO *IO)
853 pop3aggr *cpptr = IO->Data;
855 EVP3CCS_syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
856 ////??? cpptr->State ++;
857 if (cpptr->Pos == NULL)
858 cpptr->Pos = GetNewHashPos(cpptr->MsgNumbers, 0);
860 POP3_C_DispatchWriteDone(IO);
861 ReAttachIO(IO, cpptr, 0);
862 IO->NextState = eReadMessage;
863 return IO->NextState;
866 eNextState pop3_connect_ip(AsyncIO *IO)
868 pop3aggr *cpptr = IO->Data;
870 if (cpptr->IOStart == 0.0) /* whith or without DNS? */
871 cpptr->IOStart = IO->Now;
873 EVP3CCS_syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
875 return EvConnectSock(IO,
877 POP3_C_ReadTimeouts[0],
881 eNextState pop3_get_one_host_ip_done(AsyncIO *IO)
883 pop3aggr *cpptr = IO->Data;
884 struct hostent *hostent;
888 hostent = cpptr->HostLookup.VParsedDNSReply;
889 if ((cpptr->HostLookup.DNSStatus == ARES_SUCCESS) &&
890 (hostent != NULL) ) {
891 memset(&cpptr->IO.ConnectMe->Addr, 0, sizeof(struct in6_addr));
892 if (cpptr->IO.ConnectMe->IPv6) {
893 memcpy(&cpptr->IO.ConnectMe->Addr.sin6_addr.s6_addr,
894 &hostent->h_addr_list[0],
895 sizeof(struct in6_addr));
897 cpptr->IO.ConnectMe->Addr.sin6_family =
899 cpptr->IO.ConnectMe->Addr.sin6_port =
900 htons(DefaultPOP3Port);
903 struct sockaddr_in *addr =
904 (struct sockaddr_in*)
905 &cpptr->IO.ConnectMe->Addr;
907 memcpy(&addr->sin_addr.s_addr,
908 hostent->h_addr_list[0],
911 addr->sin_family = hostent->h_addrtype;
912 addr->sin_port = htons(DefaultPOP3Port);
914 return pop3_connect_ip(IO);
920 eNextState pop3_get_one_host_ip(AsyncIO *IO)
922 pop3aggr *cpptr = IO->Data;
924 cpptr->IOStart = IO->Now;
926 EVP3CCS_syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
928 EVP3CCS_syslog(LOG_DEBUG,
929 "POP3 client[%ld]: looking up %s-Record %s : %d ...\n",
931 (cpptr->IO.ConnectMe->IPv6)? "aaaa": "a",
932 cpptr->IO.ConnectMe->Host,
933 cpptr->IO.ConnectMe->Port);
935 QueueQuery((cpptr->IO.ConnectMe->IPv6)? ns_t_aaaa : ns_t_a,
936 cpptr->IO.ConnectMe->Host,
939 pop3_get_one_host_ip_done);
940 IO->NextState = eReadDNSReply;
941 return IO->NextState;
946 int pop3_do_fetching(pop3aggr *cpptr)
948 AsyncIO *IO = &cpptr->IO;
953 POP3_C_ReadServerStatus,
955 POP3_C_DispatchWriteDone,
956 POP3_C_DispatchReadDone,
963 safestrncpy(((CitContext *)cpptr->IO.CitContext)->cs_host,
965 sizeof(((CitContext *)cpptr->IO.CitContext)->cs_host));
967 if (cpptr->IO.ConnectMe->IsIP) {
968 QueueEventContext(&cpptr->IO,
972 QueueEventContext(&cpptr->IO,
973 pop3_get_one_host_ip);
979 * Scan a room's netconfig to determine whether it requires POP3 aggregation
981 void pop3client_scan_room(struct ctdlroom *qrbuf, void *data, OneRoomNetCfg *OneRNCFG)
983 const RoomNetCfgLine *pLine;
986 pthread_mutex_lock(&POP3QueueMutex);
987 if (GetHash(POP3QueueRooms, LKEY(qrbuf->QRnumber), &vptr))
989 pthread_mutex_unlock(&POP3QueueMutex);
990 EVP3CQ_syslog(LOG_DEBUG,
991 "pop3client: [%ld] %s already in progress.",
996 pthread_mutex_unlock(&POP3QueueMutex);
998 if (server_shutting_down) return;
1000 pLine = OneRNCFG->NetConfigs[pop3client];
1002 while (pLine != NULL)
1006 cptr = (pop3aggr *) malloc(sizeof(pop3aggr));
1007 memset(cptr, 0, sizeof(pop3aggr));
1008 ///TODO do we need this? cptr->roomlist_parts=1;
1009 cptr->RoomName = NewStrBufPlain(qrbuf->QRname, -1);
1010 cptr->pop3user = NewStrBufDup(pLine->Value[1]);
1011 cptr->pop3pass = NewStrBufDup(pLine->Value[2]);
1012 cptr->Url = NewStrBuf();
1013 cptr->Host = NewStrBufDup(pLine->Value[0]);
1015 cptr->keep = atol(ChrPtr(pLine->Value[3]));
1016 cptr->interval = atol(ChrPtr(pLine->Value[4]));
1018 StrBufAppendBufPlain(cptr->Url, HKEY("pop3://"), 0);
1019 StrBufUrlescUPAppend(cptr->Url, cptr->pop3user, NULL);
1020 StrBufAppendBufPlain(cptr->Url, HKEY(":"), 0);
1021 StrBufUrlescUPAppend(cptr->Url, cptr->pop3pass, NULL);
1022 StrBufAppendBufPlain(cptr->Url, HKEY("@"), 0);
1023 StrBufAppendBuf(cptr->Url, cptr->Host, 0);
1024 StrBufAppendBufPlain(cptr->Url, HKEY("/"), 0);
1025 StrBufUrlescAppend(cptr->Url, cptr->RoomName, NULL);
1027 ParseURL(&cptr->IO.ConnectMe, cptr->Url, 110);
1031 /* todo: we need to reunite the url to be shure. */
1033 pthread_mutex_lock(&POP3ueueMutex);
1034 GetHash(POP3FetchUrls, SKEY(ptr->Url), &vptr);
1035 use_this_cptr = (pop3aggr *)vptr;
1037 if (use_this_rncptr != NULL)
1039 /* mustn't attach to an active session */
1040 if (use_this_cptr->RefCount > 0)
1042 DeletePOP3Cfg(cptr);
1048 StrBufAppendBufPlain(
1049 use_this_cptr->rooms,
1052 if (use_this_cptr->roomlist_parts == 1)
1054 use_this_cptr->OtherQRnumbers
1055 = NewHash(1, lFlathash);
1057 QRnumber = (long*)malloc(sizeof(long));
1058 *QRnumber = qrbuf->QRnumber;
1059 Put(use_this_cptr->OtherQRnumbers,
1060 LKEY(qrbuf->QRnumber),
1064 use_this_cptr->roomlist_parts++;
1066 pthread_mutex_unlock(&POP3QueueMutex);
1069 pthread_mutex_unlock(&RSSQueueMutex);
1071 cptr->n = Pop3ClientID++;
1072 pthread_mutex_lock(&POP3QueueMutex);
1076 DeletePOP3Aggregator);
1078 pthread_mutex_unlock(&POP3QueueMutex);
1079 pLine = pLine->next;
1084 static int doing_pop3client = 0;
1086 void pop3client_scan(void) {
1087 static time_t last_run = 0L;
1088 time_t fastest_scan;
1095 become_session(&pop3_client_CC);
1097 if (config.c_pop3_fastest < config.c_pop3_fetch)
1098 fastest_scan = config.c_pop3_fastest;
1100 fastest_scan = config.c_pop3_fetch;
1103 * Run POP3 aggregation no more frequently than once every n seconds
1105 if ( (time(NULL) - last_run) < fastest_scan ) {
1110 * This is a simple concurrency check to make sure only one pop3client
1111 * run is done at a time. We could do this with a mutex, but since we
1112 * don't really require extremely fine granularity here, we'll do it
1113 * with a static variable instead.
1115 if (doing_pop3client) return;
1116 doing_pop3client = 1;
1118 EVP3CQM_syslog(LOG_DEBUG, "pop3client started");
1119 CtdlForEachNetCfgRoom(pop3client_scan_room, NULL, pop3client);
1121 pthread_mutex_lock(&POP3QueueMutex);
1122 it = GetNewHashPos(POP3FetchUrls, 0);
1123 while (!server_shutting_down &&
1124 GetNextHashPos(POP3FetchUrls, it, &len, &Key, &vrptr) &&
1126 cptr = (pop3aggr *)vrptr;
1127 if (cptr->RefCount == 0)
1128 if (!pop3_do_fetching(cptr))
1129 DeletePOP3Aggregator(cptr);////TODO
1132 if ((palist->interval && time(NULL) > (last_run + palist->interval))
1133 || (time(NULL) > last_run + config.c_pop3_fetch))
1134 pop3_do_fetching(palist->roomname, palist->pop3host,
1135 palist->pop3user, palist->pop3pass, palist->keep);
1137 palist = palist->next;
1142 pthread_mutex_unlock(&POP3QueueMutex);
1144 EVP3CQM_syslog(LOG_DEBUG, "pop3client ended");
1145 last_run = time(NULL);
1146 doing_pop3client = 0;
1150 void pop3_cleanup(void)
1152 /* citthread_mutex_destroy(&POP3QueueMutex); TODO */
1153 while (doing_pop3client != 0) ;
1154 DeleteHash(&POP3FetchUrls);
1155 DeleteHash(&POP3QueueRooms);
1160 void LogDebugEnablePOP3Client(const int n)
1162 POP3ClientDebugEnabled = n;
1165 CTDL_MODULE_INIT(pop3client)
1169 CtdlFillSystemContext(&pop3_client_CC, "POP3aggr");
1170 CtdlREGISTERRoomCfgType(pop3client, ParseGeneric, 0, 5, SerializeGeneric, DeleteGenericCfgLine);
1171 pthread_mutex_init(&POP3QueueMutex, NULL);
1172 POP3QueueRooms = NewHash(1, lFlathash);
1173 POP3FetchUrls = NewHash(1, NULL);
1174 CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER, PRIO_AGGR + 50);
1175 CtdlRegisterEVCleanupHook(pop3_cleanup);
1176 CtdlRegisterDebugFlagHook(HKEY("pop3client"), LogDebugEnablePOP3Client, &POP3ClientDebugEnabled);
1179 /* return our module id for the log */
1180 return "pop3client";