EV: fix possible nullpointer access in last commit.
[citadel.git] / citadel / modules / smtp / serv_smtpeventclient.c
index 2e1dcbbed148ecb9f6d98e773b594f67addef786..dd4e3e2662b08a05a10784a4e36ee4a26dda378f 100644 (file)
  * RFC 2821 - Simple Mail Transfer Protocol
  * RFC 2822 - Internet Message Format
  * RFC 2920 - SMTP Service Extension for Command Pipelining
- *  
+ *
  * The VRFY and EXPN commands have been removed from this implementation
  * because nobody uses these commands anymore, except for spammers.
  *
- * Copyright (c) 1998-2009 by the citadel.org team
+ * Copyright (c) 1998-2012 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 version 3.
+ *  
+ *  
  *
  *  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
+ *  
+ *  
+ *  
  */
 
 #include "sysdep.h"
 #include "smtpqueue.h"
 #include "smtp_clienthandlers.h"
 
-#ifdef EXPERIMENTAL_SMTP_EVENT_CLIENT
-const unsigned short DefaultMXPort = 25;
+ConstStr SMTPStates[] = {
+       {HKEY("looking up mx - record")},
+       {HKEY("evaluating what to do next")},
+       {HKEY("looking up a - record")},
+       {HKEY("looking up aaaa - record")},
+       {HKEY("connecting remote")},
+       {HKEY("smtp conversation ongoing")},
+       {HKEY("smtp sending maildata")},
+       {HKEY("smtp sending done")},
+       {HKEY("smtp successfully finished")},
+       {HKEY("failed one attempt")},
+       {HKEY("failed temporarily")},
+       {HKEY("failed permanently")}
+};
+
+void SetSMTPState(AsyncIO *IO, smtpstate State)
+{
+       CitContext* CCC = IO->CitContext;
+       if (CCC != NULL)
+               memcpy(CCC->cs_clientname, SMTPStates[State].Key, SMTPStates[State].len + 1);
+}
+
+int SMTPClientDebugEnabled = 0;
 void DeleteSmtpOutMsg(void *v)
 {
        SmtpOutMsg *Msg = v;
+       AsyncIO *IO = &Msg->IO;
+       EV_syslog(LOG_DEBUG, "%s Exit\n", __FUNCTION__);
+
+       /* these are kept in our own space and free'd below */
+       Msg->IO.ConnectMe = NULL;
 
        ares_free_data(Msg->AllMX);
        if (Msg->HostLookup.VParsedDNSReply != NULL)
                Msg->HostLookup.DNSReplyFree(Msg->HostLookup.VParsedDNSReply);
+       FreeURL(&Msg->Relay);
        FreeStrBuf(&Msg->msgtext);
        FreeAsyncIOContents(&Msg->IO);
+       memset (Msg, 0, sizeof(SmtpOutMsg)); /* just to be shure... */
        free(Msg);
 }
 
@@ -109,243 +137,331 @@ eNextState SMTP_C_Timeout(AsyncIO *IO);
 eNextState SMTP_C_ConnFail(AsyncIO *IO);
 eNextState SMTP_C_DispatchReadDone(AsyncIO *IO);
 eNextState SMTP_C_DispatchWriteDone(AsyncIO *IO);
+eNextState SMTP_C_DNSFail(AsyncIO *IO);
 eNextState SMTP_C_Terminate(AsyncIO *IO);
+eNextState SMTP_C_TerminateDB(AsyncIO *IO);
 eReadState SMTP_C_ReadServerStatus(AsyncIO *IO);
 
+eNextState mx_connect_ip(AsyncIO *IO);
+eNextState get_one_mx_host_ip(AsyncIO *IO);
+
 /******************************************************************************
  * So, we're finished with sending (regardless of success or failure)         *
  * This Message might be referenced by several Queue-Items, if we're the last,*
  * we need to free the memory and send bounce messages (on terminal failure)  *
  * else we just free our SMTP-Message struct.                                 *
  ******************************************************************************/
-void FinalizeMessageSend(SmtpOutMsg *Msg)
+eNextState FinalizeMessageSend_DB(AsyncIO *IO)
 {
-       CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
-       
-       if (DecreaseQReference(Msg->MyQItem)) 
+       const char *Status;
+       SmtpOutMsg *Msg = IO->Data;
+       StrBuf *StatusMessage;
+
+       if (Msg->MyQEntry->AllStatusMessages != NULL)
+               StatusMessage = Msg->MyQEntry->AllStatusMessages;
+       else
+               StatusMessage = Msg->MyQEntry->StatusMessage;
+
+
+       if (Msg->MyQEntry->Status == 2) {
+               SetSMTPState(IO, eSTMPfinished);
+               Status = "Delivery successful.";
+       }
+       else if (Msg->MyQEntry->Status == 5) {
+               SetSMTPState(IO, eSMTPFailTotal);
+               Status = "Delivery failed permanently; giving up.";
+       }
+       else {
+               SetSMTPState(IO, eSMTPFailTemporary);
+               Status = "Delivery failed temporarily; will retry later.";
+       }
+                       
+       EVS_syslog(LOG_INFO,
+                  "%s Time[%fs] Recipient <%s> @ <%s> (%s) Status message: %s\n",
+                  Status,
+                  Msg->IO.Now - Msg->IO.StartIO,
+                  Msg->user,
+                  Msg->node,
+                  Msg->name,
+                  ChrPtr(StatusMessage));
+
+
+       Msg->IDestructQueItem = DecreaseQReference(Msg->MyQItem);
+
+       Msg->nRemain = CountActiveQueueEntries(Msg->MyQItem, 0);
+
+       if (Msg->MyQEntry->Active && 
+           !Msg->MyQEntry->StillActive &&
+           CheckQEntryIsBounce(Msg->MyQEntry))
        {
-               int nRemain;
-               StrBuf *MsgData;
-
-               nRemain = CountActiveQueueEntries(Msg->MyQItem);
-
-               MsgData = SerializeQueueItem(Msg->MyQItem);
-               /*
-                * Uncompleted delivery instructions remain, so delete the old
-                * instructions and replace with the updated ones.
-                */
-               CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, &Msg->MyQItem->QueMsgID, 1, "");
-               smtpq_do_bounce(Msg->MyQItem,
-                               Msg->msgtext); 
-               if (nRemain > 0) {
-                       struct CtdlMessage *msg;
-                       msg = malloc(sizeof(struct CtdlMessage));
-                       memset(msg, 0, sizeof(struct CtdlMessage));
-                       msg->cm_magic = CTDLMESSAGE_MAGIC;
-                       msg->cm_anon_type = MES_NORMAL;
-                       msg->cm_format_type = FMT_RFC822;
-                       msg->cm_fields['M'] = SmashStrBuf(&MsgData);
+               /* are we casue for a bounce mail? */
+               Msg->MyQItem->SendBounceMail |= (1<<Msg->MyQEntry->Status);
+       }
+
+       if ((Msg->nRemain > 0) || Msg->IDestructQueItem)
+               Msg->QMsgData = SerializeQueueItem(Msg->MyQItem);
+       else
+               Msg->QMsgData = NULL;
+
+       /*
+        * Uncompleted delivery instructions remain, so delete the old
+        * instructions and replace with the updated ones.
+        */
+       EVS_syslog(LOG_DEBUG, "%ld", Msg->MyQItem->QueMsgID);
+       CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, &Msg->MyQItem->QueMsgID, 1, "");
+       Msg->MyQItem->QueMsgID = -1;
+
+       if (Msg->IDestructQueItem)
+               smtpq_do_bounce(Msg->MyQItem, StatusMessage, Msg->msgtext, Msg->pCurrRelay);
+
+       if (Msg->nRemain > 0)
+       {
+               struct CtdlMessage *msg;
+               msg = malloc(sizeof(struct CtdlMessage));
+               memset(msg, 0, sizeof(struct CtdlMessage));
+               msg->cm_magic = CTDLMESSAGE_MAGIC;
+               msg->cm_anon_type = MES_NORMAL;
+               msg->cm_format_type = FMT_RFC822;
+               msg->cm_fields['M'] = SmashStrBuf(&Msg->QMsgData);
+               msg->cm_fields['U'] = strdup("QMSG");
+               Msg->MyQItem->QueMsgID =
                        CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM, QP_EADDR);
-                       CtdlFreeMessage(msg);
-               }
-               else {
-                       CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, &Msg->MyQItem->MessageID, 1, "");
-                       FreeStrBuf(&MsgData);
-               }
+               EVS_syslog(LOG_DEBUG, "%ld", Msg->MyQItem->QueMsgID);
+               CtdlFreeMessage(msg);
+       }
+       else {
+               CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM,
+                                  &Msg->MyQItem->MessageID,
+                                  1,
+                                  "");
+               FreeStrBuf(&Msg->QMsgData);
+       }
+
+       RemoveContext(Msg->IO.CitContext);
+       return eAbort;
+}
+
+eNextState Terminate(AsyncIO *IO)
+{
+       SmtpOutMsg *Msg = IO->Data;
 
+       if (Msg->IDestructQueItem)
                RemoveQItem(Msg->MyQItem);
-       }
+
        DeleteSmtpOutMsg(Msg);
+       return eAbort;
+}
+eNextState FinalizeMessageSend(SmtpOutMsg *Msg)
+{
+       /* hand over to DB Queue */
+       return QueueDBOperation(&Msg->IO, FinalizeMessageSend_DB);
 }
 
 eNextState FailOneAttempt(AsyncIO *IO)
 {
-       /* 
-        * possible ways here: 
-        * - connection timeout 
-        * - 
-        */     
-}
+       SmtpOutMsg *Msg = IO->Data;
+
+       SetSMTPState(IO, eSTMPfailOne);
+       if (Msg->MyQEntry->Status == 2)
+               return eAbort;
+
+       /*
+        * possible ways here:
+        * - connection timeout
+        * - dns lookup failed
+        */
+       StopClientWatchers(IO, 1);
+
+       Msg->MyQEntry->nAttempt ++;
+       if (Msg->MyQEntry->AllStatusMessages == NULL)
+               Msg->MyQEntry->AllStatusMessages = NewStrBuf();
+
+       StrBufAppendPrintf(Msg->MyQEntry->AllStatusMessages, "%ld) ", Msg->MyQEntry->nAttempt);
+       StrBufAppendBuf(Msg->MyQEntry->AllStatusMessages, Msg->MyQEntry->StatusMessage, 0);
+       StrBufAppendBufPlain(Msg->MyQEntry->AllStatusMessages, HKEY("; "), 0);
+
+       if (Msg->pCurrRelay != NULL)
+               Msg->pCurrRelay = Msg->pCurrRelay->Next;
+       if ((Msg->pCurrRelay != NULL) &&
+           !Msg->pCurrRelay->IsRelay &&
+           Msg->MyQItem->HaveRelay)
+       {
+               EVS_syslog(LOG_DEBUG, "%s Aborting; last relay failed.\n", __FUNCTION__);
+               return eAbort;
+       }
 
-////TODO
+       if (Msg->pCurrRelay == NULL) {
+               EVS_syslog(LOG_DEBUG, "%s Aborting\n", __FUNCTION__);
+               return eAbort;
+       }
+       if (Msg->pCurrRelay->IsIP) {
+               EVS_syslog(LOG_DEBUG, "%s connecting IP\n", __FUNCTION__);
+               return mx_connect_ip(IO);
+       }
+       else {
+               EVS_syslog(LOG_DEBUG,
+                          "%s resolving next MX Record\n",
+                          __FUNCTION__);
+               return get_one_mx_host_ip(IO);
+       }
+}
 
 
 void SetConnectStatus(AsyncIO *IO)
 {
-       
-       SmtpOutMsg *SendMsg = IO->Data;
+       SmtpOutMsg *Msg = IO->Data;
        char buf[256];
        void *src;
 
        buf[0] = '\0';
 
-       if (IO->IP6) {
-               src = &IO->Addr.sin6_addr;
+       if (IO->ConnectMe->IPv6) {
+               src = &IO->ConnectMe->Addr.sin6_addr;
        }
        else {
-               struct sockaddr_in *addr = (struct sockaddr_in *)&IO->Addr;
+               struct sockaddr_in *addr;
 
+               addr = (struct sockaddr_in *)&IO->ConnectMe->Addr;
                src = &addr->sin_addr.s_addr;
        }
 
-       inet_ntop((IO->IP6)?AF_INET6:AF_INET,
+       inet_ntop((IO->ConnectMe->IPv6)?AF_INET6:AF_INET,
                  src,
-                 buf, sizeof(buf));
-       if (SendMsg->mx_host == NULL)
-               SendMsg->mx_host = "<no name>";
-
-       CtdlLogPrintf(CTDL_DEBUG, 
-                     "SMTP client[%ld]: connecting to %s [%s]:%d ...\n", 
-                     SendMsg->n, 
-                     SendMsg->mx_host, 
-                     buf,
-                     SendMsg->IO.dport);
-
-       SendMsg->MyQEntry->Status = 5; 
-       StrBufPrintf(SendMsg->MyQEntry->StatusMessage, 
-                    "Timeout while connecting %s [%s]:%d ", 
-                    SendMsg->mx_host,
+                 buf,
+                 sizeof(buf));
+
+       if (Msg->mx_host == NULL)
+               Msg->mx_host = "<no MX-Record>";
+
+       EVS_syslog(LOG_INFO,
+                 "connecting to %s [%s]:%d ...\n",
+                 Msg->mx_host,
+                 buf,
+                 Msg->IO.ConnectMe->Port);
+
+       Msg->MyQEntry->Status = 4;
+       StrBufPrintf(Msg->MyQEntry->StatusMessage,
+                    "Timeout while connecting %s [%s]:%d ",
+                    Msg->mx_host,
                     buf,
-                    SendMsg->IO.dport);
+                    Msg->IO.ConnectMe->Port);
+       Msg->IO.NextState = eConnect;
 }
 
 /*****************************************************************************
  * So we connect our Relay IP here.                                          *
  *****************************************************************************/
-eNextState mx_connect_relay_ip(AsyncIO *IO)
+eNextState mx_connect_ip(AsyncIO *IO)
 {
-       
-       SmtpOutMsg *SendMsg = IO->Data;
-
-       CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
-
-       IO->IP6 = SendMsg->pCurrRelay->af == AF_INET6;
-       
-       if (SendMsg->pCurrRelay->Port != 0)
-               IO->dport = SendMsg->pCurrRelay->Port;
+       SmtpOutMsg *Msg = IO->Data;
+       SetSMTPState(IO, eSTMPconnecting);
 
-       memset(&IO->Addr, 0, sizeof(struct sockaddr_in6));
-       if (IO->IP6) {
-               memcpy(&IO->Addr.sin6_addr.s6_addr, 
-                      &SendMsg->pCurrRelay->Addr,
-                      sizeof(struct in6_addr));
+       EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
 
-               IO->Addr.sin6_family = AF_INET6;
-               IO->Addr.sin6_port   = htons(IO->dport);
-       }
-       else {
-               struct sockaddr_in *addr = (struct sockaddr_in*) &IO->Addr;
-               /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
-               memcpy(&addr->sin_addr,///.s_addr, 
-                      &SendMsg->pCurrRelay->Addr,
-                      sizeof(struct in_addr));
-               
-               addr->sin_family = AF_INET;
-               addr->sin_port   = htons(IO->dport);
-       }
+       IO->ConnectMe = Msg->pCurrRelay;
+       Msg->State = eConnectMX;
 
        SetConnectStatus(IO);
 
-       return InitEventIO(IO, SendMsg, 
-                          SMTP_C_ConnTimeout, 
-                          SMTP_C_ReadTimeouts[0],
-                          1);
+       return EvConnectSock(IO,
+                            SMTP_C_ConnTimeout,
+                            SMTP_C_ReadTimeouts[0],
+                            1);
 }
 
 eNextState get_one_mx_host_ip_done(AsyncIO *IO)
 {
-       SmtpOutMsg *SendMsg = IO->Data;
+       SmtpOutMsg *Msg = IO->Data;
        struct hostent *hostent;
 
+       IO->ConnectMe = Msg->pCurrRelay;
+
        QueryCbDone(IO);
+       EVS_syslog(LOG_DEBUG, "%s Time[%fs]\n",
+                  __FUNCTION__,
+                  IO->Now - IO->DNS.Start);
 
-       hostent = SendMsg->HostLookup.VParsedDNSReply;
-       if ((SendMsg->HostLookup.DNSStatus == ARES_SUCCESS) && 
+       hostent = Msg->HostLookup.VParsedDNSReply;
+       if ((Msg->HostLookup.DNSStatus == ARES_SUCCESS) &&
            (hostent != NULL) ) {
-               
-               IO->IP6  = hostent->h_addrtype == AF_INET6;
-               ////IO->HEnt = hostent;
-
-               memset(&IO->Addr, 0, sizeof(struct in6_addr));
-               if (IO->IP6) {
-                       memcpy(&IO->Addr.sin6_addr.s6_addr, 
+               memset(&Msg->pCurrRelay->Addr, 0, sizeof(struct in6_addr));
+               if (Msg->pCurrRelay->IPv6) {
+                       memcpy(&Msg->pCurrRelay->Addr.sin6_addr.s6_addr,
                               &hostent->h_addr_list[0],
                               sizeof(struct in6_addr));
-                       
-                       IO->Addr.sin6_family = hostent->h_addrtype;
-                       IO->Addr.sin6_port   = htons(IO->dport);
+
+                       Msg->pCurrRelay->Addr.sin6_family =
+                               hostent->h_addrtype;
+                       Msg->pCurrRelay->Addr.sin6_port =
+                               htons(Msg->IO.ConnectMe->Port);
                }
                else {
-                       struct sockaddr_in *addr = (struct sockaddr_in*) &IO->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, 
-                              hostent->h_addr_list[0], 
+                       struct sockaddr_in *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]);
+                        */
+
+                       addr = (struct sockaddr_in*) &Msg->pCurrRelay->Addr;
+
+                       memcpy(&addr->sin_addr.s_addr,
+                              hostent->h_addr_list[0],
                               sizeof(uint32_t));
-                       
+
                        addr->sin_family = hostent->h_addrtype;
-                       addr->sin_port   = htons(IO->dport);
-                       
+                       addr->sin_port   = htons(Msg->IO.ConnectMe->Port);
+               }
+               Msg->mx_host = Msg->pCurrRelay->Host;
+               if (Msg->HostLookup.VParsedDNSReply != NULL) {
+                       Msg->HostLookup.DNSReplyFree(Msg->HostLookup.VParsedDNSReply);
+                       Msg->HostLookup.VParsedDNSReply = NULL;
+               }
+               return mx_connect_ip(IO);
+       }
+       else {
+               SetSMTPState(IO, eSTMPfailOne);
+               if (Msg->HostLookup.VParsedDNSReply != NULL) {
+                       Msg->HostLookup.DNSReplyFree(Msg->HostLookup.VParsedDNSReply);
+                       Msg->HostLookup.VParsedDNSReply = NULL;
                }
-               ////SendMsg->IO.HEnt = hostent;
-               SetConnectStatus(IO);
-               return InitEventIO(IO, 
-                                  SendMsg, 
-                                  SMTP_C_ConnTimeout, 
-                                  SMTP_C_ReadTimeouts[0],
-                                  1);
+               return FailOneAttempt(IO);
        }
-       else // TODO: here we need to find out whether there are more mx'es, backup relay, and so on
-               return SMTP_C_Terminate(IO);
 }
 
-/*
-
-       if (SendMsg->pCurrRelay != NULL) {
-               SendMsg->mx_host = Hostname = SendMsg->pCurrRelay->Host;
-               if (SendMsg->pCurrRelay->Port != 0)
-                       SendMsg->IO.dport = SendMsg->pCurrRelay->Port;
-       }
-               else
-*/
 eNextState get_one_mx_host_ip(AsyncIO *IO)
 {
-       SmtpOutMsg * SendMsg = IO->Data;
-       const char *Hostname;
-       //char *endpart;
-       //char buf[SIZ];
-
-       /* 
+       SmtpOutMsg * Msg = IO->Data;
+       /*
         * here we start with the lookup of one host. it might be...
         * - the relay host *sigh*
         * - the direct hostname if there was no mx record
         * - one of the mx'es
-        */ 
-
-       InitC_ares_dns(IO);
+        */
+       SetSMTPState(IO, (Msg->pCurrRelay->IPv6)?eSTMPalookup:eSTMPaaaalookup);
 
-       if (SendMsg->mx_host != NULL) Hostname = SendMsg->mx_host;
-       else Hostname = SendMsg->node;
+       EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
 
-       CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
+       EVS_syslog(LOG_DEBUG,
+                 "looking up %s-Record %s : %d ...\n",
+                 (Msg->pCurrRelay->IPv6)? "aaaa": "a",
+                 Msg->pCurrRelay->Host,
+                 Msg->pCurrRelay->Port);
 
-       CtdlLogPrintf(CTDL_DEBUG, 
-                     "SMTP client[%ld]: looking up %s : %d ...\n", 
-                     SendMsg->n, 
-                     Hostname, 
-                     SendMsg->IO.dport);
-// TODO: ns_t_aaaa
-       if (!QueueQuery(ns_t_a, 
-                       Hostname, 
-                       &SendMsg->IO, 
-                       &SendMsg->HostLookup, 
+       if (!QueueQuery((Msg->pCurrRelay->IPv6)? ns_t_aaaa : ns_t_a,
+                       Msg->pCurrRelay->Host,
+                       &Msg->IO,
+                       &Msg->HostLookup,
                        get_one_mx_host_ip_done))
        {
-               SendMsg->MyQEntry->Status = 5;
-               StrBufPrintf(SendMsg->MyQEntry->StatusMessage, 
-                            "No MX hosts found for <%s>", SendMsg->node);
+               Msg->MyQEntry->Status = 5;
+               StrBufPrintf(Msg->MyQEntry->StatusMessage,
+                            "No MX hosts found for <%s>", Msg->node);
+               Msg->IO.NextState = eTerminateConnection;
                return IO->NextState;
        }
+       IO->NextState = eReadDNSReply;
        return IO->NextState;
 }
 
@@ -355,44 +471,60 @@ eNextState get_one_mx_host_ip(AsyncIO *IO)
  *****************************************************************************/
 eNextState smtp_resolve_mx_record_done(AsyncIO *IO)
 {
-       SmtpOutMsg * SendMsg = IO->Data;
+       SmtpOutMsg * Msg = IO->Data;
        ParsedURL **pp;
 
        QueryCbDone(IO);
 
-       CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
-       pp = &SendMsg->Relay;
+       EVS_syslog(LOG_DEBUG, "%s Time[%fs]\n",
+                  __FUNCTION__,
+                  IO->Now - IO->DNS.Start);
+
+       pp = &Msg->Relay;
        while ((pp != NULL) && (*pp != NULL) && ((*pp)->Next != NULL))
                pp = &(*pp)->Next;
 
-       if ((IO->DNSQuery->DNSStatus == ARES_SUCCESS) && 
-           (IO->DNSQuery->VParsedDNSReply != NULL))
+       if ((IO->DNS.Query->DNSStatus == ARES_SUCCESS) &&
+           (IO->DNS.Query->VParsedDNSReply != NULL))
        { /* ok, we found mx records. */
-               SendMsg->IO.ErrMsg = SendMsg->MyQEntry->StatusMessage;
-               
 
-               SendMsg->CurrMX    = SendMsg->AllMX 
-                                  = IO->DNSQuery->VParsedDNSReply;
-               while (SendMsg->CurrMX) {
+               Msg->CurrMX
+                       = Msg->AllMX
+                       = IO->DNS.Query->VParsedDNSReply;
+               while (Msg->CurrMX) {
                        int i;
                        for (i = 0; i < 2; i++) {
                                ParsedURL *p;
 
                                p = (ParsedURL*) malloc(sizeof(ParsedURL));
                                memset(p, 0, sizeof(ParsedURL));
+                               p->Priority = Msg->CurrMX->priority;
                                p->IsIP = 0;
-                               p->Port = 25; //// TODO define.
+                               p->Port = DefaultMXPort;
                                p->IPv6 = i == 1;
-                               p->Host = SendMsg->CurrMX->host;
-                               
-                               *pp = p;
-                               pp = &p;
+                               p->Host = Msg->CurrMX->host;
+                               if (*pp == NULL)
+                                       *pp = p;
+                               else {
+                                       ParsedURL *ppp = *pp;
+
+                                       while ((ppp->Next != NULL) &&
+                                              (ppp->Next->Priority <= p->Priority))
+                                              ppp = ppp->Next;
+                                       if ((ppp == *pp) &&
+                                           (ppp->Priority > p->Priority)) {
+                                               p->Next = *pp;
+                                               *pp = p;
+                                       }
+                                       else {
+                                               p->Next = ppp->Next;
+                                               ppp->Next = p;
+                                       }
+                               }
                        }
-                       SendMsg->CurrMX    = SendMsg->CurrMX->next;
+                       Msg->CurrMX    = Msg->CurrMX->next;
                }
-//             SendMsg->mx_host   = SendMsg->CurrMX->host;
-//             SendMsg->CurrMX    = SendMsg->CurrMX->next;
-               SendMsg->CXFlags   = SendMsg->CXFlags & F_HAVE_MX;
+               Msg->CXFlags   = Msg->CXFlags & F_HAVE_MX;
        }
        else { /* else fall back to the plain hostname */
                int i;
@@ -402,38 +534,45 @@ eNextState smtp_resolve_mx_record_done(AsyncIO *IO)
                        p = (ParsedURL*) malloc(sizeof(ParsedURL));
                        memset(p, 0, sizeof(ParsedURL));
                        p->IsIP = 0;
-                       p->Port = 25; //// TODO define.
+                       p->Port = DefaultMXPort;
                        p->IPv6 = i == 1;
-                       p->Host = SendMsg->node;
-                               
+                       p->Host = Msg->node;
+
                        *pp = p;
-                       pp = &p;
+                       pp = &p->Next;
                }
-               ///     SendMsg->mx_host   = SendMsg->node;
-               SendMsg->CXFlags   = SendMsg->CXFlags & F_DIRECT;
+               Msg->CXFlags   = Msg->CXFlags & F_DIRECT;
        }
-       (*pp)->Next = SendMsg->MyQItem->FallBackHost;
+       if (Msg->MyQItem->FallBackHost != NULL)
+       {
+               Msg->MyQItem->FallBackHost->Next = *pp;
+               *pp = Msg->MyQItem->FallBackHost;
+       }
+       Msg->pCurrRelay = Msg->Relay;
        return get_one_mx_host_ip(IO);
 }
 
 eNextState resolve_mx_records(AsyncIO *IO)
 {
-       SmtpOutMsg * SendMsg = IO->Data;
+       SmtpOutMsg * Msg = IO->Data;
 
-       CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
+       SetSMTPState(IO, eSTMPmxlookup);
+
+       EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
        /* start resolving MX records here. */
-       if (!QueueQuery(ns_t_mx, 
-                       SendMsg->node, 
-                       &SendMsg->IO, 
-                       &SendMsg->MxLookup, 
+       if (!QueueQuery(ns_t_mx,
+                       Msg->node,
+                       &Msg->IO,
+                       &Msg->MxLookup,
                        smtp_resolve_mx_record_done))
        {
-               SendMsg->MyQEntry->Status = 5;
-               StrBufPrintf(SendMsg->MyQEntry->StatusMessage, 
-                            "No MX hosts found for <%s>", SendMsg->node);
+               Msg->MyQEntry->Status = 5;
+               StrBufPrintf(Msg->MyQEntry->StatusMessage,
+                            "No MX hosts found for <%s>", Msg->node);
                return IO->NextState;
        }
-       return eAbort;
+       Msg->IO.NextState = eReadDNSReply;
+       return IO->NextState;
 }
 
 
@@ -442,79 +581,106 @@ eNextState resolve_mx_records(AsyncIO *IO)
  *  so, we're going to start a SMTP delivery.  lets get it on.                *
  ******************************************************************************/
 
-SmtpOutMsg *new_smtp_outmsg(OneQueItem *MyQItem, 
-                           MailQEntry *MyQEntry, 
+SmtpOutMsg *new_smtp_outmsg(OneQueItem *MyQItem,
+                           MailQEntry *MyQEntry,
                            int MsgCount)
 {
-       SmtpOutMsg * SendMsg;
-
-       SendMsg = (SmtpOutMsg *) malloc(sizeof(SmtpOutMsg));
-       memset(SendMsg, 0, sizeof(SmtpOutMsg));
-
-       SendMsg->n                = MsgCount;
-       SendMsg->MyQEntry         = MyQEntry;
-       SendMsg->MyQItem          = MyQItem;
-       SendMsg->pCurrRelay       = MyQItem->URL;
-
-       SendMsg->IO.Data          = SendMsg;
-
-       SendMsg->IO.SendDone      = SMTP_C_DispatchWriteDone;
-       SendMsg->IO.ReadDone      = SMTP_C_DispatchReadDone;
-       SendMsg->IO.Terminate     = SMTP_C_Terminate;
-       SendMsg->IO.LineReader    = SMTP_C_ReadServerStatus;
-       SendMsg->IO.ConnFail      = SMTP_C_ConnFail;
-       SendMsg->IO.Timeout       = SMTP_C_Timeout;
-       SendMsg->IO.ShutdownAbort = SMTP_C_Shutdown;
-
-       SendMsg->IO.SendBuf.Buf   = NewStrBufPlain(NULL, 1024);
-       SendMsg->IO.RecvBuf.Buf   = NewStrBufPlain(NULL, 1024);
-       SendMsg->IO.IOBuf         = NewStrBuf();
-
-       SendMsg->IO.sock          = (-1);
-       SendMsg->IO.NextState     = eReadMessage;
-       SendMsg->IO.dport         = DefaultMXPort;
-
-       return SendMsg;
+       SmtpOutMsg * Msg;
+
+       Msg = (SmtpOutMsg *) malloc(sizeof(SmtpOutMsg));
+       if (Msg == NULL)
+               return NULL;
+       memset(Msg, 0, sizeof(SmtpOutMsg));
+
+       Msg->n                = MsgCount;
+       Msg->MyQEntry         = MyQEntry;
+       Msg->MyQItem          = MyQItem;
+       Msg->pCurrRelay       = MyQItem->URL;
+
+       InitIOStruct(&Msg->IO,
+                    Msg,
+                    eReadMessage,
+                    SMTP_C_ReadServerStatus,
+                    SMTP_C_DNSFail,
+                    SMTP_C_DispatchWriteDone,
+                    SMTP_C_DispatchReadDone,
+                    SMTP_C_Terminate,
+                    SMTP_C_TerminateDB,
+                    SMTP_C_ConnFail,
+                    SMTP_C_Timeout,
+                    SMTP_C_Shutdown);
+
+       Msg->IO.ErrMsg = Msg->MyQEntry->StatusMessage;
+
+       return Msg;
 }
 
-void smtp_try_one_queue_entry(OneQueItem *MyQItem, 
-                             MailQEntry *MyQEntry, 
-                             StrBuf *MsgText, 
-                             int KeepMsgText,  /* KeepMsgText allows us to use MsgText as ours. */
+void smtp_try_one_queue_entry(OneQueItem *MyQItem,
+                             MailQEntry *MyQEntry,
+                             StrBuf *MsgText,
+                       /*KeepMsgText allows us to use MsgText as ours.*/
+                             int KeepMsgText,
                              int MsgCount)
 {
-       SmtpOutMsg *SendMsg;
+       SmtpOutMsg *Msg;
 
-       CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
+       SMTPC_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
 
-       SendMsg = new_smtp_outmsg(MyQItem, MyQEntry, MsgCount);
-       if (KeepMsgText) SendMsg->msgtext = MsgText;
-       else             SendMsg->msgtext = NewStrBufDup(MsgText);
-       
-       if (smtp_resolve_recipients(SendMsg)) {
-               if (SendMsg->pCurrRelay == NULL)
-                       QueueEventContext(&SendMsg->IO,
+       Msg = new_smtp_outmsg(MyQItem, MyQEntry, MsgCount);
+       if (Msg == NULL) {
+               SMTPC_syslog(LOG_DEBUG, "%s Failed to alocate message context.\n", __FUNCTION__);
+               if (KeepMsgText) 
+                       FreeStrBuf (&MsgText);
+               return;
+       }
+       if (KeepMsgText) Msg->msgtext = MsgText;
+       else             Msg->msgtext = NewStrBufDup(MsgText);
+
+       if (smtp_resolve_recipients(Msg) &&
+           (!MyQItem->HaveRelay ||
+            (MyQItem->URL != NULL)))
+       {
+               safestrncpy(
+                       ((CitContext *)Msg->IO.CitContext)->cs_host,
+                       Msg->node,
+                       sizeof(((CitContext *)
+                               Msg->IO.CitContext)->cs_host));
+
+               SMTPC_syslog(LOG_DEBUG, "Starting: [%ld] <%s> CC <%d> \n",
+                            Msg->MyQItem->MessageID,
+                            ChrPtr(Msg->MyQEntry->Recipient),
+                            ((CitContext*)Msg->IO.CitContext)->cs_pid);
+               if (Msg->pCurrRelay == NULL) {
+                       SetSMTPState(&Msg->IO, eSTMPmxlookup);
+                       QueueEventContext(&Msg->IO,
                                          resolve_mx_records);
+               }
                else { /* oh... via relay host */
-                       if (SendMsg->pCurrRelay->IsIP) {
-                               QueueEventContext(&SendMsg->IO,
-                                                 mx_connect_relay_ip);
+                       if (Msg->pCurrRelay->IsIP) {
+                               SetSMTPState(&Msg->IO, eSTMPconnecting);
+                               QueueEventContext(&Msg->IO,
+                                                 mx_connect_ip);
                        }
-                       else { /* uneducated admin has chosen to add DNS to the equation... */
-                               QueueEventContext(&SendMsg->IO,
+                       else {
+                               SetSMTPState(&Msg->IO, eSTMPalookup);
+                               /* uneducated admin has chosen to
+                                  add DNS to the equation... */
+                               QueueEventContext(&Msg->IO,
                                                  get_one_mx_host_ip);
                        }
                }
        }
        else {
+               SetSMTPState(&Msg->IO, eSMTPFailTotal);
                /* No recipients? well fail then. */
-               if ((SendMsg==NULL) || 
-                   (SendMsg->MyQEntry == NULL)) {
-                       SendMsg->MyQEntry->Status = 5;
-                       StrBufPlain(SendMsg->MyQEntry->StatusMessage, 
-                                   HKEY("Invalid Recipient!"));
+               if (Msg->MyQEntry != NULL) {
+                       Msg->MyQEntry->Status = 5;
+                       if (StrLength(Msg->MyQEntry->StatusMessage) == 0)
+                               StrBufPlain(Msg->MyQEntry->StatusMessage,
+                                           HKEY("Invalid Recipient!"));
                }
-               FinalizeMessageSend(SendMsg);
+               FinalizeMessageSend_DB(&Msg->IO);
+               DeleteSmtpOutMsg(Msg);
        }
 }
 
@@ -527,54 +693,70 @@ void smtp_try_one_queue_entry(OneQueItem *MyQItem,
 /*                     SMTP CLIENT DISPATCHER                                */
 /*****************************************************************************/
 
-void SMTPSetTimeout(eNextState NextTCPState, SmtpOutMsg *pMsg)
+void SMTPSetTimeout(eNextState NextTCPState, SmtpOutMsg *Msg)
 {
-       CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
-       double Timeout;
+       double Timeout = 0.0;
+       AsyncIO *IO = &Msg->IO;
+
+       EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
+
        switch (NextTCPState) {
+       case eSendFile:
        case eSendReply:
        case eSendMore:
-               Timeout = SMTP_C_SendTimeouts[pMsg->State];
-               if (pMsg->State == eDATABody) {
-                       /* if we're sending a huge message, we need more time. */
-                       Timeout += StrLength(pMsg->msgtext) / 1024;
+               Timeout = SMTP_C_SendTimeouts[Msg->State];
+               if (Msg->State == eDATABody) {
+                       /* if we're sending a huge message,
+                        * we need more time.
+                        */
+                       Timeout += StrLength(Msg->msgtext) / 512;
                }
                break;
        case eReadMessage:
-               Timeout = SMTP_C_ReadTimeouts[pMsg->State];
-               if (pMsg->State == eDATATerminateBody) {
-                       /* 
-                        * some mailservers take a nap before accepting the message
-                        * content inspection and such.
+               Timeout = SMTP_C_ReadTimeouts[Msg->State];
+               if (Msg->State == eDATATerminateBody) {
+                       /*
+                        * some mailservers take a nap before accepting
+                        * the message content inspection and such.
                         */
-                       Timeout += StrLength(pMsg->msgtext) / 1024;
+                       Timeout += StrLength(Msg->msgtext) / 512;
                }
                break;
+       case eSendDNSQuery:
+       case eReadDNSReply:
+       case eDBQuery:
+       case eReadFile:
+       case eReadMore:
+       case eReadPayload:
+       case eConnect:
        case eTerminateConnection:
        case eAbort:
                return;
        }
-       SetNextTimeout(&pMsg->IO, Timeout);
+       SetNextTimeout(&Msg->IO, Timeout);
 }
 eNextState SMTP_C_DispatchReadDone(AsyncIO *IO)
 {
-       CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
-       SmtpOutMsg *pMsg = IO->Data;
+       EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
+       SmtpOutMsg *Msg = IO->Data;
        eNextState rc;
 
-       rc = ReadHandlers[pMsg->State](pMsg);
-       pMsg->State++;
-       SMTPSetTimeout(rc, pMsg);
+       rc = ReadHandlers[Msg->State](Msg);
+       if (rc != eAbort)
+       {
+               Msg->State++;
+               SMTPSetTimeout(rc, Msg);
+       }
        return rc;
 }
 eNextState SMTP_C_DispatchWriteDone(AsyncIO *IO)
 {
-       CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
-       SmtpOutMsg *pMsg = IO->Data;
+       EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
+       SmtpOutMsg *Msg = IO->Data;
        eNextState rc;
 
-       rc = SendHandlers[pMsg->State](pMsg);
-       SMTPSetTimeout(rc, pMsg);
+       rc = SendHandlers[Msg->State](Msg);
+       SMTPSetTimeout(rc, Msg);
        return rc;
 }
 
@@ -584,52 +766,96 @@ eNextState SMTP_C_DispatchWriteDone(AsyncIO *IO)
 /*****************************************************************************/
 eNextState SMTP_C_Terminate(AsyncIO *IO)
 {
-       SmtpOutMsg *pMsg = IO->Data;
+       SmtpOutMsg *Msg = IO->Data;
 
-       CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
-       FinalizeMessageSend(pMsg);
-       return eAbort;
+       EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
+       return FinalizeMessageSend(Msg);
+}
+eNextState SMTP_C_TerminateDB(AsyncIO *IO)
+{
+       EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
+       return Terminate(IO);
 }
 eNextState SMTP_C_Timeout(AsyncIO *IO)
 {
-       SmtpOutMsg *pMsg = IO->Data;
-
-       CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
-       StrBufPlain(IO->ErrMsg, CKEY(ReadErrors[pMsg->State]));
-       return FailOneAttempt(IO);
+       SmtpOutMsg *Msg = IO->Data;
+
+       Msg->MyQEntry->Status = 4;
+       EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
+       StrBufPrintf(IO->ErrMsg, "Timeout: %s while talking to %s",
+                    ReadErrors[Msg->State].Key,
+                    Msg->mx_host);
+       if (Msg->State > eRCPT)
+               return eAbort;
+       else
+               return FailOneAttempt(IO);
 }
 eNextState SMTP_C_ConnFail(AsyncIO *IO)
 {
-       SmtpOutMsg *pMsg = IO->Data;
+       SmtpOutMsg *Msg = IO->Data;
+
+       Msg->MyQEntry->Status = 4;
+       EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
+       StrBufPrintf(IO->ErrMsg, "Connection failure: %s while talking to %s",
+                    ReadErrors[Msg->State].Key,
+                    Msg->mx_host);
 
-       CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
-       StrBufPlain(IO->ErrMsg, CKEY(ReadErrors[pMsg->State]));
+       return FailOneAttempt(IO);
+}
+eNextState SMTP_C_DNSFail(AsyncIO *IO)
+{
+       SmtpOutMsg *Msg = IO->Data;
+       Msg->MyQEntry->Status = 4;
+       EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
        return FailOneAttempt(IO);
 }
 eNextState SMTP_C_Shutdown(AsyncIO *IO)
 {
-       CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
-       SmtpOutMsg *pMsg = IO->Data;
+       EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
+       SmtpOutMsg *Msg = IO->Data;
 
-       pMsg->MyQEntry->Status = 3;
-       StrBufPlain(pMsg->MyQEntry->StatusMessage, HKEY("server shutdown during message submit."));
-       FinalizeMessageSend(pMsg);
-       return eAbort;
+       switch (IO->NextState) {
+       case eSendDNSQuery:
+       case eReadDNSReply:
+
+               /* todo: abort c-ares */
+       case eConnect:
+       case eSendReply:
+       case eSendMore:
+       case eSendFile:
+       case eReadMessage:
+       case eReadMore:
+       case eReadPayload:
+       case eReadFile:
+               StopClientWatchers(IO, 1);
+               break;
+       case eDBQuery:
+
+               break;
+       case eTerminateConnection:
+       case eAbort:
+               break;
+       }
+       Msg->MyQEntry->Status = 3;
+       StrBufPlain(Msg->MyQEntry->StatusMessage,
+                   HKEY("server shutdown during message submit."));
+       return FinalizeMessageSend(Msg);
 }
 
 
 /**
- * @brief lineread Handler; understands when to read more SMTP lines, and when this is a one-lined reply.
+ * @brief lineread Handler;
+ * understands when to read more SMTP lines, and when this is a one-lined reply.
  */
 eReadState SMTP_C_ReadServerStatus(AsyncIO *IO)
 {
-       eReadState Finished = eBufferNotEmpty; 
+       eReadState Finished = eBufferNotEmpty;
 
        while (Finished == eBufferNotEmpty) {
                Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
-               
+
                switch (Finished) {
-               case eMustReadMore: /// read new from socket... 
+               case eMustReadMore: /// read new from socket...
                        return Finished;
                        break;
                case eBufferNotEmpty: /* shouldn't happen... */
@@ -638,19 +864,25 @@ eReadState SMTP_C_ReadServerStatus(AsyncIO *IO)
                                continue;
                        if (ChrPtr(IO->IOBuf)[3] == '-')
                                Finished = eBufferNotEmpty;
-                       else 
+                       else
                                return Finished;
                        break;
                case eReadFail: /// WHUT?
-                       ///todo: shut down! 
+                       ///todo: shut down!
                        break;
                }
        }
        return Finished;
 }
 
-#endif
+void LogDebugEnableSMTPClient(const int n)
+{
+       SMTPClientDebugEnabled = n;
+}
+
 CTDL_MODULE_INIT(smtp_eventclient)
 {
+       if (!threading)
+               CtdlRegisterDebugFlagHook(HKEY("smtpeventclient"), LogDebugEnableSMTPClient, &SMTPClientDebugEnabled);
        return "smtpeventclient";
 }