]> code.citadel.org Git - citadel.git/blobdiff - citadel/msgbase.c
Added the OPNA command for downloading attachments
[citadel.git] / citadel / msgbase.c
index e76c6d3d91f7c51fa2e172cec40da3023ea9bcef..9a037ea3cacfd75195ee42d366c5af94caea38d9 100644 (file)
 #include "sysdep_decls.h"
 #include "room_ops.h"
 #include "user_ops.h"
+#include "file_ops.h"
 #include "control.h"
 #include "dynloader.h"
 #include "tools.h"
+#include "mime_parser.h"
 
 #define MSGS_ALL       0
 #define MSGS_OLD       1
@@ -333,12 +335,74 @@ FMTEND:   cprintf("\n");
        }
 
 
+
+/*
+ * Callback function for mime parser that simply lists the part
+ */
+void list_this_part(char *name, char *filename, char *partnum, char *disp,
+                        void *content, char *cbtype, size_t length) {
+
+       cprintf("part=%s|%s|%s|%s|%s|%d\n",
+               name, filename, partnum, disp, cbtype, length);
+       }
+
+
+/*
+ * Callback function for mime parser that wants to display text
+ */
+void fixed_output(char *name, char *filename, char *partnum, char *disp,
+                        void *content, char *cbtype, size_t length) {
+
+       if (!strcasecmp(cbtype, "text/plain")) {
+               client_write(content, length);
+               }
+       else {
+               cprintf("Part %s: %s (%s) (%d bytes)\n",
+                       partnum, filename, cbtype, length);
+               }
+       }
+
+
+/*
+ * Callback function for mime parser that opens a section for downloading
+ */
+void mime_download(char *name, char *filename, char *partnum, char *disp,
+                        void *content, char *cbtype, size_t length) {
+
+       char tmpname[PATH_MAX];
+       static int seq = 0;
+
+       /* Silently go away if there's already a download open... */
+       if (CC->download_fp != NULL) return;
+
+       /* ...or if this is not the desired section */
+       if (strcasecmp(CC->desired_section, partnum)) return;
+
+       snprintf(tmpname, sizeof tmpname,
+               "/tmp/CitServer.download.%4x.%4x", getpid(), ++seq);
+
+       CC->download_fp = fopen(tmpname, "wb+");
+       if (CC->download_fp == NULL) return;
+
+       /* Unlink the file while it's open, to guarantee that the
+        * temp file will always be deleted.
+        */
+       unlink(tmpname);
+
+       fwrite(content, length, 1, CC->download_fp);
+       fflush(CC->download_fp);
+       rewind(CC->download_fp);
+
+       OpenCmdResult(filename, cbtype);
+       }
+
+
+
 /*
  * Get a message off disk.  (return value is the message's timestamp)
  * 
  */
-time_t output_message(char *msgid, int mode,
-                       int headers_only, int desired_section) {
+time_t output_message(char *msgid, int mode, int headers_only) {
        long msg_num;
        int a;
        CIT_UBYTE ch, rch;
@@ -357,7 +421,7 @@ time_t output_message(char *msgid, int mode,
        char lnode[256];
        char mid[256];
        time_t xtime = 0L;
-       /* */
+       /*                                       */
 
        msg_num = atol(msgid);
 
@@ -422,6 +486,41 @@ time_t output_message(char *msgid, int mode,
        anon_flag = *mptr++;
        format_type = *mptr++;
 
+       /* Are we downloading a MIME component? */
+       if (mode == MT_DOWNLOAD) {
+               if (format_type != 4) {
+                       cprintf("%d This is not a MIME message.\n",
+                               ERROR);
+                       }
+               else if (CC->download_fp != NULL) {
+                       cprintf("%d You already have a download open.\n",
+                               ERROR);
+                       }
+               else {
+                       /* Skip to the message body */
+                       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);
+                               }
+                       /* Now parse it */
+                       mime_parser(mptr, NULL, *mime_download);
+                       /* If there's no file open by this time, the requested
+                        * section wasn't found, so print an error
+                        */
+                       if (CC->download_fp == NULL) {
+                               cprintf("%d Section %s not found.\n",
+                                       ERROR+FILE_NOT_FOUND,
+                                       CC->desired_section);
+                               }
+                       }
+               cdb_free(dmsgtext);
+               return(xtime);
+               }
+
        /* Are we just looking for the message date? */
        if (mode == MT_DATE) while(ch = *mptr++, (ch!='M' && ch!=0)) {
                buf[0] = 0;
@@ -442,7 +541,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");
@@ -450,7 +549,8 @@ time_t output_message(char *msgid, int mode,
 
        /* begin header processing loop for Citadel message format */
 
-       if (mode == MT_CITADEL) while(ch = *mptr++, (ch!='M' && ch!=0)) {
+       if ((mode == MT_CITADEL)||(mode == MT_MIME))
+          while(ch = *mptr++, (ch!='M' && ch!=0)) {
                buf[0] = 0;
                do {
                        buf[strlen(buf)+1] = 0;
@@ -535,6 +635,14 @@ time_t output_message(char *msgid, int mode,
                return(xtime);
                }
 
+       /* do some sort of MIME output */
+       if ( (mode == MT_MIME) && (format_type == 4) ) {
+               mime_parser(mptr, NULL, *list_this_part);
+               cprintf("000\n");
+               cdb_free(dmsgtext);
+               return(xtime);
+               }
+
        if (headers_only) {
                /* give 'em a length */
                msg_len = 0L;
@@ -549,7 +657,7 @@ time_t output_message(char *msgid, int mode,
 
        /* signify start of msg text */
        if (mode == MT_CITADEL) cprintf("text\n");
-       if (mode == MT_RFC822) cprintf("\n");
+       if ( (mode == MT_RFC822) && (format_type != 4) ) cprintf("\n");
 
        /* If the format type on disk is 1 (fixed-format), then we want
         * everything to be output completely literally ... regardless of
@@ -580,7 +688,14 @@ time_t output_message(char *msgid, int mode,
        if (format_type == 0) {
                memfmout(80,mptr,0);
                }
-
+       /* If the message on disk is format 4 (MIME), we've gotta hand it
+        * off to the MIME parser.  The client has already been told that
+        * this message is format 1 (fixed format), so the callback function
+        * we use will display those parts as-is.
+        */
+       if (format_type == 4) {
+               mime_parser(mptr, NULL, *fixed_output);
+               }
 
        /* now we're done */
        cprintf("000\n");
@@ -596,13 +711,11 @@ 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);
-       desired_section = extract_int(cmdbuf, 2);
 
-       output_message(msgid,MT_CITADEL, headers_only, desired_section);
+       output_message(msgid, MT_CITADEL, headers_only);
        return;
        }
 
@@ -618,7 +731,7 @@ void cmd_msg2(char *cmdbuf)
        extract(msgid,cmdbuf,0);
        headers_only = extract_int(cmdbuf,1);
 
-       output_message(msgid,MT_RFC822,headers_only,0);
+       output_message(msgid,MT_RFC822,headers_only);
        }
 
 /* 
@@ -638,7 +751,34 @@ void cmd_msg3(char *cmdbuf)
        extract(msgid,cmdbuf,0);
        headers_only = extract_int(cmdbuf,1);
 
-       output_message(msgid,MT_RAW,headers_only,0);
+       output_message(msgid,MT_RAW,headers_only);
+       }
+
+/* 
+ * display a message (mode 4 - MIME) (FIX ... still evolving, not complete)
+ */
+void cmd_msg4(char *cmdbuf)
+{
+       char msgid[256];
+
+       extract(msgid, cmdbuf, 0);
+
+       output_message(msgid, MT_MIME, 0);
+       }
+
+
+
+/*
+ * Open a component of a MIME message as a download file 
+ */
+void cmd_opna(char *cmdbuf)
+{
+       char msgid[256];
+
+       extract(msgid, cmdbuf, 0);
+       extract(CC->desired_section, cmdbuf, 1);
+
+       output_message(msgid, MT_DOWNLOAD, 0);
        }
 
 
@@ -727,13 +867,14 @@ 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,    /* 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 */
 {
        char aaa[100];
        char hold_rm[ROOMNAMELEN];
        char actual_rm[ROOMNAMELEN];
+       char force_room[ROOMNAMELEN];
        char recipient[256];
        long newmsgid;
        char *message_in_memory;
@@ -744,8 +885,10 @@ void save_message(char *mtmp,      /* file containing proper message */
        int a;
        static int seqnum = 0;
 
-       lprintf(9, "save_message(%s,%s,%d,%d,%d)\n",
-               mtmp, rec, mtsflag, mailtype, generate_id);
+       lprintf(9, "save_message(%s,%s,%s,%d,%d)\n",
+               mtmp, rec, force_room, mailtype, generate_id);
+
+       strcpy(force_room, force);
 
        /* Strip non-printable characters out of the recipient name */
        strcpy(recipient, rec);
@@ -782,23 +925,32 @@ void save_message(char *mtmp,     /* file containing proper message */
                }
 
        /* ...or if this is a private message, go to the target mailbox. */
+       lprintf(9, "mailbox aliasing loop\n");
+lprintf(9, "1\n");
        if (strlen(recipient) > 0) {
+lprintf(9, "2\n");
                /* mailtype = alias(recipient); */
                if (mailtype == M_LOCAL) {
+lprintf(9, "3\n");
                        if (getuser(&userbuf, recipient)!=0) {
-                               mtsflag = 1; /* User not found, goto Aide */
+                               /* User not found, goto Aide */
+lprintf(9, "4\n");
+                               strcpy(force_room, AIDEROOM);
                                }
                        else {
+lprintf(9, "5\n");
                                strcpy(hold_rm, actual_rm);
+lprintf(9, "6\n");
                                MailboxName(actual_rm, &userbuf, MAILROOM);
                                }
                        }
                }
 
        /* ...or if this message is destined for Aide> then go there. */
-       if (mtsflag) {
+       lprintf(9, "actual room forcing loop\n");
+       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
@@ -822,8 +974,8 @@ void save_message(char *mtmp,       /* file containing proper message */
 
        /* 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);
+               sprintf(aaa,"./network/spoolin/netmail.%04lx.%04x.%04x",
+                       (long)getpid(), CC->cs_pid, ++seqnum);
                copy_file(mtmp,aaa);
                system("exec nohup ./netproc -i >/dev/null 2>&1 &");
                }
@@ -848,20 +1000,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);
        }
 
@@ -1005,7 +1155,7 @@ void cmd_ent0(char *entargs)
               return;
            }
           extract(newusername,entargs,4);
-          bzero(CC->fake_postname, 32);
+          memset(CC->fake_postname, 0, 32);
            strcpy(CC->fake_postname, newusername);
            cprintf("%d Ok\n",OK);
            return;
@@ -1082,7 +1232,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;
        }
@@ -1160,7 +1310,7 @@ void cmd_ent3(char *entargs)
                }
        fclose(fp);
 
-       save_message(CC->temp, recp, 0, e, 0);
+       save_message(CC->temp, recp, "", e, 0);
        }