Silence logging in non-debug usecases. The raspii users gonna love this.
[citadel.git] / citadel / modules / smtp / serv_smtpqueue.c
index abc4b5d06c9e0fd6d3bc02d62b774ebd45e154b5..a7754616ed720fc46e27b7fcec44515172c6046f 100644 (file)
  * The VRFY and EXPN commands have been removed from this implementation
  * because nobody uses these commands anymore, except for spammers.
  *
- * Copyright (c) 1998-2012 by the citadel.org team
+ * Copyright (c) 1998-2015 by the citadel.org team
  *
- *  This program is open source software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License version 3.
- *  
- *  
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
+ * This program is open source software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3.
  *
- *  
- *  
- *  
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
  */
 
 #include "sysdep.h"
@@ -492,7 +486,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;
@@ -518,12 +513,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;
@@ -569,7 +564,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);
                }
        }
@@ -602,7 +600,7 @@ void smtpq_do_bounce(OneQueItem *MyQItem, StrBuf *OMsgTxt, ParsedURL *Relay)
        boundary = NewStrBufPlain(HKEY("=_Citadel_Multipart_"));
        StrBufAppendPrintf(boundary,
                           "%s_%04x%04x",
-                          config.c_fqdn,
+                          CtdlGetConfigStr("c_fqdn"),
                           getpid(),
                           ++seq);
 
@@ -697,11 +695,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, CtdlGetConfigStr("c_nodename"), strlen(CtdlGetConfigStr("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) {
@@ -721,13 +719,13 @@ void smtpq_do_bounce(OneQueItem *MyQItem, StrBuf *OMsgTxt, ParsedURL *Relay)
 
        /* If not, post it in the Aide> room */
        if (successful_bounce == 0) {
-               CtdlSubmitMsg(bmsg, NULL, config.c_aideroom, QP_EADDR);
+               CtdlSubmitMsg(bmsg, NULL, CtdlGetConfigStr("c_aideroom"), QP_EADDR);
        }
 
        /* Free up the memory we used */
        free_recipients(valid);
        FreeStrBuf(&boundary);
-       CtdlFreeMessage(bmsg);
+       CM_Free(bmsg);
        SMTPCM_syslog(LOG_DEBUG, "Done processing bounces\n");
 }
 
@@ -838,25 +836,28 @@ void smtp_do_procmsg(long msgnum, void *userdata) {
        SMTPC_syslog(LOG_DEBUG, "smtp_do_procmsg(%ld)\n", msgnum);
        ///strcpy(envelope_from, "");
 
-       msg = CtdlFetchMessage(msgnum, 1);
+       msg = CtdlFetchMessage(msgnum, 1, 1);
        if (msg == NULL) {
                SMTPC_syslog(LOG_ERR, "tried %ld but no such message!\n",
                       msgnum);
                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);
 
@@ -1112,7 +1113,7 @@ void smtp_do_queue(void) {
        int num_activated = 0;
 
        pthread_setspecific(MyConKey, (void *)&smtp_queue_CC);
-       SMTPCM_syslog(LOG_INFO, "processing outbound queue");
+       SMTPCM_syslog(LOG_DEBUG, "processing outbound queue");
 
        if (CtdlGetRoom(&CC->room, SMTP_SPOOLOUT_ROOM) != 0) {
                SMTPC_syslog(LOG_ERR, "Cannot find room <%s>", SMTP_SPOOLOUT_ROOM);
@@ -1126,9 +1127,11 @@ void smtp_do_queue(void) {
                                                   smtp_do_procmsg,
                                                   &num_activated);
        }
-       SMTPC_syslog(LOG_INFO,
-                    "queue run completed; %d messages processed %d activated",
-                    num_processed, num_activated);
+       if (num_activated > 0) {
+               SMTPC_syslog(LOG_INFO,
+                            "queue run completed; %d messages processed %d activated",
+                            num_processed, num_activated);
+       }
 
 }
 
@@ -1199,6 +1202,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)
 {
@@ -1218,8 +1285,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);