]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/smtp/serv_smtpeventclient.c
libev migration
[citadel.git] / citadel / modules / smtp / serv_smtpeventclient.c
index 0395addabb90c6b180850dd541fd518601105070..5a4a51439eb3720da4df94f5a5dac0db6b8ca593 100644 (file)
@@ -191,7 +191,7 @@ typedef struct _stmp_out_msg {
 
        eSMTP_C_States State;
 
-       int SMTPstatus;
+///    int SMTPstatus; ->MyQEntry->Status
 
        int i_mx;
        int n_mx;
@@ -220,9 +220,12 @@ void DeleteSmtpOutMsg(void *v)
        free(Msg);
 }
 
+eNextState SMTP_C_Timeout(void *Data);
+eNextState SMTP_C_ConnFail(void *Data);
 eNextState SMTP_C_DispatchReadDone(void *Data);
 eNextState SMTP_C_DispatchWriteDone(void *Data);
-
+eNextState SMTP_C_Terminate(void *Data);
+eNextState SMTP_C_MXLookup(void *Data);
 
 typedef eNextState (*SMTPReadHandler)(SmtpOutMsg *Msg);
 typedef eNextState (*SMTPSendHandler)(SmtpOutMsg *Msg);
@@ -315,6 +318,7 @@ OneQueItem *DeserializeQueueItem(StrBuf *RawQItem, long QueMsgID)
                void *vHandler;
 
                StrBufExtract_NextToken(Line, RawQItem, &pLine, '\n');
+               if (StrLength(Line) == 0) continue;
                StrBufExtract_NextToken(Token, Line, &pItemPart, '|');
                if (GetHash(QItemHandlers, SKEY(Token), &vHandler))
                {
@@ -432,6 +436,7 @@ void FinalizeMessageSend(SmtpOutMsg *Msg)
        }
        
 /// TODO : else free message...
+       close(Msg->IO.sock);
        DeleteSmtpOutMsg(Msg);
 }
 
@@ -568,16 +573,16 @@ void resolve_mx_hosts(SmtpOutMsg *SendMsg)
        CtdlLogPrintf(CTDL_DEBUG, "SMTP client[%ld]: Number of MX hosts for <%s> is %d [%s]\n", 
                      SendMsg->n, SendMsg->node, SendMsg->num_mxhosts, SendMsg->mxhosts);
        if (SendMsg->num_mxhosts < 1) {
-               SendMsg->SMTPstatus = 5;
+               SendMsg->MyQEntry->Status = 5;
                StrBufPrintf(SendMsg->MyQEntry->StatusMessage, 
                             "No MX hosts found for <%s>", SendMsg->node);
                return; ///////TODO: abort!
        }
 
 }
-/* TODO: abort... */
-#define SMTP_ERROR(WHICH_ERR, ERRSTR) {SendMsg->SMTPstatus = WHICH_ERR; StrBufAppendBufPlain(SendMsg->MyQEntry->StatusMessage, HKEY(ERRSTR), 0); return eAbort; }
-#define SMTP_VERROR(WHICH_ERR) { SendMsg->SMTPstatus = WHICH_ERR; StrBufAppendBufPlain(SendMsg->MyQEntry->StatusMessage, &ChrPtr(SendMsg->IO.IOBuf)[4], -1, 0); return eAbort; }
+
+#define SMTP_ERROR(WHICH_ERR, ERRSTR) {SendMsg->MyQEntry->Status = WHICH_ERR; StrBufAppendBufPlain(SendMsg->MyQEntry->StatusMessage, HKEY(ERRSTR), 0); return eAbort; }
+#define SMTP_VERROR(WHICH_ERR) { SendMsg->MyQEntry->Status = WHICH_ERR; StrBufAppendBufPlain(SendMsg->MyQEntry->StatusMessage, &ChrPtr(SendMsg->IO.IOBuf)[4], -1, 0); return eAbort; }
 #define SMTP_IS_STATE(WHICH_STATE) (ChrPtr(SendMsg->IO.IOBuf)[0] == WHICH_STATE)
 
 #define SMTP_DBG_SEND() CtdlLogPrintf(CTDL_DEBUG, "SMTP client[%ld]: > %s\n", SendMsg->n, ChrPtr(SendMsg->IO.IOBuf))
@@ -621,27 +626,24 @@ void connect_one_smtpsrv(SmtpOutMsg *SendMsg)
 int connect_one_smtpsrv_xamine_result(void *Ctx)
 {
        SmtpOutMsg *SendMsg = Ctx;
+
+       CtdlLogPrintf(CTDL_DEBUG, "SMTP client[%ld]: connecting [%s:%s]!\n", 
+                     SendMsg->n, SendMsg->mx_host, SendMsg->mx_port);
+
+       SendMsg->IO.SendBuf.Buf = NewStrBufPlain(NULL, 1024);
+       SendMsg->IO.RecvBuf.Buf = NewStrBufPlain(NULL, 1024);
+       SendMsg->IO.IOBuf = NewStrBuf();
+       SendMsg->IO.ErrMsg = SendMsg->MyQEntry->StatusMessage;
+
+
        SendMsg->IO.SendBuf.fd = 
        SendMsg->IO.RecvBuf.fd = 
        SendMsg->IO.sock = sock_connect(SendMsg->mx_host, SendMsg->mx_port);
 
        StrBufPrintf(SendMsg->MyQEntry->StatusMessage, 
                     "Could not connect: %s", strerror(errno));
-       if (SendMsg->IO.sock >= 0) 
-       {
-               CtdlLogPrintf(CTDL_DEBUG, "SMTP client[%ld]: connected!\n", SendMsg->n);
-               int fdflags; 
-               fdflags = fcntl(SendMsg->IO.sock, F_GETFL);
-               if (fdflags < 0)
-                       CtdlLogPrintf(CTDL_DEBUG,
-                                     "SMTP client[%ld]: unable to get socket flags! %s \n",
-                                     SendMsg->n, strerror(errno));
-               fdflags = fdflags | O_NONBLOCK;
-               if (fcntl(SendMsg->IO.sock, F_SETFL, fdflags) < 0)
-                       CtdlLogPrintf(CTDL_DEBUG,
-                                     "SMTP client[%ld]: unable to set socket nonblocking flags! %s \n",
-                                     SendMsg->n, strerror(errno));
-       }
+
+
        if (SendMsg->IO.sock < 0) {
                if (errno > 0) {
                        StrBufPlain(SendMsg->MyQEntry->StatusMessage, 
@@ -655,23 +657,25 @@ int connect_one_smtpsrv_xamine_result(void *Ctx)
        }
        /// hier: naechsten mx ausprobieren.
        if (SendMsg->IO.sock < 0) {
-               SendMsg->SMTPstatus = 4;        /* dsn is already filled in */
+               SendMsg->MyQEntry->Status = 4;  /* dsn is already filled in */
                //// hier: abbrechen & bounce.
                return -1;
        }
 
 
-       SendMsg->IO.SendBuf.Buf = NewStrBuf();
-       SendMsg->IO.RecvBuf.Buf = NewStrBuf();
-       SendMsg->IO.IOBuf = NewStrBuf();
        InitEventIO(&SendMsg->IO, SendMsg, 
                    SMTP_C_DispatchReadDone, 
                    SMTP_C_DispatchWriteDone, 
+                   SMTP_C_Terminate,
+                   SMTP_C_Timeout,
+                   SMTP_C_ConnFail,
+                   SMTP_C_MXLookup,
                    SMTP_C_ReadServerStatus,
                    1);
        return 0;
 }
 
+
 eNextState SMTPC_read_greeting(SmtpOutMsg *SendMsg)
 {
        /* Process the SMTP greeting from the server */
@@ -888,7 +892,7 @@ eNextState SMTPC_read_data_body_reply(SmtpOutMsg *SendMsg)
        StrBufPlain(SendMsg->MyQEntry->StatusMessage, 
                    &ChrPtr(SendMsg->IO.RecvBuf.Buf)[4],
                    StrLength(SendMsg->IO.RecvBuf.Buf) - 4);
-       SendMsg->SMTPstatus = 2;
+       SendMsg->MyQEntry->Status = 2;
        return eSendReply;
 }
 
@@ -907,7 +911,7 @@ eNextState SMTPC_read_QUIT_reply(SmtpOutMsg *SendMsg)
 
        CtdlLogPrintf(CTDL_INFO, "SMTP client[%ld]: delivery to <%s> @ <%s> (%s) succeeded\n",
                      SendMsg->n, SendMsg->user, SendMsg->node, SendMsg->name);
-       return eSendReply;
+       return eTerminateConnection;
 }
 
 eNextState SMTPC_read_dummy(SmtpOutMsg *SendMsg)
@@ -933,7 +937,10 @@ void smtp_try(OneQueItem *MyQItem,
        SendMsg->n = MsgCount++;
        SendMsg->MyQEntry = MyQEntry;
        SendMsg->MyQItem = MyQItem;
-       SendMsg->msgtext = MsgText;
+       if (KeepMsgText)
+               SendMsg->msgtext = MsgText;
+       else 
+               SendMsg->msgtext = NewStrBufDup(MsgText);
 
        smtp_resolve_recipients(SendMsg);
        resolve_mx_hosts(SendMsg);
@@ -984,6 +991,7 @@ void QItem_Handle_Recipient(OneQueItem *Item, StrBuf *Line, const char **Pos)
        StrBufExtract_NextToken(Item->Current->Recipient, Line, Pos, '|');
        Item->Current->Status = StrBufExtractNext_int(Line, Pos, '|');
        StrBufExtract_NextToken(Item->Current->StatusMessage, Line, Pos, '|');
+       Item->Current = NULL; // TODO: is this always right?
 }
 
 
@@ -1040,12 +1048,12 @@ void smtp_do_procmsg(long msgnum, void *userdata) {
        long len;
        const char *Key;
 
-       CtdlLogPrintf(CTDL_DEBUG, "SMTP client: smtp_do_procmsg(%ld)\n", msgnum);
+       CtdlLogPrintf(CTDL_DEBUG, "SMTP Queue: smtp_do_procmsg(%ld)\n", msgnum);
        ///strcpy(envelope_from, "");
 
        msg = CtdlFetchMessage(msgnum, 1);
        if (msg == NULL) {
-               CtdlLogPrintf(CTDL_ERR, "SMTP client: tried %ld but no such message!\n", msgnum);
+               CtdlLogPrintf(CTDL_ERR, "SMTP Queue: tried %ld but no such message!\n", msgnum);
                return;
        }
 
@@ -1064,9 +1072,10 @@ void smtp_do_procmsg(long msgnum, void *userdata) {
        MyQItem = DeserializeQueueItem(PlainQItem, msgnum);
        FreeStrBuf(&PlainQItem);
 
-       if (MyQItem == NULL)
+       if (MyQItem == NULL) {
+               CtdlLogPrintf(CTDL_ERR, "SMTP Queue: Msg No %ld: already in progress!\n", msgnum);              
                return; /* s.b. else is already processing... */
-
+       }
 
        /*
         * Postpone delivery if we've already tried recently.
@@ -1090,7 +1099,7 @@ void smtp_do_procmsg(long msgnum, void *userdata) {
         * Bail out if there's no actual message associated with this
         */
        if (MyQItem->MessageID < 0L) {
-               CtdlLogPrintf(CTDL_ERR, "SMTP client: no 'msgid' directive found!\n");
+               CtdlLogPrintf(CTDL_ERR, "SMTP Queue: no 'msgid' directive found!\n");
                It = GetNewHashPos(MyQItem->MailQEntries, 0);
                citthread_mutex_lock(&ActiveQItemsLock);
                {
@@ -1103,6 +1112,14 @@ void smtp_do_procmsg(long msgnum, void *userdata) {
                return;
        }
 
+       It = GetNewHashPos(MyQItem->MailQEntries, 0);
+       while (GetNextHashPos(MyQItem->MailQEntries, It, &len, &Key, &vQE))
+       {
+               MailQEntry *ThisItem = vQE;
+               CtdlLogPrintf(CTDL_DEBUG, "SMTP Queue: Task: <%s> %d\n", ChrPtr(ThisItem->Recipient), ThisItem->Active);
+       }
+       DeleteHashPos(&It);
+
        CountActiveQueueEntries(MyQItem);
        if (MyQItem->ActiveDeliveries > 0)
        {
@@ -1114,7 +1131,7 @@ void smtp_do_procmsg(long msgnum, void *userdata) {
                {
                        MailQEntry *ThisItem = vQE;
                        if (ThisItem->Active == 1) {
-                               CtdlLogPrintf(CTDL_DEBUG, "SMTP client: Trying <%s>\n", ChrPtr(ThisItem->Recipient));
+                               CtdlLogPrintf(CTDL_DEBUG, "SMTP Queue: Trying <%s>\n", ChrPtr(ThisItem->Recipient));
                                smtp_try(MyQItem, ThisItem, Msg, (i == MyQItem->ActiveDeliveries));
                                i++;
                        }
@@ -1189,6 +1206,8 @@ void *smtp_queue_thread(void *arg) {
        int num_processed = 0;
        struct CitContext smtp_queue_CC;
 
+       CtdlThreadSleep(10);
+
        CtdlFillSystemContext(&smtp_queue_CC, "SMTP Send");
        citthread_setspecific(MyConKey, (void *)&smtp_queue_CC);
        CtdlLogPrintf(CTDL_DEBUG, "smtp_queue_thread() initializing\n");
@@ -1261,6 +1280,27 @@ SMTPSendHandler SendHandlers[eMaxSMTPC] = {
        SMTPC_send_QUIT
 };
 
+eNextState SMTP_C_Terminate(void *Data)
+{
+       SmtpOutMsg *pMsg = Data;
+       FinalizeMessageSend(pMsg);
+
+}
+
+eNextState SMTP_C_Timeout(void *Data)
+{
+       SmtpOutMsg *pMsg = Data;
+       FinalizeMessageSend(pMsg);
+
+}
+
+eNextState SMTP_C_ConnFail(void *Data)
+{
+       SmtpOutMsg *pMsg = Data;
+       FinalizeMessageSend(pMsg);
+
+}
+
 eNextState SMTP_C_DispatchReadDone(void *Data)
 {
        SmtpOutMsg *pMsg = Data;
@@ -1276,6 +1316,11 @@ eNextState SMTP_C_DispatchWriteDone(void *Data)
        
 }
 
+eNextState SMTP_C_MXLookup(void *Data)
+{
+
+}
+
 
 #endif
 CTDL_MODULE_INIT(smtp_eventclient)