Refactoring: create central place to init AsyncIO
[citadel.git] / citadel / modules / pop3client / serv_pop3client.c
index 83ddf64cce38b7a33f2791951712a794fb27fcd9..32426e0c3c3d148e3bf542b2bf8bd4584b9adffb 100644 (file)
@@ -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 <stdlib.h>
@@ -136,6 +136,9 @@ 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;
+       FreeAsyncIOContents(&ptr->IO);
        free(ptr);
 }
 
@@ -145,6 +148,7 @@ 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);
@@ -161,8 +165,8 @@ eNextState FinalizePOP3AggrRun(AsyncIO *IO)
        It = GetNewHashPos(POP3FetchUrls, 0);
        pthread_mutex_lock(&POP3QueueMutex);
        {
-               GetHashPosFromKey(POP3FetchUrls, SKEY(cptr->Url), It);
-               DeleteEntryFromHash(POP3FetchUrls, It);
+               if (GetHashPosFromKey(POP3FetchUrls, SKEY(cptr->Url), It))
+                       DeleteEntryFromHash(POP3FetchUrls, It);
        }
        pthread_mutex_unlock(&POP3QueueMutex);
        DeleteHashPos(&It);
@@ -174,6 +178,7 @@ eNextState FailAggregationRun(AsyncIO *IO)
        return eAbort;
 }
 
+
 #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)
@@ -186,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
@@ -301,7 +305,9 @@ eNextState POP3_FetchNetworkUsetableEntry(AsyncIO *IO)
        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 */
@@ -320,7 +326,7 @@ eNextState POP3_FetchNetworkUsetableEntry(AsyncIO *IO)
                        cdb_store(CDB_USETABLE, 
                                  SKEY(RecvMsg->CurrMsg->MsgUID), 
                                  &ut, sizeof(struct UseTable) );
-                       RecvMsg->CurrMsg->NeedFetch = 1; ////TODO0;
+                       RecvMsg->CurrMsg->NeedFetch = 0; ////TODO0;
                }
                else
                {
@@ -590,6 +596,7 @@ void POP3SetTimeout(eNextState NextTCPState, pop3aggr *pMsg)
        syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
 
        switch (NextTCPState) {
+       case eSendFile:
        case eSendReply:
        case eSendMore:
                Timeout = POP3_C_SendTimeouts[pMsg->State];
@@ -600,6 +607,7 @@ void POP3SetTimeout(eNextState NextTCPState, pop3aggr *pMsg)
   }
 */
                break;
+       case eReadFile:
        case eReadMessage:
                Timeout = POP3_C_ReadTimeouts[pMsg->State];
 /*
@@ -678,6 +686,14 @@ eNextState POP3_C_ConnFail(AsyncIO *IO)
        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)
 {
        syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
@@ -706,12 +722,14 @@ eReadState POP3_C_ReadServerStatus(AsyncIO *IO)
        case eAbort:
                Finished = eReadFail;
                break;
+       case eSendFile:
        case eSendReply: 
        case eSendMore:
        case eReadMore:
        case eReadMessage: 
                Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
                break;
+       case eReadFile:
        case eReadPayload:
                Finished = CtdlReadMessageBodyAsync(IO);
                break;
@@ -737,24 +755,17 @@ eNextState POP3_C_ReAttachToFetchMessages(AsyncIO *IO)
        return IO->NextState;
 }
 
-eNextState connect_ip(AsyncIO *IO)
+eNextState pop3_connect_ip(AsyncIO *IO)
 {
-       pop3aggr *cpptr = IO->Data;
-
        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,
+                            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;
@@ -785,13 +796,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;
        /* 
@@ -816,7 +827,7 @@ eNextState get_one_host_ip(AsyncIO *IO)
                   cpptr->IO.ConnectMe->Host, 
                   &cpptr->IO, 
                   &cpptr->HostLookup, 
-                  get_one_host_ip_done);
+                  pop3_get_one_host_ip_done);
        IO->NextState = eReadDNSReply;
        return IO->NextState;
 }
@@ -825,39 +836,29 @@ eNextState get_one_host_ip(AsyncIO *IO)
 
 int pop3_do_fetching(pop3aggr *cpptr)
 {
-       CitContext *SubC;
-
-       cpptr->IO.Data          = cpptr;
-
-       cpptr->IO.SendDone      = POP3_C_DispatchWriteDone;
-       cpptr->IO.ReadDone      = POP3_C_DispatchReadDone;
-       cpptr->IO.Terminate     = POP3_C_Terminate;
-       cpptr->IO.LineReader    = POP3_C_ReadServerStatus;
-       cpptr->IO.ConnFail      = POP3_C_ConnFail;
-       cpptr->IO.Timeout       = POP3_C_Timeout;
-       cpptr->IO.ShutdownAbort = POP3_C_Shutdown;
-       
-       cpptr->IO.SendBuf.Buf   = NewStrBufPlain(NULL, 1024);
-       cpptr->IO.RecvBuf.Buf   = NewStrBufPlain(NULL, 1024);
-       cpptr->IO.IOBuf         = NewStrBuf();
-       
-       cpptr->IO.NextState     = eReadMessage;
-/* TODO
-   syslog(LOG_DEBUG, "POP3: %s %s %s <password>\n", roomname, pop3host, pop3user);
-   syslog(LOG_DEBUG, "Connecting to <%s>\n", pop3host);
-*/
-       
-       SubC = CloneContext (&pop3_client_CC);
-       SubC->session_specific_data = (char*) cpptr;
-       cpptr->IO.CitContext = SubC;
+       InitIOStruct(&cpptr->IO,
+                    cpptr,
+                    eReadMessage,
+                    POP3_C_ReadServerStatus,
+                    POP3_C_DNSFail,
+                    POP3_C_DispatchWriteDone,
+                    POP3_C_DispatchReadDone,
+                    POP3_C_Terminate,
+                    POP3_C_ConnFail,
+                    POP3_C_Timeout,
+                    POP3_C_Shutdown);
+
+       safestrncpy(((CitContext *)cpptr->IO.CitContext)->cs_host,
+                   ChrPtr(cpptr->Url),
+                   sizeof(((CitContext *)cpptr->IO.CitContext)->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... */
+       else {
                QueueEventContext(&cpptr->IO,
-                                 get_one_host_ip);
+                                 pop3_get_one_host_ip);
        }
        return 1;
 }
@@ -890,13 +891,14 @@ void pop3client_scan_room(struct ctdlroom *qrbuf, void *data)
                              qrbuf->QRnumber, 
                              qrbuf->QRname);
                pthread_mutex_unlock(&POP3QueueMutex);
-               return;
        }
        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 */
@@ -905,14 +907,14 @@ void pop3client_scan_room(struct ctdlroom *qrbuf, void *data)
                //syslog(LOG_DEBUG, "rssclient: %s no config.\n", qrbuf->QRname);
                return;
        }
-       if (CtdlThreadCheckStop())
+       if (server_shutting_down)
                return;
        if (fstat(fd, &statbuf) == -1) {
                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) {
@@ -923,7 +925,7 @@ void pop3client_scan_room(struct ctdlroom *qrbuf, void *data)
                return;
        }
        close(fd);
-       if (CtdlThreadCheckStop())
+       if (server_shutting_down)
                return;
        
        CfgPtr = NULL;
@@ -1026,11 +1028,10 @@ void pop3client_scan_room(struct ctdlroom *qrbuf, void *data)
        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;
        long len;
@@ -1038,6 +1039,8 @@ void pop3client_scan(void) {
        void *vrptr;
        pop3aggr *cptr;
 
+       become_session(&pop3_client_CC);
+
        if (config.c_pop3_fastest < config.c_pop3_fetch)
                fastest_scan = config.c_pop3_fastest;
        else
@@ -1059,23 +1062,33 @@ void pop3client_scan(void) {
        if (doing_pop3client) return;
        doing_pop3client = 1;
 
-       syslog(LOG_DEBUG, "pop3client started\n");
+       syslog(LOG_DEBUG, "pop3client started");
        CtdlForEachRoom(pop3client_scan_room, NULL);
 
-
        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);
        pthread_mutex_unlock(&POP3QueueMutex);
 
-       syslog(LOG_DEBUG, "pop3client ended\n");
+       syslog(LOG_DEBUG, "pop3client ended");
        last_run = time(NULL);
        doing_pop3client = 0;
 }
@@ -1084,6 +1097,7 @@ void pop3client_scan(void) {
 void pop3_cleanup(void)
 {
        /* citthread_mutex_destroy(&POP3QueueMutex); TODO */
+       while (doing_pop3client != 0) ;
        DeleteHash(&POP3FetchUrls);
        DeleteHash(&POP3QueueRooms);
 }
@@ -1099,6 +1113,7 @@ CTDL_MODULE_INIT(pop3client)
                CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER);
                 CtdlRegisterCleanupHook(pop3_cleanup);
        }
-       /* return our Subversion id for the Log */
+
+       /* return our module id for the log */
         return "pop3client";
 }