SMPC: when aborting because of to many outbound job free the aborted message in the...
[citadel.git] / citadel / modules / smtp / serv_smtpqueue.c
index 3c4c2cb3e5c7b53d121c676e8d36fd79835c934f..c128fd49026fe728627dbe8d147b4fb4b3959bdb 100644 (file)
@@ -194,7 +194,7 @@ void FreeMailQEntry(void *qv)
 */
        FreeStrBuf(&Q->Recipient);
        FreeStrBuf(&Q->StatusMessage);
-
+       FreeStrBuf(&Q->AllStatusMessages);
        memset(Q, 0, sizeof(MailQEntry));
        free(Q);
 }
@@ -246,7 +246,7 @@ int CheckQEntryIsBounce(MailQEntry *ThisItem)
                return 0;
 }      
 
-int CountActiveQueueEntries(OneQueItem *MyQItem)
+int CountActiveQueueEntries(OneQueItem *MyQItem, int before)
 {
        HashPos  *It;
        long len;
@@ -258,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;
@@ -380,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);
@@ -507,7 +515,7 @@ StrBuf *smtp_load_msg(OneQueItem *MyQItem, int n, char **Author, char **Address)
  * 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;
 
@@ -558,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);
@@ -574,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",
@@ -760,9 +785,10 @@ ParsedURL *LoadRelayUrls(OneQueItem *MyQItem,
                                if (!strcmp(User + 1, Author) ||
                                    !strcmp(User + 1, Address))
                                        StrBufCutAt(One, 0, User);
-                               else
+                               else {
+                                       MyQItem->HaveRelay = 1;
                                        continue;
-
+                               }
                        }
                        if (!ParseURL(Url, One, DefaultMXPort)) {
                                SMTPC_syslog(LOG_DEBUG,
@@ -803,7 +829,7 @@ void smtp_do_procmsg(long msgnum, void *userdata) {
        StrBuf *Msg =NULL;
 
        if (mynumsessions > max_sessions_for_outbound_smtp) {
-               SMTPC_syslog(LOG_DEBUG,
+               SMTPC_syslog(LOG_INFO,
                             "skipping because of num jobs %d > %d max_sessions_for_outbound_smtp",
                             mynumsessions,
                             max_sessions_for_outbound_smtp);
@@ -902,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? 
@@ -914,13 +940,23 @@ void smtp_do_procmsg(long msgnum, void *userdata) {
            (((MyQItem->ActiveDeliveries * 2)  < max_sessions_for_outbound_smtp)))
        {
                /* abort delivery for another time. */
-               SMTPC_syslog(LOG_DEBUG,
+               SMTPC_syslog(LOG_INFO,
                             "SMTP Queue: skipping because of num jobs %d + %ld > %d max_sessions_for_outbound_smtp",
                             mynumsessions,
                             MyQItem->ActiveDeliveries,
                             max_sessions_for_outbound_smtp);
 
-               FreeQueItem(&MyQItem);
+               It = GetNewHashPos(MyQItem->MailQEntries, 0);
+               pthread_mutex_lock(&ActiveQItemsLock);
+               {
+                       if (GetHashPosFromKey(ActiveQItems,
+                                             LKEY(MyQItem->MessageID),
+                                             It))
+                       {
+                               DeleteEntryFromHash(ActiveQItems, It);
+                       }
+               }
+               pthread_mutex_unlock(&ActiveQItemsLock);
 
                return;
        }
@@ -934,12 +970,54 @@ void smtp_do_procmsg(long msgnum, void *userdata) {
                int m = MyQItem->ActiveDeliveries;
                int i = 1;
 
+               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);
 
-               It = GetNewHashPos(MyQItem->MailQEntries, 0);
                while ((i <= m) &&
                       (GetNextHashPos(MyQItem->MailQEntries,
                                       It, &len, &Key, &vQE)))