]> 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 c683a6bdf3637a9eb9c36c6e67e504646374a8d4..257e03ceede7f5676ce1ae11d376713fd2197b8f 100644 (file)
@@ -1,3 +1,4 @@
+/* $Id$ */
 #include <stdlib.h>
 #include <unistd.h>
 #include <stdio.h>
@@ -19,6 +20,7 @@
 #include "user_ops.h"
 #include "control.h"
 #include "dynloader.h"
+#include "tools.h"
 
 #define MSGS_ALL       0
 #define MSGS_OLD       1
 extern struct config config;
 
 
+/*
+ * 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]);
+                       }
+               }
+       }
+
+
 /*
  * Aliasing for network mail.
  * (Error messages have been commented out, because this is a server.)
@@ -39,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");
@@ -321,9 +346,6 @@ time_t output_message(char *msgid, int mode,
        char buf[1024];
        long msg_len;
        int msg_ok = 0;
-       char boundary[256];             /* attachment boundary */
-       char current_section = 0;       /* section currently being parsed */
-       int has_attachments = 0;
 
        struct cdbdata *dmsgtext;
        char *mptr;
@@ -335,9 +357,8 @@ time_t output_message(char *msgid, int mode,
        char lnode[256];
        char mid[256];
        time_t xtime = 0L;
-       /* */
+       /*                                       */
 
-       strcpy(boundary, "");
        msg_num = atol(msgid);
 
 
@@ -421,7 +442,7 @@ time_t output_message(char *msgid, int mode,
        /* 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");
@@ -447,10 +468,6 @@ time_t output_message(char *msgid, int mode,
                                cprintf(" [%s]",buf);
                        cprintf("\n");
                        }
-               else if (ch=='Z') {
-                       has_attachments = 1;
-                       sprintf(boundary, "--%s", buf);
-                       }
                else if (ch=='P') cprintf("path=%s\n",buf);
                else if (ch=='U') cprintf("subj=%s\n",buf);
                else if (ch=='I') cprintf("msgn=%s\n",buf);
@@ -536,21 +553,15 @@ time_t output_message(char *msgid, int mode,
 
        /* 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) {
+       if ( (format_type == 1) || (format_type == 4)) {
                strcpy(buf, "");
                while(ch = *mptr++, ch>0) {
                        if (ch == 13) ch = 10;
                        if ( (ch == 10) || (strlen(buf)>250) ) {
-                               if (has_attachments) if (!strncmp(buf, boundary, strlen(boundary))) {
-                                       ++current_section;
-                                       }
-                               if (current_section == desired_section) {
-                                       if ( (has_attachments == 0) || (strncmp(buf, boundary, strlen(boundary)))) {
-                                               cprintf("%s\n", buf);
-                                               }
-                                       }
+                               cprintf("%s\n", buf);
                                strcpy(buf, "");
                                }
                        else {
@@ -592,7 +603,7 @@ void cmd_msg0(char *cmdbuf)
        headers_only = extract_int(cmdbuf, 1);
        desired_section = extract_int(cmdbuf, 2);
 
-       output_message(msgid,MT_CITADEL, headers_only, desired_section);
+       output_message(msgid, MT_CITADEL, headers_only, desired_section);
        return;
        }
 
@@ -642,23 +653,47 @@ long send_message(char *message_in_memory,        /* pointer to buffer */
                int generate_id) {              /* 1 to generate an I field */
 
        long newmsgid;
+       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;
+               }
 
+       /* 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, message_length) < 0 ) {
+                       actual_message, actual_length) < 0 ) {
                lprintf(2, "Can't store message\n");
-               end_critical_section(S_MSGMAIN);
-               return 0L;
+               retval = 0L;
+               }
+       else {
+               retval = newmsgid;
                }
        end_critical_section(S_MSGMAIN);
 
+       if (generate_id) {
+               phree(actual_message);
+               }
+
        /* Finally, return the pointers */
-       return(newmsgid);
+       return(retval);
        }
 
 
@@ -693,7 +728,7 @@ 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 */
 {
@@ -707,13 +742,24 @@ void save_message(char *mtmp,     /* file containing proper message */
        size_t templen;
        FILE *fp;
        struct usersupp userbuf;
+       int a;
+       static int seqnum = 0;
+
+       lprintf(9, "save_message(%s,%s,%s,%d,%d)\n",
+               mtmp, rec, force_room, mailtype, generate_id);
+
+       /* 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]);
 
        /* Measure the message */
        stat(mtmp, &statbuf);
        templen = statbuf.st_size;
 
        /* Now read it into memory */
-       message_in_memory = (char *) malloc(templen);
+       message_in_memory = (char *) mallok(templen);
        if (message_in_memory == NULL) {
                lprintf(2, "Can't allocate memory to save message!\n");
                return;
@@ -724,12 +770,11 @@ void save_message(char *mtmp,     /* file containing proper message */
        fclose(fp);
 
        newmsgid = send_message(message_in_memory, templen, generate_id);
-       free(message_in_memory);
+       phree(message_in_memory);
        if (newmsgid <= 0L) return;
 
        strcpy(actual_rm, CC->quickroom.QRname);
        strcpy(hold_rm, "");
-       strcpy(recipient, rec);
 
        /* If the user is a twit, move to the twit room for posting... */
        if (TWITDETECT) if (CC->usersupp.axlevel==2) {
@@ -739,10 +784,11 @@ void save_message(char *mtmp,     /* file containing proper message */
 
        /* ...or if this is a private message, go to the target mailbox. */
        if (strlen(recipient) > 0) {
-               mailtype = alias(recipient);
+               /* mailtype = alias(recipient); */
                if (mailtype == M_LOCAL) {
                        if (getuser(&userbuf, recipient)!=0) {
-                               mtsflag = 1; /* User not found, goto Aide */
+                               /* User not found, goto Aide */
+                               strcpy(force_room, AIDEROOM);
                                }
                        else {
                                strcpy(hold_rm, actual_rm);
@@ -752,12 +798,11 @@ void save_message(char *mtmp,     /* file containing proper message */
                }
 
        /* ...or if this message is destined for Aide> then go there. */
-       if (mtsflag) {
+       if (strlen(force_room) > 0) {
                strcpy(hold_rm, actual_rm);
-               strcpy(actual_rm, AIDEROOM);
+               strcpy(actual_rm, force_room);
                }
 
-
        /* This call to usergoto() changes rooms if necessary.  It also
         * causes the latest message list to be read into memory.
         */
@@ -766,25 +811,30 @@ void save_message(char *mtmp,     /* file containing proper message */
        /* 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;
+               }
+
        /* Add the message pointer to the room */
-       AddMessageToRoom(&CC->quickroom, newmsgid);
+       CC->quickroom.QRhighest = AddMessageToRoom(&CC->quickroom, newmsgid);
 
        /* update quickroom */
-       CC->quickroom.QRhighest = newmsgid;
        lputroom(&CC->quickroom, actual_rm);
 
-       /* 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);
-
        /* Network mail - send a copy to the network program. */
        if ( (strlen(recipient)>0) && (mailtype != M_LOCAL) ) {
-               sprintf(aaa,"./network/spoolin/nm.%d",getpid());
+               sprintf(aaa,"./network/spoolin/netmail.%04x.%04x.%04x",
+                       getpid(), CC->cs_pid, ++seqnum);
                copy_file(mtmp,aaa);
-               system("exec nohup ./netproc >/dev/null 2>&1 &");
+               system("exec nohup ./netproc -i >/dev/null 2>&1 &");
                }
 
+       /* 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...
         */
@@ -800,20 +850,18 @@ void save_message(char *mtmp,     /* file containing proper message */
  */
 void aide_message(char *text)
 {
-       time_t 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);
        }
 
@@ -830,8 +878,7 @@ void make_message(
        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 */
-       char *boundary) {               /* boundary (if exist attachments) */
+       char *fake_name) {              /* who we're masquerading as */
 
        FILE *fp;
        int a;
@@ -842,6 +889,7 @@ void make_message(
        /* 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 == M_BINARY) {
                strcpy(dest_node,NODENAME);
@@ -872,13 +920,19 @@ void make_message(
           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 (boundary[0]!=0) fprintf(fp, "Z%s%c", boundary, 0);
 
        putc('M',fp);
 
@@ -905,7 +959,6 @@ void cmd_ent0(char *entargs)
        int anon_flag = 0;
        int format_type = 0;
        char newusername[256];          /* <bc> */
-       char boundary[256];
 
        int a,b;
        int e = 0;
@@ -917,7 +970,6 @@ void cmd_ent0(char *entargs)
        extract(recipient,entargs,1);
        anon_flag = extract_int(entargs,2);
        format_type = extract_int(entargs,3);
-       extract(boundary, entargs, 5);
 
        /* first check to make sure the request is valid. */
 
@@ -1024,13 +1076,13 @@ SKFALL: b=MES_NORMAL;
        
        cprintf("%d send message\n",SEND_LISTING);
        if (CC->fake_postname[0])
-          make_message(CC->temp,&CC->usersupp,buf,CC->quickroom.QRname,b,e,format_type, CC->fake_postname, boundary);
+          make_message(CC->temp,&CC->usersupp,buf,CC->quickroom.QRname,b,e,format_type, CC->fake_postname);
        else
           if (CC->fake_username[0])
-             make_message(CC->temp,&CC->usersupp,buf,CC->quickroom.QRname,b,e,format_type, CC->fake_username, boundary);
+             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, "", boundary);
-       save_message(CC->temp,buf,mtsflag,e,1);
+             make_message(CC->temp,&CC->usersupp,buf,CC->quickroom.QRname,b,e,format_type, "");
+       save_message(CC->temp,buf, (mtsflag ? AIDEROOM : ""), e,1);
         CC->fake_postname[0]='\0';
        return;
        }
@@ -1057,11 +1109,18 @@ void cmd_ent3(char *entargs)
                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 we're in Mail, check the recipient */
-       if (CC->quickroom.QRflags & QR_MAILBOX) {
-               extract(recp, entargs, 1);
+       if (strlen(recp) > 0) {
                e=alias(recp);                  /* alias and mail type */
-               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;
@@ -1069,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;
                                }
                        }
@@ -1100,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);
        }
 
 
@@ -1114,17 +1174,14 @@ 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->quickroom.QRflags & QR_MAILBOX) {
-               cprintf("%d Can't delete mail.\n",ERROR);
-               return;
-               }
+       delnum = extract_long(delstr, 0);
        
        /* get room records, obtaining a lock... */
        lgetroom(&CC->quickroom,CC->quickroom.QRname);
@@ -1202,7 +1259,7 @@ void cmd_move(char *args)
 
        /* put the message into the target room */
        lgetroom(&qtemp, targ);
-       AddMessageToRoom(&qtemp, num);
+       qtemp.QRhighest = AddMessageToRoom(&qtemp, num);
        lputroom(&qtemp, targ);
 
        cprintf("%d Message moved.\n", OK);