]> code.citadel.org Git - citadel.git/blobdiff - citadel/msgbase.c
* New server command-line option "-l" to send log output to the host
[citadel.git] / citadel / msgbase.c
index 471d8901c8ad1d020cda5f4a9e91841be48ce7af..71dc3fc2acbfe7abac81b4a5a1fc59c902bf9f6c 100644 (file)
 
 #include <ctype.h>
 #include <string.h>
-#include <syslog.h>
 #include <limits.h>
 #include <errno.h>
 #include <stdarg.h>
 #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"
@@ -288,21 +287,24 @@ int CtdlMsgCmp(struct CtdlMessage *msg, struct CtdlMessage *template) {
 /*
  * Retrieve the "seen" message list for the current room.
  */
-void CtdlGetSeen(char *buf) {
+void CtdlGetSeen(char *buf, int which_set) {
        struct visit vbuf;
 
        /* Learn about the user and room in question */
-       CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
+       CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
 
-       safestrncpy(buf, vbuf.v_seen, SIZ);
+       if (which_set == ctdlsetseen_seen)
+               safestrncpy(buf, vbuf.v_seen, SIZ);
+       if (which_set == ctdlsetseen_answered)
+               safestrncpy(buf, vbuf.v_answered, SIZ);
 }
 
 
 
 /*
- * Manipulate the "seen msgs" string.
+ * Manipulate the "seen msgs" string (or other message set strings)
  */
-void CtdlSetSeen(long target_msgnum, int target_setting) {
+void CtdlSetSeen(long target_msgnum, int target_setting, int which_set) {
        char newseen[SIZ];
        struct cdbdata *cdbfr;
        int i;
@@ -313,12 +315,13 @@ void CtdlSetSeen(long target_msgnum, int target_setting) {
        struct visit vbuf;
        long *msglist;
        int num_msgs = 0;
+       char vset[SIZ];
 
        /* Learn about the user and room in question */
-       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);
@@ -328,7 +331,11 @@ void CtdlSetSeen(long target_msgnum, int target_setting) {
                return; /* No messages at all?  No further action. */
        }
 
-       lprintf(9, "before optimize: %s\n", vbuf.v_seen);
+       /* Decide which message set we're manipulating */
+       if (which_set == ctdlsetseen_seen) strcpy(vset, vbuf.v_seen);
+       if (which_set == ctdlsetseen_answered) strcpy(vset, vbuf.v_answered);
+
+       lprintf(9, "before optimize: %s\n", vset);
        strcpy(newseen, "");
 
        for (i=0; i<num_msgs; ++i) {
@@ -338,7 +345,7 @@ void CtdlSetSeen(long target_msgnum, int target_setting) {
                        is_seen = target_setting;
                }
                else {
-                       if (is_msg_in_mset(vbuf.v_seen, msglist[i])) {
+                       if (is_msg_in_mset(vset, msglist[i])) {
                                is_seen = 1;
                        }
                }
@@ -374,10 +381,13 @@ void CtdlSetSeen(long target_msgnum, int target_setting) {
                was_seen = is_seen;
        }
 
-       safestrncpy(vbuf.v_seen, newseen, SIZ);
-       lprintf(9, " after optimize: %s\n", vbuf.v_seen);
+       /* Decide which message set we're manipulating */
+       if (which_set == ctdlsetseen_seen) strcpy(vbuf.v_seen, newseen);
+       if (which_set == ctdlsetseen_answered) strcpy(vbuf.v_answered, newseen);
+
+       lprintf(9, " after optimize: %s\n", newseen);
        phree(msglist);
-       CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
+       CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
 }
 
 
@@ -407,11 +417,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);
@@ -490,7 +500,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;
@@ -597,13 +607,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);
 }
 
 
@@ -1003,7 +1014,12 @@ void output_preferred(char *name, char *filename, char *partnum, char *disp,
                        cprintf("Content-type: %s\n", cbtype);
                        cprintf("Content-length: %d\n",
                                (int)(length + add_newline) );
-                       cprintf("Content-transfer-encoding: %s\n", encoding);
+                       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");
@@ -1095,6 +1111,7 @@ int CtdlOutputPreLoadedMsg(struct CtdlMessage *TheMessage,
        char *mptr;
        char *nl;       /* newline string */
        int suppress_f = 0;
+       int subject_found = 0;
 
        /* buffers needed for RFC822 translation */
        char suser[SIZ];
@@ -1143,12 +1160,12 @@ 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);
 
+       /* 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);
@@ -1253,8 +1270,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')
@@ -1275,6 +1294,9 @@ int CtdlOutputPreLoadedMsg(struct CtdlMessage *TheMessage,
                                }
                        }
                }
+               if (subject_found == 0) {
+                       cprintf("Subject: (no subject)%s", nl);
+               }
        }
 
        for (i=0; i<strlen(suser); ++i) {
@@ -1296,7 +1318,13 @@ int CtdlOutputPreLoadedMsg(struct CtdlMessage *TheMessage,
 
                PerformUserHooks(luser, (-1L), EVT_OUTPUTMSG);
 
-               if (strlen(fuser) > 0) {
+               if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONONLY)) {
+                       cprintf("From: x@x.org (----)%s", nl);
+               }
+               else if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONOPT)) {
+                       cprintf("From: x@x.org (anonymous)%s", nl);
+               }
+               else if (strlen(fuser) > 0) {
                        cprintf("From: %s (%s)%s", fuser, luser, nl);
                }
                else {
@@ -1304,6 +1332,11 @@ 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 */
@@ -1325,31 +1358,54 @@ START_TEXT:
                         * 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 == HEADERS_ONLY) {
-               if (do_proto) cprintf("000\n");
-               return(om_ok);
+               goto DONE;
        }
 
        /* signify start of msg text */
        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
         * everything to be output completely literally ... regardless of
@@ -1413,7 +1469,7 @@ START_TEXT:
                }
        }
 
-       /* now we're done */
+DONE:  /* now we're done */
        if (do_proto) cprintf("000\n");
        return(om_ok);
 }
@@ -1548,7 +1604,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)
@@ -1561,8 +1617,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);
@@ -1570,7 +1626,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);
@@ -1578,15 +1634,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;
@@ -1606,8 +1662,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);
                }
@@ -1630,16 +1686,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) {
@@ -1782,7 +1838,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, "");
 }
 
 
@@ -1844,7 +1900,7 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,       /* message to save */
        char recipient[SIZ];
        long newmsgid;
        char *mptr = NULL;
-       struct usersupp userbuf;
+       struct ctdluser userbuf;
        int a, i;
        struct MetaData smi;
        FILE *network_fp = NULL;
@@ -1926,17 +1982,18 @@ 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);
+       lprintf(9, "Selected room %s\n", (recps) ? CC->room.QRname : SENTITEMS);
+       strcpy(hold_rm, CC->room.QRname);
+       strcpy(actual_rm, CC->room.QRname);
        if (recps != NULL) {
                strcpy(actual_rm, SENTITEMS);
        }
 
        /* If the user is a twit, move to the twit room for posting */
-       lprintf(9, "Handling twit stuff\n");
+       lprintf(9, "Handling twit stuff: %s\n",
+                       (CC->user.axlevel == 2) ? config.c_twitroom : "OK");
        if (TWITDETECT) {
-               if (CC->usersupp.axlevel == 2) {
+               if (CC->user.axlevel == 2) {
                        strcpy(hold_rm, actual_rm);
                        strcpy(actual_rm, config.c_twitroom);
                }
@@ -1947,16 +2004,16 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,     /* message to save */
                strcpy(actual_rm, force_room);
        }
 
-       lprintf(9, "Possibly relocating\n");
-       if (strcasecmp(actual_rm, CC->quickroom.QRname)) {
-               getroom(&CC->quickroom, actual_rm);
+       lprintf(9, "Final selection: %s\n", 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) */
@@ -2014,9 +2071,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.
@@ -2082,9 +2139,9 @@ 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);
+       lprintf(9, "Returning to original room %s\n", 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
@@ -2163,8 +2220,9 @@ void quickie_message(char *from, char *to, char *room, char *text,
  */
 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;
@@ -2172,6 +2230,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);
@@ -2191,50 +2251,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) ) {
-
-               /* Measure the line and strip trailing newline characters */
-               linelen = strlen(buf);
-               if (linelen > 0) if (buf[linelen-1]==13) buf[linelen--]=0;
-               if (linelen > 0) if (buf[linelen-1]==10) buf[linelen--]=0;
-
-               /* augment the buffer if we have to */
-               if ((message_len + linelen + 2) > buffer_len) {
-                       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);
 }
 
@@ -2250,7 +2309,7 @@ char *CtdlReadMessageBody(char *terminator,       /* token signalling EOT */
  */
 
 struct CtdlMessage *CtdlMakeMessage(
-       struct usersupp *author,        /* author's usersupp structure */
+       struct ctdluser *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 */
@@ -2285,11 +2344,11 @@ struct CtdlMessage *CtdlMakeMessage(
        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 */
@@ -2302,7 +2361,7 @@ struct CtdlMessage *CtdlMakeMessage(
                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);
        }
 
@@ -2318,7 +2377,7 @@ struct CtdlMessage *CtdlMakeMessage(
        }
        else {
                msg->cm_fields['M'] = CtdlReadMessageBody("000",
-                                               config.c_maxmsglen, NULL);
+                                       config.c_maxmsglen, NULL, 0);
        }
 
        return(msg);
@@ -2337,21 +2396,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);
        }
@@ -2365,7 +2424,7 @@ 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 usersupp *who) {
+int CtdlCheckInternetMailPermission(struct ctdluser *who) {
 
        /* Globally enabled? */
        if (config.c_restrict == 0) return(1);
@@ -2395,8 +2454,8 @@ struct recptypes *validate_recipients(char *recipients) {
        int i, j;
        int mailtype;
        int invalid;
-       struct usersupp tempUS;
-       struct quickroom tempQR;
+       struct ctdluser tempUS;
+       struct ctdlroom tempQR;
 
        /* Initialize */
        ret = (struct recptypes *) malloc(sizeof(struct recptypes));
@@ -2483,11 +2542,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;
@@ -2582,12 +2653,12 @@ 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) );
@@ -2600,10 +2671,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");
                }
 
@@ -2615,7 +2686,7 @@ void cmd_ent0(char *entargs)
                        return;
                }
                if (valid->num_internet > 0) {
-                       if (CtdlCheckInternetMailPermission(&CC->usersupp)==0) {
+                       if (CtdlCheckInternetMailPermission(&CC->user)==0) {
                                cprintf("%d You do not have permission "
                                        "to send Internet mail.\n",
                                        ERROR + HIGHER_ACCESS_REQUIRED);
@@ -2625,7 +2696,7 @@ void cmd_ent0(char *entargs)
                }
 
                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);
@@ -2633,7 +2704,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);
@@ -2645,16 +2716,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;
        }
 
@@ -2681,8 +2752,8 @@ void cmd_ent0(char *entargs)
 
        /* Read in the message from the client. */
        cprintf("%d send message\n", SEND_LISTING);
-       msg = CtdlMakeMessage(&CC->usersupp, recp,
-               CC->quickroom.QRname, anonymous, format_type,
+       msg = CtdlMakeMessage(&CC->user, recp,
+               CC->room.QRname, anonymous, format_type,
                masquerade_as, subject, NULL);
 
        if (msg != NULL) {
@@ -2706,7 +2777,7 @@ int CtdlDeleteMessages(char *room_name,           /* which room */
 )
 {
 
-       struct quickroom qrbuf;
+       struct ctdlroom qrbuf;
        struct cdbdata *cdbfr;
        long *msglist = NULL;
        long *dellist = NULL;
@@ -2773,7 +2844,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) {
@@ -2795,10 +2866,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);
        }
@@ -2822,7 +2893,7 @@ 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", CIT_OK,
@@ -2855,7 +2926,7 @@ void cmd_move(char *args)
 {
        long num;
        char targ[SIZ];
-       struct quickroom qtemp;
+       struct ctdlroom qtemp;
        int err;
        int is_copy = 0;
 
@@ -2869,16 +2940,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);
@@ -2896,7 +2967,7 @@ 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", CIT_OK, (is_copy ? "copied" : "moved") );
@@ -2998,7 +3069,7 @@ 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 ctdluser *is_mailbox,    /* Mailbox room? */
                        int is_binary,          /* Is encoding necessary? */
                        int is_unique,          /* Del others of this type? */
                        unsigned int flags      /* Internal save flags */
@@ -3006,7 +3077,7 @@ void CtdlWriteObject(char *req_room,              /* Room to stuff it in */
 {
 
        FILE *fp;
-       struct quickroom qrbuf;
+       struct ctdlroom qrbuf;
        char roomname[ROOMNAMELEN];
        struct CtdlMessage *msg;
 
@@ -3032,7 +3103,7 @@ void CtdlWriteObject(char *req_room,              /* Room to stuff it in */
        rewind(fp);
        lprintf(9, "Raw length is %ld\n", (long)raw_length);
 
-       raw_message = mallok((size_t)raw_length);
+       raw_message = mallok((size_t)raw_length + 2);
        fread(raw_message, (size_t)raw_length, 1, fp);
        fclose(fp);
 
@@ -3081,7 +3152,7 @@ void CtdlWriteObject(char *req_room,              /* Room to stuff it in */
        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);
@@ -3124,9 +3195,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;
        }
 
@@ -3153,7 +3224,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');
@@ -3178,3 +3249,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);
+       }
+
+}