]> code.citadel.org Git - citadel.git/blobdiff - citadel/msgbase.c
* Bug fixes: Fix numerous char array size mismatches, signed/unsigned
[citadel.git] / citadel / msgbase.c
index 391509a51c38336a4c1f478659dc37d314b7502b..98563d64e66b6d3ac47bf49354a74b17aa59e42c 100644 (file)
@@ -624,7 +624,7 @@ void memfmout(
        int a, b, c;
        int real = 0;
        int old = 0;
-       CIT_UBYTE ch;
+       cit_uint8_t ch;
        char aaa[140];
        char buffer[SIZ];
 
@@ -708,6 +708,26 @@ void list_this_part(char *name, char *filename, char *partnum, char *disp,
                name, filename, partnum, disp, cbtype, (long)length);
 }
 
+/* 
+ * Callback function for multipart prefix
+ */
+void list_this_pref(char *name, char *filename, char *partnum, char *disp,
+                   void *content, char *cbtype, size_t length, char *encoding,
+                   void *cbuserdata)
+{
+       cprintf("pref=%s|%s\n", partnum, cbtype);
+}
+
+/* 
+ * Callback function for multipart sufffix
+ */
+void list_this_suff(char *name, char *filename, char *partnum, char *disp,
+                   void *content, char *cbtype, size_t length, char *encoding,
+                   void *cbuserdata)
+{
+       cprintf("suff=%s|%s\n", partnum, cbtype);
+}
+
 
 /*
  * Callback function for mime parser that opens a section for downloading
@@ -750,8 +770,8 @@ struct CtdlMessage *CtdlFetchMessage(long msgnum)
        struct cdbdata *dmsgtext;
        struct CtdlMessage *ret = NULL;
        char *mptr;
-       CIT_UBYTE ch;
-       CIT_UBYTE field_header;
+       cit_uint8_t ch;
+       cit_uint8_t field_header;
        size_t field_length;
 
        dmsgtext = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
@@ -861,7 +881,7 @@ void fixed_output_pre(char *name, char *filename, char *partnum, char *disp,
 {
                lprintf(9, "fixed_output_pre() type=<%s>\n", cbtype);   
                if (!strcasecmp(cbtype, "multipart/alternative")) {
-                       ma->is_ma = 1;
+                       ++ma->is_ma;
                        ma->did_print = 0;
                        return;
                }
@@ -876,7 +896,7 @@ void fixed_output_post(char *name, char *filename, char *partnum, char *disp,
 {
                lprintf(9, "fixed_output_post() type=<%s>\n", cbtype);  
                if (!strcasecmp(cbtype, "multipart/alternative")) {
-                       ma->is_ma = 0;
+                       --ma->is_ma;
                        ma->did_print = 0;
                        return;
                }
@@ -930,6 +950,73 @@ void fixed_output(char *name, char *filename, char *partnum, char *disp,
                }
        }
 
+/*
+ * The client is elegant and sophisticated and wants to be choosy about
+ * MIME content types, so figure out which multipart/alternative part
+ * we're going to send.
+ */
+void choose_preferred(char *name, char *filename, char *partnum, char *disp,
+               void *content, char *cbtype, size_t length, char *encoding,
+               void *cbuserdata)
+{
+       char buf[SIZ];
+       int i;
+
+       if (ma->is_ma > 0) {
+               for (i=0; i<num_tokens(CC->preferred_formats, '|'); ++i) {
+                       extract(buf, CC->preferred_formats, i);
+                       if (!strcasecmp(buf, cbtype)) {
+                               strcpy(ma->chosen_part, partnum);
+                       }
+               }
+       }
+}
+
+/*
+ * Now that we've chosen our preferred part, output it.
+ */
+void output_preferred(char *name, char *filename, char *partnum, char *disp,
+               void *content, char *cbtype, size_t length, char *encoding,
+               void *cbuserdata)
+{
+       int i;
+       char buf[SIZ];
+       int add_newline = 0;
+       char *text_content;
+
+       /* This is not the MIME part you're looking for... */
+       if (strcasecmp(partnum, ma->chosen_part)) return;
+
+       /* If the content-type of this part is in our preferred formats
+        * list, we can simply output it verbatim.
+        */
+       for (i=0; i<num_tokens(CC->preferred_formats, '|'); ++i) {
+               extract(buf, CC->preferred_formats, i);
+               if (!strcasecmp(buf, cbtype)) {
+                       /* Yeah!  Go!  W00t!! */
+
+                       text_content = (char *)content;
+                       if (text_content[length-1] != '\n') {
+                               ++add_newline;
+                       }
+
+                       cprintf("Content-type: %s\n", cbtype);
+                       cprintf("Content-length: %d\n",
+                               (int)(length + add_newline) );
+                       cprintf("Content-transfer-encoding: %s\n", encoding);
+                       cprintf("\n");
+                       client_write(content, length);
+                       if (add_newline) cprintf("\n");
+                       return;
+               }
+       }
+
+       /* No translations required or possible: output as text/plain */
+       cprintf("Content-type: text/plain\n\n");
+       fixed_output(name, filename, partnum, disp, content, cbtype,
+                       length, encoding, cbuserdata);
+}
+
 
 /*
  * Get a message off disk.  (returns om_* values found in msgbase.h)
@@ -969,9 +1056,10 @@ int CtdlOutputMsg(long msg_num,           /* message number (local) to fetch */
         */
 
        /*
-        * Fetch the message from disk
+        * Fetch the message from disk.
         */
        TheMessage = CtdlFetchMessage(msg_num);
+
        if (TheMessage == NULL) {
                if (do_proto) cprintf("%d Can't locate msg %ld on disk\n",
                        ERROR, msg_num);
@@ -983,6 +1071,7 @@ int CtdlOutputMsg(long msg_num,            /* message number (local) to fetch */
                        headers_only, do_proto, crlf);
 
        CtdlFreeMessage(TheMessage);
+
        return(retcode);
 }
 
@@ -1000,7 +1089,7 @@ int CtdlOutputPreLoadedMsg(struct CtdlMessage *TheMessage,
 ) {
        int i, k;
        char buf[1024];
-       CIT_UBYTE ch;
+       cit_uint8_t ch;
        char allkeys[SIZ];
        char display_name[SIZ];
        char *mptr;
@@ -1054,25 +1143,23 @@ int CtdlOutputPreLoadedMsg(struct CtdlMessage *TheMessage,
                return((CC->download_fp != NULL) ? om_ok : om_mime_error);
        }
 
+       /* Does the caller want to skip the headers? */
+       if (headers_only == HEADERS_NONE) goto START_TEXT;
+
        /* now for the user-mode message reading loops */
        if (do_proto) cprintf("%d Message %ld:\n", LISTING_FOLLOWS, msg_num);
 
-       /* Tell the client which format type we're using.  If this is a
-        * MIME message, *lie* about it and tell the user it's fixed-format.
-        */
-       if (mode == MT_CITADEL) {
-               if (TheMessage->cm_format_type == FMT_RFC822) {
-                       if (do_proto) cprintf("type=1\n");
-               }
-               else {
-                       if (do_proto) cprintf("type=%d\n",
-                               TheMessage->cm_format_type);
-               }
+       /* Tell the client which format type we're using. */
+       if ( (mode == MT_CITADEL) && (do_proto) ) {
+               cprintf("type=%d\n", TheMessage->cm_format_type);
        }
 
        /* nhdr=yes means that we're only displaying headers, no body */
-       if ((TheMessage->cm_anon_type == MES_ANONONLY) && (mode == MT_CITADEL)) {
-               if (do_proto) cprintf("nhdr=yes\n");
+       if ( (TheMessage->cm_anon_type == MES_ANONONLY)
+           && (mode == MT_CITADEL)
+          && (do_proto)
+          ) {
+               cprintf("nhdr=yes\n");
        }
 
        /* begin header processing loop for Citadel message format */
@@ -1182,8 +1269,8 @@ int CtdlOutputPreLoadedMsg(struct CtdlMessage *TheMessage,
                                else if (i == 'R')
                                        cprintf("To: %s%s", mptr, nl);
                                else if (i == 'T') {
-                                       datestring(datestamp, sizeof datestamp, atol(mptr),
-                                               DATESTRING_RFC822 );
+                                       datestring(datestamp, sizeof datestamp,
+                                               atol(mptr), DATESTRING_RFC822);
                                        cprintf("Date: %s%s", datestamp, nl);
                                }
                        }
@@ -1220,23 +1307,18 @@ int CtdlOutputPreLoadedMsg(struct CtdlMessage *TheMessage,
        }
 
        /* end header processing loop ... at this point, we're in the text */
-
+START_TEXT:
        mptr = TheMessage->cm_fields['M'];
 
        /* Tell the client about the MIME parts in this message */
        if (TheMessage->cm_format_type == FMT_RFC822) {
-               if (mode == MT_CITADEL) {
+               if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
                        mime_parser(mptr, NULL,
-                               *list_this_part, NULL, NULL,
+                               *list_this_part,
+                               *list_this_pref,
+                               *list_this_suff,
                                NULL, 0);
                }
-               else if (mode == MT_MIME) {     /* list parts only */
-                       mime_parser(mptr, NULL,
-                               *list_this_part, NULL, NULL,
-                               NULL, 0);
-                       if (do_proto) cprintf("000\n");
-                       return(om_ok);
-               }
                else if (mode == MT_RFC822) {   /* unparsed RFC822 dump */
                        /* FIXME ... we have to put some code in here to avoid
                         * printing duplicate header information when both
@@ -1253,14 +1335,15 @@ int CtdlOutputPreLoadedMsg(struct CtdlMessage *TheMessage,
                }
        }
 
-       if (headers_only) {
+       if (headers_only == HEADERS_ONLY) {
                if (do_proto) cprintf("000\n");
                return(om_ok);
        }
 
        /* signify start of msg text */
-       if (mode == MT_CITADEL)
+       if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
                if (do_proto) cprintf("text\n");
+       }
        if (mode == MT_RFC822) {
                if (TheMessage->cm_fields['U'] == NULL) {
                        cprintf("Subject: (no subject)%s", nl);
@@ -1273,6 +1356,9 @@ int CtdlOutputPreLoadedMsg(struct CtdlMessage *TheMessage,
         * what message transfer format is in use.
         */
        if (TheMessage->cm_format_type == FMT_FIXED) {
+               if (mode == MT_MIME) {
+                       cprintf("Content-type: text/plain\n\n");
+               }
                strcpy(buf, "");
                while (ch = *mptr++, ch > 0) {
                        if (ch == 13)
@@ -1297,6 +1383,9 @@ int CtdlOutputPreLoadedMsg(struct CtdlMessage *TheMessage,
         * 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");
+               }
                memfmout(80, mptr, 0, nl);
        }
 
@@ -1308,9 +1397,20 @@ int CtdlOutputPreLoadedMsg(struct CtdlMessage *TheMessage,
        if (TheMessage->cm_format_type == FMT_RFC822) {
                CtdlAllocUserData(SYM_MA_INFO, sizeof(struct ma_info));
                memset(ma, 0, sizeof(struct ma_info));
-               mime_parser(mptr, NULL,
-                       *fixed_output, *fixed_output_pre, *fixed_output_post,
-                       NULL, 0);
+
+               if (mode == MT_MIME) {
+                       strcpy(ma->chosen_part, "1");
+                       mime_parser(mptr, NULL,
+                               *choose_preferred, *fixed_output_pre,
+                               *fixed_output_post, NULL, 0);
+                       mime_parser(mptr, NULL,
+                               *output_preferred, NULL, NULL, NULL, 0);
+               }
+               else {
+                       mime_parser(mptr, NULL,
+                               *fixed_output, *fixed_output_pre,
+                               *fixed_output_post, NULL, 0);
+               }
        }
 
        /* now we're done */
@@ -1326,7 +1426,7 @@ int CtdlOutputPreLoadedMsg(struct CtdlMessage *TheMessage,
 void cmd_msg0(char *cmdbuf)
 {
        long msgid;
-       int headers_only = 0;
+       int headers_only = HEADERS_ALL;
 
        msgid = extract_long(cmdbuf, 0);
        headers_only = extract_int(cmdbuf, 1);
@@ -1342,7 +1442,7 @@ void cmd_msg0(char *cmdbuf)
 void cmd_msg2(char *cmdbuf)
 {
        long msgid;
-       int headers_only = 0;
+       int headers_only = HEADERS_ALL;
 
        msgid = extract_long(cmdbuf, 0);
        headers_only = extract_int(cmdbuf, 1);
@@ -1392,7 +1492,7 @@ void cmd_msg3(char *cmdbuf)
 
 
 /* 
- * display a message (mode 4 - MIME) (FIXME ... still evolving, not complete)
+ * Display a message using MIME content types
  */
 void cmd_msg4(char *cmdbuf)
 {
@@ -1402,6 +1502,19 @@ void cmd_msg4(char *cmdbuf)
        CtdlOutputMsg(msgid, MT_MIME, 0, 1, 0);
 }
 
+
+
+/* 
+ * Client tells us its preferred message format(s)
+ */
+void cmd_msgp(char *cmdbuf)
+{
+       safestrncpy(CC->preferred_formats, cmdbuf,
+                       sizeof(CC->preferred_formats));
+       cprintf("%d ok\n", CIT_OK);
+}
+
+
 /*
  * Open a component of a MIME message as a download file 
  */
@@ -1770,7 +1883,12 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,      /* message to save */
                }
        }
 
-       strcpy(force_room, force);
+       if (force == NULL) {
+               strcpy(force_room, "");
+       }
+       else {
+               strcpy(force_room, force);
+       }
 
        /* Learn about what's inside, because it's what's inside that counts */
        lprintf(9, "Learning what's inside\n");
@@ -2010,25 +2128,32 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,     /* message to save */
 /*
  * Convenience function for generating small administrative messages.
  */
-void quickie_message(char *from, char *to, char *room, char *text)
+void quickie_message(char *from, char *to, char *room, char *text, 
+                       int format_type, char *subject)
 {
        struct CtdlMessage *msg;
+       struct recptypes *recp = NULL;
 
        msg = mallok(sizeof(struct CtdlMessage));
        memset(msg, 0, sizeof(struct CtdlMessage));
        msg->cm_magic = CTDLMESSAGE_MAGIC;
        msg->cm_anon_type = MES_NORMAL;
-       msg->cm_format_type = 0;
+       msg->cm_format_type = format_type;
        msg->cm_fields['A'] = strdoop(from);
-       msg->cm_fields['O'] = strdoop(room);
+       if (room != NULL) msg->cm_fields['O'] = strdoop(room);
        msg->cm_fields['N'] = strdoop(NODENAME);
-       if (to != NULL)
+       if (to != NULL) {
                msg->cm_fields['R'] = strdoop(to);
+               recp = validate_recipients(to);
+       }
+       if (subject != NULL) {
+               msg->cm_fields['U'] = strdoop(subject);
+       }
        msg->cm_fields['M'] = strdoop(text);
 
-       CtdlSubmitMsg(msg, NULL, room);
+       CtdlSubmitMsg(msg, recp, room);
        CtdlFreeMessage(msg);
-       syslog(LOG_NOTICE, text);
+       if (recp != NULL) phree(recp);
 }
 
 
@@ -2073,11 +2198,10 @@ char *CtdlReadMessageBody(char *terminator,     /* token signalling EOT */
        /* read in the lines of message text one by one */
        while ( (client_gets(buf)>0) && strcmp(buf, terminator) ) {
 
-               /* strip trailing newline type stuff */
-               if (buf[strlen(buf)-1]==10) buf[strlen(buf)-1]=0;
-               if (buf[strlen(buf)-1]==13) buf[strlen(buf)-1]=0;
-
+               /* Measure the line and strip trailing newline characters */
                linelen = strlen(buf);
+               if (linelen > 0) if (buf[linelen-1]==13) buf[linelen--]=0;
+               if (linelen > 0) if (buf[linelen-1]==10) buf[linelen--]=0;
 
                /* augment the buffer if we have to */
                if ((message_len + linelen + 2) > buffer_len) {
@@ -2850,43 +2974,71 @@ void CtdlWriteObject(char *req_room,            /* Room to stuff it in */
        struct CtdlMessage *msg;
        size_t len;
 
+       char *raw_message = NULL;
+       char *encoded_message = NULL;
+       off_t raw_length = 0;
+
        if (is_mailbox != NULL)
                MailboxName(roomname, sizeof roomname, is_mailbox, req_room);
        else
                safestrncpy(roomname, req_room, sizeof(roomname));
        lprintf(9, "CtdlWriteObject() to <%s> (flags=%d)\n", roomname, flags);
 
-       strcpy(filename, tmpnam(NULL));
-       fp = fopen(filename, "w");
-       if (fp == NULL)
-               return;
 
-       tempfp = fopen(tempfilename, "r");
-       if (tempfp == NULL) {
-               fclose(fp);
-               unlink(filename);
+       fp = fopen(tempfilename, "rb");
+       if (fp == NULL) {
+               lprintf(5, "Cannot open %s: %s\n",
+                       tempfilename, strerror(errno));
                return;
        }
+       fseek(fp, 0L, SEEK_END);
+       raw_length = ftell(fp);
+       rewind(fp);
+       lprintf(9, "Raw length is %ld\n", (long)raw_length);
 
-       fprintf(fp, "Content-type: %s\n", content_type);
-       lprintf(9, "Content-type: %s\n", content_type);
+       raw_message = mallok((size_t)raw_length);
+       fread(raw_message, (size_t)raw_length, 1, fp);
+       fclose(fp);
 
-       if (is_binary == 0) {
-               fprintf(fp, "Content-transfer-encoding: 7bit\n\n");
-               while (ch = getc(tempfp), ch > 0)
-                       putc(ch, fp);
-               fclose(tempfp);
-               putc(0, fp);
-               fclose(fp);
-       } else {
-               fprintf(fp, "Content-transfer-encoding: base64\n\n");
-               fclose(tempfp);
-               fclose(fp);
-               snprintf(cmdbuf, sizeof cmdbuf, "./base64 -e <%s >>%s",
-                       tempfilename, filename);
-               system(cmdbuf);
+       if (is_binary) {
+               encoded_message = mallok((size_t)
+                       (((raw_length * 134) / 100) + 4096 ) );
+       }
+       else {
+               encoded_message = mallok((size_t)(raw_length + 4096));
        }
 
+       sprintf(encoded_message, "Content-type: %s\n", content_type);
+
+       if (is_binary) {
+               sprintf(&encoded_message[strlen(encoded_message)],
+                       "Content-transfer-encoding: base64\n\n"
+               );
+       }
+       else {
+               sprintf(&encoded_message[strlen(encoded_message)],
+                       "Content-transfer-encoding: 7bit\n\n"
+               );
+       }
+
+       if (is_binary) {
+               CtdlEncodeBase64(
+                       &encoded_message[strlen(encoded_message)],
+                       raw_message,
+                       (int)raw_length
+               );
+       }
+       else {
+               raw_message[raw_length] = 0;
+               memcpy(
+                       &encoded_message[strlen(encoded_message)],
+                       raw_message,
+                       (int)(raw_length+1)
+               );
+       }
+
+       phree(raw_message);
+
        lprintf(9, "Allocating\n");
        msg = mallok(sizeof(struct CtdlMessage));
        memset(msg, 0, sizeof(struct CtdlMessage));
@@ -2899,15 +3051,7 @@ void CtdlWriteObject(char *req_room,             /* Room to stuff it in */
        msg->cm_fields['H'] = strdoop(config.c_humannode);
        msg->cm_flags = flags;
        
-       lprintf(9, "Loading\n");
-       fp = fopen(filename, "rb");
-       fseek(fp, 0L, SEEK_END);
-       len = ftell(fp);
-       rewind(fp);
-       msg->cm_fields['M'] = mallok(len);
-       fread(msg->cm_fields['M'], len, 1, fp);
-       fclose(fp);
-       unlink(filename);
+       msg->cm_fields['M'] = encoded_message;
 
        /* Create the requested room if we have to. */
        if (getroom(&qrbuf, roomname) != 0) {