X-Git-Url: https://code.citadel.org/?a=blobdiff_plain;f=citadel%2Fmodules%2Fpop3client%2Fserv_pop3client.c;h=8a12d15037402f33a7d000665b0b9a577e0053aa;hb=15fc2bf9cd4d2a34fd91aa16c4f632ee46e72dd8;hp=433309f38895ed0d9e6877f510ad40848e985d3c;hpb=3e3d387a634d0d82ed22a8db85f4cc036b60df17;p=citadel.git diff --git a/citadel/modules/pop3client/serv_pop3client.c b/citadel/modules/pop3client/serv_pop3client.c index 433309f38..8a12d1503 100644 --- a/citadel/modules/pop3client/serv_pop3client.c +++ b/citadel/modules/pop3client/serv_pop3client.c @@ -158,6 +158,44 @@ void HfreeFetchItem(void *vItem) free(Item); } + + +typedef enum _POP3State { + eCreated, + eGreeting, + eUser, + ePassword, + eListing, + eUseTable, + eGetMsgID, + eGetMsg, + eStoreMsg, + eDelete, + eQuit +} POP3State; + +ConstStr POP3States[] = { + {HKEY("Aggregator created")}, + {HKEY("Reading Greeting")}, + {HKEY("Sending User")}, + {HKEY("Sending Password")}, + {HKEY("Listing")}, + {HKEY("Fetching Usetable")}, + {HKEY("Get MSG ID")}, + {HKEY("Get Message")}, + {HKEY("Store Msg")}, + {HKEY("Delete Upstream")}, + {HKEY("Quit")} +}; + +static void SetPOP3State(AsyncIO *IO, POP3State State) +{ + CitContext* CCC = IO->CitContext; + if (CCC != NULL) + memcpy(CCC->cs_clientname, POP3States[State].Key, POP3States[State].len + 1); +} + + struct pop3aggr { AsyncIO IO; @@ -199,8 +237,10 @@ void DeletePOP3Aggregator(void *vptr) 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; + if (((struct CitContext*)ptr->IO.CitContext)) { + ((struct CitContext*)ptr->IO.CitContext)->state = CON_IDLE; + ((struct CitContext*)ptr->IO.CitContext)->kill_me = 1; + } FreeAsyncIOContents(&ptr->IO); free(ptr); } @@ -238,6 +278,7 @@ eNextState FailAggregationRun(AsyncIO *IO) eNextState POP3C_ReadGreeting(pop3aggr *RecvMsg) { AsyncIO *IO = &RecvMsg->IO; + SetPOP3State(IO, eGreeting); POP3C_DBG_READ(); /* Read the server greeting */ if (!POP3C_OK) return eTerminateConnection; @@ -247,6 +288,7 @@ eNextState POP3C_ReadGreeting(pop3aggr *RecvMsg) eNextState POP3C_SendUser(pop3aggr *RecvMsg) { AsyncIO *IO = &RecvMsg->IO; + SetPOP3State(IO, eUser); /* Identify ourselves. NOTE: we have to append a CR to each command. * The LF will automatically be appended by sock_puts(). Believe it * or not, leaving out the CR will cause problems if the server happens @@ -270,6 +312,7 @@ eNextState POP3C_GetUserState(pop3aggr *RecvMsg) eNextState POP3C_SendPassword(pop3aggr *RecvMsg) { AsyncIO *IO = &RecvMsg->IO; + SetPOP3State(IO, ePassword); /* Password */ StrBufPrintf(RecvMsg->IO.SendBuf.Buf, "PASS %s\r\n", ChrPtr(RecvMsg->pop3pass)); @@ -289,6 +332,8 @@ eNextState POP3C_GetPassState(pop3aggr *RecvMsg) eNextState POP3C_SendListCommand(pop3aggr *RecvMsg) { AsyncIO *IO = &RecvMsg->IO; + SetPOP3State(IO, eListing); + /* Get the list of messages */ StrBufPlain(RecvMsg->IO.SendBuf.Buf, HKEY("LIST\r\n")); POP3C_DBG_SEND(); @@ -330,6 +375,17 @@ eNextState POP3C_GetListOneLine(pop3aggr *RecvMsg) return eSendReply; } + + /* + * work around buggy pop3 servers which send + * empty lines in their listings. + */ + if ((StrLength(RecvMsg->IO.IOBuf) == 0) || + !isdigit(ChrPtr(RecvMsg->IO.IOBuf)[0])) + { + return eReadMore; + } + OneMsg = (FetchItem*) malloc(sizeof(FetchItem)); memset(OneMsg, 0, sizeof(FetchItem)); OneMsg->MSGID = atol(ChrPtr(RecvMsg->IO.IOBuf)); @@ -360,9 +416,10 @@ eNextState POP3_FetchNetworkUsetableEntry(AsyncIO *IO) long HKLen; const char *HKey; void *vData; - struct cdbdata *cdbut; pop3aggr *RecvMsg = (pop3aggr *) IO->Data; + SetPOP3State(IO, eUseTable); + if((RecvMsg->Pos != NULL) && GetNextHashPos(RecvMsg->MsgNumbers, RecvMsg->Pos, @@ -370,32 +427,19 @@ eNextState POP3_FetchNetworkUsetableEntry(AsyncIO *IO) &HKey, &vData)) { - struct UseTable ut; if (server_shutting_down) return eAbort; - RecvMsg->CurrMsg = (FetchItem*) vData; - EVP3CCS_syslog(LOG_DEBUG, - "CHECKING: whether %s has already been seen: ", - ChrPtr(RecvMsg->CurrMsg->MsgUID)); - - /* Find out if we've already seen this item */ - 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(RecvMsg->CurrMsg->MsgUID)); - if (cdbut != NULL) { + if (CheckIfAlreadySeen("POP3 Item Seen", + RecvMsg->CurrMsg->MsgUID, + IO->Now, + IO->Now, //// todo + eCheckUpdate, + IO->ID, CCID) + != 0) + { /* Item has already been seen */ - EVP3CCSM_syslog(LOG_DEBUG, "YES\n"); - cdb_free(cdbut); - - /* rewrite the record anyway, to update the timestamp */ - cdb_store(CDB_USETABLE, - SKEY(RecvMsg->CurrMsg->MsgUID), - &ut, sizeof(struct UseTable) ); - RecvMsg->CurrMsg->NeedFetch = 0; ////TODO0; + RecvMsg->CurrMsg->NeedFetch = 0; } else { @@ -410,8 +454,7 @@ eNextState POP3_FetchNetworkUsetableEntry(AsyncIO *IO) /* ok, now we know them all, * continue with reading the actual messages. */ DeleteHashPos(&RecvMsg->Pos); - StopDBWatchers(IO); - return QueueEventContext(IO, POP3_C_ReAttachToFetchMessages); + return DBQueueEventContext(IO, POP3_C_ReAttachToFetchMessages); } } @@ -422,6 +465,7 @@ eNextState POP3C_GetOneMessagID(pop3aggr *RecvMsg) const char *HKey; void *vData; + SetPOP3State(IO, eGetMsgID); #if 0 int rc; rc = TestValidateHash(RecvMsg->MsgNumbers); @@ -447,8 +491,8 @@ eNextState POP3C_GetOneMessagID(pop3aggr *RecvMsg) DeleteHashPos(&RecvMsg->Pos); /// done receiving uidls.. start looking them up now. RecvMsg->Pos = GetNewHashPos(RecvMsg->MsgNumbers, 0); - return QueueDBOperation(&RecvMsg->IO, - POP3_FetchNetworkUsetableEntry); + return EventQueueDBOperation(&RecvMsg->IO, + POP3_FetchNetworkUsetableEntry); } return eReadMore; /* TODO */ } @@ -491,6 +535,8 @@ eNextState POP3C_SendGetOneMsg(pop3aggr *RecvMsg) const char *HKey; void *vData; + SetPOP3State(IO, eGetMsg); + RecvMsg->CurrMsg = NULL; while ((RecvMsg->Pos != NULL) && GetNextHashPos(RecvMsg->MsgNumbers, @@ -535,23 +581,20 @@ eNextState POP3C_ReadMessageBodyFollowing(pop3aggr *RecvMsg) eNextState POP3C_StoreMsgRead(AsyncIO *IO) { pop3aggr *RecvMsg = (pop3aggr *) IO->Data; - struct UseTable ut; + + SetPOP3State(IO, eStoreMsg); EVP3CCS_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) ); - StopDBWatchers(IO); - return QueueEventContext(&RecvMsg->IO, POP3_C_ReAttachToFetchMessages); + CheckIfAlreadySeen("POP3 Item Seen", + RecvMsg->CurrMsg->MsgUID, + IO->Now, + IO->Now, //// todo + eWrite, + IO->ID, CCID); + + return DBQueueEventContext(&RecvMsg->IO, POP3_C_ReAttachToFetchMessages); } eNextState POP3C_SaveMsg(AsyncIO *IO) { @@ -582,13 +625,15 @@ eNextState POP3C_ReadMessageBody(pop3aggr *RecvMsg) EVP3CM_syslog(LOG_DEBUG, "Converting message..."); RecvMsg->CurrMsg->Msg = convert_internet_message_buf(&RecvMsg->IO.ReadMsg->MsgBuf); - StopClientWatchers(IO, 0); - return QueueDBOperation(&RecvMsg->IO, POP3C_SaveMsg); + return EventQueueDBOperation(&RecvMsg->IO, POP3C_SaveMsg); } eNextState POP3C_SendDelete(pop3aggr *RecvMsg) { AsyncIO *IO = &RecvMsg->IO; + + SetPOP3State(IO, eDelete); + if (!RecvMsg->keep) { StrBufPrintf(RecvMsg->IO.SendBuf.Buf, "DELE %ld\r\n", RecvMsg->CurrMsg->MSGID); @@ -611,6 +656,8 @@ eNextState POP3C_ReadDeleteState(pop3aggr *RecvMsg) eNextState POP3C_SendQuit(pop3aggr *RecvMsg) { AsyncIO *IO = &RecvMsg->IO; + SetPOP3State(IO, eQuit); + /* Log out */ StrBufPlain(RecvMsg->IO.SendBuf.Buf, HKEY("QUIT\r\n3)")); @@ -983,22 +1030,10 @@ int pop3_do_fetching(pop3aggr *cpptr) /* * Scan a room's netconfig to determine whether it requires POP3 aggregation */ -void pop3client_scan_room(struct ctdlroom *qrbuf, void *data) +void pop3client_scan_room(struct ctdlroom *qrbuf, void *data, OneRoomNetCfg *OneRNCFG) { - StrBuf *CfgData; - StrBuf *CfgType; - StrBuf *Line; - - struct stat statbuf; - char filename[PATH_MAX]; - int fd; - int Done; + const RoomNetCfgLine *pLine; void *vptr; - const char *CfgPtr, *lPtr; - const char *Err; - -// pop3_room_counter *Count = NULL; -// pop3aggr *cpptr; pthread_mutex_lock(&POP3QueueMutex); if (GetHash(POP3QueueRooms, LKEY(qrbuf->QRnumber), &vptr)) @@ -1014,167 +1049,88 @@ void pop3client_scan_room(struct ctdlroom *qrbuf, void *data) if (server_shutting_down) return; - assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir); + pLine = OneRNCFG->NetConfigs[pop3client]; - if (server_shutting_down) - return; + while (pLine != NULL) + { + pop3aggr *cptr; - /* Only do net processing for rooms that have netconfigs */ - fd = open(filename, 0); - if (fd <= 0) { - return; - } - if (server_shutting_down) - return; - if (fstat(fd, &statbuf) == -1) { - EVP3CQ_syslog(LOG_INFO, - "ERROR: could not stat configfile '%s' - %s", - filename, - strerror(errno)); - return; - } - 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); - EVP3CQ_syslog(LOG_INFO, - "ERROR: reading config '%s' - %s", - filename, strerror(errno)); - return; - } - close(fd); - if (server_shutting_down) - return; + cptr = (pop3aggr *) malloc(sizeof(pop3aggr)); + memset(cptr, 0, sizeof(pop3aggr)); + ///TODO do we need this? cptr->roomlist_parts=1; + cptr->RoomName = NewStrBufPlain(qrbuf->QRname, -1); + cptr->pop3user = NewStrBufDup(pLine->Value[1]); + cptr->pop3pass = NewStrBufDup(pLine->Value[2]); + cptr->Url = NewStrBuf(); + cptr->Host = NewStrBufDup(pLine->Value[0]); - CfgPtr = NULL; - CfgType = NewStrBuf(); - Line = NewStrBufPlain(NULL, StrLength(CfgData)); - Done = 0; + cptr->keep = atol(ChrPtr(pLine->Value[3])); + cptr->interval = atol(ChrPtr(pLine->Value[4])); - while (!Done) - { - Done = StrBufSipLine(Line, CfgData, &CfgPtr) == 0; - if (StrLength(Line) > 0) - { - lPtr = NULL; - StrBufExtract_NextToken(CfgType, Line, &lPtr, '|'); - if (!strcasecmp("pop3client", ChrPtr(CfgType))) - { - pop3aggr *cptr; -/* - 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->RoomName = - NewStrBufPlain(qrbuf->QRname, -1); - cptr->pop3user = - NewStrBufPlain(NULL, StrLength(Line)); - cptr->pop3pass = - NewStrBufPlain(NULL, StrLength(Line)); - cptr->Url = NewStrBuf(); - cptr->Host = - NewStrBufPlain(NULL, StrLength(Line)); - - StrBufExtract_NextToken(cptr->Host, 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, - '|'); - - StrBufAppendBufPlain(cptr->Url, HKEY("pop3://"), 0); - StrBufUrlescUPAppend(cptr->Url, cptr->pop3user, NULL); - StrBufAppendBufPlain(cptr->Url, HKEY(":"), 0); - StrBufUrlescUPAppend(cptr->Url, cptr->pop3pass, NULL); - StrBufAppendBufPlain(cptr->Url, HKEY("@"), 0); - StrBufAppendBuf(cptr->Url, cptr->Host, 0); - StrBufAppendBufPlain(cptr->Url, HKEY("/"), 0); - StrBufUrlescAppend(cptr->Url, cptr->RoomName, NULL); - - ParseURL(&cptr->IO.ConnectMe, cptr->Url, 110); + StrBufAppendBufPlain(cptr->Url, HKEY("pop3://"), 0); + StrBufUrlescUPAppend(cptr->Url, cptr->pop3user, NULL); + StrBufAppendBufPlain(cptr->Url, HKEY(":"), 0); + StrBufUrlescUPAppend(cptr->Url, cptr->pop3pass, NULL); + StrBufAppendBufPlain(cptr->Url, HKEY("@"), 0); + StrBufAppendBuf(cptr->Url, cptr->Host, 0); + StrBufAppendBufPlain(cptr->Url, HKEY("/"), 0); + StrBufUrlescAppend(cptr->Url, cptr->RoomName, NULL); + + ParseURL(&cptr->IO.ConnectMe, cptr->Url, 110); #if 0 /* todo: we need to reunite the url to be shure. */ - pthread_mutex_lock(&POP3ueueMutex); - GetHash(POP3FetchUrls, SKEY(ptr->Url), &vptr); - use_this_cptr = (pop3aggr *)vptr; + pthread_mutex_lock(&POP3ueueMutex); + GetHash(POP3FetchUrls, SKEY(ptr->Url), &vptr); + use_this_cptr = (pop3aggr *)vptr; - if (use_this_rncptr != NULL) - { - /* mustn't attach to an active session */ - if (use_this_cptr->RefCount > 0) - { - DeletePOP3Cfg(cptr); + if (use_this_rncptr != NULL) + { + /* mustn't attach to an active session */ + if (use_this_cptr->RefCount > 0) + { + DeletePOP3Cfg(cptr); /// Count->count--; - } - else - { - long *QRnumber; - StrBufAppendBufPlain( - use_this_cptr->rooms, - qrbuf->QRname, - -1, 0); - if (use_this_cptr->roomlist_parts == 1) - { - use_this_cptr->OtherQRnumbers - = NewHash(1, lFlathash); - } - QRnumber = (long*)malloc(sizeof(long)); - *QRnumber = qrbuf->QRnumber; - Put(use_this_cptr->OtherQRnumbers, - LKEY(qrbuf->QRnumber), - QRnumber, - NULL); - - use_this_cptr->roomlist_parts++; - } - pthread_mutex_unlock(&POP3QueueMutex); - continue; + } + else + { + long *QRnumber; + StrBufAppendBufPlain( + use_this_cptr->rooms, + qrbuf->QRname, + -1, 0); + if (use_this_cptr->roomlist_parts == 1) + { + use_this_cptr->OtherQRnumbers + = NewHash(1, lFlathash); } - pthread_mutex_unlock(&RSSQueueMutex); -#endif - cptr->n = Pop3ClientID++; - pthread_mutex_lock(&POP3QueueMutex); - Put(POP3FetchUrls, - SKEY(cptr->Url), - cptr, - DeletePOP3Aggregator); - - pthread_mutex_unlock(&POP3QueueMutex); - + QRnumber = (long*)malloc(sizeof(long)); + *QRnumber = qrbuf->QRnumber; + Put(use_this_cptr->OtherQRnumbers, + LKEY(qrbuf->QRnumber), + QRnumber, + NULL); + + use_this_cptr->roomlist_parts++; } - + pthread_mutex_unlock(&POP3QueueMutex); + continue; } + pthread_mutex_unlock(&RSSQueueMutex); +#endif + cptr->n = Pop3ClientID++; + pthread_mutex_lock(&POP3QueueMutex); + Put(POP3FetchUrls, + SKEY(cptr->Url), + cptr, + DeletePOP3Aggregator); - ///fclose(fp); + pthread_mutex_unlock(&POP3QueueMutex); + pLine = pLine->next; } - FreeStrBuf(&Line); - FreeStrBuf(&CfgType); - FreeStrBuf(&CfgData); } static int doing_pop3client = 0; @@ -1212,7 +1168,7 @@ void pop3client_scan(void) { doing_pop3client = 1; EVP3CQM_syslog(LOG_DEBUG, "pop3client started"); - CtdlForEachRoom(pop3client_scan_room, NULL); + CtdlForEachNetCfgRoom(pop3client_scan_room, NULL, pop3client); pthread_mutex_lock(&POP3QueueMutex); it = GetNewHashPos(POP3FetchUrls, 0); @@ -1263,6 +1219,7 @@ CTDL_MODULE_INIT(pop3client) if (!threading) { CtdlFillSystemContext(&pop3_client_CC, "POP3aggr"); + CtdlREGISTERRoomCfgType(pop3client, ParseGeneric, 0, 5, SerializeGeneric, DeleteGenericCfgLine); pthread_mutex_init(&POP3QueueMutex, NULL); POP3QueueRooms = NewHash(1, lFlathash); POP3FetchUrls = NewHash(1, NULL);