SMTP-Client: add the stati of all delivery attempts to the status messages.
[citadel.git] / citadel / modules / smtp / serv_smtpqueue.c
index b874d1268452870449d405922989cb31d4a4f1b8..fb74ed2ad76d7afc6b31bb0a5e1c133701c79857 100644 (file)
@@ -94,6 +94,7 @@ struct CitContext smtp_queue_CC;
 pthread_mutex_t ActiveQItemsLock;
 HashList *ActiveQItems  = NULL;
 HashList *QItemHandlers = NULL;
+const unsigned short DefaultMXPort = 25;
 int max_sessions_for_outbound_smtp = 500; /* how many sessions might be active till we stop adding more smtp jobs */
 int ndelay_count = 50; /* every n queued messages we will sleep... */
 int delay_msec = 5000; /* this many seconds. */
@@ -193,7 +194,7 @@ void FreeMailQEntry(void *qv)
 */
        FreeStrBuf(&Q->Recipient);
        FreeStrBuf(&Q->StatusMessage);
-
+       FreeStrBuf(&Q->AllStatusMessages);
        memset(Q, 0, sizeof(MailQEntry));
        free(Q);
 }
@@ -245,7 +246,7 @@ int CheckQEntryIsBounce(MailQEntry *ThisItem)
                return 0;
 }      
 
-int CountActiveQueueEntries(OneQueItem *MyQItem)
+int CountActiveQueueEntries(OneQueItem *MyQItem, int before)
 {
        HashPos  *It;
        long len;
@@ -257,15 +258,20 @@ int CountActiveQueueEntries(OneQueItem *MyQItem)
        It = GetNewHashPos(MyQItem->MailQEntries, 0);
        while (GetNextHashPos(MyQItem->MailQEntries, It, &len, &Key, &vQE))
        {
+               int Active;
                MailQEntry *ThisItem = vQE;
 
                if (CheckQEntryActive(ThisItem))
                {
                        ActiveDeliveries++;
-                       ThisItem->Active = 1;
+                       Active = 1;
                }
                else
-                       ThisItem->Active = 0;
+                       Active = 0;
+               if (before)
+                       ThisItem->Active = Active;
+               else
+                       ThisItem->StillActive = Active;
        }
        DeleteHashPos(&It);
        return ActiveDeliveries;
@@ -379,7 +385,10 @@ StrBuf *SerializeQueueItem(OneQueItem *MyQItem)
                StrBufAppendBufPlain(QMessage, HKEY("|"), 0);
                StrBufAppendPrintf(QMessage, "%d", ThisItem->Status);
                StrBufAppendBufPlain(QMessage, HKEY("|"), 0);
-               StrBufAppendBuf(QMessage, ThisItem->StatusMessage, 0);
+               if (ThisItem->AllStatusMessages != NULL)
+                       StrBufAppendBuf(QMessage, ThisItem->AllStatusMessages, 0);
+               else
+                       StrBufAppendBuf(QMessage, ThisItem->StatusMessage, 0);
        }
        DeleteHashPos(&It);
        StrBufAppendBufPlain(QMessage, HKEY("\n"), 0);
@@ -472,7 +481,7 @@ void QItem_Handle_Attempted(OneQueItem *Item, StrBuf *Line, const char **Pos)
 /**
  * this one has to have the context for loading the message via the redirect buffer...
  */
-StrBuf *smtp_load_msg(OneQueItem *MyQItem, int n)
+StrBuf *smtp_load_msg(OneQueItem *MyQItem, int n, char **Author, char **Address)
 {
        CitContext *CCC=CC;
        StrBuf *SendMsg;
@@ -481,7 +490,9 @@ StrBuf *smtp_load_msg(OneQueItem *MyQItem, int n)
        CtdlOutputMsg(MyQItem->MessageID,
                      MT_RFC822, HEADERS_ALL,
                      0, 1, NULL,
-                     (ESC_DOT|SUPPRESS_ENV_TO) );
+                     (ESC_DOT|SUPPRESS_ENV_TO),
+                     Author,
+                     Address);
 
        SendMsg = CCC->redirect_buffer;
        CCC->redirect_buffer = NULL;
@@ -504,7 +515,7 @@ StrBuf *smtp_load_msg(OneQueItem *MyQItem, int n)
  * instructions for "5" codes (permanent fatal errors) and produce/deliver
  * a "bounce" message (delivery status notification).
  */
-void smtpq_do_bounce(OneQueItem *MyQItem, StrBuf *OMsgTxt)
+void smtpq_do_bounce(OneQueItem *MyQItem, StrBuf *OMsgTxt, ParsedURL *Relay)
 {
        static int seq = 0;
 
@@ -555,6 +566,7 @@ void smtpq_do_bounce(OneQueItem *MyQItem, StrBuf *OMsgTxt)
                {
                        ++num_bounces;
 
+                       StrBufAppendBufPlain(Msg, HKEY(" "), 0);
                        StrBufAppendBuf(Msg, ThisItem->Recipient, 0);
                        StrBufAppendBufPlain(Msg, HKEY(": "), 0);
                        StrBufAppendBuf(Msg, ThisItem->StatusMessage, 0);
@@ -571,6 +583,22 @@ void smtpq_do_bounce(OneQueItem *MyQItem, StrBuf *OMsgTxt)
                return;
        }
 
+       if ((StrLength(MyQItem->SenderRoom) == 0) && MyQItem->HaveRelay) {
+               const char *RelayUrlStr = "[not found]";
+               /* one message that relaying is broken is enough; no extra room error message. */
+               StrBuf *RelayDetails = NewStrBuf();
+
+               if (Relay != NULL)
+                       RelayUrlStr = ChrPtr(Relay->URL);
+
+               StrBufPrintf(RelayDetails,
+                            "Relaying via %s failed permanently. \n Reason:\n%s\n Revalidate your relay configuration.",
+                            RelayUrlStr,
+                            ChrPtr(Msg));
+                CtdlAideMessage(ChrPtr(RelayDetails), "Relaying Failed");
+               FreeStrBuf(&RelayDetails);
+       }
+
        boundary = NewStrBufPlain(HKEY("=_Citadel_Multipart_"));
        StrBufAppendPrintf(boundary,
                           "%s_%04x%04x",
@@ -703,6 +731,81 @@ void smtpq_do_bounce(OneQueItem *MyQItem, StrBuf *OMsgTxt)
        SMTPCM_syslog(LOG_DEBUG, "Done processing bounces\n");
 }
 
+ParsedURL *LoadRelayUrls(OneQueItem *MyQItem,
+                        char *Author,
+                        char *Address)
+{
+       int nRelays = 0;
+       ParsedURL *RelayUrls = NULL;
+       char mxbuf[SIZ];
+       ParsedURL **Url = &MyQItem->URL;
+
+       nRelays = get_hosts(mxbuf, "fallbackhost");
+       if (nRelays > 0) {
+               StrBuf *All;
+               StrBuf *One;
+               const char *Pos = NULL;
+               All = NewStrBufPlain(mxbuf, -1);
+               One = NewStrBufPlain(NULL, StrLength(All) + 1);
+               
+               while ((Pos != StrBufNOTNULL) &&
+                      ((Pos == NULL) ||
+                       !IsEmptyStr(Pos)))
+               {
+                       StrBufExtract_NextToken(One, All, &Pos, '|');
+                       if (!ParseURL(Url, One, DefaultMXPort)) {
+                               SMTPC_syslog(LOG_DEBUG,
+                                            "Failed to parse: %s\n",
+                                            ChrPtr(One));
+                       }
+                       else {
+                               (*Url)->IsRelay = 1;
+                               MyQItem->HaveRelay = 1;
+                       }
+               }
+               FreeStrBuf(&All);
+               FreeStrBuf(&One);
+       }
+       nRelays = get_hosts(mxbuf, "smarthost");
+       if (nRelays > 0) {
+               char *User;
+               StrBuf *All;
+               StrBuf *One;
+               const char *Pos = NULL;
+               All = NewStrBufPlain(mxbuf, -1);
+               One = NewStrBufPlain(NULL, StrLength(All) + 1);
+               
+               while ((Pos != StrBufNOTNULL) &&
+                      ((Pos == NULL) ||
+                       !IsEmptyStr(Pos)))
+               {
+                       StrBufExtract_NextToken(One, All, &Pos, '|');
+                       User = strchr(ChrPtr(One), ' ');
+                       if (User != NULL) {
+                               if (!strcmp(User + 1, Author) ||
+                                   !strcmp(User + 1, Address))
+                                       StrBufCutAt(One, 0, User);
+                               else {
+                                       MyQItem->HaveRelay = 1;
+                                       continue;
+                               }
+                       }
+                       if (!ParseURL(Url, One, DefaultMXPort)) {
+                               SMTPC_syslog(LOG_DEBUG,
+                                            "Failed to parse: %s\n",
+                                            ChrPtr(One));
+                       }
+                       else {
+                               ///if (!Url->IsIP)) // todo dupe me fork ipv6
+                               (*Url)->IsRelay = 1;
+                               MyQItem->HaveRelay = 1;
+                       }
+               }
+               FreeStrBuf(&All);
+               FreeStrBuf(&One);
+       }
+       return RelayUrls;
+}
 /*
  * smtp_do_procmsg()
  *
@@ -712,6 +815,8 @@ void smtp_do_procmsg(long msgnum, void *userdata) {
        time_t now;
        int mynumsessions = num_sessions;
        struct CtdlMessage *msg = NULL;
+       char *Author = NULL;
+       char *Address = NULL;
        char *instr = NULL;
        StrBuf *PlainQItem;
        OneQueItem *MyQItem;
@@ -720,8 +825,6 @@ void smtp_do_procmsg(long msgnum, void *userdata) {
        void *vQE;
        long len;
        const char *Key;
-       int nRelays = 0;
-       ParsedURL *RelayUrls = NULL;
        int HaveBuffers = 0;
        StrBuf *Msg =NULL;
 
@@ -813,62 +916,6 @@ void smtp_do_procmsg(long msgnum, void *userdata) {
                return;
        }
 
-       {
-               char mxbuf[SIZ];
-               ParsedURL **Url = &MyQItem->URL;
-               nRelays = get_hosts(mxbuf, "smarthost");
-               if (nRelays > 0) {
-                       StrBuf *All;
-                       StrBuf *One;
-                       const char *Pos = NULL;
-                       All = NewStrBufPlain(mxbuf, -1);
-                       One = NewStrBufPlain(NULL, StrLength(All) + 1);
-
-                       while ((Pos != StrBufNOTNULL) &&
-                              ((Pos == NULL) ||
-                               !IsEmptyStr(Pos)))
-                       {
-                               StrBufExtract_NextToken(One, All, &Pos, '|');
-                               if (!ParseURL(Url, One, 25)) {
-                                       SMTPC_syslog(LOG_DEBUG,
-                                                    "Failed to parse: %s\n",
-                                                    ChrPtr(One));
-                               }
-                               else {
-                                       ///if (!Url->IsIP)) // todo dupe me fork ipv6
-                                       Url = &(*Url)->Next;
-                               }
-                       }
-                       FreeStrBuf(&All);
-                       FreeStrBuf(&One);
-               }
-
-               Url = &MyQItem->FallBackHost;
-               nRelays = get_hosts(mxbuf, "fallbackhost");
-               if (nRelays > 0) {
-                       StrBuf *All;
-                       StrBuf *One;
-                       const char *Pos = NULL;
-                       All = NewStrBufPlain(mxbuf, -1);
-                       One = NewStrBufPlain(NULL, StrLength(All) + 1);
-
-                       while ((Pos != StrBufNOTNULL) &&
-                              ((Pos == NULL) ||
-                               !IsEmptyStr(Pos)))
-                       {
-                               StrBufExtract_NextToken(One, All, &Pos, '|');
-                               if (!ParseURL(Url, One, 25)) {
-                                       SMTPC_syslog(LOG_DEBUG,
-                                                    "Failed to parse: %s\n",
-                                                    ChrPtr(One));
-                               }
-                               else
-                                       Url = &(*Url)->Next;
-                       }
-                       FreeStrBuf(&All);
-                       FreeStrBuf(&One);
-               }
-       }
 
        It = GetNewHashPos(MyQItem->MailQEntries, 0);
        while (GetNextHashPos(MyQItem->MailQEntries, It, &len, &Key, &vQE))
@@ -881,7 +928,7 @@ void smtp_do_procmsg(long msgnum, void *userdata) {
        DeleteHashPos(&It);
 
        MyQItem->NotYetShutdownDeliveries = 
-       MyQItem->ActiveDeliveries = CountActiveQueueEntries(MyQItem);
+               MyQItem->ActiveDeliveries = CountActiveQueueEntries(MyQItem, 1);
 
        /* failsafe against overload: 
         * will we exceed the limit set? 
@@ -907,12 +954,60 @@ void smtp_do_procmsg(long msgnum, void *userdata) {
 
        if (MyQItem->ActiveDeliveries > 0)
        {
+               ParsedURL *RelayUrls = NULL;
                int nActivated = 0;
                int n = MsgCount++;
                int m = MyQItem->ActiveDeliveries;
                int i = 1;
-               Msg = smtp_load_msg(MyQItem, n);
+
                It = GetNewHashPos(MyQItem->MailQEntries, 0);
+
+               Msg = smtp_load_msg(MyQItem, n, &Author, &Address);
+               RelayUrls = LoadRelayUrls(MyQItem, Author, Address);
+               if ((RelayUrls == NULL) && MyQItem->HaveRelay) {
+
+                       while ((i <= m) &&
+                              (GetNextHashPos(MyQItem->MailQEntries,
+                                              It, &len, &Key, &vQE)))
+                       {
+                               int KeepBuffers = (i == m);
+                               MailQEntry *ThisItem = vQE;
+                               StrBufPrintf(ThisItem->StatusMessage,
+                                            "No relay configured matching %s / %s", 
+                                            (Author != NULL)? Author : "",
+                                            (Address != NULL)? Address : "");
+                               ThisItem->Status = 5;
+
+                               nActivated++;
+
+                               if (i > 1) n = MsgCount++;
+                               syslog(LOG_INFO,
+                                      "SMTPC: giving up on <%ld> <%s> %d / %d \n",
+                                      MyQItem->MessageID,
+                                      ChrPtr(ThisItem->Recipient),
+                                      i,
+                                      m);
+                               (*((int*) userdata)) ++;
+                               smtp_try_one_queue_entry(MyQItem,
+                                                        ThisItem,
+                                                        Msg,
+                                                        KeepBuffers,
+                                                        n,
+                                                        RelayUrls);
+
+                               if (KeepBuffers) HaveBuffers = 1;
+
+                               i++;
+                       }
+                       if (Author != NULL) free (Author);
+                       if (Address != NULL) free (Address);
+                       DeleteHashPos(&It);
+
+                       return;
+               }
+               if (Author != NULL) free (Author);
+               if (Address != NULL) free (Address);
+
                while ((i <= m) &&
                       (GetNextHashPos(MyQItem->MailQEntries,
                                       It, &len, &Key, &vQE)))
@@ -1136,7 +1231,7 @@ CTDL_MODULE_INIT(smtp_queu)
                CtdlRegisterEVCleanupHook(smtp_evq_cleanup);
 
                CtdlRegisterProtoHook(cmd_smtp, "SMTP", "SMTP utility commands");
-               CtdlRegisterSessionHook(smtp_do_queue, EVT_TIMER);
+               CtdlRegisterSessionHook(smtp_do_queue, EVT_TIMER, PRIO_SEND + 10);
        }
 
        /* return our Subversion id for the Log */