]> code.citadel.org Git - citadel.git/blobdiff - citadel/imap_fetch.c
* More IMAP tweaks
[citadel.git] / citadel / imap_fetch.c
index ed13cca423556cdeb46bbf7d090e55512ff58337..5bfc50520eaa31101c884c8d98a108456466a49c 100644 (file)
@@ -36,6 +36,7 @@
 #include "msgbase.h"
 #include "tools.h"
 #include "internet_addressing.h"
+#include "mime_parser.h"
 #include "serv_imap.h"
 #include "imap_tools.h"
 #include "imap_fetch.h"
 
 
 
+struct imap_fetch_part {
+       char desired_section[256];
+       FILE *output_fp;
+};
+
 /*
  * Individual field functions for imap_do_fetch_msg() ...
  */
@@ -82,7 +88,7 @@ void imap_fetch_internaldate(struct CtdlMessage *msg) {
  *     "RFC822.SIZE"   size of translated message
  *     "RFC822.TEXT"   body only (without leading blank line)
  */
-void imap_fetch_rfc822(int msgnum, char *whichfmt) {
+void imap_fetch_rfc822(int msgnum, char *whichfmt, struct CtdlMessage *msg) {
        FILE *tmp;
        char buf[1024];
        char *ptr;
@@ -100,8 +106,11 @@ void imap_fetch_rfc822(int msgnum, char *whichfmt) {
         * Load the message into a temp file for translation and measurement
         */ 
        CtdlRedirectOutput(tmp, -1);
-       CtdlOutputMsg(msgnum, MT_RFC822, 0, 0, 1);
+       CtdlOutputPreLoadedMsg(msg, msgnum, MT_RFC822, 0, 0, 1);
        CtdlRedirectOutput(NULL, -1);
+       if (!is_valid_message(msg)) {
+               lprintf(1, "WARNING: output clobbered the message!\n");
+       }
 
        /*
         * Now figure out where the headers/text break is.  IMAP considers the
@@ -154,10 +163,190 @@ void imap_fetch_rfc822(int msgnum, char *whichfmt) {
 }
 
 
+
+/*
+ * Load a specific part of a message into the temp file to be output to a
+ * client.  FIXME we can handle parts like "2" and "2.1" and even "2.MIME"
+ * but we still can't handle "2.HEADER" (which might not be a problem, because
+ * we currently don't have the ability to break out nested RFC822's anyway).
+ *
+ * Note: mime_parser() was called with dont_decode set to 1, so we have the
+ * luxury of simply spewing without having to re-encode.
+ */
+void imap_load_part(char *name, char *filename, char *partnum, char *disp,
+                   void *content, char *cbtype, size_t length, char *encoding,
+                   void *cbuserdata)
+{
+       struct imap_fetch_part *imfp;
+       char mbuf2[1024];
+
+       imfp = (struct imap_fetch_part *)cbuserdata;
+
+       if (!strcasecmp(partnum, imfp->desired_section)) {
+               fwrite(content, length, 1, imfp->output_fp);
+       }
+
+       sprintf(mbuf2, "%s.MIME", partnum);
+
+       if (!strcasecmp(imfp->desired_section, mbuf2)) {
+               fprintf(imfp->output_fp, "Content-type: %s", cbtype);
+               if (strlen(name) > 0)
+                       fprintf(imfp->output_fp, "; name=\"%s\"", name);
+               fprintf(imfp->output_fp, "\r\n");
+               if (strlen(encoding) > 0)
+                       fprintf(imfp->output_fp,
+                               "Content-Transfer-Encoding: %s\r\n", encoding);
+               if (strlen(encoding) > 0) {
+                       fprintf(imfp->output_fp, "Content-Disposition: %s",
+                                       disp);
+                       if (strlen(filename) > 0) {
+                               fprintf(imfp->output_fp, "; filename=\"%s\"",
+                                       filename);
+                       }
+                       fprintf(imfp->output_fp, "\r\n");
+               }
+               fprintf(imfp->output_fp, "Content-Length: %d\r\n", length);
+               fprintf(imfp->output_fp, "\r\n");
+       }
+                       
+
+}
+
+
+/* 
+ * Called by imap_fetch_envelope() to output the "From" field.
+ * This is in its own function because its logic is kind of complex.  We
+ * really need to make this suck less.
+ */
+void imap_output_envelope_from(struct CtdlMessage *msg) {
+       char user[1024], node[1024], name[1024];
+
+       cprintf("((");                          /* open double-parens */
+       imap_strout(msg->cm_fields['A']);       /* personal name */
+       cprintf(" NIL ");                       /* source route (not used) */
+
+       if (msg->cm_fields['F'] != NULL) {
+               process_rfc822_addr(msg->cm_fields['F'], user, node, name);
+               imap_strout(user);              /* mailbox name (user id) */
+               cprintf(" ");
+               if (!strcasecmp(node, config.c_nodename)) {
+                       imap_strout(config.c_fqdn);
+               }
+               else {
+                       imap_strout(node);              /* host name */
+               }
+       }
+       else {
+               imap_strout(msg->cm_fields['A']); /* mailbox name (user id) */
+               cprintf(" ");
+               imap_strout(msg->cm_fields['N']);       /* host name */
+       }
+       
+       cprintf(")) ");                         /* close double-parens */
+}
+
+
+/*
+ * Implements the ENVELOPE fetch item
+ * 
+ * FIXME ... we only output some of the fields right now.  Definitely need
+ *           to do all of them.  Accurately, too.
+ *
+ * Note that the imap_strout() function can cleverly output NULL fields as NIL,
+ * so we don't have to check for that condition like we do elsewhere.
+ */
+void imap_fetch_envelope(long msgnum, struct CtdlMessage *msg) {
+       char datestringbuf[256];
+       time_t msgdate;
+       char *fieldptr = NULL;
+
+       /* Parse the message date into an IMAP-format date string */
+       if (msg->cm_fields['T'] != NULL) {
+               msgdate = atol(msg->cm_fields['T']);
+       }
+       else {
+               msgdate = time(NULL);
+       }
+       datestring(datestringbuf, msgdate, DATESTRING_IMAP);
+
+       /* Now start spewing data fields.  The order is important, as it is
+        * defined by the protocol specification.  Nonexistent fields must
+        * be output as NIL, existent fields must be quoted or literalled.
+        * The imap_strout() function conveniently does all this for us.
+        */
+       cprintf("ENVELOPE (");
+
+       /* Date */
+       imap_strout(datestringbuf);
+       cprintf(" ");
+
+       /* Subject */
+       imap_strout(msg->cm_fields['U']);
+       cprintf(" ");
+
+       /* From */
+       imap_output_envelope_from(msg);
+
+       /* Sender */
+       if (0) {
+               /* FIXME ... check for a *real* Sender: field */
+       }
+       else {
+               imap_output_envelope_from(msg);
+       }
+
+       /* Reply-to */
+       if (0) {
+               /* FIXME ... check for a *real* Reply-to: field */
+       }
+       else {
+               imap_output_envelope_from(msg);
+       }
+
+       cprintf("NIL ");        /* to */
+
+       cprintf("NIL ");        /* cc */
+
+       cprintf("NIL ");        /* bcc */
+
+       /* In-reply-to */
+       fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "In-reply-to");
+       imap_strout(fieldptr);
+       cprintf(" ");
+       if (fieldptr != NULL) phree(fieldptr);
+
+       /* message ID */
+       imap_strout(msg->cm_fields['I']);
+
+       cprintf(") ");
+}
+
+
+/*
+ * Strip any non header information out of a chunk of RFC822 data on disk
+ */
+void imap_strip_headers(FILE *fp) {
+       char buf[1024];
+
+       rewind(fp);
+       while (fgets(buf, sizeof buf, fp) != NULL) {
+               striplt(buf);
+               if (strlen(buf) == 0) {
+                       fflush(fp);
+                       ftruncate(fileno(fp), ftell(fp));
+               }
+       }
+       fflush(fp);
+       fprintf(fp, "\r\n");    /* add the trailing newline */
+       rewind(fp);
+}
+
+
 /*
  * Implements the BODY and BODY.PEEK fetch items
  */
-void imap_fetch_body(long msgnum, char *item, int is_peek) {
+void imap_fetch_body(long msgnum, char *item, int is_peek,
+               struct CtdlMessage *msg) {
        char section[1024];
        char partial[1024];
        int is_partial = 0;
@@ -167,6 +356,7 @@ void imap_fetch_body(long msgnum, char *item, int is_peek) {
        long bytes_remaining = 0;
        long blocksize;
        long pstart, pbytes;
+       struct imap_fetch_part imfp;
 
        /* extract section */
        strcpy(section, item);
@@ -189,6 +379,7 @@ void imap_fetch_body(long msgnum, char *item, int is_peek) {
        for (i=0; i<strlen(partial); ++i) {
                if (partial[i]=='>') partial[i] = 0;
        }
+       if (is_partial == 0) strcpy(partial, "");
        lprintf(9, "Partial is %s\n", partial);
 
        tmp = tmpfile();
@@ -201,7 +392,7 @@ void imap_fetch_body(long msgnum, char *item, int is_peek) {
 
        if (!strcmp(section, "")) {             /* the whole thing */
                CtdlRedirectOutput(tmp, -1);
-               CtdlOutputMsg(msgnum, MT_RFC822, 0, 0, 1);
+               CtdlOutputPreLoadedMsg(msg, msgnum, MT_RFC822, 0, 0, 1);
                CtdlRedirectOutput(NULL, -1);
        }
 
@@ -211,8 +402,24 @@ void imap_fetch_body(long msgnum, char *item, int is_peek) {
         */
        else if (!strncasecmp(section, "HEADER", 6)) {
                CtdlRedirectOutput(tmp, -1);
-               CtdlOutputMsg(msgnum, MT_RFC822, 1, 0, 1);
+               CtdlOutputPreLoadedMsg(msg, msgnum, MT_RFC822, 1, 0, 1);
                CtdlRedirectOutput(NULL, -1);
+               imap_strip_headers(tmp);
+       }
+
+       /*
+        * Anything else must be a part specifier.
+        * (Note value of 1 passed as 'dont_decode' so client gets it encoded)
+        */
+       else {
+               safestrncpy(imfp.desired_section, section,
+                               sizeof(imfp.desired_section));
+               imfp.output_fp = tmp;
+
+               mime_parser(msg->cm_fields['M'], NULL,
+                               *imap_load_part,
+                               (void *)&imfp,
+                               1);
        }
 
 
@@ -250,6 +457,57 @@ void imap_fetch_body(long msgnum, char *item, int is_peek) {
 }
 
 
+/*
+ * Spew the BODYSTRUCTURE data for a message.  (Do you need a silencer if
+ * you're going to shoot a MIME?  Do you need a reason to shoot Mark Crispin?
+ * No, and no.)
+ *
+ * FIXME finish the implementation
+ */
+void imap_fetch_bodystructure (long msgnum, char *item,
+               struct CtdlMessage *msg) {
+       FILE *tmp;
+       char buf[1024];
+       long lines = 0L;
+       long bytes = 0L;
+
+
+       /* For non-RFC822 (ordinary Citadel) messages, this is short and
+        * sweet...
+        */
+       if (msg->cm_format_type != FMT_RFC822) {
+
+               /* *sigh* We have to RFC822-format the message just to be able
+                * to measure it.
+                */
+               tmp = tmpfile();
+               if (tmp == NULL) return;
+               CtdlRedirectOutput(tmp, -1);
+               CtdlOutputPreLoadedMsg(msg, msgnum, MT_RFC822, 0, 0, 1);
+               CtdlRedirectOutput(NULL, -1);
+
+               rewind(tmp);
+               while (fgets(buf, sizeof buf, tmp) != NULL) ++lines;
+               bytes = ftell(tmp);
+               fclose(tmp);
+
+               cprintf("BODYSTRUCTURE (\"TEXT\" \"PLAIN\" "
+                       "(\"CHARSET\" \"US-ASCII\") NIL NIL "
+                       "\"7BIT\" %ld %ld) ", bytes, lines);
+
+               return;
+       }
+
+       /* For messages already stored in RFC822 format, we have to parse. */
+       /* FIXME do this! */
+
+
+}
+
+
+
+
+
 
 /*
  * imap_do_fetch() calls imap_do_fetch_msg() to output the deta of an
@@ -264,16 +522,19 @@ void imap_do_fetch_msg(int seq, struct CtdlMessage *msg,
        for (i=0; i<num_items; ++i) {
 
                if (!strncasecmp(itemlist[i], "BODY[", 5)) {
-                       imap_fetch_body(IMAP->msgids[seq-1], itemlist[i], 0);
+                       imap_fetch_body(IMAP->msgids[seq-1], itemlist[i],
+                                       0, msg);
                }
                else if (!strncasecmp(itemlist[i], "BODY.PEEK[", 10)) {
-                       imap_fetch_body(IMAP->msgids[seq-1], itemlist[i], 1);
+                       imap_fetch_body(IMAP->msgids[seq-1], itemlist[i],
+                                       1, msg);
                }
                else if (!strcasecmp(itemlist[i], "BODYSTRUCTURE")) {
-                       /* FIXME do something here */
+                       imap_fetch_bodystructure(IMAP->msgids[seq-1],
+                                       itemlist[i], msg);
                }
                else if (!strcasecmp(itemlist[i], "ENVELOPE")) {
-                       /* FIXME do something here */
+                       imap_fetch_envelope(IMAP->msgids[seq-1], msg);
                }
                else if (!strcasecmp(itemlist[i], "FLAGS")) {
                        imap_fetch_flags(msg);
@@ -282,16 +543,16 @@ void imap_do_fetch_msg(int seq, struct CtdlMessage *msg,
                        imap_fetch_internaldate(msg);
                }
                else if (!strcasecmp(itemlist[i], "RFC822")) {
-                       imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i]);
+                       imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i], msg);
                }
                else if (!strcasecmp(itemlist[i], "RFC822.HEADER")) {
-                       imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i]);
+                       imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i], msg);
                }
                else if (!strcasecmp(itemlist[i], "RFC822.SIZE")) {
-                       imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i]);
+                       imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i], msg);
                }
                else if (!strcasecmp(itemlist[i], "RFC822.TEXT")) {
-                       imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i]);
+                       imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i], msg);
                }
                else if (!strcasecmp(itemlist[i], "UID")) {
                        imap_fetch_uid(seq);