* Renamed:
[citadel.git] / citadel / msgbase.c
index edafb1a6df69b3d5194920d2a5df711bb1132122..c4f7550575b97ac635f5d77a72a068c01b0327b3 100644 (file)
@@ -36,7 +36,7 @@
 #include <sys/stat.h>
 #include "citadel.h"
 #include "server.h"
-#include "dynloader.h"
+#include "serv_extensions.h"
 #include "database.h"
 #include "msgbase.h"
 #include "support.h"
@@ -284,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->user, &CC->room);
+
+       safestrncpy(buf, vbuf.v_seen, SIZ);
+}
+
+
+
 /*
  * Manipulate the "seen msgs" string.
  */
@@ -300,12 +315,10 @@ 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);
+       CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
 
        /* Load the message list */
-       cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->quickroom.QRnumber, sizeof(long));
+       cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
        if (cdbfr != NULL) {
                msglist = mallok(cdbfr->len);
                memcpy(msglist, cdbfr->ptr, cdbfr->len);
@@ -364,7 +377,7 @@ void CtdlSetSeen(long target_msgnum, int target_setting) {
        safestrncpy(vbuf.v_seen, newseen, SIZ);
        lprintf(9, " after optimize: %s\n", vbuf.v_seen);
        phree(msglist);
-       CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
+       CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
 }
 
 
@@ -373,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 *),
@@ -395,11 +407,11 @@ int CtdlForEachMessage(int mode, long ref,
 
        /* Learn about the user and room in question */
        get_mm();
-       getuser(&CC->usersupp, CC->curr_user);
-       CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
+       getuser(&CC->user, CC->curr_user);
+       CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
 
        /* Load the message list */
-       cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->quickroom.QRnumber, sizeof(long));
+       cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
        if (cdbfr != NULL) {
                msglist = mallok(cdbfr->len);
                memcpy(msglist, cdbfr->ptr, cdbfr->len);
@@ -414,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;
                        }
@@ -474,7 +490,7 @@ int CtdlForEachMessage(int mode, long ref,
                                || ((mode == MSGS_EQ) && (thismsg == ref))
                            )
                            ) {
-                               if ((mode == MSGS_NEW) && (CC->usersupp.flags & US_LASTOLD) && (lastold > 0L) && (printed_lastold == 0) && (!is_seen)) {
+                               if ((mode == MSGS_NEW) && (CC->user.flags & US_LASTOLD) && (lastold > 0L) && (printed_lastold == 0) && (!is_seen)) {
                                        if (CallBack)
                                                CallBack(lastold, userdata);
                                        printed_lastold = 1;
@@ -550,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");
@@ -582,13 +597,14 @@ void do_help_subst(char *buffer)
        help_subst(buffer, "^nodename", config.c_nodename);
        help_subst(buffer, "^humannode", config.c_humannode);
        help_subst(buffer, "^fqdn", config.c_fqdn);
-       help_subst(buffer, "^username", CC->usersupp.fullname);
-       snprintf(buf2, sizeof buf2, "%ld", CC->usersupp.usernum);
+       help_subst(buffer, "^username", CC->user.fullname);
+       snprintf(buf2, sizeof buf2, "%ld", CC->user.usernum);
        help_subst(buffer, "^usernum", buf2);
        help_subst(buffer, "^sysadm", config.c_sysadm);
        help_subst(buffer, "^variantname", CITADEL);
        snprintf(buf2, sizeof buf2, "%d", config.c_maxsessions);
        help_subst(buffer, "^maxsessions", buf2);
+       help_subst(buffer, "^bbsdir", BBSDIR);
 }
 
 
@@ -609,7 +625,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];
 
@@ -693,6 +709,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
@@ -735,8 +771,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));
@@ -846,7 +882,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;
                }
@@ -861,7 +897,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;
                }
@@ -877,7 +913,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);       
 
@@ -893,26 +928,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);
                }
@@ -922,6 +951,78 @@ 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) );
+                       if (strlen(encoding) > 0) {
+                               cprintf("Content-transfer-encoding: %s\n", encoding);
+                       }
+                       else {
+                               cprintf("Content-transfer-encoding: 7bit\n");
+                       }
+                       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)
@@ -961,9 +1062,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);
@@ -975,6 +1077,7 @@ int CtdlOutputMsg(long msg_num,            /* message number (local) to fetch */
                        headers_only, do_proto, crlf);
 
        CtdlFreeMessage(TheMessage);
+
        return(retcode);
 }
 
@@ -992,12 +1095,13 @@ 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;
        char *nl;       /* newline string */
        int suppress_f = 0;
+       int subject_found = 0;
 
        /* buffers needed for RFC822 translation */
        char suser[SIZ];
@@ -1049,22 +1153,20 @@ int CtdlOutputPreLoadedMsg(struct CtdlMessage *TheMessage,
        /* 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);
-               }
+       /* 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. */
+       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 */
@@ -1158,8 +1260,10 @@ int CtdlOutputPreLoadedMsg(struct CtdlMessage *TheMessage,
                                        cprintf("Path: %s%s", mptr, nl);
                                }
  ****/
-                               else if (i == 'U')
+                               else if (i == 'U') {
                                        cprintf("Subject: %s%s", mptr, nl);
+                                       subject_found = 1;
+                               }
                                else if (i == 'I')
                                        safestrncpy(mid, mptr, sizeof mid);
                                else if (i == 'H')
@@ -1174,12 +1278,15 @@ 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);
                                }
                        }
                }
+               if (subject_found == 0) {
+                       cprintf("Subject: (no subject)%s", nl);
+               }
        }
 
        for (i=0; i<strlen(suser); ++i) {
@@ -1209,55 +1316,79 @@ int CtdlOutputPreLoadedMsg(struct CtdlMessage *TheMessage,
                }
 
                cprintf("Organization: %s%s", lnode, nl);
+
+               /* Blank line signifying RFC822 end-of-headers */
+               if (TheMessage->cm_format_type != FMT_RFC822) {
+                       cprintf("%s", nl);
+               }
        }
 
        /* 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
                         * Citadel and RFC822 headers exist.  Preference should
                         * probably be given to the RFC822 headers.
                         */
+                       int done_rfc822_hdrs = 0;
                        while (ch=*(mptr++), ch!=0) {
-                               if (ch==13) ;
-                               else if (ch==10) cprintf("%s", nl);
-                               else cprintf("%c", ch);
+                               if (ch==13) {
+                                       /* do nothing */
+                               }
+                               else if (ch==10) {
+                                       if (!done_rfc822_hdrs) {
+                                               if (headers_only != HEADERS_NONE) {
+                                                       cprintf("%s", nl);
+                                               }
+                                       }
+                                       else {
+                                               if (headers_only != HEADERS_ONLY) {
+                                                       cprintf("%s", nl);
+                                               }
+                                       }
+                                       if ((*(mptr) == 13) || (*(mptr) == 10)) {
+                                               done_rfc822_hdrs = 1;
+                                       }
+                               }
+                               else {
+                                       if (done_rfc822_hdrs) {
+                                               if (headers_only != HEADERS_NONE) {
+                                                       cprintf("%c", ch);
+                                               }
+                                       }
+                                       else {
+                                               if (headers_only != HEADERS_ONLY) {
+                                                       cprintf("%c", ch);
+                                               }
+                                       }
+                                       if ((*mptr == 13) || (*mptr == 10)) {
+                                               done_rfc822_hdrs = 1;
+                                       }
+                               }
                        }
-                       if (do_proto) cprintf("000\n");
-                       return(om_ok);
+                       goto DONE;
                }
        }
 
-       if (headers_only) {
-               if (do_proto) cprintf("000\n");
-               return(om_ok);
+       if (headers_only == HEADERS_ONLY) {
+               goto DONE;
        }
 
        /* 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);
-               }
-               cprintf("%s", nl);
        }
 
        /* If the format type on disk is 1 (fixed-format), then we want
@@ -1265,6 +1396,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)
@@ -1289,6 +1423,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);
        }
 
@@ -1300,12 +1437,23 @@ 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 */
+DONE:  /* now we're done */
        if (do_proto) cprintf("000\n");
        return(om_ok);
 }
@@ -1318,7 +1466,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);
@@ -1334,7 +1482,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);
@@ -1384,7 +1532,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)
 {
@@ -1394,6 +1542,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 
  */
@@ -1427,7 +1588,7 @@ int CtdlSaveMsgPointerInRoom(char *roomname, long msgid, int flags) {
        lprintf(9, "CtdlSaveMsgPointerInRoom(%s, %ld, %d)\n",
                roomname, msgid, flags);
 
-       strcpy(hold_rm, CC->quickroom.QRname);
+       strcpy(hold_rm, CC->room.QRname);
 
        /* We may need to check to see if this message is real */
        if (  (flags & SM_VERIFY_GOODNESS)
@@ -1440,8 +1601,8 @@ int CtdlSaveMsgPointerInRoom(char *roomname, long msgid, int flags) {
        /* Perform replication checks if necessary */
        if ( (flags & SM_DO_REPL_CHECK) && (msg != NULL) ) {
 
-               if (getroom(&CC->quickroom,
-                  ((roomname != NULL) ? roomname : CC->quickroom.QRname) )
+               if (getroom(&CC->room,
+                  ((roomname != NULL) ? roomname : CC->room.QRname) )
                   != 0) {
                        lprintf(9, "No such room <%s>\n", roomname);
                        if (msg != NULL) CtdlFreeMessage(msg);
@@ -1449,7 +1610,7 @@ int CtdlSaveMsgPointerInRoom(char *roomname, long msgid, int flags) {
                }
 
                if (ReplicationChecks(msg) != 0) {
-                       getroom(&CC->quickroom, hold_rm);
+                       getroom(&CC->room, hold_rm);
                        if (msg != NULL) CtdlFreeMessage(msg);
                        lprintf(9, "Did replication, and newer exists\n");
                        return(0);
@@ -1457,15 +1618,15 @@ int CtdlSaveMsgPointerInRoom(char *roomname, long msgid, int flags) {
        }
 
        /* Now the regular stuff */
-       if (lgetroom(&CC->quickroom,
-          ((roomname != NULL) ? roomname : CC->quickroom.QRname) )
+       if (lgetroom(&CC->room,
+          ((roomname != NULL) ? roomname : CC->room.QRname) )
           != 0) {
                lprintf(9, "No such room <%s>\n", roomname);
                if (msg != NULL) CtdlFreeMessage(msg);
                return(ERROR + ROOM_NOT_FOUND);
        }
 
-        cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->quickroom.QRnumber, sizeof(long));
+        cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
         if (cdbfr == NULL) {
                 msglist = NULL;
                 num_msgs = 0;
@@ -1485,8 +1646,8 @@ int CtdlSaveMsgPointerInRoom(char *roomname, long msgid, int flags) {
         */
         if (num_msgs > 0) for (i=0; i<num_msgs; ++i) {
                if (msglist[i] == msgid) {
-                       lputroom(&CC->quickroom);       /* unlock the room */
-                       getroom(&CC->quickroom, hold_rm);
+                       lputroom(&CC->room);    /* unlock the room */
+                       getroom(&CC->room, hold_rm);
                        if (msg != NULL) CtdlFreeMessage(msg);
                        return(ERROR + ALREADY_EXISTS);
                }
@@ -1509,16 +1670,16 @@ int CtdlSaveMsgPointerInRoom(char *roomname, long msgid, int flags) {
         highest_msg = msglist[num_msgs - 1];
 
         /* Write it back to disk. */
-        cdb_store(CDB_MSGLISTS, &CC->quickroom.QRnumber, sizeof(long),
+        cdb_store(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long),
                   msglist, num_msgs * sizeof(long));
 
         /* Free up the memory we used. */
         phree(msglist);
 
        /* Update the highest-message pointer and unlock the room. */
-       CC->quickroom.QRhighest = highest_msg;
-       lputroom(&CC->quickroom);
-       getroom(&CC->quickroom, hold_rm);
+       CC->room.QRhighest = highest_msg;
+       lputroom(&CC->room);
+       getroom(&CC->room, hold_rm);
 
        /* Bump the reference count for this message. */
        if ((flags & SM_DONT_BUMP_REF)==0) {
@@ -1661,7 +1822,7 @@ void check_repl(long msgnum, void *userdata) {
        lprintf(9, "older!\n");
 
        /* Existing isn't newer?  Then delete the old one(s). */
-       CtdlDeleteMessages(CC->quickroom.QRname, msgnum, "");
+       CtdlDeleteMessages(CC->room.QRname, msgnum, "");
 }
 
 
@@ -1691,8 +1852,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.
@@ -1724,7 +1884,7 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,       /* message to save */
        char recipient[SIZ];
        long newmsgid;
        char *mptr = NULL;
-       struct usersupp userbuf;
+       struct user userbuf;
        int a, i;
        struct MetaData smi;
        FILE *network_fp = NULL;
@@ -1763,7 +1923,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");
@@ -1802,8 +1967,8 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,       /* message to save */
 
        /* Goto the correct room */
        lprintf(9, "Switching rooms\n");
-       strcpy(hold_rm, CC->quickroom.QRname);
-       strcpy(actual_rm, CC->quickroom.QRname);
+       strcpy(hold_rm, CC->room.QRname);
+       strcpy(actual_rm, CC->room.QRname);
        if (recps != NULL) {
                strcpy(actual_rm, SENTITEMS);
        }
@@ -1811,7 +1976,7 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,       /* message to save */
        /* If the user is a twit, move to the twit room for posting */
        lprintf(9, "Handling twit stuff\n");
        if (TWITDETECT) {
-               if (CC->usersupp.axlevel == 2) {
+               if (CC->user.axlevel == 2) {
                        strcpy(hold_rm, actual_rm);
                        strcpy(actual_rm, config.c_twitroom);
                }
@@ -1823,15 +1988,15 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,     /* message to save */
        }
 
        lprintf(9, "Possibly relocating\n");
-       if (strcasecmp(actual_rm, CC->quickroom.QRname)) {
-               getroom(&CC->quickroom, actual_rm);
+       if (strcasecmp(actual_rm, CC->room.QRname)) {
+               getroom(&CC->room, actual_rm);
        }
 
        /*
         * If this message has no O (room) field, generate one.
         */
        if (msg->cm_fields['O'] == NULL) {
-               msg->cm_fields['O'] = strdoop(CC->quickroom.QRname);
+               msg->cm_fields['O'] = strdoop(CC->room.QRname);
        }
 
        /* Perform "before save" hooks (aborting if any return nonzero) */
@@ -1868,7 +2033,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);
                }
        }
 
@@ -1889,9 +2054,9 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,       /* message to save */
 
        /* Bump this user's messages posted counter. */
        lprintf(9, "Updating user\n");
-       lgetuser(&CC->usersupp, CC->curr_user);
-       CC->usersupp.posted = CC->usersupp.posted + 1;
-       lputuser(&CC->usersupp);
+       lgetuser(&CC->user, CC->curr_user);
+       CC->user.posted = CC->user.posted + 1;
+       lputuser(&CC->user);
 
        /* If this is private, local mail, make a copy in the
         * recipient's mailbox and bump the reference count.
@@ -1903,12 +2068,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);
                }
        }
 
@@ -1957,8 +2123,8 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,       /* message to save */
 
        /* Go back to the room we started from */
        lprintf(9, "Returning to original room\n");
-       if (strcasecmp(hold_rm, CC->quickroom.QRname))
-               getroom(&CC->quickroom, hold_rm);
+       if (strcasecmp(hold_rm, CC->room.QRname))
+               getroom(&CC->room, hold_rm);
 
        /* For internet mail, generate delivery instructions.
         * Yes, this is recursive.  Deal with it.  Infinite recursion does
@@ -2002,36 +2168,44 @@ 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);
 }
 
 
 
 /*
- * Back end function used by make_message() and similar functions
+ * Back end function used by CtdlMakeMessage() and similar functions
  */
 char *CtdlReadMessageBody(char *terminator,    /* token signalling EOT */
                        size_t maxlen,          /* maximum message length */
-                       char *exist             /* if non-null, append to it;
+                       char *exist,            /* if non-null, append to it;
                                                   exist is ALWAYS freed  */
+                       int crlf                /* CRLF newlines instead of LF */
                        ) {
        char buf[SIZ];
        int linelen;
@@ -2039,6 +2213,8 @@ char *CtdlReadMessageBody(char *terminator,       /* token signalling EOT */
        size_t buffer_len = 0;
        char *ptr;
        char *m;
+       int flushing = 0;
+       int finished = 0;
 
        if (exist == NULL) {
                m = mallok(4096);
@@ -2058,51 +2234,49 @@ char *CtdlReadMessageBody(char *terminator,     /* token signalling EOT */
 
        /* flush the input if we have nowhere to store it */
        if (m == NULL) {
-               while ( (client_gets(buf)>0) && strcmp(buf, terminator) ) ;;
-               return(NULL);
+               flushing = 1;
        }
 
        /* 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;
-
-               linelen = strlen(buf);
-
-               /* augment the buffer if we have to */
-               if ((message_len + linelen + 2) > buffer_len) {
-                       lprintf(9, "realloking\n");
-                       ptr = reallok(m, (buffer_len * 2) );
-                       if (ptr == NULL) {      /* flush if can't allocate */
-                               while ( (client_gets(buf)>0) &&
-                                       strcmp(buf, terminator)) ;;
-                               return(m);
-                       } else {
-                               buffer_len = (buffer_len * 2);
-                               m = ptr;
-                               lprintf(9, "buffer_len is %ld\n", (long)buffer_len);
-                       }
+       do {
+               if (client_gets(buf) < 1) finished = 1;
+               if (!strcmp(buf, terminator)) finished = 1;
+               if (crlf) {
+                       strcat(buf, "\r\n");
+               }
+               else {
+                       strcat(buf, "\n");
                }
 
-               /* Add the new line to the buffer.  NOTE: this loop must avoid
-                * using functions like strcat() and strlen() because they
-                * traverse the entire buffer upon every call, and doing that
-                * for a multi-megabyte message slows it down beyond usability.
-                */
-               strcpy(&m[message_len], buf);
-               m[message_len + linelen] = '\n';
-               m[message_len + linelen + 1] = 0;
-               message_len = message_len + linelen + 1;
+               if ( (!flushing) && (!finished) ) {
+                       /* Measure the line */
+                       linelen = strlen(buf);
+       
+                       /* augment the buffer if we have to */
+                       if ((message_len + linelen) >= buffer_len) {
+                               ptr = reallok(m, (buffer_len * 2) );
+                               if (ptr == NULL) {      /* flush if can't allocate */
+                                       flushing = 1;
+                               } else {
+                                       buffer_len = (buffer_len * 2);
+                                       m = ptr;
+                                       lprintf(9, "buffer_len is now %ld\n", (long)buffer_len);
+                               }
+                       }
+       
+                       /* Add the new line to the buffer.  NOTE: this loop must avoid
+                       * using functions like strcat() and strlen() because they
+                       * traverse the entire buffer upon every call, and doing that
+                       * for a multi-megabyte message slows it down beyond usability.
+                       */
+                       strcpy(&m[message_len], buf);
+                       message_len += linelen;
+               }
 
                /* if we've hit the max msg length, flush the rest */
-               if (message_len >= maxlen) {
-                       while ( (client_gets(buf)>0)
-                               && strcmp(buf, terminator)) ;;
-                       return(m);
-               }
-       }
+               if (message_len >= maxlen) flushing = 1;
+
+       } while (!finished);
        return(m);
 }
 
@@ -2111,16 +2285,21 @@ char *CtdlReadMessageBody(char *terminator,     /* token signalling EOT */
 
 /*
  * Build a binary message to be saved on disk.
+ * (NOTE: if you supply 'preformatted_text', the buffer you give it
+ * will become part of the message.  This means you are no longer
+ * responsible for managing that memory -- it will be freed along with
+ * the rest of the fields when CtdlFreeMessage() is called.)
  */
 
-static struct CtdlMessage *make_message(
-       struct usersupp *author,        /* author's usersupp structure */
+struct CtdlMessage *CtdlMakeMessage(
+       struct user *author,    /* author's user structure */
        char *recipient,                /* NULL if it's not mail */
        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 *subject                   /* Subject (optional) */
+       char *subject,                  /* Subject (optional) */
+       char *preformatted_text         /* ...or NULL to read text from client */
 ) {
        char dest_node[SIZ];
        char buf[SIZ];
@@ -2148,11 +2327,11 @@ static struct CtdlMessage *make_message(
        else
                msg->cm_fields['A'] = strdoop(author->fullname);
 
-       if (CC->quickroom.QRflags & QR_MAILBOX) {               /* room */
-               msg->cm_fields['O'] = strdoop(&CC->quickroom.QRname[11]);
+       if (CC->room.QRflags & QR_MAILBOX) {            /* room */
+               msg->cm_fields['O'] = strdoop(&CC->room.QRname[11]);
        }
        else {
-               msg->cm_fields['O'] = strdoop(CC->quickroom.QRname);
+               msg->cm_fields['O'] = strdoop(CC->room.QRname);
        }
 
        msg->cm_fields['N'] = strdoop(NODENAME);                /* nodename */
@@ -2165,7 +2344,7 @@ static struct CtdlMessage *make_message(
                msg->cm_fields['D'] = strdoop(dest_node);
        }
 
-       if ( (author == &CC->usersupp) && (CC->cs_inet_email != NULL) ) {
+       if ( (author == &CC->user) && (strlen(CC->cs_inet_email) > 0) ) {
                msg->cm_fields['F'] = strdoop(CC->cs_inet_email);
        }
 
@@ -2176,8 +2355,13 @@ static struct CtdlMessage *make_message(
                }
        }
 
-       msg->cm_fields['M'] = CtdlReadMessageBody("000",
-                                               config.c_maxmsglen, NULL);
+       if (preformatted_text != NULL) {
+               msg->cm_fields['M'] = preformatted_text;
+       }
+       else {
+               msg->cm_fields['M'] = CtdlReadMessageBody("000",
+                                       config.c_maxmsglen, NULL, 0);
+       }
 
        return(msg);
 }
@@ -2195,21 +2379,21 @@ int CtdlDoIHavePermissionToPostInThisRoom(char *errmsgbuf, size_t n) {
                return (ERROR + NOT_LOGGED_IN);
        }
 
-       if ((CC->usersupp.axlevel < 2)
-           && ((CC->quickroom.QRflags & QR_MAILBOX) == 0)) {
+       if ((CC->user.axlevel < 2)
+           && ((CC->room.QRflags & QR_MAILBOX) == 0)) {
                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)) {
+       if ((CC->user.axlevel < 4)
+          && (CC->room.QRflags & QR_NETWORK)) {
                snprintf(errmsgbuf, n, "Need net privileges to enter here.");
                return (ERROR + HIGHER_ACCESS_REQUIRED);
        }
 
-       if ((CC->usersupp.axlevel < 6)
-          && (CC->quickroom.QRflags & QR_READONLY)) {
+       if ((CC->user.axlevel < 6)
+          && (CC->room.QRflags & QR_READONLY)) {
                snprintf(errmsgbuf, n, "Sorry, this is a read-only room.");
                return (ERROR + HIGHER_ACCESS_REQUIRED);
        }
@@ -2219,6 +2403,27 @@ int CtdlDoIHavePermissionToPostInThisRoom(char *errmsgbuf, size_t n) {
 }
 
 
+/*
+ * Check to see if the specified user has Internet mail permission
+ * (returns nonzero if permission is granted)
+ */
+int CtdlCheckInternetMailPermission(struct user *who) {
+
+       /* Globally enabled? */
+       if (config.c_restrict == 0) return(1);
+
+       /* User flagged ok? */
+       if (who->flags & US_INTERNET) return(2);
+
+       /* Aide level access? */
+       if (who->axlevel >= 6) return(3);
+
+       /* No mail for you! */
+       return(0);
+}
+
+
+
 /*
  * Validate recipients, count delivery types and errors, and handle aliasing
  * FIXME check for dupes!!!!!
@@ -2232,8 +2437,8 @@ struct recptypes *validate_recipients(char *recipients) {
        int i, j;
        int mailtype;
        int invalid;
-       struct usersupp tempUS;
-       struct quickroom tempQR;
+       struct user tempUS;
+       struct room tempQR;
 
        /* Initialize */
        ret = (struct recptypes *) malloc(sizeof(struct recptypes));
@@ -2284,7 +2489,7 @@ struct recptypes *validate_recipients(char *recipients) {
                        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, "|");
                                        }
@@ -2320,11 +2525,23 @@ struct recptypes *validate_recipients(char *recipients) {
                                }
                                break;
                        case MES_INTERNET:
-                               ++ret->num_internet;
-                               if (strlen(ret->recp_internet) > 0) {
-                                       strcat(ret->recp_internet, "|");
+                               /* Yes, you're reading this correctly: if the target
+                                * domain points back to the local system or an attached
+                                * Citadel directory, the address is invalid.  That's
+                                * because if the address were valid, we would have
+                                * already translated it to a local address by now.
+                                */
+                               if (IsDirectory(this_recp)) {
+                                       ++ret->num_error;
+                                       invalid = 1;
+                               }
+                               else {
+                                       ++ret->num_internet;
+                                       if (strlen(ret->recp_internet) > 0) {
+                                               strcat(ret->recp_internet, "|");
+                                       }
+                                       strcat(ret->recp_internet, this_recp);
                                }
-                               strcat(ret->recp_internet, this_recp);
                                break;
                        case MES_IGNET:
                                ++ret->num_ignet;
@@ -2419,16 +2636,16 @@ void cmd_ent0(char *entargs)
        /* Check some other permission type things. */
 
        if (post == 2) {
-               if (CC->usersupp.axlevel < 6) {
+               if (CC->user.axlevel < 6) {
                        cprintf("%d You don't have permission to masquerade.\n",
                                ERROR + HIGHER_ACCESS_REQUIRED);
                        return;
                }
-               extract(newusername, entargs, 4);
+               extract(newusername, entargs, 5);
                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;
@@ -2437,10 +2654,10 @@ void cmd_ent0(char *entargs)
         * 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->room.QRflags & QR_MAILBOX)
+          && (!strcasecmp(&CC->room.QRname[11], MAILROOM)) ) {
 
-               if (CC->usersupp.axlevel < 2) {
+               if (CC->user.axlevel < 2) {
                        strcpy(recp, "sysop");
                }
 
@@ -2451,9 +2668,18 @@ void cmd_ent0(char *entargs)
                        phree(valid);
                        return;
                }
+               if (valid->num_internet > 0) {
+                       if (CtdlCheckInternetMailPermission(&CC->user)==0) {
+                               cprintf("%d You do not have permission "
+                                       "to send Internet mail.\n",
+                                       ERROR + HIGHER_ACCESS_REQUIRED);
+                               phree(valid);
+                               return;
+                       }
+               }
 
                if ( ( (valid->num_internet + valid->num_ignet) > 0)
-                  && (CC->usersupp.axlevel < 4) ) {
+                  && (CC->user.axlevel < 4) ) {
                        cprintf("%d Higher access required for network mail.\n",
                                ERROR + HIGHER_ACCESS_REQUIRED);
                        phree(valid);
@@ -2461,7 +2687,7 @@ void cmd_ent0(char *entargs)
                }
        
                if ((RESTRICT_INTERNET == 1) && (valid->num_internet > 0)
-                   && ((CC->usersupp.flags & US_INTERNET) == 0)
+                   && ((CC->user.flags & US_INTERNET) == 0)
                    && (!CC->internal_pgm)) {
                        cprintf("%d You don't have access to Internet mail.\n",
                                ERROR + HIGHER_ACCESS_REQUIRED);
@@ -2473,16 +2699,16 @@ void cmd_ent0(char *entargs)
 
        /* Is this a room which has anonymous-only or anonymous-option? */
        anonymous = MES_NORMAL;
-       if (CC->quickroom.QRflags & QR_ANONONLY) {
+       if (CC->room.QRflags & QR_ANONONLY) {
                anonymous = MES_ANONONLY;
        }
-       if (CC->quickroom.QRflags & QR_ANONOPT) {
+       if (CC->room.QRflags & QR_ANONOPT) {
                if (anon_flag == 1) {   /* only if the user requested it */
                        anonymous = MES_ANONOPT;
                }
        }
 
-       if ((CC->quickroom.QRflags & QR_MAILBOX) == 0) {
+       if ((CC->room.QRflags & QR_MAILBOX) == 0) {
                recp[0] = 0;
        }
 
@@ -2490,7 +2716,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;
@@ -2509,9 +2735,9 @@ 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, subject);
+       msg = CtdlMakeMessage(&CC->user, recp,
+               CC->room.QRname, anonymous, format_type,
+               masquerade_as, subject, NULL);
 
        if (msg != NULL) {
                CtdlSubmitMsg(msg, valid, "");
@@ -2534,7 +2760,7 @@ int CtdlDeleteMessages(char *room_name,           /* which room */
 )
 {
 
-       struct quickroom qrbuf;
+       struct room qrbuf;
        struct cdbdata *cdbfr;
        long *msglist = NULL;
        long *dellist = NULL;
@@ -2601,7 +2827,7 @@ int CtdlDeleteMessages(char *room_name,           /* which room */
         * 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
+        * and we don't want that happening during an S_ROOMS critical
         * section.
         */
        if (num_deleted) for (i=0; i<num_deleted; ++i) {
@@ -2623,10 +2849,10 @@ int CtdlDeleteMessages(char *room_name,         /* which room */
  * the current room (returns 1 for yes, 0 for no)
  */
 int CtdlDoIHavePermissionToDeleteMessagesFromThisRoom(void) {
-       getuser(&CC->usersupp, CC->curr_user);
-       if ((CC->usersupp.axlevel < 6)
-           && (CC->usersupp.usernum != CC->quickroom.QRroomaide)
-           && ((CC->quickroom.QRflags & QR_MAILBOX) == 0)
+       getuser(&CC->user, CC->curr_user);
+       if ((CC->user.axlevel < 6)
+           && (CC->user.usernum != CC->room.QRroomaide)
+           && ((CC->room.QRflags & QR_MAILBOX) == 0)
            && (!(CC->internal_pgm))) {
                return(0);
        }
@@ -2650,10 +2876,10 @@ void cmd_dele(char *delstr)
        }
        delnum = extract_long(delstr, 0);
 
-       num_deleted = CtdlDeleteMessages(CC->quickroom.QRname, delnum, "");
+       num_deleted = CtdlDeleteMessages(CC->room.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);
@@ -2683,7 +2909,7 @@ void cmd_move(char *args)
 {
        long num;
        char targ[SIZ];
-       struct quickroom qtemp;
+       struct room qtemp;
        int err;
        int is_copy = 0;
 
@@ -2697,16 +2923,16 @@ void cmd_move(char *args)
                return;
        }
 
-       getuser(&CC->usersupp, CC->curr_user);
+       getuser(&CC->user, CC->curr_user);
        /* Aides can move/copy */
-       if ((CC->usersupp.axlevel < 6)
+       if ((CC->user.axlevel < 6)
            /* Roomaides can move/copy */
-           && (CC->usersupp.usernum != CC->quickroom.QRroomaide)
+           && (CC->user.usernum != CC->room.QRroomaide)
            /* Permit move/copy to/from personal rooms */
-           && (!((CC->quickroom.QRflags & QR_MAILBOX)
+           && (!((CC->room.QRflags & QR_MAILBOX)
                            && (qtemp.QRflags & QR_MAILBOX)))
            /* Permit only copy from public to personal room */
-           && (!(is_copy && !(CC->quickroom.QRflags & QR_MAILBOX)
+           && (!(is_copy && !(CC->room.QRflags & QR_MAILBOX)
                            && (qtemp.QRflags & QR_MAILBOX)))) {
                cprintf("%d Higher access required.\n",
                        ERROR + HIGHER_ACCESS_REQUIRED);
@@ -2724,10 +2950,10 @@ void cmd_move(char *args)
         * if this is a 'move' rather than a 'copy' operation.
         */
        if (is_copy == 0) {
-               CtdlDeleteMessages(CC->quickroom.QRname, num, "");
+               CtdlDeleteMessages(CC->room.QRname, num, "");
        }
 
-       cprintf("%d Message %s.\n", OK, (is_copy ? "copied" : "moved") );
+       cprintf("%d Message %s.\n", CIT_OK, (is_copy ? "copied" : "moved") );
 }
 
 
@@ -2826,86 +3052,102 @@ void AdjRefCount(long msgnum, int incr)
 void CtdlWriteObject(char *req_room,           /* Room to stuff it in */
                        char *content_type,     /* MIME type of this object */
                        char *tempfilename,     /* Where to fetch it from */
-                       struct usersupp *is_mailbox,    /* Mailbox room? */
+                       struct user *is_mailbox,        /* Mailbox room? */
                        int is_binary,          /* Is encoding necessary? */
                        int is_unique,          /* Del others of this type? */
                        unsigned int flags      /* Internal save flags */
                        )
 {
 
-       FILE *fp, *tempfp;
-       char filename[PATH_MAX];
-       char cmdbuf[SIZ];
-       char ch;
-       struct quickroom qrbuf;
+       FILE *fp;
+       struct room qrbuf;
        char roomname[ROOMNAMELEN];
        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, 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);
 
-       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 + 2);
+       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));
        msg->cm_magic = CTDLMESSAGE_MAGIC;
        msg->cm_anon_type = MES_NORMAL;
        msg->cm_format_type = 4;
-       msg->cm_fields['A'] = strdoop(CC->usersupp.fullname);
+       msg->cm_fields['A'] = strdoop(CC->user.fullname);
        msg->cm_fields['O'] = strdoop(req_room);
        msg->cm_fields['N'] = strdoop(config.c_nodename);
        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) {
                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.
@@ -2936,9 +3178,9 @@ char *CtdlGetSysConfig(char *sysconfname) {
        struct CtdlMessage *msg;
        char buf[SIZ];
        
-       strcpy(hold_rm, CC->quickroom.QRname);
-       if (getroom(&CC->quickroom, SYSCONFIGROOM) != 0) {
-               getroom(&CC->quickroom, hold_rm);
+       strcpy(hold_rm, CC->room.QRname);
+       if (getroom(&CC->room, SYSCONFIGROOM) != 0) {
+               getroom(&CC->room, hold_rm);
                return NULL;
        }
 
@@ -2946,7 +3188,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);
@@ -2965,7 +3207,7 @@ char *CtdlGetSysConfig(char *sysconfname) {
                }
        }
 
-       getroom(&CC->quickroom, hold_rm);
+       getroom(&CC->room, hold_rm);
 
        if (conf != NULL) do {
                extract_token(buf, conf, 0, '\n');
@@ -2990,3 +3232,50 @@ void CtdlPutSysConfig(char *sysconfname, char *sysconfdata) {
        CtdlWriteObject(SYSCONFIGROOM, sysconfname, temp, NULL, 0, 1, 0);
        unlink(temp);
 }
+
+
+/*
+ * Determine whether a given Internet address belongs to the current user
+ */
+int CtdlIsMe(char *addr) {
+       struct recptypes *recp;
+       int i;
+
+       recp = validate_recipients(addr);
+       if (recp == NULL) return(0);
+
+       if (recp->num_local == 0) {
+               phree(recp);
+               return(0);
+       }
+
+       for (i=0; i<recp->num_local; ++i) {
+               extract(addr, recp->recp_local, i);
+               if (!strcasecmp(addr, CC->user.fullname)) {
+                       phree(recp);
+                       return(1);
+               }
+       }
+
+       phree(recp);
+       return(0);
+}
+
+
+/*
+ * Citadel protocol command to do the same
+ */
+void cmd_isme(char *argbuf) {
+       char addr[SIZ];
+
+       if (CtdlAccessCheck(ac_logged_in)) return;
+       extract(addr, argbuf, 0);
+
+       if (CtdlIsMe(addr)) {
+               cprintf("%d %s\n", CIT_OK, addr);
+       }
+       else {
+               cprintf("%d Not you.\n", ERROR);
+       }
+
+}