MSG: the database returns the buffer size, not the stringlength.
[citadel.git] / citadel / msgbase.c
index d1270f4d1e75b18b0fa9e744eed4e821d56663e9..21c1aabb41bb16ff61581832b76673965439f7c0 100644 (file)
@@ -1129,6 +1129,13 @@ struct CtdlMessage *CtdlFetchMessage(long msgnum, int with_body)
        ret->cm_anon_type = *mptr++;    /* Anon type byte */
        ret->cm_format_type = *mptr++;  /* Format type byte */
 
+
+       if (dmsgtext->ptr[dmsgtext->len - 1] != '\0')
+       {
+               MSG_syslog(LOG_ERR, "CtdlFetchMessage(%ld, %d) Forcefully terminating message!!\n", msgnum, with_body);
+               dmsgtext->ptr[dmsgtext->len - 1] = '\0';
+       }
+
        /*
         * The rest is zero or more arbitrary fields.  Load them in.
         * We're done when we encounter either a zero-length field or
@@ -1158,7 +1165,7 @@ struct CtdlMessage *CtdlFetchMessage(long msgnum, int with_body)
        if ( (CM_IsEmpty(ret, eMesageText)) && (with_body) ) {
                dmsgtext = cdb_fetch(CDB_BIGMSGS, &msgnum, sizeof(long));
                if (dmsgtext != NULL) {
-                       CM_SetAsField(ret, eMesageText, &dmsgtext->ptr, dmsgtext->len);
+                       CM_SetAsField(ret, eMesageText, &dmsgtext->ptr, dmsgtext->len - 1);
                        cdb_free(dmsgtext);
                }
        }
@@ -1584,11 +1591,10 @@ int CtdlOutputMsg(long msg_num,         /* message number (local) to fetch */
        if (section) if (!IsEmptyStr(section)) if (strcmp(section, "0")) {
                memset(&encap, 0, sizeof encap);
                safestrncpy(encap.desired_section, section, sizeof encap.desired_section);
-               mime_parser(TheMessage->cm_fields[eMesageText],
-                       NULL,
-                       *extract_encapsulated_message,
-                       NULL, NULL, (void *)&encap, 0
-               );
+               mime_parser(CM_RANGE(TheMessage, eMesageText),
+                           *extract_encapsulated_message,
+                           NULL, NULL, (void *)&encap, 0
+                       );
 
                if ((Author != NULL) && (*Author == NULL))
                {
@@ -2004,7 +2010,6 @@ int CtdlOutputPreLoadedMsg(
 ) {
        struct CitContext *CCC = CC;
        int i;
-       char *mptr = NULL;
        const char *nl; /* newline string */
        struct ma_info ma;
 
@@ -2051,8 +2056,8 @@ int CtdlOutputPreLoadedMsg(
                                ERROR + RESOURCE_BUSY);
                } else {
                        /* Parse the message text component */
-                       mptr = TheMessage->cm_fields[eMesageText];
-                       mime_parser(mptr, NULL, *mime_download, NULL, NULL, NULL, 0);
+                       mime_parser(CM_RANGE(TheMessage, eMesageText),
+                                   *mime_download, NULL, NULL, NULL, 0);
                        /* If there's no file open by this time, the requested
                         * section wasn't found, so print an error
                         */
@@ -2078,8 +2083,8 @@ int CtdlOutputPreLoadedMsg(
                        /* Parse the message text component */
                        int found_it = 0;
 
-                       mptr = TheMessage->cm_fields[eMesageText];
-                       mime_parser(mptr, NULL, *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 (!found_it) {
@@ -2119,7 +2124,7 @@ int CtdlOutputPreLoadedMsg(
        strcpy(suser, "");
        strcpy(luser, "");
        strcpy(fuser, "");
-       strcpy(snode, NODENAME);
+       memcpy(snode, CFG_KEY(c_nodename) + 1);
        if (mode == MT_RFC822) 
                OutputRFC822MsgHeaders(
                        TheMessage,
@@ -2176,9 +2181,8 @@ START_TEXT:
        /* Tell the client about the MIME parts in this message */
        if (TheMessage->cm_format_type == FMT_RFC822) {
                if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
-                       mptr = TheMessage->cm_fields[eMesageText];
                        memset(&ma, 0, sizeof(struct ma_info));
-                       mime_parser(mptr, NULL,
+                       mime_parser(CM_RANGE(TheMessage, eMesageText),
                                (do_proto ? *list_this_part : NULL),
                                (do_proto ? *list_this_pref : NULL),
                                (do_proto ? *list_this_suff : NULL),
@@ -2217,12 +2221,10 @@ START_TEXT:
         * message to the reader's screen width.
         */
        if (TheMessage->cm_format_type == FMT_CITADEL) {
-               mptr = TheMessage->cm_fields[eMesageText];
-
                if (mode == MT_MIME) {
                        cprintf("Content-type: text/x-citadel-variformat\n\n");
                }
-               memfmout(mptr, nl);
+               memfmout(TheMessage->cm_fields[eMesageText], nl);
        }
 
        /* If the message on disk is format 4 (MIME), we've gotta hand it
@@ -2238,17 +2240,17 @@ START_TEXT:
                        strcpy(ma.chosen_part, "1");
                        ma.chosen_pref = 9999;
                        ma.dont_decode = CCC->msg4_dont_decode;
-                       mime_parser(mptr, NULL,
-                               *choose_preferred, *fixed_output_pre,
-                               *fixed_output_post, (void *)&ma, 1);
-                       mime_parser(mptr, NULL,
-                               *output_preferred, NULL, NULL, (void *)&ma, 1);
+                       mime_parser(CM_RANGE(TheMessage, eMesageText),
+                                   *choose_preferred, *fixed_output_pre,
+                                   *fixed_output_post, (void *)&ma, 1);
+                       mime_parser(CM_RANGE(TheMessage, eMesageText),
+                                   *output_preferred, NULL, NULL, (void *)&ma, 1);
                }
                else {
                        ma.use_fo_hooks = 1;
-                       mime_parser(mptr, NULL,
-                               *fixed_output, *fixed_output_pre,
-                               *fixed_output_post, (void *)&ma, 0);
+                       mime_parser(CM_RANGE(TheMessage, eMesageText),
+                                   *fixed_output, *fixed_output_pre,
+                                   *fixed_output_post, (void *)&ma, 0);
                }
 
        }
@@ -2982,7 +2984,7 @@ void quickie_message(const char *from,
 
        if (fromaddr != NULL) CM_SetField(msg, erFc822Addr, fromaddr, strlen(fromaddr));
        if (room != NULL) CM_SetField(msg, eOriginalRoom, room, strlen(room));
-       CM_SetField(msg, eNodeName, NODENAME, strlen(NODENAME));
+       CM_SetField(msg, eNodeName, CFG_KEY(c_nodename));
        if (to != NULL) {
                CM_SetField(msg, eRecipient, to, strlen(to));
                recp = validate_recipients(to, NULL, 0);
@@ -3434,6 +3436,7 @@ struct CtdlMessage *CtdlMakeMessageLen(
        }
        StrBufRFC2047encode(&FakeEncAuthor, FakeAuthor);
        CM_SetAsFieldSB(msg, eAuthor, &FakeEncAuthor);
+       FreeStrBuf(&FakeAuthor);
 
        if (CCC->room.QRflags & QR_MAILBOX) {           /* room */
                CM_SetField(msg, eOriginalRoom, &CCC->room.QRname[11], strlen(&CCC->room.QRname[11]));
@@ -3442,8 +3445,8 @@ struct CtdlMessage *CtdlMakeMessageLen(
                CM_SetField(msg, eOriginalRoom, CCC->room.QRname, strlen(CCC->room.QRname));
        }
 
-       CM_SetField(msg, eNodeName, NODENAME, strlen(NODENAME));
-       CM_SetField(msg, eHumanNode, HUMANNODE, strlen(HUMANNODE));
+       CM_SetField(msg, eNodeName, CFG_KEY(c_nodename));
+       CM_SetField(msg, eHumanNode, CFG_KEY(c_humannode));
 
        if (rcplen > 0) {
                CM_SetField(msg, eRecipient, recipient, rcplen);
@@ -3494,9 +3497,10 @@ struct CtdlMessage *CtdlMakeMessageLen(
                CM_SetField(msg, eMesageText, preformatted_text, textlen);
        }
        else {
-               preformatted_text = CtdlReadMessageBody(HKEY("000"), config.c_maxmsglen, NULL, 0, 0);
-               if (preformatted_text != NULL) {
-                       CM_SetField(msg, eMesageText, preformatted_text, strlen(preformatted_text));
+               StrBuf *MsgBody;
+               MsgBody = CtdlReadMessageBodyBuf(HKEY("000"), config.c_maxmsglen, NULL, 0, 0);
+               if (MsgBody != NULL) {
+                       CM_SetAsFieldSB(msg, eMesageText, &MsgBody);
                }
        }
 
@@ -3975,8 +3979,8 @@ void CtdlWriteObject(char *req_room,                      /* Room to stuff it in */
        msg->cm_format_type = 4;
        CM_SetField(msg, eAuthor, CCC->user.fullname, strlen(CCC->user.fullname));
        CM_SetField(msg, eOriginalRoom, req_room, strlen(req_room));
-       CM_SetField(msg, eNodeName, config.c_nodename, strlen(config.c_nodename));
-       CM_SetField(msg, eHumanNode, config.c_humannode, strlen(config.c_humannode));
+       CM_SetField(msg, eNodeName, CFG_KEY(c_nodename));
+       CM_SetField(msg, eHumanNode, CFG_KEY(c_humannode));
        msg->cm_flags = flags;
        
        CM_SetAsFieldSB(msg, eMesageText, &encoded_message);