Removed an unused parameter from CtdlSubmitMsg(). Why was it even there?
[citadel.git] / citadel / journaling.c
index ec158b43c80999e120a1d02669002900aa6d3acb..375589750db5e8a83e20ce94f1591409bf4e64a6 100644 (file)
@@ -1,54 +1,25 @@
 /*
- * $Id:  $
- *
  * Message journaling functions.
  *
+ * Copyright (c) 1987-2020 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.
  */
 
-#include "sysdep.h"
-#include <stdlib.h>
-#include <unistd.h>
 #include <stdio.h>
-#include <fcntl.h>
-
-#if TIME_WITH_SYS_TIME
-# include <sys/time.h>
-# include <time.h>
-#else
-# if HAVE_SYS_TIME_H
-#  include <sys/time.h>
-# else
-#  include <time.h>
-# endif
-#endif
-
-
-#include <ctype.h>
-#include <string.h>
-#include <limits.h>
-#include <errno.h>
-#include <stdarg.h>
-#include <sys/stat.h>
-#include "citadel.h"
-#include "server.h"
-#include "serv_extensions.h"
-#include "database.h"
-#include "msgbase.h"
-#include "support.h"
-#include "sysdep_decls.h"
+#include <libcitadel.h>
+#include "ctdl_module.h"
 #include "citserver.h"
-#include "room_ops.h"
-#include "user_ops.h"
-#include "file_ops.h"
 #include "config.h"
-#include "control.h"
-#include "tools.h"
-#include "mime_parser.h"
-#include "html.h"
-#include "genstamp.h"
+#include "user_ops.h"
+#include "serv_vcard.h"                        /* Needed for vcard_getuser and extract_inet_email_addrs */
 #include "internet_addressing.h"
-#include "vcard.h"
-#include "serv_vcard.h"
 #include "journaling.h"
 
 struct jnlq *jnlq = NULL;      /* journal queue */
@@ -57,30 +28,29 @@ struct jnlq *jnlq = NULL;   /* journal queue */
  * Hand off a copy of a message to be journalized.
  */
 void JournalBackgroundSubmit(struct CtdlMessage *msg,
-                       char *saved_rfc822_version,
-                       struct recptypes *recps) {
+                       StrBuf *saved_rfc822_version,
+                       recptypes *recps) {
 
        struct jnlq *jptr = NULL;
 
        /* Avoid double journaling! */
-       if (msg->cm_fields['J'] != NULL) {
-               free(saved_rfc822_version);
+       if (!CM_IsEmpty(msg, eJournal)) {
+               FreeStrBuf(&saved_rfc822_version);
                return;
        }
 
        jptr = (struct jnlq *)malloc(sizeof(struct jnlq));
        if (jptr == NULL) {
-               free(saved_rfc822_version);
+               FreeStrBuf(&saved_rfc822_version);
                return;
        }
        memset(jptr, 0, sizeof(struct jnlq));
-       if (recps != NULL) memcpy(&jptr->recps, recps, sizeof(struct recptypes));
-       if (msg->cm_fields['A'] != NULL) jptr->from = strdup(msg->cm_fields['A']);
-       if (msg->cm_fields['N'] != NULL) jptr->node = strdup(msg->cm_fields['N']);
-       if (msg->cm_fields['F'] != NULL) jptr->rfca = strdup(msg->cm_fields['F']);
-       if (msg->cm_fields['U'] != NULL) jptr->subj = strdup(msg->cm_fields['U']);
-       if (msg->cm_fields['I'] != NULL) jptr->msgn = strdup(msg->cm_fields['I']);
-       jptr->rfc822 = saved_rfc822_version;
+       if (recps != NULL) memcpy(&jptr->recps, recps, sizeof(recptypes));
+       if (!CM_IsEmpty(msg, eAuthor)) jptr->from = strdup(msg->cm_fields[eAuthor]);
+       if (!CM_IsEmpty(msg, erFc822Addr)) jptr->rfca = strdup(msg->cm_fields[erFc822Addr]);
+       if (!CM_IsEmpty(msg, eMsgSubject)) jptr->subj = strdup(msg->cm_fields[eMsgSubject]);
+       if (!CM_IsEmpty(msg, emessageId)) jptr->msgn = strdup(msg->cm_fields[emessageId]);
+       jptr->rfc822 = SmashStrBuf(&saved_rfc822_version);
 
        /* Add to the queue */
        begin_critical_section(S_JOURNAL_QUEUE);
@@ -92,13 +62,14 @@ void JournalBackgroundSubmit(struct CtdlMessage *msg,
 
 /*
  * Convert a local user name to an internet email address for the journal
+ * FIXME - grab the user's Internet email address from the user record, not from vCard !!!!
  */
 void local_to_inetemail(char *inetemail, char *localuser, size_t inetemail_len) {
        struct ctdluser us;
        struct vCard *v;
 
        strcpy(inetemail, "");
-       if (getuser(&us, localuser) != 0) {
+       if (CtdlGetUser(&us, localuser) != 0) {
                return;
        }
 
@@ -107,7 +78,7 @@ void local_to_inetemail(char *inetemail, char *localuser, size_t inetemail_len)
                return;
        }
 
-       extract_primary_inet_email(inetemail, inetemail_len, v);
+       extract_inet_email_addrs(inetemail, inetemail_len, NULL, 0, v, 1);
        vcard_free(v);
 }
 
@@ -118,20 +89,23 @@ void local_to_inetemail(char *inetemail, char *localuser, size_t inetemail_len)
 void JournalRunQueueMsg(struct jnlq *jmsg) {
 
        struct CtdlMessage *journal_msg = NULL;
-       struct recptypes *journal_recps = NULL;
-       char *message_text = NULL;
+       recptypes *journal_recps = NULL;
+       StrBuf *message_text = NULL;
        char mime_boundary[256];
+       long mblen;
+       long rfc822len;
        char recipient[256];
        char inetemail[256];
        static int seq = 0;
        int i;
 
-       journal_recps = validate_recipients(config.c_journal_dest);
+       if (jmsg == NULL)
+               return;
+       journal_recps = validate_recipients(CtdlGetConfigStr("c_journal_dest"), NULL, 0);
        if (journal_recps != NULL) {
 
                if (  (journal_recps->num_local > 0)
                   || (journal_recps->num_internet > 0)
-                  || (journal_recps->num_ignet > 0)
                   || (journal_recps->num_room > 0)
                ) {
 
@@ -144,104 +118,116 @@ void JournalRunQueueMsg(struct jnlq *jmsg) {
                        journal_msg->cm_magic = CTDLMESSAGE_MAGIC;
                        journal_msg->cm_anon_type = MES_NORMAL;
                        journal_msg->cm_format_type = FMT_RFC822;
-                       journal_msg->cm_fields['J'] = strdup("is journal");
-                       journal_msg->cm_fields['A'] = jmsg->from;
-                       journal_msg->cm_fields['N'] = jmsg->node;
-                       journal_msg->cm_fields['F'] = jmsg->rfca;
-                       journal_msg->cm_fields['U'] = jmsg->subj;
+                       CM_SetField(journal_msg, eJournal, HKEY("is journal"));
 
-                       sprintf(mime_boundary, "--Citadel-Journal-%08lx-%04x--", time(NULL), ++seq);
-                       message_text = malloc(strlen(jmsg->rfc822) + sizeof(struct recptypes) + 1024);
+                       if (!IsEmptyStr(jmsg->from)) {
+                               CM_SetField(journal_msg, eAuthor, jmsg->from, -1);
+                       }
+
+                       if (!IsEmptyStr(jmsg->rfca)) {
+                               CM_SetField(journal_msg, erFc822Addr, jmsg->rfca, -1);
+                       }
+
+                       if (!IsEmptyStr(jmsg->subj)) {
+                               CM_SetField(journal_msg, eMsgSubject, jmsg->subj, -1);
+                       }
+
+                       mblen = snprintf(mime_boundary, sizeof(mime_boundary),
+                                        "--Citadel-Journal-%08lx-%04x--", time(NULL), ++seq);
+
+                       if (!IsEmptyStr(jmsg->rfc822)) {
+                               rfc822len = strlen(jmsg->rfc822);
+                       }
+                       else {
+                               rfc822len = 0;
+                       }
+
+                       message_text = NewStrBufPlain(NULL, rfc822len + sizeof(recptypes) + 1024);
 
                        /*
                         * Here is where we begin to compose the journalized message.
-                        * NOTE: the superfluous "Content-Identifer: ExJournalReport" header was
-                        *       requested by a paying customer, and yes, it is intentionally
-                        *       spelled wrong.  Do NOT remove or change it.
+                        * (The "ExJournalReport" header is consumed by some email retention services which assume the journaling agent is Exchange.)
                         */
-                       sprintf(message_text,
-                               "Content-type: multipart/mixed; boundary=\"%s\"\r\n"
-                               "Content-Identifer: ExJournalReport\r\n"
-                               "MIME-Version: 1.0\r\n"
-                               "\n"
-                               "--%s\r\n"
-                               "Content-type: text/plain\r\n"
-                               "\r\n"
-                               "Sender: %s "
-                       ,
-                               mime_boundary,
-                               mime_boundary,
-                               ( journal_msg->cm_fields['A'] ? journal_msg->cm_fields['A'] : "(null)" )
+                       StrBufAppendBufPlain(
+                               message_text, 
+                               HKEY("Content-type: multipart/mixed; boundary=\""),
+                               0
                        );
 
-                       if (journal_msg->cm_fields['F']) {
-                               sprintf(&message_text[strlen(message_text)], "<%s>",
-                                       journal_msg->cm_fields['F']);
-                       }
-                       else if (journal_msg->cm_fields['N']) {
-                               sprintf(&message_text[strlen(message_text)], "@ %s",
-                                       journal_msg->cm_fields['N']);
-                       }
+                       StrBufAppendBufPlain(message_text, mime_boundary, mblen, 0);
 
-                       sprintf(&message_text[strlen(message_text)],
-                               "\r\n"
-                               "Message-ID: <%s>\r\n"
-                               "Recipients:\r\n"
-                       ,
-                               jmsg->msgn
+                       StrBufAppendBufPlain(
+                               message_text, 
+                               HKEY("\"\r\n"
+                                    "Content-Identifier: ExJournalReport\r\n"
+                                    "MIME-Version: 1.0\r\n"
+                                    "\n"
+                                    "--"),
+                               0
                        );
 
+                       StrBufAppendBufPlain(message_text, mime_boundary, mblen, 0);
+
+                       StrBufAppendBufPlain(
+                               message_text, 
+                               HKEY("\r\n"
+                                    "Content-type: text/plain\r\n"
+                                    "\r\n"
+                                    "Sender: "), 0);
+
+                       if (CM_IsEmpty(journal_msg, eAuthor))
+                               StrBufAppendBufPlain(
+                                       message_text, 
+                                       journal_msg->cm_fields[eAuthor], -1, 0);
+                       else
+                               StrBufAppendBufPlain(
+                                       message_text, 
+                                       HKEY("(null)"), 0);
+
+                       if (!CM_IsEmpty(journal_msg, erFc822Addr)) {
+                               StrBufAppendPrintf(message_text, " <%s>",
+                                                  journal_msg->cm_fields[erFc822Addr]);
+                       }
+
+                       StrBufAppendBufPlain(message_text, HKEY("\r\nMessage-ID: <"), 0);
+                       StrBufAppendBufPlain(message_text, jmsg->msgn, -1, 0);
+                       StrBufAppendBufPlain(message_text, HKEY(">\r\nRecipients:\r\n"), 0);
+
                        if (jmsg->recps.num_local > 0) {
                                for (i=0; i<jmsg->recps.num_local; ++i) {
-                                       extract_token(recipient, jmsg->recps.recp_local,
-                                                       i, '|', sizeof recipient);
+                                       extract_token(recipient, jmsg->recps.recp_local, i, '|', sizeof recipient);
                                        local_to_inetemail(inetemail, recipient, sizeof inetemail);
-                                       sprintf(&message_text[strlen(message_text)],
-                                               "       %s <%s>\r\n", recipient, inetemail);
-                               }
-                       }
-
-                       if (jmsg->recps.num_ignet > 0) {
-                               for (i=0; i<jmsg->recps.num_ignet; ++i) {
-                                       extract_token(recipient, jmsg->recps.recp_ignet,
-                                                       i, '|', sizeof recipient);
-                                       sprintf(&message_text[strlen(message_text)],
-                                               "       %s\r\n", recipient);
+                                       StrBufAppendPrintf(message_text, "      %s <%s>\r\n", recipient, inetemail);
                                }
                        }
 
                        if (jmsg->recps.num_internet > 0) {
                                for (i=0; i<jmsg->recps.num_internet; ++i) {
-                                       extract_token(recipient, jmsg->recps.recp_internet,
-                                                       i, '|', sizeof recipient);
-                                       sprintf(&message_text[strlen(message_text)],
-                                               "       %s\r\n", recipient);
+                                       extract_token(recipient, jmsg->recps.recp_internet, i, '|', sizeof recipient);
+                                       StrBufAppendPrintf(message_text, "      %s\r\n", recipient);
                                }
                        }
 
-                       sprintf(&message_text[strlen(message_text)],
-                               "\r\n"
-                               "--%s\r\n"
-                               "Content-type: message/rfc822\r\n"
-                               "\r\n"
-                               "%s"
-                               "--%s--\r\n"
-                       ,
-                               mime_boundary,
-                               jmsg->rfc822,
-                               mime_boundary
-                       );
+                       StrBufAppendBufPlain(message_text, HKEY("\r\n" "--"), 0);
+                       StrBufAppendBufPlain(message_text, mime_boundary, mblen, 0);
+                       StrBufAppendBufPlain(message_text, HKEY("\r\nContent-type: message/rfc822\r\n\r\n"), 0);
+                       StrBufAppendBufPlain(message_text, jmsg->rfc822, rfc822len, 0);
+                       StrBufAppendBufPlain(message_text, HKEY("--"), 0);
+                       StrBufAppendBufPlain(message_text, mime_boundary, mblen, 0);
+                       StrBufAppendBufPlain(message_text, HKEY("--\r\n"), 0);
 
-                       journal_msg->cm_fields['M'] = message_text;
+                       CM_SetAsFieldSB(journal_msg, eMesageText, &message_text);
                        free(jmsg->rfc822);
                        free(jmsg->msgn);
+                       jmsg->rfc822 = NULL;
+                       jmsg->msgn = NULL;
                        
                        /* Submit journal message */
                        CtdlSubmitMsg(journal_msg, journal_recps, "");
-                       CtdlFreeMessage(journal_msg);
+                       CM_Free(journal_msg);
                }
 
-               free(journal_recps);
+               free_recipients(journal_recps);
        }
 
        /* We are responsible for freeing this memory. */