]> code.citadel.org Git - citadel.git/blobdiff - citadel/msgbase.c
* Bugfixes
[citadel.git] / citadel / msgbase.c
index afbb717ff6b5f6589d8d0c6b689b0b2b06cdeca5..94405b53fc5465d02a5a869fdb6d75bca9eb4cc2 100644 (file)
 extern struct config config;
 long config_msgnum;
 
+
+/* 
+ * This really belongs in serv_network.c, but I don't know how to export
+ * symbols between modules.
+ */
+struct FilterList *filterlist = NULL;
+
+
+/*
+ * These are the four-character field headers we use when outputting
+ * messages in Citadel format (as opposed to RFC822 format).
+ */
 char *msgkeys[] = {
-       "", "", "", "", "", "", "", ""
-       "", "", "", "", "", "", "", ""
-       "", "", "", "", "", "", "", ""
-       "", "", "", "", "", "", "", ""
-       "", "", "", "", "", "", "", ""
-       "", "", "", "", "", "", "", ""
-       "", "", "", "", "", "", "", ""
-       "", "", "", "", "", "", "", ""
-       ""
+       NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
+       NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
+       NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
+       NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
+       NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
+       NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
+       NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
+       NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
+       NULL
        "from",
-       "", "", "",
+       NULL, NULL, NULL,
        "exti",
        "rfca",
-       ""
+       NULL
        "hnod",
        "msgn",
-       "", "", "",
+       NULL, NULL, NULL,
        "text",
        "node",
        "room",
        "path",
-       "",
+       NULL,
        "rcpt",
        "spec",
        "time",
        "subj",
-       "",
-       "",
-       "",
-       "",
-       ""
+       NULL,
+       NULL,
+       NULL,
+       NULL,
+       NULL
 };
 
 /*
@@ -131,6 +143,7 @@ int alias(char *name)
        char testnode[SIZ];
        char buf[SIZ];
 
+       striplt(name);
        remove_any_whitespace_to_the_left_or_right_of_at_symbol(name);
 
        fp = fopen("network/mail.aliases", "r");
@@ -157,6 +170,12 @@ int alias(char *name)
                        strcpy(name, bbb);
        }
        fclose(fp);
+
+       /* Hit the Global Address Book */
+       if (CtdlDirectoryLookup(aaa, name) == 0) {
+               strcpy(name, aaa);
+       }
+
        lprintf(7, "Mail is being forwarded to %s\n", name);
 
        /* Change "user @ xxx" to "user" if xxx is an alias for this host */
@@ -170,14 +189,12 @@ int alias(char *name)
        }
 
        /* determine local or remote type, see citadel.h */
-
        at = haschar(name, '@');
        if (at == 0) return(MES_LOCAL);         /* no @'s - local address */
        if (at > 1) return(MES_ERROR);          /* >1 @'s - invalid address */
        remove_any_whitespace_to_the_left_or_right_of_at_symbol(name);
 
        /* figure out the delivery mode */
-
        extract_token(node, name, 1, '@');
 
        /* If there are one or more dots in the nodename, we assume that it
@@ -267,6 +284,21 @@ int CtdlMsgCmp(struct CtdlMessage *msg, struct CtdlMessage *template) {
 }
 
 
+
+/*
+ * Retrieve the "seen" message list for the current room.
+ */
+void CtdlGetSeen(char *buf) {
+       struct visit vbuf;
+
+       /* Learn about the user and room in question */
+       CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
+
+       safestrncpy(buf, vbuf.v_seen, SIZ);
+}
+
+
+
 /*
  * Manipulate the "seen msgs" string.
  */
@@ -283,8 +315,6 @@ void CtdlSetSeen(long target_msgnum, int target_setting) {
        int num_msgs = 0;
 
        /* Learn about the user and room in question */
-       get_mm();
-       getuser(&CC->usersupp, CC->curr_user);
        CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
 
        /* Load the message list */
@@ -319,17 +349,24 @@ void CtdlSetSeen(long target_msgnum, int target_setting) {
                }
                if (  ((is_seen == 0) && (was_seen == 1))
                   || ((is_seen == 1) && (i == num_msgs-1)) ) {
+                       size_t tmp;
+
                        if ( (strlen(newseen) + 20) > SIZ) {
                                strcpy(newseen, &newseen[20]);
                                newseen[0] = '*';
                        }
-                       if (strlen(newseen) > 0) strcat(newseen, ",");
+                       tmp = strlen(newseen);
+                       if (tmp > 0) {
+                               strcat(newseen, ",");
+                               tmp++;
+                       }
                        if (lo == hi) {
-                               sprintf(&newseen[strlen(newseen)], "%ld", lo);
+                               snprintf(&newseen[tmp], sizeof newseen - tmp,
+                                        "%ld", lo);
                        }
                        else {
-                               sprintf(&newseen[strlen(newseen)], "%ld:%ld",
-                                       lo, hi);
+                               snprintf(&newseen[tmp], sizeof newseen - tmp,
+                                        "%ld:%ld", lo, hi);
                        }
                        lo = (-1L);
                        hi = (-1L);
@@ -349,7 +386,6 @@ void CtdlSetSeen(long target_msgnum, int target_setting) {
  * current room.  (Returns the number of messages processed.)
  */
 int CtdlForEachMessage(int mode, long ref,
-                       int moderation_level,
                        char *content_type,
                        struct CtdlMessage *compare,
                        void (*CallBack) (long, void *),
@@ -390,19 +426,23 @@ int CtdlForEachMessage(int mode, long ref,
         * Now begin the traversal.
         */
        if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
-               GetMetaData(&smi, msglist[a]);
-
-               /* Filter out messages that are moderated below the level
-                * currently being viewed at.
-                */
-               if (smi.meta_mod < moderation_level) {
-                       msglist[a] = 0L;
-               }
 
                /* If the caller is looking for a specific MIME type, filter
                 * out all messages which are not of the type requested.
                 */
                if (content_type != NULL) if (strlen(content_type) > 0) {
+
+                       /* This call to GetMetaData() sits inside this loop
+                        * so that we only do the extra database read per msg
+                        * if we need to.  Doing the extra read all the time
+                        * really kills the server.  If we ever need to use
+                        * metadata for another search criterion, we need to
+                        * move the read somewhere else -- but still be smart
+                        * enough to only do the read if the caller has
+                        * specified something that will need it.
+                        */
+                       GetMetaData(&smi, msglist[a]);
+
                        if (strcasecmp(smi.meta_content_type, content_type)) {
                                msglist[a] = 0L;
                        }
@@ -526,7 +566,6 @@ void cmd_msgs(char *cmdbuf)
        }
 
        CtdlForEachMessage(mode, cm_ref,
-               CC->usersupp.moderation_filter,
                NULL, template, simple_listing, NULL);
        if (template != NULL) CtdlFreeMessage(template);
        cprintf("000\n");
@@ -559,11 +598,11 @@ void do_help_subst(char *buffer)
        help_subst(buffer, "^humannode", config.c_humannode);
        help_subst(buffer, "^fqdn", config.c_fqdn);
        help_subst(buffer, "^username", CC->usersupp.fullname);
-       sprintf(buf2, "%ld", CC->usersupp.usernum);
+       snprintf(buf2, sizeof buf2, "%ld", CC->usersupp.usernum);
        help_subst(buffer, "^usernum", buf2);
        help_subst(buffer, "^sysadm", config.c_sysadm);
        help_subst(buffer, "^variantname", CITADEL);
-       sprintf(buf2, "%d", config.c_maxsessions);
+       snprintf(buf2, sizeof buf2, "%d", config.c_maxsessions);
        help_subst(buffer, "^maxsessions", buf2);
 }
 
@@ -669,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
@@ -794,7 +853,6 @@ void CtdlFreeMessage(struct CtdlMessage *msg)
 {
        int i;
 
-       lprintf(9, "CtdlFreeMessage() called\n");
        if (is_valid_message(msg) == 0) return;
 
        for (i = 0; i < 256; ++i)
@@ -823,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;
                }
@@ -838,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;
                }
@@ -854,7 +912,6 @@ void fixed_output(char *name, char *filename, char *partnum, char *disp,
                char *ptr;
                char *wptr;
                size_t wlen;
-               CIT_UBYTE ch = 0;
 
                lprintf(9, "fixed_output() type=<%s>\n", cbtype);       
 
@@ -870,26 +927,20 @@ void fixed_output(char *name, char *filename, char *partnum, char *disp,
        
                if ( (!strcasecmp(cbtype, "text/plain")) 
                   || (strlen(cbtype)==0) ) {
-                       wlen = length;
                        wptr = content;
-                       while (wlen--) {
-                               ch = *wptr++;
-                               /**********
-                               if (ch==10) cprintf("\r\n");
-                               else cprintf("%c", ch);
-                                **********/
-                               cprintf("%c", ch);
+                       if (length > 0) {
+                               client_write(wptr, length);
+                               if (wptr[length-1] != '\n') {
+                                       cprintf("\n");
+                               }
                        }
-                       if (ch != '\n') cprintf("\n");
                }
                else if (!strcasecmp(cbtype, "text/html")) {
                        ptr = html_to_ascii(content, 80, 0);
                        wlen = strlen(ptr);
-                       wptr = ptr;
-                       while (wlen--) {
-                               ch = *wptr++;
-                               if (ch==10) cprintf("\r\n");
-                               else cprintf("%c", ch);
+                       client_write(ptr, wlen);
+                       if (ptr[wlen-1] != '\n') {
+                               cprintf("\n");
                        }
                        phree(ptr);
                }
@@ -899,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",
+                               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)
@@ -938,9 +1056,22 @@ int CtdlOutputMsg(long msg_num,           /* message number (local) to fetch */
         */
 
        /*
-        * Fetch the message from disk
+        * Fetch the message from disk.  We also keep the most recently
+        * read message in memory, in case we want to read it again, or fetch
+        * MIME parts out of it, or whatever.
         */
-       TheMessage = CtdlFetchMessage(msg_num);
+       if ( (CC->cached_msg != NULL) && (CC->cached_msgnum == msg_num) ) {
+               TheMessage = CC->cached_msg;
+       }
+       else {
+               TheMessage = CtdlFetchMessage(msg_num);
+               if (CC->cached_msg != NULL) {
+                       phree(CC->cached_msg); /* FIXME efence? */
+               }
+               CC->cached_msg = TheMessage;
+               CC->cached_msgnum = msg_num;
+       }
+
        if (TheMessage == NULL) {
                if (do_proto) cprintf("%d Can't locate msg %ld on disk\n",
                        ERROR, msg_num);
@@ -951,7 +1082,9 @@ int CtdlOutputMsg(long msg_num,            /* message number (local) to fetch */
                        TheMessage, msg_num, mode,
                        headers_only, do_proto, crlf);
 
-       CtdlFreeMessage(TheMessage);
+       /* don't free the memory; we're keeping it in the cache */
+       /* CtdlFreeMessage(TheMessage); */
+
        return(retcode);
 }
 
@@ -974,6 +1107,7 @@ int CtdlOutputPreLoadedMsg(struct CtdlMessage *TheMessage,
        char display_name[SIZ];
        char *mptr;
        char *nl;       /* newline string */
+       int suppress_f = 0;
 
        /* buffers needed for RFC822 translation */
        char suser[SIZ];
@@ -985,7 +1119,7 @@ int CtdlOutputPreLoadedMsg(struct CtdlMessage *TheMessage,
        char datestamp[SIZ];
        /*                                       */
 
-       sprintf(mid, "%ld", msg_num);
+       snprintf(mid, sizeof mid, "%ld", msg_num);
        nl = (crlf ? "\r\n" : "\n");
 
        if (!is_valid_message(TheMessage)) {
@@ -1022,25 +1156,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 */
@@ -1063,21 +1195,39 @@ int CtdlOutputPreLoadedMsg(struct CtdlMessage *TheMessage,
                        if ((is_room_aide())
                            && ((TheMessage->cm_anon_type == MES_ANONONLY)
                             || (TheMessage->cm_anon_type == MES_ANONOPT))) {
-                               sprintf(&display_name[strlen(display_name)],
-                                       " [%s]", buf);
+                               size_t tmp = strlen(display_name);
+                               snprintf(&display_name[tmp],
+                                        sizeof display_name - tmp,
+                                        " [%s]", buf);
                        }
                }
 
+               /* Don't show Internet address for users on the
+                * local Citadel network.
+                */
+               suppress_f = 0;
+               if (TheMessage->cm_fields['N'] != NULL)
+                  if (strlen(TheMessage->cm_fields['N']) > 0)
+                     if (haschar(TheMessage->cm_fields['N'], '.') == 0) {
+                       suppress_f = 1;
+               }
+               
+               /* Now spew the header fields in the order we like them. */
                strcpy(allkeys, FORDER);
                for (i=0; i<strlen(allkeys); ++i) {
                        k = (int) allkeys[i];
                        if (k != 'M') {
-                               if (TheMessage->cm_fields[k] != NULL) {
+                               if ( (TheMessage->cm_fields[k] != NULL)
+                                  && (msgkeys[k] != NULL) ) {
                                        if (k == 'A') {
                                                if (do_proto) cprintf("%s=%s\n",
                                                        msgkeys[k],
                                                        display_name);
                                        }
+                                       else if ((k == 'F') && (suppress_f)) {
+                                               /* do nothing */
+                                       }
+                                       /* Masquerade display name if needed */
                                        else {
                                                if (do_proto) cprintf("%s=%s\n",
                                                        msgkeys[k],
@@ -1119,19 +1269,21 @@ int CtdlOutputPreLoadedMsg(struct CtdlMessage *TheMessage,
                                else if (i == 'U')
                                        cprintf("Subject: %s%s", mptr, nl);
                                else if (i == 'I')
-                                       strcpy(mid, mptr);
+                                       safestrncpy(mid, mptr, sizeof mid);
                                else if (i == 'H')
-                                       strcpy(lnode, mptr);
+                                       safestrncpy(lnode, mptr, sizeof lnode);
+                               else if (i == 'F')
+                                       safestrncpy(fuser, mptr, sizeof fuser);
                                else if (i == 'O')
                                        cprintf("X-Citadel-Room: %s%s",
                                                mptr, nl);
                                else if (i == 'N')
-                                       strcpy(snode, mptr);
+                                       safestrncpy(snode, mptr, sizeof snode);
                                else if (i == 'R')
                                        cprintf("To: %s%s", mptr, nl);
                                else if (i == 'T') {
-                                       datestring(datestamp, atol(mptr),
-                                               DATESTRING_RFC822 );
+                                       datestring(datestamp, sizeof datestamp,
+                                               atol(mptr), DATESTRING_RFC822);
                                        cprintf("Date: %s%s", datestamp, nl);
                                }
                        }
@@ -1168,23 +1320,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
@@ -1201,14 +1348,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);
@@ -1221,6 +1369,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)
@@ -1245,6 +1396,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);
        }
 
@@ -1256,9 +1410,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 */
@@ -1274,7 +1439,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);
@@ -1290,7 +1455,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);
@@ -1340,7 +1505,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)
 {
@@ -1350,6 +1515,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 
  */
@@ -1506,7 +1684,7 @@ long send_message(struct CtdlMessage *msg,        /* pointer to buffer */
 
        /* Get a new message number */
        newmsgid = get_new_message_number();
-       sprintf(msgidbuf, "%ld@%s", newmsgid, config.c_fqdn);
+       snprintf(msgidbuf, sizeof msgidbuf, "%ld@%s", newmsgid, config.c_fqdn);
 
        /* Generate an ID if we don't have one already */
        if (msg->cm_fields['I']==NULL) {
@@ -1647,8 +1825,7 @@ int ReplicationChecks(struct CtdlMessage *msg) {
        memset(template, 0, sizeof(struct CtdlMessage));
        template->cm_fields['E'] = strdoop(msg->cm_fields['E']);
 
-       CtdlForEachMessage(MSGS_ALL, 0L, (-127), NULL, template,
-               check_repl, NULL);
+       CtdlForEachMessage(MSGS_ALL, 0L, NULL, template, check_repl, NULL);
 
        /* If a newer message exists with the same Extended ID, abort
         * this save.
@@ -1698,7 +1875,7 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,       /* message to save */
         */
        if (msg->cm_fields['T'] == NULL) {
                lprintf(9, "Generating timestamp\n");
-               sprintf(aaa, "%ld", (long)time(NULL));
+               snprintf(aaa, sizeof aaa, "%ld", (long)time(NULL));
                msg->cm_fields['T'] = strdoop(aaa);
        }
 
@@ -1719,7 +1896,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");
@@ -1824,7 +2006,7 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,       /* message to save */
        if ((!CC->internal_pgm) || (recps == NULL)) {
                if (CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 0) != 0) {
                        lprintf(3, "ERROR saving message pointer!\n");
-                       CtdlSaveMsgPointerInRoom(AIDEROOM, newmsgid, 0);
+                       CtdlSaveMsgPointerInRoom(config.c_aideroom, newmsgid, 0);
                }
        }
 
@@ -1859,12 +2041,13 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,     /* message to save */
                lprintf(9, "Delivering private local mail to <%s>\n",
                        recipient);
                if (getuser(&userbuf, recipient) == 0) {
-                       MailboxName(actual_rm, &userbuf, MAILROOM);
+                       MailboxName(actual_rm, sizeof actual_rm, &userbuf, MAILROOM);
                        CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 0);
+                       BumpNewMailCounter(userbuf.usernum);
                }
                else {
                        lprintf(9, "No user <%s>\n", recipient);
-                       CtdlSaveMsgPointerInRoom(AIDEROOM, newmsgid, 0);
+                       CtdlSaveMsgPointerInRoom(config.c_aideroom, newmsgid, 0);
                }
        }
 
@@ -1894,7 +2077,7 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,       /* message to save */
                
                serialize_message(&smr, msg);
                if (smr.len > 0) {
-                       sprintf(aaa,
+                       snprintf(aaa, sizeof aaa,
                                "./network/spoolin/netmail.%04lx.%04x.%04x",
                                (long) getpid(), CC->cs_pid, ++seqnum);
                        network_fp = fopen(aaa, "wb+");
@@ -1925,7 +2108,7 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,       /* message to save */
         if (recps->num_internet > 0) {
                lprintf(9, "Generating delivery instructions\n");
                instr = mallok(SIZ * 2);
-               sprintf(instr,
+               snprintf(instr, SIZ * 2,
                        "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
                        "bounceto|%s@%s\n",
                        SPOOLMIME, newmsgid, (long)time(NULL),
@@ -1933,9 +2116,10 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,      /* message to save */
                );
 
                for (i=0; i<num_tokens(recps->recp_internet, '|'); ++i) {
+                       size_t tmp = strlen(instr);
                        extract(recipient, recps->recp_internet, i);
-                       sprintf(&instr[strlen(instr)],
-                               "remote|%s|0||\n", recipient);
+                       snprintf(&instr[tmp], SIZ * 2 - tmp,
+                                "remote|%s|0||\n", recipient);
                }
 
                imsg = mallok(sizeof(struct CtdlMessage));
@@ -1957,25 +2141,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);
 }
 
 
@@ -1997,10 +2188,18 @@ char *CtdlReadMessageBody(char *terminator,     /* token signalling EOT */
 
        if (exist == NULL) {
                m = mallok(4096);
+               m[0] = 0;
+               buffer_len = 4096;
+               message_len = 0;
        }
        else {
-               m = reallok(exist, strlen(exist) + 4096);
-               if (m == NULL) phree(exist);
+               message_len = strlen(exist);
+               buffer_len = message_len + 4096;
+               m = reallok(exist, buffer_len);
+               if (m == NULL) {
+                       phree(exist);
+                       return m;
+               }
        }
 
        /* flush the input if we have nowhere to store it */
@@ -2009,12 +2208,6 @@ char *CtdlReadMessageBody(char *terminator,      /* token signalling EOT */
                return(NULL);
        }
 
-       /* otherwise read it into memory */
-       else {
-               buffer_len = 4096;
-               m[0] = 0;
-               message_len = 0;
-       }
        /* read in the lines of message text one by one */
        while ( (client_gets(buf)>0) && strcmp(buf, terminator) ) {
 
@@ -2072,7 +2265,8 @@ static struct CtdlMessage *make_message(
        char *room,                     /* room where it's going */
        int type,                       /* see MES_ types in header file */
        int format_type,                /* variformat, plain text, MIME... */
-       char *fake_name                 /* who we're masquerading as */
+       char *fake_name,                /* who we're masquerading as */
+       char *subject                   /* Subject (optional) */
 ) {
        char dest_node[SIZ];
        char buf[SIZ];
@@ -2089,10 +2283,10 @@ static struct CtdlMessage *make_message(
 
        striplt(recipient);
 
-       sprintf(buf, "cit%ld", author->usernum);                /* Path */
+       snprintf(buf, sizeof buf, "cit%ld", author->usernum);   /* Path */
        msg->cm_fields['P'] = strdoop(buf);
 
-       sprintf(buf, "%ld", (long)time(NULL));                  /* timestamp */
+       snprintf(buf, sizeof buf, "%ld", (long)time(NULL));     /* timestamp */
        msg->cm_fields['T'] = strdoop(buf);
 
        if (fake_name[0])                                       /* author */
@@ -2117,6 +2311,17 @@ static struct CtdlMessage *make_message(
                msg->cm_fields['D'] = strdoop(dest_node);
        }
 
+       if ( (author == &CC->usersupp) && (CC->cs_inet_email != NULL) ) {
+               msg->cm_fields['F'] = strdoop(CC->cs_inet_email);
+       }
+
+       if (subject != NULL) {
+               striplt(subject);
+               if (strlen(subject) > 0) {
+                       msg->cm_fields['U'] = strdoop(subject);
+               }
+       }
+
        msg->cm_fields['M'] = CtdlReadMessageBody("000",
                                                config.c_maxmsglen, NULL);
 
@@ -2129,29 +2334,29 @@ static struct CtdlMessage *make_message(
  * room.  Returns a *CITADEL ERROR CODE* and puts a message in errmsgbuf, or
  * returns 0 on success.
  */
-int CtdlDoIHavePermissionToPostInThisRoom(char *errmsgbuf) {
+int CtdlDoIHavePermissionToPostInThisRoom(char *errmsgbuf, size_t n) {
 
        if (!(CC->logged_in)) {
-               sprintf(errmsgbuf, "Not logged in.");
+               snprintf(errmsgbuf, n, "Not logged in.");
                return (ERROR + NOT_LOGGED_IN);
        }
 
        if ((CC->usersupp.axlevel < 2)
            && ((CC->quickroom.QRflags & QR_MAILBOX) == 0)) {
-               sprintf(errmsgbuf, "Need to be validated to enter "
+               snprintf(errmsgbuf, n, "Need to be validated to enter "
                                "(except in %s> to sysop)", MAILROOM);
                return (ERROR + HIGHER_ACCESS_REQUIRED);
        }
 
        if ((CC->usersupp.axlevel < 4)
           && (CC->quickroom.QRflags & QR_NETWORK)) {
-               sprintf(errmsgbuf, "Need net privileges to enter here.");
+               snprintf(errmsgbuf, n, "Need net privileges to enter here.");
                return (ERROR + HIGHER_ACCESS_REQUIRED);
        }
 
        if ((CC->usersupp.axlevel < 6)
           && (CC->quickroom.QRflags & QR_READONLY)) {
-               sprintf(errmsgbuf, "Sorry, this is a read-only room.");
+               snprintf(errmsgbuf, n, "Sorry, this is a read-only room.");
                return (ERROR + HIGHER_ACCESS_REQUIRED);
        }
 
@@ -2167,12 +2372,14 @@ int CtdlDoIHavePermissionToPostInThisRoom(char *errmsgbuf) {
 struct recptypes *validate_recipients(char *recipients) {
        struct recptypes *ret;
        char this_recp[SIZ];
+       char this_recp_cooked[SIZ];
        char append[SIZ];
        int num_recps;
-       int i;
+       int i, j;
        int mailtype;
        int invalid;
        struct usersupp tempUS;
+       struct quickroom tempQR;
 
        /* Initialize */
        ret = (struct recptypes *) malloc(sizeof(struct recptypes));
@@ -2183,6 +2390,7 @@ struct recptypes *validate_recipients(char *recipients) {
        ret->num_internet = 0;
        ret->num_ignet = 0;
        ret->num_error = 0;
+       ret->num_room = 0;
 
        if (recipients == NULL) {
                num_recps = 0;
@@ -2207,12 +2415,22 @@ struct recptypes *validate_recipients(char *recipients) {
                striplt(this_recp);
                lprintf(9, "Evaluating recipient #%d <%s>\n", i, this_recp);
                mailtype = alias(this_recp);
+               mailtype = alias(this_recp);
+               mailtype = alias(this_recp);
+               for (j=0; j<=strlen(this_recp); ++j) {
+                       if (this_recp[j]=='_') {
+                               this_recp_cooked[j] = ' ';
+                       }
+                       else {
+                               this_recp_cooked[j] = this_recp[j];
+                       }
+               }
                invalid = 0;
                switch(mailtype) {
                        case MES_LOCAL:
                                if (!strcasecmp(this_recp, "sysop")) {
                                        ++ret->num_room;
-                                       strcpy(this_recp, AIDEROOM);
+                                       strcpy(this_recp, config.c_aideroom);
                                        if (strlen(ret->recp_room) > 0) {
                                                strcat(ret->recp_room, "|");
                                        }
@@ -2226,6 +2444,22 @@ struct recptypes *validate_recipients(char *recipients) {
                                        }
                                        strcat(ret->recp_local, this_recp);
                                }
+                               else if (getuser(&tempUS, this_recp_cooked) == 0) {
+                                       ++ret->num_local;
+                                       strcpy(this_recp, tempUS.fullname);
+                                       if (strlen(ret->recp_local) > 0) {
+                                               strcat(ret->recp_local, "|");
+                                       }
+                                       strcat(ret->recp_local, this_recp);
+                               }
+                               else if ( (!strncasecmp(this_recp, "room_", 5))
+                                     && (!getroom(&tempQR, &this_recp_cooked[5])) ) {
+                                       ++ret->num_room;
+                                       if (strlen(ret->recp_room) > 0) {
+                                               strcat(ret->recp_room, "|");
+                                       }
+                                       strcat(ret->recp_room, &this_recp_cooked[5]);
+                               }
                                else {
                                        ++ret->num_error;
                                        invalid = 1;
@@ -2252,13 +2486,13 @@ struct recptypes *validate_recipients(char *recipients) {
                }
                if (invalid) {
                        if (strlen(ret->errormsg) == 0) {
-                               sprintf(append,
-                                       "Invalid recipient: %s",
-                                       this_recp);
+                               snprintf(append, sizeof append,
+                                        "Invalid recipient: %s",
+                                        this_recp);
                        }
                        else {
-                               sprintf(append, 
-                                       ", %s", this_recp);
+                               snprintf(append, sizeof append,
+                                        ", %s", this_recp);
                        }
                        if ( (strlen(ret->errormsg) + strlen(append)) < SIZ) {
                                strcat(ret->errormsg, append);
@@ -2269,7 +2503,8 @@ struct recptypes *validate_recipients(char *recipients) {
                                strcpy(append, this_recp);
                        }
                        else {
-                               sprintf(append, ", %s", this_recp);
+                               snprintf(append, sizeof append, ", %s",
+                                        this_recp);
                        }
                        if ( (strlen(ret->display_recp)+strlen(append)) < SIZ) {
                                strcat(ret->display_recp, append);
@@ -2283,6 +2518,13 @@ struct recptypes *validate_recipients(char *recipients) {
                strcpy(ret->errormsg, "No recipients specified.");
        }
 
+       lprintf(9, "validate_recipients()\n");
+       lprintf(9, " local: %d <%s>\n", ret->num_local, ret->recp_local);
+       lprintf(9, "  room: %d <%s>\n", ret->num_room, ret->recp_room);
+       lprintf(9, "  inet: %d <%s>\n", ret->num_internet, ret->recp_internet);
+       lprintf(9, " ignet: %d <%s>\n", ret->num_ignet, ret->recp_ignet);
+       lprintf(9, " error: %d <%s>\n", ret->num_error, ret->errormsg);
+
        return(ret);
 }
 
@@ -2304,15 +2546,17 @@ void cmd_ent0(char *entargs)
        char errmsg[SIZ];
        int err = 0;
        struct recptypes *valid = NULL;
+       char subject[SIZ];
 
        post = extract_int(entargs, 0);
        extract(recp, entargs, 1);
        anon_flag = extract_int(entargs, 2);
        format_type = extract_int(entargs, 3);
+       extract(subject, entargs, 4);
 
        /* first check to make sure the request is valid. */
 
-       err = CtdlDoIHavePermissionToPostInThisRoom(errmsg);
+       err = CtdlDoIHavePermissionToPostInThisRoom(errmsg, sizeof errmsg);
        if (err) {
                cprintf("%d %s\n", err, errmsg);
                return;
@@ -2330,12 +2574,18 @@ void cmd_ent0(char *entargs)
                memset(CC->fake_postname, 0, sizeof(CC->fake_postname) );
                safestrncpy(CC->fake_postname, newusername,
                        sizeof(CC->fake_postname) );
-               cprintf("%d ok\n", OK);
+               cprintf("%d ok\n", CIT_OK);
                return;
        }
        CC->cs_flags |= CS_POSTING;
 
-       if (CC->quickroom.QRflags & QR_MAILBOX) {
+       /* In the Mail> room we have to behave a little differently --
+        * make sure the user has specified at least one recipient.  Then
+        * validate the recipient(s).
+        */
+       if ( (CC->quickroom.QRflags & QR_MAILBOX)
+          && (!strcasecmp(&CC->quickroom.QRname[11], MAILROOM)) ) {
+
                if (CC->usersupp.axlevel < 2) {
                        strcpy(recp, "sysop");
                }
@@ -2386,7 +2636,7 @@ void cmd_ent0(char *entargs)
         * success without creating the message.
         */
        if (post == 0) {
-               cprintf("%d %s\n", OK,
+               cprintf("%d %s\n", CIT_OK,
                        ((valid != NULL) ? valid->display_recp : "") );
                phree(valid);
                return;
@@ -2406,7 +2656,8 @@ void cmd_ent0(char *entargs)
        /* Read in the message from the client. */
        cprintf("%d send message\n", SEND_LISTING);
        msg = make_message(&CC->usersupp, recp,
-               CC->quickroom.QRname, anonymous, format_type, masquerade_as);
+               CC->quickroom.QRname, anonymous, format_type,
+               masquerade_as, subject);
 
        if (msg != NULL) {
                CtdlSubmitMsg(msg, valid, "");
@@ -2432,6 +2683,7 @@ int CtdlDeleteMessages(char *room_name,           /* which room */
        struct quickroom qrbuf;
        struct cdbdata *cdbfr;
        long *msglist = NULL;
+       long *dellist = NULL;
        int num_msgs = 0;
        int i;
        int num_deleted = 0;
@@ -2451,6 +2703,7 @@ int CtdlDeleteMessages(char *room_name,           /* which room */
 
        if (cdbfr != NULL) {
                msglist = mallok(cdbfr->len);
+               dellist = mallok(cdbfr->len);
                memcpy(msglist, cdbfr->ptr, cdbfr->len);
                num_msgs = cdbfr->len / sizeof(long);
                cdb_free(cdbfr);
@@ -2476,9 +2729,8 @@ int CtdlDeleteMessages(char *room_name,           /* which room */
 
                        /* Delete message only if all bits are set */
                        if (delete_this == 0x03) {
-                               AdjRefCount(msglist[i], -1);
+                               dellist[num_deleted++] = msglist[i];
                                msglist[i] = 0L;
-                               ++num_deleted;
                        }
                }
 
@@ -2487,9 +2739,25 @@ int CtdlDeleteMessages(char *room_name,          /* which room */
                          msglist, (num_msgs * sizeof(long)));
 
                qrbuf.QRhighest = msglist[num_msgs - 1];
-               phree(msglist);
        }
        lputroom(&qrbuf);
+
+       /* Go through the messages we pulled out of the index, and decrement
+        * their reference counts by 1.  If this is the only room the message
+        * was in, the reference count will reach zero and the message will
+        * automatically be deleted from the database.  We do this in a
+        * separate pass because there might be plug-in hooks getting called,
+        * and we don't want that happening during an S_QUICKROOM critical
+        * section.
+        */
+       if (num_deleted) for (i=0; i<num_deleted; ++i) {
+               PerformDeleteHooks(qrbuf.QRname, dellist[i]);
+               AdjRefCount(dellist[i], -1);
+       }
+
+       /* Now free the memory we used, and go away. */
+       if (msglist != NULL) phree(msglist);
+       if (dellist != NULL) phree(dellist);
        lprintf(9, "%d message(s) deleted.\n", num_deleted);
        return (num_deleted);
 }
@@ -2531,7 +2799,7 @@ void cmd_dele(char *delstr)
        num_deleted = CtdlDeleteMessages(CC->quickroom.QRname, delnum, "");
 
        if (num_deleted) {
-               cprintf("%d %d message%s deleted.\n", OK,
+               cprintf("%d %d message%s deleted.\n", CIT_OK,
                        num_deleted, ((num_deleted != 1) ? "s" : ""));
        } else {
                cprintf("%d Message %ld not found.\n", ERROR, delnum);
@@ -2570,19 +2838,27 @@ void cmd_move(char *args)
        targ[ROOMNAMELEN - 1] = 0;
        is_copy = extract_int(args, 2);
 
+       if (getroom(&qtemp, targ) != 0) {
+               cprintf("%d '%s' does not exist.\n", ERROR, targ);
+               return;
+       }
+
        getuser(&CC->usersupp, CC->curr_user);
+       /* Aides can move/copy */
        if ((CC->usersupp.axlevel < 6)
-           && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
+           /* Roomaides can move/copy */
+           && (CC->usersupp.usernum != CC->quickroom.QRroomaide)
+           /* Permit move/copy to/from personal rooms */
+           && (!((CC->quickroom.QRflags & QR_MAILBOX)
+                           && (qtemp.QRflags & QR_MAILBOX)))
+           /* Permit only copy from public to personal room */
+           && (!(is_copy && !(CC->quickroom.QRflags & QR_MAILBOX)
+                           && (qtemp.QRflags & QR_MAILBOX)))) {
                cprintf("%d Higher access required.\n",
                        ERROR + HIGHER_ACCESS_REQUIRED);
                return;
        }
 
-       if (getroom(&qtemp, targ) != 0) {
-               cprintf("%d '%s' does not exist.\n", ERROR, targ);
-               return;
-       }
-
        err = CtdlCopyMsgToRoom(num, targ);
        if (err != 0) {
                cprintf("%d Cannot store message in %s: error %d\n",
@@ -2597,7 +2873,7 @@ void cmd_move(char *args)
                CtdlDeleteMessages(CC->quickroom.QRname, num, "");
        }
 
-       cprintf("%d Message %s.\n", OK, (is_copy ? "copied" : "moved") );
+       cprintf("%d Message %s.\n", CIT_OK, (is_copy ? "copied" : "moved") );
 }
 
 
@@ -2713,7 +2989,7 @@ void CtdlWriteObject(char *req_room,              /* Room to stuff it in */
        size_t len;
 
        if (is_mailbox != NULL)
-               MailboxName(roomname, is_mailbox, req_room);
+               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);
@@ -2744,7 +3020,7 @@ void CtdlWriteObject(char *req_room,              /* Room to stuff it in */
                fprintf(fp, "Content-transfer-encoding: base64\n\n");
                fclose(tempfp);
                fclose(fp);
-               sprintf(cmdbuf, "./base64 -e <%s >>%s",
+               snprintf(cmdbuf, sizeof cmdbuf, "./base64 -e <%s >>%s",
                        tempfilename, filename);
                system(cmdbuf);
        }
@@ -2775,7 +3051,7 @@ void CtdlWriteObject(char *req_room,              /* Room to stuff it in */
        if (getroom(&qrbuf, roomname) != 0) {
                create_room(roomname, 
                        ( (is_mailbox != NULL) ? 5 : 3 ),
-                       "", 0, 1);
+                       "", 0, 1, 0);
        }
        /* If the caller specified this object as unique, delete all
         * other objects of this type that are currently in the room.
@@ -2816,7 +3092,7 @@ char *CtdlGetSysConfig(char *sysconfname) {
        /* We want the last (and probably only) config in this room */
        begin_critical_section(S_CONFIG);
        config_msgnum = (-1L);
-       CtdlForEachMessage(MSGS_LAST, 1, (-127), sysconfname, NULL,
+       CtdlForEachMessage(MSGS_LAST, 1, sysconfname, NULL,
                CtdlGetSysConfigBackend, NULL);
        msgnum = config_msgnum;
        end_critical_section(S_CONFIG);