Implemented configuration settings for journaling.
[citadel.git] / citadel / msgbase.c
index 51d78547b73f0d7ca55045e18f9d07d2fd7b4d07..69d7d2621049ac1d42deb37f782636e9902d4a0a 100644 (file)
@@ -5,10 +5,6 @@
  *
  */
 
-#ifdef DLL_EXPORT
-#define IN_LIBCIT
-#endif
-
 #include "sysdep.h"
 #include <stdlib.h>
 #include <unistd.h>
@@ -54,6 +50,7 @@
 #include "serv_fulltext.h"
 #include "vcard.h"
 #include "euidindex.h"
+#include "journaling.h"
 
 long config_msgnum;
 struct addresses_to_be_filed *atbf = NULL;
@@ -86,7 +83,8 @@ char *msgkeys[] = {
        NULL, 
        "hnod",
        "msgn",
-       NULL, NULL, NULL,
+       "jrnl",
+       NULL, NULL,
        "text",
        "node",
        "room",
@@ -247,10 +245,10 @@ void get_mm(void)
        FILE *fp;
 
        fp = fopen(
-#ifndef HAVE_RUN_DIR
+#ifndef HAVE_DATA_DIR
                           "."
 #else
-                          RUN_DIR
+                          DATA_DIR
 #endif
                           "/citadel.control", "r");
        if (fp == NULL) {
@@ -281,7 +279,7 @@ void headers_listing(long msgnum, void *userdata)
        struct CtdlMessage *msg;
 
        msg = CtdlFetchMessage(msgnum, 0);
-       if (msg < 0L) {
+       if (msg == NULL) {
                cprintf("%ld|0|||||\n", msgnum);
                return;
        }
@@ -1070,7 +1068,9 @@ void fixed_output_pre(char *name, char *filename, char *partnum, char *disp,
        if (!strcasecmp(cbtype, "multipart/alternative")) {
                ++ma->is_ma;
                ma->did_print = 0;
-               return;
+       }
+       if (!strcasecmp(cbtype, "message/rfc822")) {
+               ++ma->freeze;
        }
 }
 
@@ -1088,7 +1088,9 @@ void fixed_output_post(char *name, char *filename, char *partnum, char *disp,
        if (!strcasecmp(cbtype, "multipart/alternative")) {
                --ma->is_ma;
                ma->did_print = 0;
-       return;
+       }
+       if (!strcasecmp(cbtype, "message/rfc822")) {
+               --ma->freeze;
        }
 }
 
@@ -1114,7 +1116,7 @@ void fixed_output(char *name, char *filename, char *partnum, char *disp,
         * If we're in the middle of a multipart/alternative scope and
         * we've already printed another section, skip this one.
         */     
-       if ( (ma->is_ma == 1) && (ma->did_print == 1) ) {
+       if ( (ma->is_ma) && (ma->did_print) ) {
                lprintf(CTDL_DEBUG, "Skipping part %s (%s)\n",
                        partnum, cbtype);
                return;
@@ -1167,8 +1169,8 @@ void choose_preferred(char *name, char *filename, char *partnum, char *disp,
        if (ma->is_ma > 0) {
                for (i=0; i<num_tokens(CC->preferred_formats, '|'); ++i) {
                        extract_token(buf, CC->preferred_formats, i, '|', sizeof buf);
-                       if (!strcasecmp(buf, cbtype)) {
-                               strcpy(ma->chosen_part, partnum);
+                       if ( (!strcasecmp(buf, cbtype)) && (!ma->freeze) ) {
+                               safestrncpy(ma->chosen_part, partnum, sizeof ma->chosen_part);
                        }
                }
        }
@@ -1311,7 +1313,7 @@ int CtdlOutputMsg(long msg_num,           /* message number (local) to fetch */
        /* Here is the weird form of this command, to process only an
         * encapsulated message/rfc822 section.
         */
-       if (section) {
+       if (section) if (strlen(section)>0) if (strcmp(section, "0")) {
                memset(&encap, 0, sizeof encap);
                safestrncpy(encap.desired_section, section, sizeof encap.desired_section);
                mime_parser(TheMessage->cm_fields['M'],
@@ -2137,6 +2139,8 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,       /* message to save */
        char *hold_R, *hold_D;
        char *collected_addresses = NULL;
        struct addresses_to_be_filed *aptr = NULL;
+       char *saved_rfc822_version = NULL;
+       int qualified_for_journaling = 0;
 
        lprintf(CTDL_DEBUG, "CtdlSubmitMsg() called\n");
        if (is_valid_message(msg) == 0) return(-1);     /* self check */
@@ -2265,13 +2269,16 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,     /* message to save */
        safestrncpy(smi.meta_content_type, content_type,
                        sizeof smi.meta_content_type);
 
-       /* As part of the new metadata record, measure how
-        * big this message will be when displayed as RFC822.
-        * Both POP and IMAP use this, and it's best to just take the hit now
-        * instead of having to potentially measure thousands of messages when
-        * a mailbox is opened later.
+       /*
+        * Measure how big this message will be when rendered as RFC822.
+        * We do this for two reasons:
+        * 1. We need the RFC822 length for the new metadata record, so the
+        *    POP and IMAP services don't have to calculate message lengths
+        *    while the user is waiting (multiplied by potentially hundreds
+        *    or thousands of messages).
+        * 2. If journaling is enabled, we will need an RFC822 version of the
+        *    message to attach to the journalized copy.
         */
-
        if (CC->redirect_buffer != NULL) {
                lprintf(CTDL_ALERT, "CC->redirect_buffer is not NULL during message submission!\n");
                abort();
@@ -2281,7 +2288,7 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,       /* message to save */
        CC->redirect_alloc = SIZ;
        CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1);
        smi.meta_rfc822_length = CC->redirect_len;
-       free(CC->redirect_buffer);
+       saved_rfc822_version = CC->redirect_buffer;
        CC->redirect_buffer = NULL;
        CC->redirect_len = 0;
        CC->redirect_alloc = 0;
@@ -2432,6 +2439,7 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,       /* message to save */
                imsg->cm_anon_type = MES_NORMAL;
                imsg->cm_format_type = FMT_RFC822;
                imsg->cm_fields['A'] = strdup("Citadel");
+               imsg->cm_fields['J'] = strdup("do not journal");
                imsg->cm_fields['M'] = instr;
                CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM);
                CtdlFreeMessage(imsg);
@@ -2456,11 +2464,42 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,     /* message to save */
                atbf = aptr;
                end_critical_section(S_ATBF);
        }
-       return(newmsgid);
-}
 
+       /*
+        * Determine whether this message qualifies for journaling.
+        */
+       if (msg->cm_fields['J'] != NULL) {
+               qualified_for_journaling = 0;
+       }
+       else {
+               if (recps == NULL) {
+                       qualified_for_journaling = config.c_journal_pubmsgs;
+               }
+               else if (recps->num_local + recps->num_ignet + recps->num_internet > 0) {
+                       qualified_for_journaling = config.c_journal_email;
+               }
+               else {
+                       qualified_for_journaling = config.c_journal_pubmsgs;
+               }
+       }
 
+       /*
+        * Do we have to perform journaling?  If so, hand off the saved
+        * RFC822 version will be handed off to the journaler for background
+        * submit.  Otherwise, we have to free the memory ourselves.
+        */
+       if (saved_rfc822_version != NULL) {
+               if (qualified_for_journaling) {
+                       JournalBackgroundSubmit(msg, saved_rfc822_version, recps);
+               }
+               else {
+                       free(saved_rfc822_version);
+               }
+       }
 
+       /* Done. */
+       return(newmsgid);
+}
 
 
 
@@ -2744,7 +2783,7 @@ struct recptypes *validate_recipients(char *supplied_recipients) {
        char this_recp[256];
        char this_recp_cooked[256];
        char append[SIZ];
-       int num_recps;
+       int num_recps = 0;
        int i, j;
        int mailtype;
        int invalid;
@@ -3360,6 +3399,7 @@ void cmd_move(char *args)
 
        num = extract_long(args, 0);
        extract_token(targ, args, 1, '|', sizeof targ);
+       convert_room_name_macros(targ, sizeof targ);
        targ[ROOMNAMELEN - 1] = 0;
        is_copy = extract_int(args, 2);
 
@@ -3536,12 +3576,12 @@ void CtdlWriteObject(char *req_room,            /* Room to stuff it in */
        char *encoded_message = NULL;
        off_t raw_length = 0;
 
-       if (is_mailbox != NULL)
+       if (is_mailbox != NULL) {
                MailboxName(roomname, sizeof roomname, is_mailbox, req_room);
-       else
+       }
+       else {
                safestrncpy(roomname, req_room, sizeof(roomname));
-       lprintf(CTDL_DEBUG, "CtdlWriteObject() to <%s> (flags=%d)\n", roomname, flags);
-
+       }
 
        fp = fopen(tempfilename, "rb");
        if (fp == NULL) {
@@ -3690,7 +3730,7 @@ void CtdlPutSysConfig(char *sysconfname, char *sysconfdata) {
        char temp[PATH_MAX];
        FILE *fp;
 
-       strcpy(temp, tmpnam(NULL));
+       CtdlMakeTempFileName(temp, sizeof temp);
 
        fp = fopen(temp, "w");
        if (fp == NULL) return;