remember the lengths of config strings.
[citadel.git] / citadel / modules / smtp / serv_smtpqueue.c
index b56f40301d4e826c1774ba58a667356b877b0e49..9a618e092ee88731c465c2e2683a397293a2a0f7 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);
@@ -515,12 +518,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 +569,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);
                }
        }
@@ -694,11 +700,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) {
@@ -724,7 +730,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");
 }
 
@@ -826,7 +832,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);
@@ -842,18 +848,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);
 
@@ -937,13 +946,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;
        }
@@ -992,7 +1011,7 @@ void smtp_do_procmsg(long msgnum, void *userdata) {
                                                         n,
                                                         RelayUrls);
 
-                               if (KeepBuffers) HaveBuffers = 1;
+                               if (KeepBuffers) HaveBuffers++;
 
                                i++;
                        }
@@ -1034,7 +1053,7 @@ void smtp_do_procmsg(long msgnum, void *userdata) {
                                                         n,
                                                         RelayUrls);
 
-                               if (KeepBuffers) HaveBuffers = 1;
+                               if (KeepBuffers) HaveBuffers++;
 
                                i++;
                        }
@@ -1186,6 +1205,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)
 {
@@ -1205,8 +1288,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);