Added message-id to the list of fields CtdlOutputMsg() can potentially return to...
[citadel.git] / citadel / modules / smtp / serv_smtpqueue.c
index cd684c9ff01784ee28877a7a68f7b6167eaa9112..df04bdcc5f713f66ebdb138c9d2bc73ab1067458 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);
 }
@@ -385,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);
@@ -489,7 +492,8 @@ StrBuf *smtp_load_msg(OneQueItem *MyQItem, int n, char **Author, char **Address)
                      0, 1, NULL,
                      (ESC_DOT|SUPPRESS_ENV_TO),
                      Author,
-                     Address);
+                     Address,
+                       NULL);
 
        SendMsg = CCC->redirect_buffer;
        CCC->redirect_buffer = NULL;
@@ -515,12 +519,12 @@ StrBuf *smtp_load_msg(OneQueItem *MyQItem, int n, char **Author, char **Address)
 void smtpq_do_bounce(OneQueItem *MyQItem, StrBuf *OMsgTxt, ParsedURL *Relay)
 {
        static int seq = 0;
-
+       
        struct CtdlMessage *bmsg = NULL;
        StrBuf *boundary;
        StrBuf *Msg = NULL;
        StrBuf *BounceMB;
-       struct recptypes *valid;
+       recptypes *valid;
        time_t now;
 
        HashPos *It;
@@ -566,7 +570,10 @@ void smtpq_do_bounce(OneQueItem *MyQItem, StrBuf *OMsgTxt, ParsedURL *Relay)
                        StrBufAppendBufPlain(Msg, HKEY(" "), 0);
                        StrBufAppendBuf(Msg, ThisItem->Recipient, 0);
                        StrBufAppendBufPlain(Msg, HKEY(": "), 0);
-                       StrBufAppendBuf(Msg, ThisItem->StatusMessage, 0);
+                       if (ThisItem->AllStatusMessages != NULL)
+                               StrBufAppendBuf(Msg, ThisItem->AllStatusMessages, 0);
+                       else
+                               StrBufAppendBuf(Msg, ThisItem->StatusMessage, 0);
                        StrBufAppendBufPlain(Msg, HKEY("\r\n"), 0);
                }
        }
@@ -581,12 +588,16 @@ void smtpq_do_bounce(OneQueItem *MyQItem, StrBuf *OMsgTxt, ParsedURL *Relay)
        }
 
        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.",
-                            ChrPtr(Relay->URL),
+                            RelayUrlStr,
                             ChrPtr(Msg));
                 CtdlAideMessage(ChrPtr(RelayDetails), "Relaying Failed");
                FreeStrBuf(&RelayDetails);
@@ -690,11 +701,11 @@ void smtpq_do_bounce(OneQueItem *MyQItem, StrBuf *OMsgTxt, ParsedURL *Relay)
        bmsg->cm_anon_type = MES_NORMAL;
        bmsg->cm_format_type = FMT_RFC822;
 
-       bmsg->cm_fields['O'] = strdup(MAILROOM);
-       bmsg->cm_fields['A'] = strdup("Citadel");
-       bmsg->cm_fields['N'] = strdup(config.c_nodename);
-       bmsg->cm_fields['U'] = strdup("Delivery Status Notification (Failure)");
-       bmsg->cm_fields['M'] = SmashStrBuf(&BounceMB);
+       CM_SetField(bmsg, eOriginalRoom, HKEY(MAILROOM));
+       CM_SetField(bmsg, eAuthor, HKEY("Citadel"));
+       CM_SetField(bmsg, eNodeName, CFG_KEY(c_nodename));
+       CM_SetField(bmsg, eMsgSubject, HKEY("Delivery Status Notification (Failure)"));
+       CM_SetAsFieldSB(bmsg, eMesageText, &BounceMB);
 
        /* First try the user who sent the message */
        if (StrLength(MyQItem->BounceTo) == 0) {
@@ -720,7 +731,7 @@ void smtpq_do_bounce(OneQueItem *MyQItem, StrBuf *OMsgTxt, ParsedURL *Relay)
        /* Free up the memory we used */
        free_recipients(valid);
        FreeStrBuf(&boundary);
-       CtdlFreeMessage(bmsg);
+       CM_Free(bmsg);
        SMTPCM_syslog(LOG_DEBUG, "Done processing bounces\n");
 }
 
@@ -778,9 +789,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,
@@ -821,7 +833,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);
@@ -837,18 +849,21 @@ void smtp_do_procmsg(long msgnum, void *userdata) {
                return;
        }
 
-       pch = instr = msg->cm_fields['M'];
+       pch = instr = msg->cm_fields[eMesageText];
 
        /* Strip out the headers (no not amd any other non-instruction) line */
        while (pch != NULL) {
                pch = strchr(pch, '\n');
-               if ((pch != NULL) && (*(pch + 1) == '\n')) {
+               if ((pch != NULL) &&
+                   ((*(pch + 1) == '\n') ||
+                    (*(pch + 1) == '\r')))
+               {
                        instr = pch + 2;
                        pch = NULL;
                }
        }
        PlainQItem = NewStrBufPlain(instr, -1);
-       CtdlFreeMessage(msg);
+       CM_Free(msg);
        MyQItem = DeserializeQueueItem(PlainQItem, msgnum);
        FreeStrBuf(&PlainQItem);
 
@@ -932,13 +947,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;
        }
@@ -952,12 +977,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++;
+
+                               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)))
@@ -987,7 +1054,7 @@ void smtp_do_procmsg(long msgnum, void *userdata) {
                                                         n,
                                                         RelayUrls);
 
-                               if (KeepBuffers) HaveBuffers = 1;
+                               if (KeepBuffers) HaveBuffers++;
 
                                i++;
                        }
@@ -1139,6 +1206,70 @@ void cmd_smtp(char *argbuf) {
 
 }
 
+int smtp_aftersave(struct CtdlMessage *msg,
+                  recptypes *recps)
+{
+       /* For internet mail, generate delivery instructions.
+        * Yes, this is recursive.  Deal with it.  Infinite recursion does
+        * not happen because the delivery instructions message does not
+        * contain a recipient.
+        */
+       if ((recps != NULL) && (recps->num_internet > 0)) {
+               struct CtdlMessage *imsg = NULL;
+               char recipient[SIZ];
+               CitContext *CCC = MyContext();
+               StrBuf *SpoolMsg = NewStrBuf();
+               long nTokens;
+               int i;
+
+               MSGM_syslog(LOG_DEBUG, "Generating delivery instructions\n");
+
+               StrBufPrintf(SpoolMsg,
+                            "Content-type: "SPOOLMIME"\n"
+                            "\n"
+                            "msgid|%s\n"
+                            "submitted|%ld\n"
+                            "bounceto|%s\n",
+                            msg->cm_fields[eVltMsgNum],
+                            (long)time(NULL),
+                            recps->bounce_to);
+
+               if (recps->envelope_from != NULL) {
+                       StrBufAppendBufPlain(SpoolMsg, HKEY("envelope_from|"), 0);
+                       StrBufAppendBufPlain(SpoolMsg, recps->envelope_from, -1, 0);
+                       StrBufAppendBufPlain(SpoolMsg, HKEY("\n"), 0);
+               }
+               if (recps->sending_room != NULL) {
+                       StrBufAppendBufPlain(SpoolMsg, HKEY("source_room|"), 0);
+                       StrBufAppendBufPlain(SpoolMsg, recps->sending_room, -1, 0);
+                       StrBufAppendBufPlain(SpoolMsg, HKEY("\n"), 0);
+               }
+
+               nTokens = num_tokens(recps->recp_internet, '|');
+               for (i = 0; i < nTokens; i++) {
+                       long len;
+                       len = extract_token(recipient, recps->recp_internet, i, '|', sizeof recipient);
+                       if (len > 0) {
+                               StrBufAppendBufPlain(SpoolMsg, HKEY("remote|"), 0);
+                               StrBufAppendBufPlain(SpoolMsg, recipient, len, 0);
+                               StrBufAppendBufPlain(SpoolMsg, HKEY("|0||\n"), 0);
+                       }
+               }
+
+               imsg = malloc(sizeof(struct CtdlMessage));
+               memset(imsg, 0, sizeof(struct CtdlMessage));
+               imsg->cm_magic = CTDLMESSAGE_MAGIC;
+               imsg->cm_anon_type = MES_NORMAL;
+               imsg->cm_format_type = FMT_RFC822;
+               CM_SetField(imsg, eMsgSubject, HKEY("QMSG"));
+               CM_SetField(imsg, eAuthor, HKEY("Citadel"));
+               CM_SetField(imsg, eJournal, HKEY("do not journal"));
+               CM_SetAsFieldSB(imsg, eMesageText, &SpoolMsg);
+               CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM, QP_EADDR);
+               CM_Free(imsg);
+       }
+       return 0;
+}
 
 CTDL_MODULE_INIT(smtp_queu)
 {
@@ -1158,8 +1289,7 @@ CTDL_MODULE_INIT(smtp_queu)
                if ((pstr != NULL) && (*pstr != '\0'))
                        delay_msec = atol(pstr) * 1000; /* this many seconds. */
 
-
-
+               CtdlRegisterMessageHook(smtp_aftersave, EVT_AFTERSAVE);
 
                CtdlFillSystemContext(&smtp_queue_CC, "SMTP_Send");
                ActiveQItems = NewHash(1, lFlathash);