X-Git-Url: https://code.citadel.org/?a=blobdiff_plain;f=citadel%2Fmodules%2Fpop3client%2Fserv_pop3client.c;h=8a0d23c38f6c64537d9d2f44573082169509a43a;hb=cf7cb2463d47a4a9ed36c8d1c13f188418389437;hp=827976a3c93f0b11a250d709f601d93cd0b76347;hpb=969d199c6fb0d1f54220dc906202946a0b43f387;p=citadel.git diff --git a/citadel/modules/pop3client/serv_pop3client.c b/citadel/modules/pop3client/serv_pop3client.c index 827976a3c..8a0d23c38 100644 --- a/citadel/modules/pop3client/serv_pop3client.c +++ b/citadel/modules/pop3client/serv_pop3client.c @@ -1,21 +1,21 @@ /* * Consolidate mail from remote POP3 accounts. * - * Copyright (c) 2007-2009 by the citadel.org team + * Copyright (c) 2007-2011 by the citadel.org team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. + * This program is open source software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include @@ -53,8 +53,9 @@ #include "event_client.h" +struct CitContext pop3_client_CC; -citthread_mutex_t POP3QueueMutex; /* locks the access to the following vars: */ +pthread_mutex_t POP3QueueMutex; /* locks the access to the following vars: */ HashList *POP3QueueRooms = NULL; /* rss_room_counter */ HashList *POP3FetchUrls = NULL; /* -> rss_aggregator; ->RefCount access to be locked too. */ @@ -68,9 +69,11 @@ typedef enum ePOP3_C_States { GetUserState, GetPassState, GetListCommandState, + GetListOneLine, GetOneMessageIDState, ReadMessageBodyFollowing, ReadMessageBody, + GetDeleteState, ReadQuitState, POP3C_MaxRead }ePOP3_C_States; @@ -78,9 +81,11 @@ typedef enum ePOP3_C_States { typedef struct _FetchItem { long MSGID; + long MSGSize; StrBuf *MsgUIDL; StrBuf *MsgUID; int NeedFetch; + struct CtdlMessage *Msg; } FetchItem; void HfreeFetchItem(void *vItem) @@ -96,10 +101,10 @@ typedef struct __pop3aggr { long n; long RefCount; - ParsedURL Pop3Host; +/// ParsedURL *Pop3Host; DNSQueryParts HostLookup; - StrBuf *rooms; +// StrBuf *rooms; long QRnumber; HashList *OtherQRnumbers; @@ -121,10 +126,20 @@ void DeletePOP3Aggregator(void *vptr) pop3aggr *ptr = vptr; DeleteHashPos(&ptr->Pos); DeleteHash(&ptr->MsgNumbers); - FreeStrBuf(&ptr->rooms); +// FreeStrBuf(&ptr->rooms); FreeStrBuf(&ptr->pop3user); FreeStrBuf(&ptr->pop3pass); FreeStrBuf(&ptr->RoomName); + FreeURL(&ptr->IO.ConnectMe); + FreeStrBuf(&ptr->Url); + FreeStrBuf(&ptr->IO.IOBuf); + FreeStrBuf(&ptr->IO.SendBuf.Buf); + FreeStrBuf(&ptr->IO.RecvBuf.Buf); + DeleteAsyncMsg(&ptr->IO.ReadMsg); + ((struct CitContext*)ptr->IO.CitContext)->state = CON_IDLE; + ((struct CitContext*)ptr->IO.CitContext)->kill_me = 1; + FreeAsyncIOContents(&ptr->IO); + free(ptr); } @@ -133,14 +148,28 @@ typedef eNextState(*Pop3ClientHandler)(pop3aggr* RecvMsg); eNextState POP3_C_Shutdown(AsyncIO *IO); eNextState POP3_C_Timeout(AsyncIO *IO); eNextState POP3_C_ConnFail(AsyncIO *IO); +eNextState POP3_C_DNSFail(AsyncIO *IO); eNextState POP3_C_DispatchReadDone(AsyncIO *IO); eNextState POP3_C_DispatchWriteDone(AsyncIO *IO); eNextState POP3_C_Terminate(AsyncIO *IO); eReadState POP3_C_ReadServerStatus(AsyncIO *IO); +eNextState POP3_C_ReAttachToFetchMessages(AsyncIO *IO); eNextState FinalizePOP3AggrRun(AsyncIO *IO) { + HashPos *It; + pop3aggr *cptr = (pop3aggr *)IO->Data; + syslog(LOG_DEBUG, "Terminating Aggregator; bye.\n"); + + It = GetNewHashPos(POP3FetchUrls, 0); + pthread_mutex_lock(&POP3QueueMutex); + { + if (GetHashPosFromKey(POP3FetchUrls, SKEY(cptr->Url), It)) + DeleteEntryFromHash(POP3FetchUrls, It); + } + pthread_mutex_unlock(&POP3QueueMutex); + DeleteHashPos(&It); return eAbort; } @@ -149,8 +178,9 @@ eNextState FailAggregationRun(AsyncIO *IO) return eAbort; } -#define POP3C_DBG_SEND() CtdlLogPrintf(CTDL_DEBUG, "POP3 client[%ld]: > %s\n", RecvMsg->n, ChrPtr(RecvMsg->IO.SendBuf.Buf)) -#define POP3C_DBG_READ() CtdlLogPrintf(CTDL_DEBUG, "POP3 client[%ld]: < %s\n", RecvMsg->n, ChrPtr(RecvMsg->IO.IOBuf)) + +#define POP3C_DBG_SEND() syslog(LOG_DEBUG, "POP3 client[%ld]: > %s\n", RecvMsg->n, ChrPtr(RecvMsg->IO.SendBuf.Buf)) +#define POP3C_DBG_READ() syslog(LOG_DEBUG, "POP3 client[%ld]: < %s\n", RecvMsg->n, ChrPtr(RecvMsg->IO.IOBuf)) #define POP3C_OK (strncasecmp(ChrPtr(RecvMsg->IO.IOBuf), "+OK", 3) == 0) eNextState POP3C_ReadGreeting(pop3aggr *RecvMsg) @@ -161,7 +191,6 @@ eNextState POP3C_ReadGreeting(pop3aggr *RecvMsg) else return eSendReply; } - eNextState POP3C_SendUser(pop3aggr *RecvMsg) { /* Identify ourselves. NOTE: we have to append a CR to each command. The LF will @@ -187,7 +216,7 @@ eNextState POP3C_SendPassword(pop3aggr *RecvMsg) /* Password */ StrBufPrintf(RecvMsg->IO.SendBuf.Buf, "PASS %s\r\n", ChrPtr(RecvMsg->pop3pass)); - CtdlLogPrintf(CTDL_DEBUG, "\n"); + syslog(LOG_DEBUG, "\n"); // POP3C_DBG_SEND(); return eReadMessage; } @@ -212,12 +241,17 @@ eNextState POP3C_GetListCommandState(pop3aggr *RecvMsg) POP3C_DBG_READ(); if (!POP3C_OK) return eTerminateConnection; RecvMsg->MsgNumbers = NewHash(1, NULL); + RecvMsg->State++; return eReadMore; } eNextState POP3C_GetListOneLine(pop3aggr *RecvMsg) { +#if 0 + int rc; +#endif + const char *pch; FetchItem *OneMsg = NULL; POP3C_DBG_READ(); @@ -237,75 +271,119 @@ eNextState POP3C_GetListOneLine(pop3aggr *RecvMsg) } OneMsg = (FetchItem*) malloc(sizeof(FetchItem)); memset(OneMsg, 0, sizeof(FetchItem)); - OneMsg->MSGID = atoi(ChrPtr(RecvMsg->IO.IOBuf)); - Put(RecvMsg->MsgNumbers, LKEY(OneMsg->MSGID), OneMsg, HfreeFetchItem); + OneMsg->MSGID = atol(ChrPtr(RecvMsg->IO.IOBuf)); + pch = strchr(ChrPtr(RecvMsg->IO.IOBuf), ' '); + if (pch != NULL) + { + OneMsg->MSGSize = atol(pch + 1); + } +#if 0 + rc = TestValidateHash(RecvMsg->MsgNumbers); + if (rc != 0) + syslog(LOG_DEBUG, "Hash Invalid: %d\n", rc); +#endif + + Put(RecvMsg->MsgNumbers, LKEY(OneMsg->MSGID), OneMsg, HfreeFetchItem); +#if 0 + rc = TestValidateHash(RecvMsg->MsgNumbers); + if (rc != 0) + syslog(LOG_DEBUG, "Hash Invalid: %d\n", rc); +#endif //RecvMsg->State --; /* read next Line */ return eReadMore; } -eNextState POP3C_GetOneMessagID(pop3aggr *RecvMsg) +eNextState POP3_FetchNetworkUsetableEntry(AsyncIO *IO) { long HKLen; const char *HKey; void *vData; - - if(GetNextHashPos(RecvMsg->MsgNumbers, RecvMsg->Pos, &HKLen, &HKey, &vData)) - { - RecvMsg->CurrMsg = (FetchItem*) vData; - /* Find out the UIDL of the message, to determine whether we've already downloaded it */ - StrBufPrintf(RecvMsg->IO.SendBuf.Buf, - "UIDL %ld\r\n", RecvMsg->CurrMsg->MSGID); - POP3C_DBG_SEND(); - } - else - { - DeleteHashPos(&RecvMsg->Pos); - /// done receiving uidls.. start looking them up now. - RecvMsg->Pos = GetNewHashPos(RecvMsg->MsgNumbers, 0); - - } - return eReadMore; /* TODO */ -} - -#if 0 -eNextState FetchNetworkUsetableEntry(AsyncIO *IO) -{ struct cdbdata *cdbut; - networker_save_message *Ctx = (networker_save_message *) IO->Data; + pop3aggr *RecvMsg = (pop3aggr *) IO->Data; if(GetNextHashPos(RecvMsg->MsgNumbers, RecvMsg->Pos, &HKLen, &HKey, &vData)) { - + struct UseTable ut; + if (server_shutting_down) + return eAbort; + + RecvMsg->CurrMsg = (FetchItem*) vData; + syslog(LOG_DEBUG, "CHECKING: whether %s has already been seen: ", ChrPtr(RecvMsg->CurrMsg->MsgUID)); /* Find out if we've already seen this item */ - strcpy(Ctx->ut.ut_msgid, ChrPtr(Ctx->MsgGUID)); /// TODO - Ctx->ut.ut_timestamp = time(NULL); + safestrncpy(ut.ut_msgid, + ChrPtr(RecvMsg->CurrMsg->MsgUID), + sizeof(ut.ut_msgid)); + ut.ut_timestamp = time(NULL);/// TODO: libev timestamp! - cdbut = cdb_fetch(CDB_USETABLE, SKEY(Ctx->MsgGUID)); + cdbut = cdb_fetch(CDB_USETABLE, SKEY(RecvMsg->CurrMsg->MsgUID)); if (cdbut != NULL) { /* Item has already been seen */ - CtdlLogPrintf(CTDL_DEBUG, "%s has already been seen\n", ChrPtr(Ctx->MsgGUID)); + syslog(LOG_DEBUG, "YES\n"); cdb_free(cdbut); /* rewrite the record anyway, to update the timestamp */ cdb_store(CDB_USETABLE, - SKEY(Ctx->MsgGUID), - &Ctx->ut, sizeof(struct UseTable) ); - return eAbort; + SKEY(RecvMsg->CurrMsg->MsgUID), + &ut, sizeof(struct UseTable) ); + RecvMsg->CurrMsg->NeedFetch = 0; ////TODO0; } else { - NextDBOperation(IO, RSSSaveMessage); - return eSendMore; + syslog(LOG_DEBUG, "NO\n"); + RecvMsg->CurrMsg->NeedFetch = 1; } + return NextDBOperation(&RecvMsg->IO, POP3_FetchNetworkUsetableEntry); + } + else + { + /* ok, now we know them all, continue with reading the actual messages. */ + DeleteHashPos(&RecvMsg->Pos); + + return QueueEventContext(IO, POP3_C_ReAttachToFetchMessages); } - return eReadMessage; } -#endif +eNextState POP3C_GetOneMessagID(pop3aggr *RecvMsg) +{ + long HKLen; + const char *HKey; + void *vData; + +#if 0 + int rc; + rc = TestValidateHash(RecvMsg->MsgNumbers); + if (rc != 0) + syslog(LOG_DEBUG, "Hash Invalid: %d\n", rc); +#endif + if(GetNextHashPos(RecvMsg->MsgNumbers, RecvMsg->Pos, &HKLen, &HKey, &vData)) + { + RecvMsg->CurrMsg = (FetchItem*) vData; + /* Find out the UIDL of the message, to determine whether we've already downloaded it */ + StrBufPrintf(RecvMsg->IO.SendBuf.Buf, + "UIDL %ld\r\n", RecvMsg->CurrMsg->MSGID); + POP3C_DBG_SEND(); + } + else + { + RecvMsg->State++; + DeleteHashPos(&RecvMsg->Pos); + /// done receiving uidls.. start looking them up now. + RecvMsg->Pos = GetNewHashPos(RecvMsg->MsgNumbers, 0); + return QueueDBOperation(&RecvMsg->IO, POP3_FetchNetworkUsetableEntry); + } + return eReadMore; /* TODO */ +} eNextState POP3C_GetOneMessageIDState(pop3aggr *RecvMsg) { +#if 0 + int rc; + rc = TestValidateHash(RecvMsg->MsgNumbers); + if (rc != 0) + syslog(LOG_DEBUG, "Hash Invalid: %d\n", rc); +#endif + POP3C_DBG_READ(); if (!POP3C_OK) return eTerminateConnection; RecvMsg->CurrMsg->MsgUIDL = NewStrBufPlain(NULL, StrLength(RecvMsg->IO.IOBuf)); @@ -313,41 +391,39 @@ eNextState POP3C_GetOneMessageIDState(pop3aggr *RecvMsg) StrBufExtract_token(RecvMsg->CurrMsg->MsgUIDL, RecvMsg->IO.IOBuf, 2, ' '); StrBufPrintf(RecvMsg->CurrMsg->MsgUID, - "pop3/%s/%s@%s", + "pop3/%s/%s:%s@%s", ChrPtr(RecvMsg->RoomName), ChrPtr(RecvMsg->CurrMsg->MsgUIDL), - RecvMsg->Pop3Host.Host); - return eReadMessage; + RecvMsg->IO.ConnectMe->User, + RecvMsg->IO.ConnectMe->Host); + RecvMsg->State --; + return eSendReply; } -eNextState POP3C_GetOneMessageIDFromUseTable(pop3aggr *RecvMsg) -{ - struct cdbdata *cdbut; - struct UseTable ut; +eNextState POP3C_SendGetOneMsg(pop3aggr *RecvMsg) +{ + long HKLen; + const char *HKey; + void *vData; - cdbut = cdb_fetch(CDB_USETABLE, SKEY(RecvMsg->CurrMsg->MsgUID)); - if (cdbut != NULL) { - /* message has already been seen */ - CtdlLogPrintf(CTDL_DEBUG, "%s has already been seen\n", ChrPtr(RecvMsg->CurrMsg->MsgUID)); - cdb_free(cdbut); + RecvMsg->CurrMsg = NULL; + while (GetNextHashPos(RecvMsg->MsgNumbers, RecvMsg->Pos, &HKLen, &HKey, &vData) && + (RecvMsg->CurrMsg = (FetchItem*) vData, RecvMsg->CurrMsg->NeedFetch == 0)) + {} - /* rewrite the record anyway, to update the timestamp */ - strcpy(ut.ut_msgid, ChrPtr(RecvMsg->CurrMsg->MsgUID)); - ut.ut_timestamp = time(NULL); - cdb_store(CDB_USETABLE, SKEY(RecvMsg->CurrMsg->MsgUID), &ut, sizeof(struct UseTable) ); + if ((RecvMsg->CurrMsg != NULL ) && (RecvMsg->CurrMsg->NeedFetch == 1)) + { + /* Message has not been seen. Tell the server to fetch the message... */ + StrBufPrintf(RecvMsg->IO.SendBuf.Buf, + "RETR %ld\r\n", RecvMsg->CurrMsg->MSGID); + POP3C_DBG_SEND(); + return eReadMessage; + } + else { + RecvMsg->State = ReadQuitState; + return POP3_C_DispatchWriteDone(&RecvMsg->IO); } - - return eReadMessage; -} - -eNextState POP3C_SendGetOneMsg(pop3aggr *RecvMsg) -{ - /* Message has not been seen. Tell the server to fetch the message... */ - StrBufPrintf(RecvMsg->IO.SendBuf.Buf, - "RETR %ld\r\n", RecvMsg->CurrMsg->MSGID); - POP3C_DBG_SEND(); - return eReadMessage; } @@ -355,56 +431,79 @@ eNextState POP3C_ReadMessageBodyFollowing(pop3aggr *RecvMsg) { POP3C_DBG_READ(); if (!POP3C_OK) return eTerminateConnection; - else return eSendReply; + RecvMsg->IO.ReadMsg = NewAsyncMsg(HKEY("."), + RecvMsg->CurrMsg->MSGSize, + config.c_maxmsglen, + NULL, -1, + 1); + + return eReadPayload; } +eNextState POP3C_StoreMsgRead(AsyncIO *IO) +{ + pop3aggr *RecvMsg = (pop3aggr *) IO->Data; + struct UseTable ut; -eNextState POP3C_ReadMessageBody(pop3aggr *RecvMsg) + syslog(LOG_DEBUG, "MARKING: %s as seen: ", ChrPtr(RecvMsg->CurrMsg->MsgUID)); + + safestrncpy(ut.ut_msgid, + ChrPtr(RecvMsg->CurrMsg->MsgUID), + sizeof(ut.ut_msgid)); + ut.ut_timestamp = time(NULL); /* TODO: use libev time */ + cdb_store(CDB_USETABLE, + ChrPtr(RecvMsg->CurrMsg->MsgUID), + StrLength(RecvMsg->CurrMsg->MsgUID), + &ut, + sizeof(struct UseTable) ); + + return QueueEventContext(&RecvMsg->IO, POP3_C_ReAttachToFetchMessages); +} +eNextState POP3C_SaveMsg(AsyncIO *IO) { -#if 0 -//TODO - /* If we get to this point, the message is on its way. Read it. */ - body = CtdlReadMessageBody(HKEY("."), config.c_maxmsglen, NULL, 1, &sock); - if (body == NULL) goto bail; - - CtdlLogPrintf(CTDL_DEBUG, "Converting message...\n"); - msg = convert_internet_message(body); - body = NULL; /* yes, this should be dereferenced, NOT freed */ - - /* Do Something With It (tm) */ - msgnum = CtdlSubmitMsg(msg, NULL, roomname, 0); + long msgnum; + pop3aggr *RecvMsg = (pop3aggr *) IO->Data; + + /* Do Something With It (tm) */ + msgnum = CtdlSubmitMsg(RecvMsg->CurrMsg->Msg, + NULL, + ChrPtr(RecvMsg->RoomName), + 0); if (msgnum > 0L) { /* Message has been committed to the store */ /* write the uidl to the use table so we don't fetch this message again */ } - CtdlFreeMessage(msg); -#endif - return eReadMessage; + CtdlFreeMessage(RecvMsg->CurrMsg->Msg); + + return NextDBOperation(&RecvMsg->IO, POP3C_StoreMsgRead); } -eNextState POP3C_StoreMsgRead(pop3aggr *RecvMsg) +eNextState POP3C_ReadMessageBody(pop3aggr *RecvMsg) { -#if 0 - strcpy(ut.ut_msgid, utmsgid); - ut.ut_timestamp = time(NULL); - cdb_store(CDB_USETABLE, utmsgid, strlen(utmsgid), - &ut, sizeof(struct UseTable) ); -#endif - return eReadMessage;/// TODO + syslog(LOG_DEBUG, "Converting message...\n"); + RecvMsg->CurrMsg->Msg = convert_internet_message_buf(&RecvMsg->IO.ReadMsg->MsgBuf); + + return QueueDBOperation(&RecvMsg->IO, POP3C_SaveMsg); } + eNextState POP3C_SendDelete(pop3aggr *RecvMsg) { if (!RecvMsg->keep) { StrBufPrintf(RecvMsg->IO.SendBuf.Buf, "DELE %ld\r\n", RecvMsg->CurrMsg->MSGID); POP3C_DBG_SEND(); + return eReadMessage; + } + else { + RecvMsg->State = ReadMessageBodyFollowing; + return POP3_C_DispatchWriteDone(&RecvMsg->IO); } - return eReadMessage; } eNextState POP3C_ReadDeleteState(pop3aggr *RecvMsg) { POP3C_DBG_READ(); + RecvMsg->State = GetOneMessageIDState; return eReadMessage; } @@ -421,7 +520,7 @@ eNextState POP3C_SendQuit(pop3aggr *RecvMsg) eNextState POP3C_ReadQuitState(pop3aggr *RecvMsg) { POP3C_DBG_READ(); - return eAbort; + return eTerminateConnection; } const long POP3_C_ConnTimeout = 1000; @@ -432,9 +531,11 @@ Pop3ClientHandler POP3C_ReadHandlers[] = { POP3C_GetUserState, POP3C_GetPassState, POP3C_GetListCommandState, + POP3C_GetListOneLine, POP3C_GetOneMessageIDState, POP3C_ReadMessageBodyFollowing, POP3C_ReadMessageBody, + POP3C_ReadDeleteState, POP3C_ReadQuitState, }; @@ -464,9 +565,10 @@ Pop3ClientHandler POP3C_SendHandlers[] = { POP3C_SendUser, POP3C_SendPassword, POP3C_SendListCommand, - POP3C_GetListOneLine, + NULL, POP3C_GetOneMessagID, POP3C_SendGetOneMsg, + NULL, POP3C_SendDelete, POP3C_SendQuit }; @@ -491,9 +593,10 @@ void POP3SetTimeout(eNextState NextTCPState, pop3aggr *pMsg) { double Timeout = 0.0; - CtdlLogPrintf(CTDL_DEBUG, "POP3: %s\n", __FUNCTION__); + syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__); switch (NextTCPState) { + case eSendFile: case eSendReply: case eSendMore: Timeout = POP3_C_SendTimeouts[pMsg->State]; @@ -504,6 +607,7 @@ void POP3SetTimeout(eNextState NextTCPState, pop3aggr *pMsg) } */ break; + case eReadFile: case eReadMessage: Timeout = POP3_C_ReadTimeouts[pMsg->State]; /* @@ -516,10 +620,15 @@ void POP3SetTimeout(eNextState NextTCPState, pop3aggr *pMsg) } */ break; + case eReadPayload: + Timeout = 100000; + /* TODO!!! */ + break; case eSendDNSQuery: case eReadDNSReply: case eConnect: case eTerminateConnection: + case eDBQuery: case eAbort: case eReadMore://// TODO return; @@ -528,18 +637,19 @@ void POP3SetTimeout(eNextState NextTCPState, pop3aggr *pMsg) } eNextState POP3_C_DispatchReadDone(AsyncIO *IO) { - CtdlLogPrintf(CTDL_DEBUG, "POP3: %s\n", __FUNCTION__); + syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__); pop3aggr *pMsg = IO->Data; eNextState rc; rc = POP3C_ReadHandlers[pMsg->State](pMsg); - pMsg->State++; + if (rc != eReadMore) + pMsg->State++; POP3SetTimeout(rc, pMsg); return rc; } eNextState POP3_C_DispatchWriteDone(AsyncIO *IO) { - CtdlLogPrintf(CTDL_DEBUG, "POP3: %s\n", __FUNCTION__); + syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__); pop3aggr *pMsg = IO->Data; eNextState rc; @@ -556,7 +666,7 @@ eNextState POP3_C_Terminate(AsyncIO *IO) { /// pop3aggr *pMsg = (pop3aggr *)IO->Data; - CtdlLogPrintf(CTDL_DEBUG, "POP3: %s\n", __FUNCTION__); + syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__); FinalizePOP3AggrRun(IO); return eAbort; } @@ -564,7 +674,7 @@ eNextState POP3_C_Timeout(AsyncIO *IO) { pop3aggr *pMsg = IO->Data; - CtdlLogPrintf(CTDL_DEBUG, "POP3: %s\n", __FUNCTION__); + syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__); StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State])); return FailAggregationRun(IO); } @@ -572,13 +682,21 @@ eNextState POP3_C_ConnFail(AsyncIO *IO) { pop3aggr *pMsg = (pop3aggr *)IO->Data; - CtdlLogPrintf(CTDL_DEBUG, "POP3: %s\n", __FUNCTION__); + syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__); + StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State])); + return FailAggregationRun(IO); +} +eNextState POP3_C_DNSFail(AsyncIO *IO) +{ + pop3aggr *pMsg = (pop3aggr *)IO->Data; + + syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__); StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State])); return FailAggregationRun(IO); } eNextState POP3_C_Shutdown(AsyncIO *IO) { - CtdlLogPrintf(CTDL_DEBUG, "POP3: %s\n", __FUNCTION__); + syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__); //// pop3aggr *pMsg = IO->Data; ////pMsg->MyQEntry->Status = 3; @@ -595,26 +713,26 @@ eReadState POP3_C_ReadServerStatus(AsyncIO *IO) { eReadState Finished = eBufferNotEmpty; - while (Finished == eBufferNotEmpty) { + switch (IO->NextState) { + case eSendDNSQuery: + case eReadDNSReply: + case eDBQuery: + case eConnect: + case eTerminateConnection: + case eAbort: + Finished = eReadFail; + break; + case eSendFile: + case eSendReply: + case eSendMore: + case eReadMore: + case eReadMessage: Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf); - - switch (Finished) { - case eMustReadMore: /// read new from socket... - return Finished; - break; - case eBufferNotEmpty: /* shouldn't happen... */ - case eReadSuccess: /// done for now... - if (StrLength(IO->IOBuf) < 4) - continue; - if (ChrPtr(IO->IOBuf)[3] == '-') - Finished = eBufferNotEmpty; - else - return Finished; - break; - case eReadFail: /// WHUT? - ///todo: shut down! - break; - } + break; + case eReadFile: + case eReadPayload: + Finished = CtdlReadMessageBodyAsync(IO); + break; } return Finished; } @@ -622,24 +740,39 @@ eReadState POP3_C_ReadServerStatus(AsyncIO *IO) /***************************************************************************** * So we connect our Server IP here. * *****************************************************************************/ -eNextState connect_ip(AsyncIO *IO) +eNextState POP3_C_ReAttachToFetchMessages(AsyncIO *IO) +{ + pop3aggr *cpptr = IO->Data; + + syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__); +////??? cpptr->State ++; + if (cpptr->Pos == NULL) + cpptr->Pos = GetNewHashPos(cpptr->MsgNumbers, 0); + + POP3_C_DispatchWriteDone(IO); + ReAttachIO(IO, cpptr, 0); + IO->NextState = eReadMessage; + return IO->NextState; +} + +eNextState pop3_connect_ip(AsyncIO *IO) { pop3aggr *cpptr = IO->Data; - CtdlLogPrintf(CTDL_DEBUG, "POP3: %s\n", __FUNCTION__); + syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__); //// IO->ConnectMe = &cpptr->Pop3Host; /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */ /////// SetConnectStatus(IO); - return InitEventIO(IO, cpptr, - POP3_C_ConnTimeout, - POP3_C_ReadTimeouts[0], - 1); + return EvConnectSock(IO, cpptr, + POP3_C_ConnTimeout, + POP3_C_ReadTimeouts[0], + 1); } -eNextState get_one_host_ip_done(AsyncIO *IO) +eNextState pop3_get_one_host_ip_done(AsyncIO *IO) { pop3aggr *cpptr = IO->Data; struct hostent *hostent; @@ -649,17 +782,17 @@ eNextState get_one_host_ip_done(AsyncIO *IO) hostent = cpptr->HostLookup.VParsedDNSReply; if ((cpptr->HostLookup.DNSStatus == ARES_SUCCESS) && (hostent != NULL) ) { - memset(&cpptr->Pop3Host.Addr, 0, sizeof(struct in6_addr)); - if (cpptr->Pop3Host.IPv6) { - memcpy(&cpptr->Pop3Host.Addr.sin6_addr.s6_addr, + memset(&cpptr->IO.ConnectMe->Addr, 0, sizeof(struct in6_addr)); + if (cpptr->IO.ConnectMe->IPv6) { + memcpy(&cpptr->IO.ConnectMe->Addr.sin6_addr.s6_addr, &hostent->h_addr_list[0], sizeof(struct in6_addr)); - cpptr->Pop3Host.Addr.sin6_family = hostent->h_addrtype; - cpptr->Pop3Host.Addr.sin6_port = htons(DefaultPOP3Port); + cpptr->IO.ConnectMe->Addr.sin6_family = hostent->h_addrtype; + cpptr->IO.ConnectMe->Addr.sin6_port = htons(DefaultPOP3Port); } else { - struct sockaddr_in *addr = (struct sockaddr_in*) &cpptr->Pop3Host.Addr; + struct sockaddr_in *addr = (struct sockaddr_in*) &cpptr->IO.ConnectMe->Addr; /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */ // addr->sin_addr.s_addr = htonl((uint32_t)&hostent->h_addr_list[0]); memcpy(&addr->sin_addr.s_addr, @@ -670,13 +803,13 @@ eNextState get_one_host_ip_done(AsyncIO *IO) addr->sin_port = htons(DefaultPOP3Port); } - return connect_ip(IO); + return pop3_connect_ip(IO); } else return eAbort; } -eNextState get_one_host_ip(AsyncIO *IO) +eNextState pop3_get_one_host_ip(AsyncIO *IO) { pop3aggr *cpptr = IO->Data; /* @@ -688,27 +821,20 @@ eNextState get_one_host_ip(AsyncIO *IO) InitC_ares_dns(IO); - CtdlLogPrintf(CTDL_DEBUG, "POP3: %s\n", __FUNCTION__); + syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__); - CtdlLogPrintf(CTDL_DEBUG, + syslog(LOG_DEBUG, "POP3 client[%ld]: looking up %s-Record %s : %d ...\n", cpptr->n, - (cpptr->Pop3Host.IPv6)? "aaaa": "a", - cpptr->Pop3Host.Host, - cpptr->Pop3Host.Port); - - if (!QueueQuery((cpptr->Pop3Host.IPv6)? ns_t_aaaa : ns_t_a, - cpptr->Pop3Host.Host, - &cpptr->IO, - &cpptr->HostLookup, - get_one_host_ip_done)) - { -// cpptr->MyQEntry->Status = 5; -// StrBufPrintf(SendMsg->MyQEntry->StatusMessage, -// "No MX hosts found for <%s>", SendMsg->node); - cpptr->IO.NextState = eTerminateConnection; - return IO->NextState; - } + (cpptr->IO.ConnectMe->IPv6)? "aaaa": "a", + cpptr->IO.ConnectMe->Host, + cpptr->IO.ConnectMe->Port); + + QueueQuery((cpptr->IO.ConnectMe->IPv6)? ns_t_aaaa : ns_t_a, + cpptr->IO.ConnectMe->Host, + &cpptr->IO, + &cpptr->HostLookup, + pop3_get_one_host_ip_done); IO->NextState = eReadDNSReply; return IO->NextState; } @@ -726,6 +852,7 @@ int pop3_do_fetching(pop3aggr *cpptr) cpptr->IO.Terminate = POP3_C_Terminate; cpptr->IO.LineReader = POP3_C_ReadServerStatus; cpptr->IO.ConnFail = POP3_C_ConnFail; + cpptr->IO.DNS.Fail = POP3_C_DNSFail; cpptr->IO.Timeout = POP3_C_Timeout; cpptr->IO.ShutdownAbort = POP3_C_Shutdown; @@ -735,21 +862,24 @@ int pop3_do_fetching(pop3aggr *cpptr) cpptr->IO.NextState = eReadMessage; /* TODO - CtdlLogPrintf(CTDL_DEBUG, "POP3: %s %s %s \n", roomname, pop3host, pop3user); - CtdlLogPrintf(CTDL_NOTICE, "Connecting to <%s>\n", pop3host); + syslog(LOG_DEBUG, "POP3: %s %s %s \n", roomname, pop3host, pop3user); + syslog(LOG_DEBUG, "Connecting to <%s>\n", pop3host); */ - SubC = CloneContext (CC); + SubC = CloneContext (&pop3_client_CC); SubC->session_specific_data = (char*) cpptr; cpptr->IO.CitContext = SubC; + safestrncpy(SubC->cs_host, + ChrPtr(cpptr->Url), + sizeof(SubC->cs_host)); if (cpptr->IO.ConnectMe->IsIP) { QueueEventContext(&cpptr->IO, - connect_ip); + pop3_connect_ip); } else { /* uneducated admin has chosen to add DNS to the equation... */ QueueEventContext(&cpptr->IO, - get_one_host_ip); + pop3_get_one_host_ip); } return 1; } @@ -771,51 +901,52 @@ void pop3client_scan_room(struct ctdlroom *qrbuf, void *data) const char *CfgPtr, *lPtr; const char *Err; - pop3_room_counter *Count = NULL; +// pop3_room_counter *Count = NULL; // pop3aggr *cpptr; - citthread_mutex_lock(&POP3QueueMutex); + pthread_mutex_lock(&POP3QueueMutex); if (GetHash(POP3QueueRooms, LKEY(qrbuf->QRnumber), &vptr)) { - CtdlLogPrintf(CTDL_DEBUG, + syslog(LOG_DEBUG, "pop3client: [%ld] %s already in progress.\n", qrbuf->QRnumber, qrbuf->QRname); - citthread_mutex_unlock(&POP3QueueMutex); - return; + pthread_mutex_unlock(&POP3QueueMutex); } - citthread_mutex_unlock(&POP3QueueMutex); + pthread_mutex_unlock(&POP3QueueMutex); + + if (server_shutting_down) return; assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir); - if (CtdlThreadCheckStop()) + if (server_shutting_down) return; /* Only do net processing for rooms that have netconfigs */ fd = open(filename, 0); if (fd <= 0) { - //CtdlLogPrintf(CTDL_DEBUG, "rssclient: %s no config.\n", qrbuf->QRname); + //syslog(LOG_DEBUG, "rssclient: %s no config.\n", qrbuf->QRname); return; } - if (CtdlThreadCheckStop()) + if (server_shutting_down) return; if (fstat(fd, &statbuf) == -1) { - CtdlLogPrintf(CTDL_DEBUG, "ERROR: could not stat configfile '%s' - %s\n", + syslog(LOG_DEBUG, "ERROR: could not stat configfile '%s' - %s\n", filename, strerror(errno)); return; } - if (CtdlThreadCheckStop()) + if (server_shutting_down) return; CfgData = NewStrBufPlain(NULL, statbuf.st_size + 1); if (StrBufReadBLOB(CfgData, &fd, 1, statbuf.st_size, &Err) < 0) { close(fd); FreeStrBuf(&CfgData); - CtdlLogPrintf(CTDL_DEBUG, "ERROR: reading config '%s' - %s
\n", + syslog(LOG_DEBUG, "ERROR: reading config '%s' - %s
\n", filename, strerror(errno)); return; } close(fd); - if (CtdlThreadCheckStop()) + if (server_shutting_down) return; CfgPtr = NULL; @@ -833,40 +964,43 @@ void pop3client_scan_room(struct ctdlroom *qrbuf, void *data) if (!strcasecmp("pop3client", ChrPtr(CfgType))) { pop3aggr *cptr; - + StrBuf *Tmp; +/* if (Count == NULL) { Count = malloc(sizeof(pop3_room_counter)); Count->count = 0; } Count->count ++; +*/ cptr = (pop3aggr *) malloc(sizeof(pop3aggr)); memset(cptr, 0, sizeof(pop3aggr)); /// TODO do we need this? cptr->roomlist_parts = 1; - cptr->rooms = NewStrBufPlain(qrbuf->QRname, -1); + cptr->RoomName = NewStrBufPlain(qrbuf->QRname, -1); cptr->pop3user = NewStrBufPlain(NULL, StrLength(Line)); cptr->pop3pass = NewStrBufPlain(NULL, StrLength(Line)); cptr->Url = NewStrBuf(); + Tmp = NewStrBuf(); - StrBufExtract_NextToken(cptr->Url, Line, &lPtr, '|'); + StrBufExtract_NextToken(Tmp, Line, &lPtr, '|'); StrBufExtract_NextToken(cptr->pop3user, Line, &lPtr, '|'); StrBufExtract_NextToken(cptr->pop3pass, Line, &lPtr, '|'); cptr->keep = StrBufExtractNext_long(Line, &lPtr, '|'); cptr->interval = StrBufExtractNext_long(Line, &lPtr, '|'); + StrBufPrintf(cptr->Url, "pop3://%s:%s@%s/%s", + ChrPtr(cptr->pop3user), + ChrPtr(cptr->pop3pass), + ChrPtr(Tmp), + ChrPtr(cptr->RoomName)); + FreeStrBuf(&Tmp); ParseURL(&cptr->IO.ConnectMe, cptr->Url, 110); - cptr->IO.ConnectMe->CurlCreds = cptr->pop3user; - cptr->IO.ConnectMe->User = ChrPtr(cptr->IO.ConnectMe->CurlCreds); - cptr->IO.ConnectMe->UrlWithoutCred = cptr->pop3pass; - cptr->IO.ConnectMe->Pass = ChrPtr(cptr->IO.ConnectMe->UrlWithoutCred); - - #if 0 /* todo: we need to reunite the url to be shure. */ - citthread_mutex_lock(&POP3ueueMutex); + pthread_mutex_lock(&POP3ueueMutex); GetHash(POP3FetchUrls, SKEY(ptr->Url), &vptr); use_this_cptr = (pop3aggr *)vptr; @@ -876,7 +1010,7 @@ void pop3client_scan_room(struct ctdlroom *qrbuf, void *data) if (use_this_cptr->RefCount > 0) { DeletePOP3Cfg(cptr); - Count->count--; +/// Count->count--; } else { @@ -893,15 +1027,15 @@ void pop3client_scan_room(struct ctdlroom *qrbuf, void *data) Put(use_this_cptr->OtherQRnumbers, LKEY(qrbuf->QRnumber), QRnumber, NULL); use_this_cptr->roomlist_parts++; } - citthread_mutex_unlock(&POP3QueueMutex); + pthread_mutex_unlock(&POP3QueueMutex); continue; } - citthread_mutex_unlock(&RSSQueueMutex); + pthread_mutex_unlock(&RSSQueueMutex); #endif - citthread_mutex_lock(&POP3QueueMutex); + pthread_mutex_lock(&POP3QueueMutex); Put(POP3FetchUrls, SKEY(cptr->Url), cptr, DeletePOP3Aggregator); - citthread_mutex_unlock(&POP3QueueMutex); + pthread_mutex_unlock(&POP3QueueMutex); } @@ -910,12 +1044,15 @@ void pop3client_scan_room(struct ctdlroom *qrbuf, void *data) ///fclose(fp); } + FreeStrBuf(&Line); + FreeStrBuf(&CfgType); + FreeStrBuf(&CfgData); } +static int doing_pop3client = 0; void pop3client_scan(void) { static time_t last_run = 0L; - static int doing_pop3client = 0; /// struct pop3aggr *pptr; time_t fastest_scan; HashPos *it; @@ -945,23 +1082,33 @@ void pop3client_scan(void) { if (doing_pop3client) return; doing_pop3client = 1; - CtdlLogPrintf(CTDL_DEBUG, "pop3client started\n"); + syslog(LOG_DEBUG, "pop3client started"); CtdlForEachRoom(pop3client_scan_room, NULL); - - citthread_mutex_lock(&POP3QueueMutex); + pthread_mutex_lock(&POP3QueueMutex); it = GetNewHashPos(POP3FetchUrls, 0); - while (GetNextHashPos(POP3FetchUrls, it, &len, &Key, &vrptr) && + while (!server_shutting_down && + GetNextHashPos(POP3FetchUrls, it, &len, &Key, &vrptr) && (vrptr != NULL)) { cptr = (pop3aggr *)vrptr; if (cptr->RefCount == 0) if (!pop3_do_fetching(cptr)) DeletePOP3Aggregator(cptr);////TODO + +/* + if ((palist->interval && time(NULL) > (last_run + palist->interval)) + || (time(NULL) > last_run + config.c_pop3_fetch)) + pop3_do_fetching(palist->roomname, palist->pop3host, + palist->pop3user, palist->pop3pass, palist->keep); + pptr = palist; + palist = palist->next; + free(pptr); +*/ } DeleteHashPos(&it); - citthread_mutex_unlock(&POP3QueueMutex); + pthread_mutex_unlock(&POP3QueueMutex); - CtdlLogPrintf(CTDL_DEBUG, "pop3client ended\n"); + syslog(LOG_DEBUG, "pop3client ended"); last_run = time(NULL); doing_pop3client = 0; } @@ -969,7 +1116,8 @@ void pop3client_scan(void) { void pop3_cleanup(void) { - citthread_mutex_destroy(&POP3QueueMutex); + /* citthread_mutex_destroy(&POP3QueueMutex); TODO */ + while (doing_pop3client != 0) ; DeleteHash(&POP3FetchUrls); DeleteHash(&POP3QueueRooms); } @@ -978,12 +1126,14 @@ CTDL_MODULE_INIT(pop3client) { if (!threading) { - citthread_mutex_init(&POP3QueueMutex, NULL); + CtdlFillSystemContext(&pop3_client_CC, "POP3aggr"); + pthread_mutex_init(&POP3QueueMutex, NULL); POP3QueueRooms = NewHash(1, lFlathash); POP3FetchUrls = NewHash(1, NULL); CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER); CtdlRegisterCleanupHook(pop3_cleanup); } - /* return our Subversion id for the Log */ + + /* return our module id for the log */ return "pop3client"; }