rename InitEventIO to EvConnectSock, since this suits better what this function does...
[citadel.git] / citadel / modules / pop3client / serv_pop3client.c
index d1e03c4b2875f5b66248261972e218d280191ce6..8a0d23c38f6c64537d9d2f44573082169509a43a 100644 (file)
@@ -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);
@@ -322,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
                {
@@ -592,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];
@@ -602,6 +607,7 @@ void POP3SetTimeout(eNextState NextTCPState, pop3aggr *pMsg)
   }
 */
                break;
+       case eReadFile:
        case eReadMessage:
                Timeout = POP3_C_ReadTimeouts[pMsg->State];
 /*
@@ -680,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__);
@@ -708,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;
@@ -739,7 +755,7 @@ eNextState POP3_C_ReAttachToFetchMessages(AsyncIO *IO)
        return IO->NextState;
 }
 
-eNextState connect_ip(AsyncIO *IO)
+eNextState pop3_connect_ip(AsyncIO *IO)
 {
        pop3aggr *cpptr = IO->Data;
 
@@ -750,13 +766,13 @@ eNextState connect_ip(AsyncIO *IO)
 
        /////// 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;
@@ -787,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;
        /* 
@@ -818,7 +834,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;
 }
@@ -836,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;
        
@@ -852,14 +869,17 @@ int pop3_do_fetching(pop3aggr *cpptr)
        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;
 }
@@ -1029,10 +1049,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;
@@ -1097,7 +1117,8 @@ void pop3client_scan(void) {
 void pop3_cleanup(void)
 {
        /* citthread_mutex_destroy(&POP3QueueMutex); TODO */
-//     DeleteHash(&POP3FetchUrls);
+       while (doing_pop3client != 0) ;
+       DeleteHash(&POP3FetchUrls);
        DeleteHash(&POP3QueueRooms);
 }