SMTP-Client: fix trigger when to send bounce messages
[citadel.git] / citadel / modules / smtp / serv_smtpeventclient.c
index f08efde76e61ce1419281bf33267167ccaddf67c..e85dfe411f1f96c2a90b6c4cd1cb66faf2dcc043 100644 (file)
 #include "smtp_clienthandlers.h"
 
 int SMTPClientDebugEnabled = 0;
-const unsigned short DefaultMXPort = 25;
 void DeleteSmtpOutMsg(void *v)
 {
        SmtpOutMsg *Msg = v;
        AsyncIO *IO = &Msg->IO;
-       EVS_syslog(LOG_DEBUG, "%s Exit\n", __FUNCTION__);
+       EV_syslog(LOG_DEBUG, "%s Exit\n", __FUNCTION__);
 
        /* these are kept in our own space and free'd below */
        Msg->IO.ConnectMe = NULL;
@@ -154,9 +153,10 @@ eNextState FinalizeMessageSend_DB(AsyncIO *IO)
 
        Msg->IDestructQueItem = DecreaseQReference(Msg->MyQItem);
 
-       Msg->nRemain = CountActiveQueueEntries(Msg->MyQItem);
+       Msg->nRemain = CountActiveQueueEntries(Msg->MyQItem, 0);
 
        if (Msg->MyQEntry->Active && 
+           !Msg->MyQEntry->StillActive &&
            CheckQEntryIsBounce(Msg->MyQEntry))
        {
                /* are we casue for a bounce mail? */
@@ -234,10 +234,17 @@ eNextState FailOneAttempt(AsyncIO *IO)
         * - connection timeout
         * - dns lookup failed
         */
-       StopClientWatchers(IO);
+       StopClientWatchers(IO, 1);
 
        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;
+       }
 
        if (Msg->pCurrRelay == NULL) {
                EVS_syslog(LOG_DEBUG, "%s Aborting\n", __FUNCTION__);
@@ -322,6 +329,8 @@ eNextState get_one_mx_host_ip_done(AsyncIO *IO)
        SmtpOutMsg *Msg = IO->Data;
        struct hostent *hostent;
 
+       IO->ConnectMe = Msg->pCurrRelay;
+
        QueryCbDone(IO);
        EVS_syslog(LOG_DEBUG, "%s Time[%fs]\n",
                   __FUNCTION__,
@@ -339,7 +348,7 @@ eNextState get_one_mx_host_ip_done(AsyncIO *IO)
                        Msg->pCurrRelay->Addr.sin6_family =
                                hostent->h_addrtype;
                        Msg->pCurrRelay->Addr.sin6_port =
-                               htons(DefaultMXPort);
+                               htons(Msg->IO.ConnectMe->Port);
                }
                else {
                        struct sockaddr_in *addr;
@@ -357,7 +366,7 @@ eNextState get_one_mx_host_ip_done(AsyncIO *IO)
                               sizeof(uint32_t));
 
                        addr->sin_family = hostent->h_addrtype;
-                       addr->sin_port   = htons(DefaultMXPort);
+                       addr->sin_port   = htons(Msg->IO.ConnectMe->Port);
                }
                Msg->mx_host = Msg->pCurrRelay->Host;
                if (Msg->HostLookup.VParsedDNSReply != NULL) {
@@ -608,7 +617,7 @@ void smtp_try_one_queue_entry(OneQueItem *MyQItem,
        }
        else {
                /* No recipients? well fail then. */
-               if (Msg->MyQEntry == NULL) {
+               if (Msg->MyQEntry != NULL) {
                        Msg->MyQEntry->Status = 5;
                        StrBufPlain(Msg->MyQEntry->StatusMessage,
                                    HKEY("Invalid Recipient!"));
@@ -743,6 +752,28 @@ eNextState SMTP_C_Shutdown(AsyncIO *IO)
        EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
        SmtpOutMsg *Msg = IO->Data;
 
+       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."));