]> code.citadel.org Git - citadel.git/blobdiff - citadel/msgbase.c
* Various changes to begin work on support for MIME messages
[citadel.git] / citadel / msgbase.c
index a14cec67f8d5803c1232ed13c3237d9828c14801..257e03ceede7f5676ce1ae11d376713fd2197b8f 100644 (file)
@@ -1,3 +1,4 @@
+/* $Id$ */
 #include <stdlib.h>
 #include <unistd.h>
 #include <stdio.h>
 #include "citadel.h"
 #include "server.h"
 #include <errno.h>
-#include "proto.h"
+#include <sys/stat.h>
+#include "database.h"
+#include "msgbase.h"
+#include "support.h"
+#include "sysdep_decls.h"
+#include "room_ops.h"
+#include "user_ops.h"
+#include "control.h"
+#include "dynloader.h"
+#include "tools.h"
 
 #define MSGS_ALL       0
 #define MSGS_OLD       1
 #define MSGS_GT                5
 
 extern struct config config;
-int twitroom=-1;
+
+
+/*
+ * This function is self explanatory.
+ * (What can I say, I'm in a weird mood today...)
+ */
+void remove_any_whitespace_to_the_left_or_right_of_at_symbol(char *name) {
+       int i;
+
+       for (i=0; i<strlen(name); ++i) if (name[i]=='@') {
+               if (i>0) if (isspace(name[i-1])) {
+                       strcpy(&name[i-1], &name[i]);
+                       i = 0;
+                       }
+               while (isspace(name[i+1])) {
+                       strcpy(&name[i+1], &name[i+2]);
+                       }
+               }
+       }
 
 
 /*
@@ -32,6 +60,10 @@ int alias(char *name)                /* process alias and routing info for mail */
        FILE *fp;
        int a,b;
        char aaa[300],bbb[300];
+
+       lprintf(9, "alias() called for <%s>\n", name);
+
+       remove_any_whitespace_to_the_left_or_right_of_at_symbol(name);
        
        fp=fopen("network/mail.aliases","r");
        if (fp==NULL) fp=fopen("/dev/null","r");
@@ -59,10 +91,10 @@ GNA:        strcpy(aaa,""); strcpy(bbb,"");
                fclose(fp);
                goto DETYPE;
                }
-       if (strucmp(name,aaa)) goto GNA;
+       if (strcasecmp(name,aaa)) goto GNA;
        fclose(fp);
        strcpy(name,bbb);
-       /* cprintf("*** Mail is being forwarded to %s\n",name); */
+       lprintf(7, "Mail is being forwarded to %s\n", name);
 
 DETYPE:        /* determine local or remote type, see citadel.h */
        for (a=0; a<strlen(name); ++a) if (name[a]=='!') return(M_INTERNET);
@@ -72,7 +104,7 @@ DETYPE:      /* determine local or remote type, see citadel.h */
                                if (name[b]=='.') return(M_INTERNET);
        b=0; for (a=0; a<strlen(name); ++a) if (name[a]=='@') ++b;
        if (b>1) {
-               /* cprintf("Too many @'s in address\n"); */
+               lprintf(7, "Too many @'s in address\n");
                return(M_ERROR);
                }
        if (b==1) {
@@ -83,7 +115,7 @@ DETYPE:      /* determine local or remote type, see citadel.h */
                if (fp==NULL) return(M_ERROR);
 GETSN:         do {
                        a=getstring(fp,aaa);
-                       } while ((a>=0)&&(strucmp(aaa,bbb)));
+                       } while ((a>=0)&&(strcasecmp(aaa,bbb)));
                a=getstring(fp,aaa);
                if (!strncmp(aaa,"use ",4)) {
                        strcpy(bbb,&aaa[4]);
@@ -131,27 +163,28 @@ void get_mm(void) {
  */
 void cmd_msgs(char *cmdbuf)
 {
-       int a;
-       int mode;
+       int a = 0;
+       int mode = 0;
        char which[256];
-       int cm_howmany;
-       long cm_gt;
+       int cm_howmany = 0;
+       long cm_gt = 0L;
+       struct visit vbuf;
 
        extract(which,cmdbuf,0);
 
        mode = MSGS_ALL;
        strcat(which,"   ");
-       if (!struncmp(which,"OLD",3))   mode = MSGS_OLD;
-       if (!struncmp(which,"NEW",3))   mode = MSGS_NEW;
-       if (!struncmp(which,"FIRST",5)) {
+       if (!strncasecmp(which,"OLD",3))        mode = MSGS_OLD;
+       if (!strncasecmp(which,"NEW",3))        mode = MSGS_NEW;
+       if (!strncasecmp(which,"FIRST",5))      {
                mode = MSGS_FIRST;
                cm_howmany = extract_int(cmdbuf,1);
                }
-       if (!struncmp(which,"LAST",4))  {
+       if (!strncasecmp(which,"LAST",4))       {
                mode = MSGS_LAST;
                cm_howmany = extract_int(cmdbuf,1);
                }
-       if (!struncmp(which,"GT",2))    {
+       if (!strncasecmp(which,"GT",2)) {
                mode = MSGS_GT;
                cm_gt = extract_long(cmdbuf,1);
                }
@@ -160,30 +193,31 @@ void cmd_msgs(char *cmdbuf)
                cprintf("%d not logged in\n",ERROR+NOT_LOGGED_IN);
                return;
                }
-       if (CC->curr_rm < 0) {
-               cprintf("%d no room\n",ERROR);
-               return;
-               }
        get_mm();
-       get_fullroom(&CC->fullroom,CC->curr_rm);
+       get_msglist(&CC->quickroom);
        getuser(&CC->usersupp,CC->curr_user);
-       cprintf("%d messages...\n",LISTING_FOLLOWS);
-       for (a=0; a<MSGSPERRM; ++a) 
-              if ((CC->fullroom.FRnum[a] >=0)
+       CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
+
+       cprintf("%d Message list...\n",LISTING_FOLLOWS);
+       if (CC->num_msgs != 0) {
+          for (a=0; a<(CC->num_msgs); ++a) 
+              if ((MessageFromList(a) >=0)
               && ( 
 
 (mode==MSGS_ALL)
-|| ((mode==MSGS_OLD) && (CC->fullroom.FRnum[a] <= CC->usersupp.lastseen[CC->curr_rm]))
-|| ((mode==MSGS_NEW) && (CC->fullroom.FRnum[a] > CC->usersupp.lastseen[CC->curr_rm]))
-|| ((mode==MSGS_NEW) && (CC->fullroom.FRnum[a] >= CC->usersupp.lastseen[CC->curr_rm])
+|| ((mode==MSGS_OLD) && (MessageFromList(a) <= vbuf.v_lastseen))
+|| ((mode==MSGS_NEW) && (MessageFromList(a) > vbuf.v_lastseen))
+|| ((mode==MSGS_NEW) && (MessageFromList(a) >= vbuf.v_lastseen)
                     && (CC->usersupp.flags & US_LASTOLD))
-|| ((mode==MSGS_LAST)&& (a>=(MSGSPERRM-cm_howmany)))
+|| ((mode==MSGS_LAST)&& (a>=(CC->num_msgs-cm_howmany)))
 || ((mode==MSGS_FIRST)&&(a<cm_howmany))
-|| ((mode==MSGS_GT) && (CC->fullroom.FRnum[a] > cm_gt))
+|| ((mode==MSGS_GT) && (MessageFromList(a) > cm_gt))
 
                        )
-               )
-               cprintf("%ld\n",CC->fullroom.FRnum[a]);
+               ) {
+                       cprintf("%ld\n", MessageFromList(a));
+                       }
+          }
        cprintf("000\n");
        }
 
@@ -235,7 +269,9 @@ void memfmout(int width, char *mptr, char subst)
                        /* where are we going to get our text from? */
                        /* nonzero if we should use hypertext mode */
        {
-       int a,b,c,real,old;
+       int a,b,c;
+       int real = 0;
+       int old = 0;
        CIT_UBYTE ch;
        char aaa[140];
        char buffer[256];
@@ -298,15 +334,14 @@ FMTEND:   cprintf("\n");
 
 
 /*
- * get a message off disk.
+ * Get a message off disk.  (return value is the message's timestamp)
  * 
  */
-void output_message(char *msgid, int mode, int headers_only)
-{
+time_t output_message(char *msgid, int mode,
+                       int headers_only, int desired_section) {
        long msg_num;
-       int a,och,len;
+       int a;
        CIT_UBYTE ch, rch;
-       FILE *msg;
        CIT_UBYTE format_type,anon_flag;
        char buf[1024];
        long msg_len;
@@ -321,53 +356,58 @@ void output_message(char *msgid, int mode, int headers_only)
        char snode[256];
        char lnode[256];
        char mid[256];
-       long xtime;
-       /* */
+       time_t xtime = 0L;
+       /*                                       */
 
        msg_num = atol(msgid);
 
 
-       if ((!(CC->logged_in))&&(!(CC->internal_pgm))) {
+       if ((!(CC->logged_in))&&(!(CC->internal_pgm))&&(mode!=MT_DATE)) {
                cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
-               return;
-               }
-       if (CC->curr_rm < 0) {
-               cprintf("%d No room selected.\n",ERROR);
-               return;
+               return(xtime);
                }
 
-       /* We used to need to check in the current room's fullroom table
+       /* We used to need to check in the current room's message list
         * to determine where the message's disk position.  We no longer need
         * to do this, but we do it anyway as a security measure, in order to
         * prevent rogue clients from reading messages not in the current room.
         */
-       for (a=0; a<MSGSPERRM; ++a) if (CC->fullroom.FRnum[a] == msg_num) {
-               msg_ok = 1;
+
+       msg_ok = 0;
+       if (CC->num_msgs > 0) {
+               for (a=0; a<CC->num_msgs; ++a) {
+                       if (MessageFromList(a) == msg_num) {
+                               msg_ok = 1;
+                               }
+                       }
                }
+
        if (!msg_ok) {
-               cprintf("%d Message %ld is not in this room.\n",
-                       ERROR, msg_num);
-               return;
+               if (mode != MT_DATE)
+                       cprintf("%d Message %ld is not in this room.\n",
+                               ERROR, msg_num);
+               return(xtime);
                }
        
 
        dmsgtext = cdb_fetch(CDB_MSGMAIN, &msg_num, sizeof(long));
        
        if (dmsgtext == NULL) {
-               cprintf("%d Can't find message %ld\n", ERROR+INTERNAL_ERROR);
-               return;
+               if (mode != MT_DATE)
+                       cprintf("%d Can't find message %ld\n",
+                               ERROR+INTERNAL_ERROR);
+               return(xtime);
                }
 
        msg_len = (long) dmsgtext->len;
        mptr = dmsgtext->ptr;
-       lprintf(9, "Returned message length is %ld\n", msg_len);
 
        /* this loop spews out the whole message if we're doing raw format */
        if (mode == MT_RAW) {
                cprintf("%d %ld\n", BINARY_FOLLOWS, msg_len);
                client_write(dmsgtext->ptr, (int) msg_len);
                cdb_free(dmsgtext);
-               return;
+               return(xtime);
                }
 
        /* Otherwise, we'll start parsing it field by field... */
@@ -376,16 +416,33 @@ void output_message(char *msgid, int mode, int headers_only)
                cprintf("%d Illegal message format on disk\n",
                        ERROR+INTERNAL_ERROR);
                cdb_free(dmsgtext);
-               return;
+               return(xtime);
                }
 
        anon_flag = *mptr++;
        format_type = *mptr++;
 
+       /* Are we just looking for the message date? */
+       if (mode == MT_DATE) while(ch = *mptr++, (ch!='M' && ch!=0)) {
+               buf[0] = 0;
+               do {
+                       buf[strlen(buf)+1] = 0;
+                       rch = *mptr++;
+                       buf[strlen(buf)] = rch;
+                       } while (rch > 0);
+
+               if (ch=='T') {
+                       xtime = atol(buf);
+                       cdb_free(dmsgtext);
+                       return(xtime);
+                       }
+               }
+
+
        /* now for the user-mode message reading loops */
        cprintf("%d Message %ld:\n",LISTING_FOLLOWS,msg_num);
 
-       if (mode == MT_CITADEL) cprintf("type=%d\n",format_type);
+       if (mode == MT_CITADEL) cprintf("type=%d\n", format_type);
 
        if ( (anon_flag == MES_ANON) && (mode == MT_CITADEL) ) {
                cprintf("nhdr=yes\n");
@@ -402,6 +459,7 @@ void output_message(char *msgid, int mode, int headers_only)
                        } while (rch > 0);
 
                if (ch=='A') {
+                       PerformUserHooks(buf, (-1L), EVT_OUTPUTMSG);
                        if (anon_flag==MES_ANON) cprintf("from=****");
                        else if (anon_flag==MES_AN2) cprintf("from=anonymous");
                        else cprintf("from=%s",buf);
@@ -459,10 +517,11 @@ void output_message(char *msgid, int mode, int headers_only)
                }
 
        if (mode == MT_RFC822) {
-               if (!strucmp(snode, NODENAME)) {
+               if (!strcasecmp(snode, NODENAME)) {
                        strcpy(snode, FQDN);
                        }
                cprintf("Message-ID: <%s@%s>\n", mid, snode);
+               PerformUserHooks(luser, (-1L), EVT_OUTPUTMSG);
                cprintf("From: %s@%s (%s)\n",
                        suser, snode, luser);
                cprintf("Organization: %s\n", lnode);
@@ -472,22 +531,20 @@ void output_message(char *msgid, int mode, int headers_only)
 
        if (ch==0) {
                cprintf("text\n*** ?Message truncated\n000\n");
-               fclose(msg);
                cdb_free(dmsgtext);
-               return;
+               return(xtime);
                }
 
        if (headers_only) {
                /* give 'em a length */
                msg_len = 0L;
-               while(och=ch, ch = *mptr++, ch>0) {
+               while(ch = *mptr++, ch>0) {
                        ++msg_len;
                        }
                cprintf("mlen=%ld\n", msg_len);
                cprintf("000\n");
-               fclose(msg);
                cdb_free(dmsgtext);
-               return;
+               return(xtime);
                }
 
        /* signify start of msg text */
@@ -496,25 +553,23 @@ void output_message(char *msgid, int mode, int headers_only)
 
        /* If the format type on disk is 1 (fixed-format), then we want
         * everything to be output completely literally ... regardless of
-        * what message transfer format is in use.
+        * what message transfer format is in use.  Format type 4 is 
+        * temporarily being output this way as well.
         */
-       if (format_type == 1) {
-               och = 0;
-               len = 0;
-               while(och=ch, ch = *mptr++, ch>0) {
+       if ( (format_type == 1) || (format_type == 4)) {
+               strcpy(buf, "");
+               while(ch = *mptr++, ch>0) {
                        if (ch == 13) ch = 10;
-                       ++len;
-                       if ((ch!=10)||(och!=10)) {
-                               cprintf("%c", ch);
-                               if (ch==10) len = 0;
+                       if ( (ch == 10) || (strlen(buf)>250) ) {
+                               cprintf("%s\n", buf);
+                               strcpy(buf, "");
                                }
-                       if (len>=250) {
-                               len = 0;
-                               /* cprintf("%c", ch); */
-                               cprintf("%c", 10);
+                       else {
+                               buf[strlen(buf)+1] = 0;
+                               buf[strlen(buf)] = ch;
                                }
                        }
-               if (len!=0) cprintf("%c", 10);
+               if (strlen(buf)>0) cprintf("%s\n", buf);
                }
        /* If the message on disk is format 0 (Citadel vari-format), we
         * output using the formatter at 80 columns.  This is the final output
@@ -531,6 +586,7 @@ void output_message(char *msgid, int mode, int headers_only)
        /* now we're done */
        cprintf("000\n");
        cdb_free(dmsgtext);
+       return(xtime);
        }
 
 
@@ -541,11 +597,14 @@ void cmd_msg0(char *cmdbuf)
 {
        char msgid[256];
        int headers_only = 0;
+       int desired_section = 0;
 
        extract(msgid,cmdbuf,0);
-       headers_only = extract_int(cmdbuf,1);
+       headers_only = extract_int(cmdbuf, 1);
+       desired_section = extract_int(cmdbuf, 2);
 
-       output_message(msgid,MT_CITADEL,headers_only);
+       output_message(msgid, MT_CITADEL, headers_only, desired_section);
+       return;
        }
 
 
@@ -560,7 +619,7 @@ void cmd_msg2(char *cmdbuf)
        extract(msgid,cmdbuf,0);
        headers_only = extract_int(cmdbuf,1);
 
-       output_message(msgid,MT_RFC822,headers_only);
+       output_message(msgid,MT_RFC822,headers_only,0);
        }
 
 /* 
@@ -580,103 +639,65 @@ void cmd_msg3(char *cmdbuf)
        extract(msgid,cmdbuf,0);
        headers_only = extract_int(cmdbuf,1);
 
-       output_message(msgid,MT_RAW,headers_only);
+       output_message(msgid,MT_RAW,headers_only,0);
        }
 
 
 
 /*
- * message base operation to send a message to the master file
+ * Message base operation to send a message to the master file
+ * (returns new message number)
  */
-void send_message(char *filename, struct smreturn *retbuf, int generate_id)
-                               /* tempfilename of proper message */
-                               /* return information */
-                               /* set to 1 to generate an 'I' field */
-{
+long send_message(char *message_in_memory,     /* pointer to buffer */
+               size_t message_length,          /* length of buffer */
+               int generate_id) {              /* 1 to generate an I field */
 
-       FILE *fp;
        long newmsgid;
-
-       char *message_in_memory;
-       size_t templen;
-
-       fp = fopen(filename, "rb");
-
-       /* Measure the message */
-       lprintf(9, "Measuring the message\n");
-       fseek(fp, 0L, SEEK_END);
-       templen = ftell(fp);
-
-       /* Now read it into memory */
-       lprintf(9, "Allocating %ld bytes\n", templen);
-       message_in_memory = (char *) malloc(templen);
-       if (message_in_memory == NULL) {
-               lprintf(2, "Can't allocate memory to save message!\n");
-               fclose(fp);     
-               retbuf->smnumber = 0L;
-               retbuf->smpos = 0L;
-               return;
-               }
-
-       lprintf(9, "Reading it into memory\n"); 
-       fseek(fp, 0L, SEEK_SET);
-       fread(message_in_memory, templen, 1, fp);
-       fclose(fp);
+       char *actual_message;
+       size_t actual_length;
+       long retval;
+       char msgidbuf[32];
 
        /* Get a new message number */
        newmsgid = get_new_message_number();
 
-       /* Write our little bundle of joy into the message base */
+       if (generate_id) {
+               sprintf(msgidbuf, "I%ld", newmsgid);
+               actual_length = message_length + strlen(msgidbuf) + 1;
+               actual_message = mallok(actual_length);
+               memcpy(actual_message, message_in_memory, 3);
+               memcpy(&actual_message[3], msgidbuf, (strlen(msgidbuf)+1) );
+               memcpy(&actual_message[strlen(msgidbuf)+4],
+                       &message_in_memory[3], message_length - 3);
+               }
+       
+       else {
+               actual_message = message_in_memory;
+               actual_length = message_length;
+               }
 
-       lprintf(9, "Storing message %ld\n", newmsgid);
+       /* Write our little bundle of joy into the message base */
        begin_critical_section(S_MSGMAIN);
        if ( cdb_store(CDB_MSGMAIN, &newmsgid, sizeof(long),
-                       message_in_memory, templen) < 0 ) {
+                       actual_message, actual_length) < 0 ) {
                lprintf(2, "Can't store message\n");
-               retbuf->smnumber = 0L;
-               retbuf->smpos = 0L;
-               return;
+               retval = 0L;
+               }
+       else {
+               retval = newmsgid;
                }
        end_critical_section(S_MSGMAIN);
-       free(message_in_memory);
 
+       if (generate_id) {
+               phree(actual_message);
+               }
 
        /* Finally, return the pointers */
-       retbuf->smnumber = newmsgid;
-       retbuf->smpos= 1L; /* FIX we really won't be needing this */
+       return(retval);
        }
 
 
 
-
-
-
-void loadtroom(void) {
-       struct quickroom qrbuf;
-       int a;
-       unsigned newflags;
-
-       /* first try to locate the twit room */
-       for (a=0; a<MAXROOMS; ++a) {
-               getroom(&qrbuf,a);
-               if (!strucmp(qrbuf.QRname,config.c_twitroom)) {
-                       twitroom = a;
-                       return;
-                       }
-               }
-
-       /* if not found, try to create it  -  put it in the last slot */
-       twitroom = get_free_room_slot(-1);
-       if (twitroom>=0) {
-               newflags = create_room(twitroom,config.c_twitroom,0,"",0);
-               return;
-               }
-
-       /* as a last resort, point to Aide> */
-       twitroom = 2;
-       }
-
-
 /*
  * this is a simple file copy routine.
  */
@@ -707,152 +728,169 @@ void copy_file(char *from, char *to)
  */
 void save_message(char *mtmp,  /* file containing proper message */
                char *rec,      /* Recipient (if mail) */
-               char mtsflag,   /* 0 for normal, 1 to force Aide> room */
+               char *force_room, /* if non-zero length, force a room */
                int mailtype,   /* local or remote type, see citadel.h */
                int generate_id) /* set to 1 to generate an 'I' field */
 {
-       int a,e;
-       struct usersupp tempUS;
        char aaa[100];
-       struct smreturn smreturn;
-       int hold_rm;
+       char hold_rm[ROOMNAMELEN];
+       char actual_rm[ROOMNAMELEN];
+       char recipient[256];
+       long newmsgid;
+       char *message_in_memory;
+       struct stat statbuf;
+       size_t templen;
+       FILE *fp;
+       struct usersupp userbuf;
+       int a;
+       static int seqnum = 0;
 
-       send_message(mtmp,&smreturn,generate_id);
-       hold_rm=(-1);
+       lprintf(9, "save_message(%s,%s,%s,%d,%d)\n",
+               mtmp, rec, force_room, mailtype, generate_id);
 
-       /* if the user is a twit, move to the twit room for posting */
-       if (TWITDETECT) if (CC->usersupp.axlevel==2) {
-               if (twitroom<0) loadtroom();
-               hold_rm=CC->curr_rm;
-               CC->curr_rm=twitroom;
-               }
+       /* Strip non-printable characters out of the recipient name */
+       strcpy(recipient, rec);
+       for (a=0; a<strlen(recipient); ++a)
+               if (!isprint(recipient[a]))
+                       strcpy(&recipient[a], &recipient[a+1]);
 
-       /* and if this message is destined for Aide> then go there */
-       if (mtsflag) {
-               hold_rm=CC->curr_rm;
-               CC->curr_rm=2;
+       /* Measure the message */
+       stat(mtmp, &statbuf);
+       templen = statbuf.st_size;
+
+       /* Now read it into memory */
+       message_in_memory = (char *) mallok(templen);
+       if (message_in_memory == NULL) {
+               lprintf(2, "Can't allocate memory to save message!\n");
+               return;
                }
 
-       /* this call to usergoto() changes rooms if necessary.  It also
-          causes the latest fullroom structure to be read into memory */
-       usergoto(CC->curr_rm,0);
+       fp = fopen(mtmp, "rb");
+       fread(message_in_memory, templen, 1, fp);
+       fclose(fp);
 
-       /* Store the message pointer, but NOT for sent mail! */
-       if (CC->curr_rm != 1) {
+       newmsgid = send_message(message_in_memory, templen, generate_id);
+       phree(message_in_memory);
+       if (newmsgid <= 0L) return;
 
-               /* read in the quickroom record, obtaining a lock... */
-               lgetroom(&CC->quickroom,CC->curr_rm);
-               get_fullroom(&CC->fullroom,CC->curr_rm);
+       strcpy(actual_rm, CC->quickroom.QRname);
+       strcpy(hold_rm, "");
 
-               /* Delete the oldest message if there is one */
-               if (CC->fullroom.FRnum[0] != 0L) {
-                       cdb_delete(CDB_MSGMAIN,
-                               &CC->fullroom.FRnum[0], sizeof(long));
-                       }
+       /* If the user is a twit, move to the twit room for posting... */
+       if (TWITDETECT) if (CC->usersupp.axlevel==2) {
+               strcpy(hold_rm, actual_rm);
+               strcpy(actual_rm, config.c_twitroom);
+               }
 
-               /* Now scroll... */
-               for (a=0; a<(MSGSPERRM-1); ++a) {
-                       CC->fullroom.FRnum[a]=CC->fullroom.FRnum[a+1];
+       /* ...or if this is a private message, go to the target mailbox. */
+       if (strlen(recipient) > 0) {
+               /* mailtype = alias(recipient); */
+               if (mailtype == M_LOCAL) {
+                       if (getuser(&userbuf, recipient)!=0) {
+                               /* User not found, goto Aide */
+                               strcpy(force_room, AIDEROOM);
+                               }
+                       else {
+                               strcpy(hold_rm, actual_rm);
+                               MailboxName(actual_rm, &userbuf, MAILROOM);
+                               }
                        }
-       
-               /* ...and add the new message */
-               CC->fullroom.FRnum[MSGSPERRM-1]=smreturn.smnumber;
-       
-               /* Write it back to disk. */
-               put_fullroom(&CC->fullroom,CC->curr_rm);
-       
-               /* update quickroom */
-               CC->quickroom.QRhighest=CC->fullroom.FRnum[MSGSPERRM-1];
-               lputroom(&CC->quickroom,CC->curr_rm);
                }
 
-       /* Bump this user's messages posted counter.  Also, if the user is a
-          twit, give them access to the twit room. */
-       lgetuser(&CC->usersupp,CC->curr_user);
-       CC->usersupp.posted = CC->usersupp.posted + 1;
-       if (CC->curr_rm==twitroom) CC->usersupp.generation[twitroom]=CC->quickroom.QRgen;
-       lputuser(&CC->usersupp,CC->curr_user);
+       /* ...or if this message is destined for Aide> then go there. */
+       if (strlen(force_room) > 0) {
+               strcpy(hold_rm, actual_rm);
+               strcpy(actual_rm, force_room);
+               }
 
-       /* if mail, there's still more to do, if not, skip it */
-       if ((CC->curr_rm!=1)||(mtsflag)) goto ENTFIN;
+       /* This call to usergoto() changes rooms if necessary.  It also
+        * causes the latest message list to be read into memory.
+        */
+       usergoto(actual_rm, 0);
 
-       /* network mail - send a copy to the network program */
-       if (mailtype!=M_LOCAL) {
-               sprintf(aaa,"./network/spoolin/nm.%d",getpid());
-               copy_file(mtmp,aaa);
-               system("exec nohup ./netproc >/dev/null 2>&1 &");
+       /* read in the quickroom record, obtaining a lock... */
+       lgetroom(&CC->quickroom, actual_rm);
+
+       /* Fix an obscure bug */
+       if (!strcasecmp(CC->quickroom.QRname, AIDEROOM)) {
+               CC->quickroom.QRflags = CC->quickroom.QRflags & ~QR_MAILBOX;
                }
 
-       /* local mail - put a copy in the recipient's mailbox */
-       if (mailtype==M_LOCAL) {
-               if (lgetuser(&tempUS,rec)==0) {
-                       if (tempUS.mailnum[0] != 0L) {
-                               cdb_delete(CDB_MSGMAIN, &tempUS.mailnum[0],
-                                       sizeof(long));
-                               }
-                       for (e=0; e<(MAILSLOTS-1); ++e) {
-                               tempUS.mailnum[e]=tempUS.mailnum[e+1];
-                               }
-                       tempUS.mailnum[MAILSLOTS-1]=smreturn.smnumber;
-                       lputuser(&tempUS,rec);
-                       }
+       /* Add the message pointer to the room */
+       CC->quickroom.QRhighest = AddMessageToRoom(&CC->quickroom, newmsgid);
+
+       /* update quickroom */
+       lputroom(&CC->quickroom, actual_rm);
+
+       /* Network mail - send a copy to the network program. */
+       if ( (strlen(recipient)>0) && (mailtype != M_LOCAL) ) {
+               sprintf(aaa,"./network/spoolin/netmail.%04x.%04x.%04x",
+                       getpid(), CC->cs_pid, ++seqnum);
+               copy_file(mtmp,aaa);
+               system("exec nohup ./netproc -i >/dev/null 2>&1 &");
                }
 
-       /* if we've posted in a room other than the current room, then we
-          have to now go back to the current room... */
-ENTFIN:        if (hold_rm!=(-1)) {
-               usergoto(hold_rm,0);
+       /* Bump this user's messages posted counter. */
+       lgetuser(&CC->usersupp, CC->curr_user);
+       CC->usersupp.posted = CC->usersupp.posted + 1;
+       lputuser(&CC->usersupp, CC->curr_user);
+
+       /* If we've posted in a room other than the current room, then we
+        * have to now go back to the current room...
+        */
+       if (strlen(hold_rm) > 0) {
+               usergoto(hold_rm, 0);
                }
        unlink(mtmp);           /* delete the temporary file */
        }
 
 
 /*
- * generate an administrative message and post it in the Aide> room
+ * Generate an administrative message and post it in the Aide> room.
  */
 void aide_message(char *text)
 {
-       long now;
        FILE *fp;
 
-       time(&now);
        fp=fopen(CC->temp,"wb");
        fprintf(fp,"%c%c%c",255,MES_NORMAL,0);
        fprintf(fp,"Psysop%c",0);
-       fprintf(fp,"T%ld%c",now,0);
+       fprintf(fp,"T%ld%c", time(NULL), 0);
        fprintf(fp,"ACitadel%c",0);
        fprintf(fp,"OAide%c",0);
        fprintf(fp,"N%s%c",NODENAME,0);
        fprintf(fp,"M%s\n%c",text,0);
        fclose(fp);
-       save_message(CC->temp,"",1,M_LOCAL,1);
+       save_message(CC->temp,"",AIDEROOM,M_LOCAL,1);
        syslog(LOG_NOTICE,text);
        }
 
 
 
 /*
- * build a binary message to be saved on disk
+ * Build a binary message to be saved on disk.
  */
-void make_message(char *filename, struct usersupp *author, char *recipient, char *room, int type, int net_type, int format_type, char *fake_name)
-                       /* temporary file name */
-                          /* author's usersupp structure */
-                       /* NULL if it's not mail */
-                       /* room where it's going */
-                       /* see MES_ types in header file */
-                       /* local or remote type, see citadel.h */
-                       /* format type (see citadel.h) */
-{ 
+void make_message(
+       char *filename,                 /* temporary file name */
+       struct usersupp *author,        /* author's usersupp 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 net_type,                   /* see MES_ types in header file */
+       int format_type,                /* local or remote (see citadel.h) */
+       char *fake_name) {              /* who we're masquerading as */
+
        FILE *fp;
        int a;
-       long now;
+       time_t now;
        char dest_node[32];
        char buf[256];
 
-       /* don't confuse the poor folks if it's not routed mail. */
-       strcpy(dest_node,"");
+       /* Don't confuse the poor folks if it's not routed mail. */
+       strcpy(dest_node, "");
 
-       /* if net_type is M_BINARY, split out the destination node */
+
+       /* If net_type is M_BINARY, split out the destination node. */
        if (net_type == M_BINARY) {
                strcpy(dest_node,NODENAME);
                for (a=0; a<strlen(recipient); ++a) {
@@ -882,12 +920,19 @@ void make_message(char *filename, struct usersupp *author, char *recipient, char
           fprintf(fp,"A%s%c",fake_name,0);
        else
           fprintf(fp,"A%s%c",author->fullname,0);      /* author */
-       fprintf(fp,"O%s%c",CC->quickroom.QRname,0);     /* room */
+
+       if (CC->quickroom.QRflags & QR_MAILBOX) {       /* room */
+               fprintf(fp,"O%s%c", &CC->quickroom.QRname[11], 0);
+               }
+       else {
+               fprintf(fp,"O%s%c",CC->quickroom.QRname,0);
+               }
+
        fprintf(fp,"N%s%c",NODENAME,0);                 /* nodename */
        fprintf(fp,"H%s%c",HUMANNODE,0);                /* human nodename */
 
-       if (recipient[0]!=0) fprintf(fp,"R%s%c",recipient,0);
-       if (dest_node[0]!=0) fprintf(fp,"D%s%c",dest_node,0);
+       if (recipient[0]!=0) fprintf(fp, "R%s%c", recipient, 0);
+       if (dest_node[0]!=0) fprintf(fp, "D%s%c", dest_node, 0);
 
        putc('M',fp);
 
@@ -915,7 +960,8 @@ void cmd_ent0(char *entargs)
        int format_type = 0;
        char newusername[256];          /* <bc> */
 
-       int a,b,e;
+       int a,b;
+       int e = 0;
        int mtsflag = 0;
        struct usersupp tempUS;
        char buf[256];
@@ -931,13 +977,10 @@ void cmd_ent0(char *entargs)
                cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
                return;
                }
-       if (CC->curr_rm < 0) {
-               cprintf("%d No room selected.\n",ERROR);
-               return;
-               }
-       if ((CC->usersupp.axlevel<2)&&(CC->curr_rm!=1)) {
-               cprintf("%d Need to be validated to enter (except in Mail> to sysop)\n",
+       if ((CC->usersupp.axlevel<2)&&((CC->quickroom.QRflags&QR_MAILBOX)==0)) {
+               cprintf("%d Need to be validated to enter ",
                        ERROR+HIGHER_ACCESS_REQUIRED);
+               cprintf("(except in %s> to sysop)\n", MAILROOM);
                return;
                }
        if ((CC->usersupp.axlevel<4)&&(CC->quickroom.QRflags&QR_NETWORK)) {
@@ -957,7 +1000,8 @@ void cmd_ent0(char *entargs)
         if (post==2) {                 /* <bc> */
            if (CC->usersupp.axlevel<6)
            {
-              cprintf("%d\nYou don't have sufficient permission to do an aide post.\n", ERROR+HIGHER_ACCESS_REQUIRED);
+              cprintf("%d You don't have permission to do an aide post.\n",
+               ERROR+HIGHER_ACCESS_REQUIRED);
               return;
            }
           extract(newusername,entargs,4);
@@ -970,14 +1014,12 @@ void cmd_ent0(char *entargs)
        CC->cs_flags |= CS_POSTING;
        
        buf[0]=0;
-       if (CC->curr_rm==1) {
+       if (CC->quickroom.QRflags & QR_MAILBOX) {
                if (CC->usersupp.axlevel>=2) {
                        strcpy(buf,recipient);
                        }
                else strcpy(buf,"sysop");
-               lprintf(9, "aliasing...\n");
                e=alias(buf);                   /* alias and mail type */
-               lprintf(9,"...type is %d\n", e);
                if ((buf[0]==0) || (e==M_ERROR)) {
                        cprintf("%d Unknown address - cannot send message.\n",
                                ERROR+NO_SUCH_USER);
@@ -995,12 +1037,12 @@ void cmd_ent0(char *entargs)
                                ERROR+HIGHER_ACCESS_REQUIRED);
                        return;
                        }
-               if (!strucmp(buf,"sysop")) {
+               if (!strcasecmp(buf,"sysop")) {
                        mtsflag=1;
                        goto SKFALL;
                        }
                if (e!=M_LOCAL) goto SKFALL;    /* don't search local file  */
-               if (!strucmp(buf,CC->usersupp.fullname)) {
+               if (!strcasecmp(buf,CC->usersupp.fullname)) {
                        cprintf("%d Can't send mail to yourself!\n",
                                ERROR+NO_SUCH_USER);
                        return;
@@ -1009,9 +1051,7 @@ void cmd_ent0(char *entargs)
                /* Check to make sure the user exists; also get the correct
                * upper/lower casing of the name. 
                */
-               lprintf(9, "checking validity of %s\n", buf);
                a = getuser(&tempUS,buf);
-               lprintf(9, "getuser() returned %d\n", a);
                if (a != 0) {
                        cprintf("%d No such user.\n",ERROR+NO_SUCH_USER);
                        return;
@@ -1021,10 +1061,10 @@ void cmd_ent0(char *entargs)
        
 SKFALL: b=MES_NORMAL;
        if (CC->quickroom.QRflags&QR_ANONONLY) b=MES_ANON;
-       if (CC->quickroom.QRflags&QR_ANON2) {
+       if (CC->quickroom.QRflags&QR_ANONOPT) {
                if (anon_flag==1) b=MES_AN2;
                }
-       if (CC->curr_rm!=1) buf[0]=0;
+       if ((CC->quickroom.QRflags & QR_MAILBOX) == 0) buf[0]=0;
 
        /* If we're only checking the validity of the request, return
         * success without creating the message.
@@ -1042,7 +1082,7 @@ SKFALL: b=MES_NORMAL;
              make_message(CC->temp,&CC->usersupp,buf,CC->quickroom.QRname,b,e,format_type, CC->fake_username);
           else
              make_message(CC->temp,&CC->usersupp,buf,CC->quickroom.QRname,b,e,format_type, "");
-       save_message(CC->temp,buf,mtsflag,e,1);
+       save_message(CC->temp,buf, (mtsflag ? AIDEROOM : ""), e,1);
         CC->fake_postname[0]='\0';
        return;
        }
@@ -1056,7 +1096,8 @@ void cmd_ent3(char *entargs)
 {
        char recp[256];
        char buf[256];
-       int a, e;
+       int a;
+       int e = 0;
        struct usersupp tempUS;
        long msglen;
        long bloklen;
@@ -1068,17 +1109,18 @@ void cmd_ent3(char *entargs)
                return;
                }
 
-       if (CC->curr_rm < 0) {
-               cprintf("%d No room selected.\n",ERROR);
-               return;
-               }
+       /* See if there's a recipient, but make sure it's a real one */
+       extract(recp, entargs, 1);
+       for (a=0; a<strlen(recp); ++a)
+               if (!isprint(recp[a]))
+                       strcpy(&recp[a], &recp[a+1]);
+       while (isspace(recp[0])) strcpy(recp, &recp[1]);
+       while (isspace(recp[strlen(recp)-1])) recp[strlen(recp)-1] = 0;
 
-       if (CC->curr_rm == 1) { /* If we're in Mail, check the recipient */
-               extract(recp, entargs, 1);
-               lprintf(9, "aliasing...\n");
+       /* If we're in Mail, check the recipient */
+       if (strlen(recp) > 0) {
                e=alias(recp);                  /* alias and mail type */
-               lprintf(9,"...type is %d\n", e);
-               if ((buf[0]==0) || (e==M_ERROR)) {
+               if ((recp[0]==0) || (e==M_ERROR)) {
                        cprintf("%d Unknown address - cannot send message.\n",
                                ERROR+NO_SUCH_USER);
                        return;
@@ -1086,7 +1128,8 @@ void cmd_ent3(char *entargs)
                if (e == M_LOCAL) {
                        a = getuser(&tempUS,recp);
                        if (a!=0) {
-                               cprintf("%d No such user.\n", ERROR+NO_SUCH_USER);
+                               cprintf("%d No such user.\n",
+                                       ERROR+NO_SUCH_USER);
                                return;
                                }
                        }
@@ -1117,7 +1160,7 @@ void cmd_ent3(char *entargs)
                }
        fclose(fp);
 
-       save_message(CC->temp, recp, 0, e, 0);
+       save_message(CC->temp, recp, "", e, 0);
        }
 
 
@@ -1131,34 +1174,32 @@ void cmd_dele(char *delstr)
 
        getuser(&CC->usersupp,CC->curr_user);
        if ((CC->usersupp.axlevel < 6)
-          && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
+          && (CC->usersupp.usernum != CC->quickroom.QRroomaide)
+          && ((CC->quickroom.QRflags & QR_MAILBOX) == 0)) {
                cprintf("%d Higher access required.\n",
                        ERROR+HIGHER_ACCESS_REQUIRED);
                return;
                }
 
-       delnum = atol(delstr);
-       if (CC->curr_rm==1) {
-               cprintf("%d Can't delete mail.\n",ERROR);
-               return;
-               }
+       delnum = extract_long(delstr, 0);
        
        /* get room records, obtaining a lock... */
-       lgetroom(&CC->quickroom,CC->curr_rm);
-       get_fullroom(&CC->fullroom,CC->curr_rm);
+       lgetroom(&CC->quickroom,CC->quickroom.QRname);
+       get_msglist(&CC->quickroom);
 
        ok = 0;
-       for (a=0; a<MSGSPERRM; ++a)
-               if (CC->fullroom.FRnum[a]==delnum) {
-                       CC->fullroom.FRnum[a]=0L;
+       if (CC->num_msgs > 0) for (a=0; a<(CC->num_msgs); ++a) {
+               if (MessageFromList(a) == delnum) {
+                       SetMessageInList(a, 0L);
                        ok = 1;
                        }
+               }
 
-       sort_fullroom(&CC->fullroom);
-       CC->quickroom.QRhighest = CC->fullroom.FRnum[MSGSPERRM-1];
+       CC->num_msgs = sort_msglist(CC->msglist, CC->num_msgs);
+       CC->quickroom.QRhighest = MessageFromList(CC->num_msgs - 1);
 
-       put_fullroom(&CC->fullroom,CC->curr_rm);
-       lputroom(&CC->quickroom,CC->curr_rm);
+       put_msglist(&CC->quickroom);
+       lputroom(&CC->quickroom,CC->quickroom.QRname);
        if (ok==1) {
                cdb_delete(CDB_MSGMAIN, &delnum, sizeof(long));
                cprintf("%d Message deleted.\n",OK);
@@ -1175,19 +1216,12 @@ void cmd_move(char *args)
        long num;
        char targ[32];
        int a;
-       int targ_slot;
        struct quickroom qtemp;
-       struct fullroom ftemp;
        int foundit;
 
        num = extract_long(args,0);
        extract(targ,args,1);
        
-       if (CC->curr_rm < 0) {
-               cprintf("%d no room\n",ERROR);
-               return;
-               }
-
        getuser(&CC->usersupp,CC->curr_user);
        if ((CC->usersupp.axlevel < 6)
           && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
@@ -1196,53 +1230,37 @@ void cmd_move(char *args)
                return;
                }
 
-       targ_slot = (-1);
-       for (a=0; a<MAXROOMS; ++a) {
-               getroom(&qtemp,a);
-               if (!strucmp(qtemp.QRname,targ)) {
-                       targ_slot = a;
-                       a = MAXROOMS;
-                       }
-               }
-       if (targ_slot < 0) {
+       if (getroom(&qtemp, targ) != 0) {
                cprintf("%d '%s' does not exist.\n",ERROR,targ);
                return;
                }
 
        /* yank the message out of the current room... */
-       lgetroom(&CC->quickroom,CC->curr_rm);
-       get_fullroom(&CC->fullroom,CC->curr_rm);
+       lgetroom(&CC->quickroom, CC->quickroom.QRname);
+       get_msglist(&CC->quickroom);
 
        foundit = 0;
-       for (a=0; a<MSGSPERRM; ++a) {
-               if (CC->fullroom.FRnum[a] == num) {
+       for (a=0; a<(CC->num_msgs); ++a) {
+               if (MessageFromList(a) == num) {
                        foundit = 1;
-                       CC->fullroom.FRnum[a] = 0L;
+                       SetMessageInList(a, 0L);
                        }
                }
        if (foundit) {
-               sort_fullroom(&CC->fullroom);
-               put_fullroom(&CC->fullroom,CC->curr_rm);
-               CC->quickroom.QRhighest = CC->fullroom.FRnum[MSGSPERRM-1];
+               CC->num_msgs = sort_msglist(CC->msglist, CC->num_msgs);
+               put_msglist(&CC->quickroom);
+               CC->quickroom.QRhighest = MessageFromList((CC->num_msgs)-1);
                }
-       lputroom(&CC->quickroom,CC->curr_rm);
+       lputroom(&CC->quickroom,CC->quickroom.QRname);
        if (!foundit) {
                cprintf("%d msg %ld does not exist.\n",ERROR,num);
                return;
                }
 
-       lgetroom(&qtemp,targ_slot);
-       get_fullroom(&ftemp,targ_slot);
-       if (CC->fullroom.FRnum[0] != 0L) {
-               cdb_delete(CDB_MSGMAIN, &CC->fullroom.FRnum[0], sizeof(long));
-               }
-       for (a=0; a<MSGSPERRM-1; ++a) {
-               ftemp.FRnum[a]=ftemp.FRnum[a+1];
-               }
-       ftemp.FRnum[MSGSPERRM-1] = num;
-       sort_fullroom(&ftemp);
-       qtemp.QRhighest = ftemp.FRnum[MSGSPERRM-1];
-       put_fullroom(&ftemp,targ_slot);
-       lputroom(&qtemp,targ_slot);
-       cprintf("%d ok\n",OK);
+       /* put the message into the target room */
+       lgetroom(&qtemp, targ);
+       qtemp.QRhighest = AddMessageToRoom(&qtemp, num);
+       lputroom(&qtemp, targ);
+
+       cprintf("%d Message moved.\n", OK);
        }