EVENT Client: parametrize when to close the fd on detach or not
[citadel.git] / citadel / modules / smtp / serv_smtpeventclient.c
index 0d3428fea894f5ed529b75e2b50254875ad2064d..56deb4f5034f1785952ea0e6cf6ca4b6830bfdb9 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;
@@ -203,14 +202,16 @@ eNextState FinalizeMessageSend_DB(AsyncIO *IO)
        }
 
        RemoveContext(Msg->IO.CitContext);
-       if (Msg->IDestructQueItem)
-               RemoveQItem(Msg->MyQItem);
        return eAbort;
 }
 
 eNextState Terminate(AsyncIO *IO)
 {
        SmtpOutMsg *Msg = IO->Data;
+
+       if (Msg->IDestructQueItem)
+               RemoveQItem(Msg->MyQItem);
+
        DeleteSmtpOutMsg(Msg);
        return eAbort;
 }
@@ -232,7 +233,7 @@ eNextState FailOneAttempt(AsyncIO *IO)
         * - connection timeout
         * - dns lookup failed
         */
-       StopClientWatchers(IO);
+       StopClientWatchers(IO, 1);
 
        if (Msg->pCurrRelay != NULL)
                Msg->pCurrRelay = Msg->pCurrRelay->Next;
@@ -320,6 +321,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__,
@@ -337,7 +340,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;
@@ -355,7 +358,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) {
@@ -528,6 +531,8 @@ SmtpOutMsg *new_smtp_outmsg(OneQueItem *MyQItem,
        SmtpOutMsg * Msg;
 
        Msg = (SmtpOutMsg *) malloc(sizeof(SmtpOutMsg));
+       if (Msg == NULL)
+               return NULL;
        memset(Msg, 0, sizeof(SmtpOutMsg));
 
        Msg->n                = MsgCount;
@@ -565,6 +570,12 @@ void smtp_try_one_queue_entry(OneQueItem *MyQItem,
        SMTPC_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
 
        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);
 
@@ -598,8 +609,7 @@ void smtp_try_one_queue_entry(OneQueItem *MyQItem,
        }
        else {
                /* No recipients? well fail then. */
-               if ((Msg==NULL) ||
-                   (Msg->MyQEntry == NULL)) {
+               if (Msg->MyQEntry != NULL) {
                        Msg->MyQEntry->Status = 5;
                        StrBufPlain(Msg->MyQEntry->StatusMessage,
                                    HKEY("Invalid Recipient!"));