]> code.citadel.org Git - citadel.git/blobdiff - citadel/msgbase.c
Committing all of my little comment syntax changes and getting them out of the way...
[citadel.git] / citadel / msgbase.c
index 6e626ac52c5876ede82da970760bf599a0674f7f..406107e1c8e22a121005970b19a5cc43f96fb480 100644 (file)
@@ -1,6 +1,6 @@
 // Implements the message store.
 //
-// Copyright (c) 1987-2021 by the citadel.org team
+// Copyright (c) 1987-2022 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.
@@ -16,6 +16,7 @@
 #include <stdio.h>
 #include <regex.h>
 #include <sys/stat.h>
+#include <assert.h>
 #include <libcitadel.h>
 #include "ctdl_module.h"
 #include "citserver.h"
@@ -384,7 +385,7 @@ int CtdlMsgCmp(struct CtdlMessage *msg, struct CtdlMessage *template) {
                }
        }
 
-       /* All compares succeeded: we have a match! */
+       // All compares succeeded: we have a match!
        return 0;
 }
 
@@ -464,7 +465,7 @@ void CtdlSetSeen(long *target_msgnums, int num_target_msgnums,
        is_set = malloc(num_msgs * sizeof(char));
        memset(is_set, 0, (num_msgs * sizeof(char)) );
 
-       /* Decide which message set we're manipulating */
+       // Decide which message set we're manipulating
        switch(which_set) {
        case ctdlsetseen_seen:
                vset = NewStrBufPlain(vbuf.v_seen, -1);
@@ -652,7 +653,7 @@ int CtdlForEachMessage(int mode, long ref, char *search_string,
                need_to_free_re = 1;
        }
 
-       /* Learn about the user and room in question */
+       // Learn about the user and room in question
        if (server_shutting_down) {
                if (need_to_free_re) regfree(&re);
                return -1;
@@ -670,18 +671,18 @@ int CtdlForEachMessage(int mode, long ref, char *search_string,
                return -1;
        }
 
-       /* Load the message list */
+       // Load the message list
        cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
        if (cdbfr == NULL) {
                if (need_to_free_re) regfree(&re);
-               return 0;       /* No messages at all?  No further action. */
+               return 0;       // No messages at all?  No further action.
        }
 
        msglist = (long *) cdbfr->ptr;
        num_msgs = cdbfr->len / sizeof(long);
 
-       cdbfr->ptr = NULL;      /* clear this so that cdb_free() doesn't free it */
-       cdb_free(cdbfr);        /* we own this memory now */
+       cdbfr->ptr = NULL;      // clear this so that cdb_free() doesn't free it
+       cdb_free(cdbfr);        // we own this memory now
 
        /*
         * Now begin the traversal.
@@ -801,18 +802,17 @@ int CtdlForEachMessage(int mode, long ref, char *search_string,
                                is_seen = 0;
                        }
                        else {
-                               is_seen = is_msg_in_sequence_set(
-                                                       vbuf.v_seen, thismsg);
+                               is_seen = is_msg_in_sequence_set(vbuf.v_seen, thismsg);
                                if (is_seen) lastold = thismsg;
                        }
-                       if ((thismsg > 0L)
-                           && (
-
-                                      (mode == MSGS_ALL)
-                                      || ((mode == MSGS_OLD) && (is_seen))
-                                      || ((mode == MSGS_NEW) && (!is_seen))
-                                      || ((mode == MSGS_LAST) && (a >= (num_msgs - ref)))
-                                  || ((mode == MSGS_FIRST) && (a < ref))
+                       if (
+                               (thismsg > 0L)
+                       && (
+                               (mode == MSGS_ALL)
+                               || ((mode == MSGS_OLD) && (is_seen))
+                               || ((mode == MSGS_NEW) && (!is_seen))
+                               || ((mode == MSGS_LAST) && (a >= (num_msgs - ref)))
+                               || ((mode == MSGS_FIRST) && (a < ref))
                                || ((mode == MSGS_GT) && (thismsg > ref))
                                || ((mode == MSGS_LT) && (thismsg < ref))
                                || ((mode == MSGS_EQ) && (thismsg == ref))
@@ -1052,6 +1052,7 @@ void mime_spew_section(char *name, char *filename, char *partnum, char *disp,
                (!IsEmptyStr(partnum) && (!strcasecmp(CC->download_desired_section, partnum)))
        ||      (!IsEmptyStr(cbid) && (!strcasecmp(CC->download_desired_section, cbid)))
        ) {
+               syslog(LOG_DEBUG, "\033[32mYES part %s len %d\033[0m" , partnum, (int)length);
                *found_it = 1;
                cprintf("%d %d|-1|%s|%s|%s\n",
                        BINARY_FOLLOWS,
@@ -1062,6 +1063,9 @@ void mime_spew_section(char *name, char *filename, char *partnum, char *disp,
                );
                client_write(content, length);
        }
+       else {
+               syslog(LOG_DEBUG, "\033[31m NO part %s\033[0m", partnum);
+       }
 }
 
 
@@ -1224,7 +1228,6 @@ void fixed_output_post(char *name, char *filename, char *partnum, char *disp,
 
 
 // Inline callback function for mime parser that wants to display text
-//
 void fixed_output(char *name, char *filename, char *partnum, char *disp,
                void *content, char *cbtype, char *cbcharset, size_t length,
                char *encoding, char *cbid, void *cbuserdata)
@@ -1243,7 +1246,6 @@ 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) && (ma->did_print) ) {
                syslog(LOG_DEBUG, "msgbase: skipping part %s (%s)", partnum, cbtype);
                return;
@@ -1296,7 +1298,6 @@ void fixed_output(char *name, char *filename, char *partnum, char *disp,
 // and then set ma->chosen_pref to that MIME type's position in our preference
 // list.  If we then hit another match, we only replace the first match if
 // the preference value is lower.
-//
 void choose_preferred(char *name, char *filename, char *partnum, char *disp,
                void *content, char *cbtype, char *cbcharset, size_t length,
                char *encoding, char *cbid, void *cbuserdata)
@@ -1321,7 +1322,6 @@ void choose_preferred(char *name, char *filename, char *partnum, char *disp,
 
 
 // Now that we've chosen our preferred part, output it.
-//
 void output_preferred(char *name, 
                      char *filename, 
                      char *partnum, 
@@ -2037,44 +2037,41 @@ int CtdlOutputPreLoadedMsg(
                return((CC->download_fp != NULL) ? om_ok : om_mime_error);
        }
 
-       /* MT_SPEW_SECTION is like MT_DOWNLOAD except it outputs the whole MIME part
-        * in a single server operation instead of opening a download file.
-        */
+       // MT_SPEW_SECTION is like MT_DOWNLOAD except it outputs the whole MIME part
+       // in a single server operation instead of opening a download file.
        if (mode == MT_SPEW_SECTION) {
                if (TheMessage->cm_format_type != FMT_RFC822) {
                        if (do_proto)
                                cprintf("%d This is not a MIME message.\n",
                                ERROR + ILLEGAL_VALUE);
-               } else {
-                       /* Parse the message text component */
+               }
+               else {
+                       // Locate and parse the component specified by the caller
                        int found_it = 0;
+                       mime_parser(CM_RANGE(TheMessage, eMesageText), *mime_spew_section, NULL, NULL, (void *)&found_it, 0);
 
-                       mime_parser(CM_RANGE(TheMessage, eMesageText),
-                                   *mime_spew_section, NULL, NULL, (void *)&found_it, 0);
-                       /* If section wasn't found, print an error
-                        */
+                       // If section wasn't found, print an error
                        if (!found_it) {
-                               if (do_proto) cprintf(
-                                       "%d Section %s not found.\n",
-                                       ERROR + FILE_NOT_FOUND,
-                                       CC->download_desired_section);
+                               if (do_proto) {
+                                       cprintf( "%d Section %s not found.\n", ERROR + FILE_NOT_FOUND, CC->download_desired_section);
+                               }
                        }
                }
                return((CC->download_fp != NULL) ? om_ok : om_mime_error);
        }
 
-       /* now for the user-mode message reading loops */
+       // now for the user-mode message reading loops
        if (do_proto) cprintf("%d msg:\n", LISTING_FOLLOWS);
 
-       /* Does the caller want to skip the headers? */
+       // Does the caller want to skip the headers?
        if (headers_only == HEADERS_NONE) goto START_TEXT;
 
-       /* Tell the client which format type we're using. */
+       // Tell the client which format type we're using.
        if ( (mode == MT_CITADEL) && (do_proto) ) {
                cprintf("type=%d\n", TheMessage->cm_format_type);       // Tell the client which format type we're using.
        }
 
-       /* nhdr=yes means that we're only displaying headers, no body */
+       // nhdr=yes means that we're only displaying headers, no body
        if ( (TheMessage->cm_anon_type == MES_ANONONLY)
           && ((mode == MT_CITADEL) || (mode == MT_MIME))
           && (do_proto)
@@ -2086,7 +2083,7 @@ int CtdlOutputPreLoadedMsg(
                OutputCtdlMsgHeaders(TheMessage, do_proto);
        }
 
-       /* begin header processing loop for RFC822 transfer format */
+       // begin header processing loop for RFC822 transfer format
        strcpy(suser, "");
        strcpy(luser, "");
        strcpy(fuser, "");
@@ -2136,11 +2133,11 @@ int CtdlOutputPreLoadedMsg(
                }
        }
 
-       /* end header processing loop ... at this point, we're in the text */
+       // end header processing loop ... at this point, we're in the text
 START_TEXT:
        if (headers_only == HEADERS_FAST) goto DONE;
 
-       /* Tell the client about the MIME parts in this message */
+       // Tell the client about the MIME parts in this message
        if (TheMessage->cm_format_type == FMT_RFC822) {
                if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
                        memset(&ma, 0, sizeof(struct ma_info));
@@ -2150,7 +2147,7 @@ START_TEXT:
                                (do_proto ? *list_this_suff : NULL),
                                (void *)&ma, 1);
                }
-               else if (mode == MT_RFC822) {   /* unparsed RFC822 dump */
+               else if (mode == MT_RFC822) {   // unparsed RFC822 dump
                        Dump_RFC822HeadersBody(
                                TheMessage,
                                headers_only,
@@ -2164,7 +2161,7 @@ START_TEXT:
                goto DONE;
        }
 
-       /* signify start of msg text */
+       // signify start of msg text
        if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
                if (do_proto) cprintf("text\n");
        }
@@ -2172,16 +2169,16 @@ START_TEXT:
        if (TheMessage->cm_format_type == FMT_FIXED) 
                DumpFormatFixed(
                        TheMessage,
-                       mode,           /* how would you like that message? */
+                       mode,           // how would you like that message?
                        nl, nlen);
 
-       /* If the message on disk is format 0 (Citadel vari-format), we
-        * output using the formatter at 80 columns.  This is the final output
-        * form if the transfer format is RFC822, but if the transfer format
-        * is Citadel proprietary, it'll still work, because the indentation
-        * for new paragraphs is correct and the client will reformat the
-        * message to the reader's screen width.
-        */
+       // If the message on disk is format 0 (Citadel vari-format), we
+       // output using the formatter at 80 columns.  This is the final output
+       // form if the transfer format is RFC822, but if the transfer format
+       // is Citadel proprietary, it'll still work, because the indentation
+       // for new paragraphs is correct and the client will reformat the
+       // message to the reader's screen width.
+       //
        if (TheMessage->cm_format_type == FMT_CITADEL) {
                if (mode == MT_MIME) {
                        cprintf("Content-type: text/x-citadel-variformat\n\n");
@@ -2189,11 +2186,11 @@ START_TEXT:
                memfmout(TheMessage->cm_fields[eMesageText], nl);
        }
 
-       /* If the message on disk is format 4 (MIME), we've gotta hand it
-        * off to the MIME parser.  The client has already been told that
-        * this message is format 1 (fixed format), so the callback function
-        * we use will display those parts as-is.
-        */
+       // If the message on disk is format 4 (MIME), we've gotta hand it
+       // off to the MIME parser.  The client has already been told that
+       // this message is format 1 (fixed format), so the callback function
+       // we use will display those parts as-is.
+       //
        if (TheMessage->cm_format_type == FMT_RFC822) {
                memset(&ma, 0, sizeof(struct ma_info));
 
@@ -2222,17 +2219,16 @@ DONE:   /* now we're done */
        return(om_ok);
 }
 
-/*
- * Save one or more message pointers into a specified room
- * (Returns 0 for success, nonzero for failure)
- * roomname may be NULL to use the current room
- *
- * Note that the 'supplied_msg' field may be set to NULL, in which case
- * the message will be fetched from disk, by number, if we need to perform
- * replication checks.  This adds an additional database read, so if the
- * caller already has the message in memory then it should be supplied.  (Obviously
- * this mode of operation only works if we're saving a single message.)
- */
+// Save one or more message pointers into a specified room
+// (Returns 0 for success, nonzero for failure)
+// roomname may be NULL to use the current room
+//
+// Note that the 'supplied_msg' field may be set to NULL, in which case
+// the message will be fetched from disk, by number, if we need to perform
+// replication checks.  This adds an additional database read, so if the
+// caller already has the message in memory then it should be supplied.  (Obviously
+// this mode of operation only works if we're saving a single message.)
+//
 int CtdlSaveMsgPointersInRoom(char *roomname, long newmsgidlist[], int num_newmsgs,
                        int do_repl_check, struct CtdlMessage *supplied_msg, int suppress_refcount_adj
 ) {
@@ -2885,7 +2881,7 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,       /* message to save */
                if (recps == NULL) {
                        qualified_for_journaling = CtdlGetConfigInt("c_journal_pubmsgs");
                }
-               else if (recps->num_local + recps->num_ignet + recps->num_internet > 0) {
+               else if (recps->num_local + recps->num_internet > 0) {
                        qualified_for_journaling = CtdlGetConfigInt("c_journal_email");
                }
                else {
@@ -2973,12 +2969,11 @@ long quickie_message(const char *from,
 /*
  * Back end function used by CtdlMakeMessage() and similar functions
  */
-StrBuf *CtdlReadMessageBodyBuf(char *terminator,       /* token signalling EOT */
+StrBuf *CtdlReadMessageBodyBuf(char *terminator,       // token signalling EOT
                               long tlen,
-                              size_t maxlen,           /* maximum message length */
-                              StrBuf *exist,           /* if non-null, append to it;
-                                                          exist is ALWAYS freed  */
-                              int crlf                 /* CRLF newlines instead of LF */
+                              size_t maxlen,           // maximum message length
+                              StrBuf *exist,           // if non-null, append to it; exist is ALWAYS freed
+                              int crlf                 // CRLF newlines instead of LF
 ) {
        StrBuf *Message;
        StrBuf *LineBuf;
@@ -3023,25 +3018,28 @@ StrBuf *CtdlReadMessageBodyBuf(char *terminator,        /* token signalling EOT */
                }
 
                /* if we've hit the max msg length, flush the rest */
-               if (StrLength(Message) >= maxlen) flushing = 1;
+               if (StrLength(Message) >= maxlen) {
+                       flushing = 1;
+               }
 
        } while (!finished);
        FreeStrBuf(&LineBuf);
+
+       if (flushing) {
+               syslog(LOG_ERR, "msgbase: exceeded maximum message length of %d - message was truncated", maxlen);
+       }
+
        return Message;
 }
 
 
-/*
- * Back end function used by CtdlMakeMessage() and similar functions
- */
-char *CtdlReadMessageBody(char *terminator,    /* token signalling EOT */
+// Back end function used by CtdlMakeMessage() and similar functions
+char *CtdlReadMessageBody(char *terminator,    // token signalling EOT
                          long tlen,
-                         size_t maxlen,                /* maximum message length */
-                         StrBuf *exist,                /* if non-null, append to it;
-                                                  exist is ALWAYS freed  */
-                         int crlf              /* CRLF newlines instead of LF */
-       ) 
-{
+                         size_t maxlen,        // maximum message length
+                         StrBuf *exist,        // if non-null, append to it; exist is ALWAYS freed
+                         int crlf              // CRLF newlines instead of LF
+) {
        StrBuf *Message;
 
        Message = CtdlReadMessageBodyBuf(terminator,
@@ -3050,10 +3048,12 @@ char *CtdlReadMessageBody(char *terminator,     /* token signalling EOT */
                                         exist,
                                         crlf
        );
-       if (Message == NULL)
+       if (Message == NULL) {
                return NULL;
-       else
+       }
+       else {
                return SmashStrBuf(&Message);
+       }
 }