]> code.citadel.org Git - citadel.git/blobdiff - citadel/imap_fetch.c
* I dunno
[citadel.git] / citadel / imap_fetch.c
index 9b653b85f6073e0f7ed23d20144b62a9075bb70c..6f07f64ffcec71bf455da58f2c2236268a2c569c 100644 (file)
@@ -45,7 +45,7 @@
 
 
 struct imap_fetch_part {
-       char desired_section[256];
+       char desired_section[SIZ];
        FILE *output_fp;
 };
 
@@ -64,7 +64,7 @@ void imap_fetch_flags(struct CtdlMessage *msg) {
 }
 
 void imap_fetch_internaldate(struct CtdlMessage *msg) {
-       char buf[256];
+       char buf[SIZ];
        time_t msgdate;
 
        if (msg->cm_fields['T'] != NULL) {
@@ -108,6 +108,9 @@ void imap_fetch_rfc822(int msgnum, char *whichfmt, struct CtdlMessage *msg) {
        CtdlRedirectOutput(tmp, -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
@@ -210,6 +213,39 @@ void imap_load_part(char *name, char *filename, char *partnum, char *disp,
 }
 
 
+/* 
+ * 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
  * 
@@ -220,34 +256,64 @@ void imap_load_part(char *name, char *filename, char *partnum, char *disp,
  * so we don't have to check for that condition like we do elsewhere.
  */
 void imap_fetch_envelope(long msgnum, struct CtdlMessage *msg) {
-       char buf[256];
+       char datestringbuf[SIZ];
        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);
 
-       datestring(buf, 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 */
-       cprintf("\"%s\" ", buf);
+       /* Date */
+       imap_strout(datestringbuf);
+       cprintf(" ");
 
-       /* subject */
+       /* Subject */
        imap_strout(msg->cm_fields['U']);
        cprintf(" ");
 
-       cprintf("NIL ");        /* from */
-       cprintf("NIL ");        /* sender */
-       cprintf("NIL ");        /* reply-to */
+       /* 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 */
-       cprintf("NIL ");        /* in-reply-to */
+
+       /* 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']);
@@ -314,7 +380,7 @@ void imap_fetch_body(long msgnum, char *item, int is_peek,
                if (partial[i]=='>') partial[i] = 0;
        }
        if (is_partial == 0) strcpy(partial, "");
-       lprintf(9, "Partial is %s\n", partial);
+       if (strlen(partial) > 0) lprintf(9, "Partial is %s\n", partial);
 
        tmp = tmpfile();
        if (tmp == NULL) {
@@ -351,7 +417,7 @@ void imap_fetch_body(long msgnum, char *item, int is_peek,
                imfp.output_fp = tmp;
 
                mime_parser(msg->cm_fields['M'], NULL,
-                               *imap_load_part,
+                               *imap_load_part, NULL, NULL,
                                (void *)&imfp,
                                1);
        }
@@ -390,6 +456,95 @@ void imap_fetch_body(long msgnum, char *item, int is_peek,
        }
 }
 
+/*
+ * Called immediately before outputting a multipart bodystructure
+ */
+void imap_fetch_bodystructure_pre(
+               char *name, char *filename, char *partnum, char *disp,
+               void *content, char *cbtype, size_t length, char *encoding,
+               void *cbuserdata
+               ) {
+
+       cprintf("(");
+}
+
+
+
+/*
+ * Called immediately after outputting a multipart bodystructure
+ */
+void imap_fetch_bodystructure_post(
+               char *name, char *filename, char *partnum, char *disp,
+               void *content, char *cbtype, size_t length, char *encoding,
+               void *cbuserdata
+               ) {
+
+       char subtype[SIZ];
+
+       extract_token(subtype, cbtype, 1, '/');
+       imap_strout(subtype);
+       cprintf(")");
+}
+
+
+
+/*
+ * Output the info for a MIME part in the format required by BODYSTRUCTURE.
+ *
+ * FIXME .... this needs oodles of work to get completed.
+ */
+void imap_fetch_bodystructure_part(
+               char *name, char *filename, char *partnum, char *disp,
+               void *content, char *cbtype, size_t length, char *encoding,
+               void *cbuserdata
+               ) {
+
+       char buf[SIZ];
+
+       cprintf("(");
+
+       if (cbtype == NULL) {
+               cprintf("\"TEXT\" \"PLAIN\" ");
+       }
+       else {
+               extract_token(buf, cbtype, 0, '/');
+               imap_strout(buf);
+               cprintf(" ");
+               extract_token(buf, cbtype, 1, '/');
+               imap_strout(buf);
+               cprintf(" ");
+       }
+
+       cprintf("(\"CHARSET\" \"US-ASCII\"");
+
+       if (name != NULL) {
+               cprintf(" \"NAME\" ");
+               imap_strout(name);
+       }
+
+       if (filename != NULL) {
+               cprintf(" \"FILENAME\" ");
+               imap_strout(name);
+       }
+
+       cprintf(") ");
+
+       cprintf("NIL NIL ");
+
+       if (encoding != NULL) {
+               imap_strout(encoding);
+       }
+       else {
+               imap_strout("7BIT");
+       }
+       cprintf(" ");
+
+       cprintf("%ld ", length);        /* bytes */
+       cprintf("NIL) ");               /* lines */
+
+}
+
+
 
 /*
  * Spew the BODYSTRUCTURE data for a message.  (Do you need a silencer if
@@ -427,15 +582,20 @@ void imap_fetch_bodystructure (long msgnum, char *item,
 
                cprintf("BODYSTRUCTURE (\"TEXT\" \"PLAIN\" "
                        "(\"CHARSET\" \"US-ASCII\") NIL NIL "
-                       "\"7BIT\" %ld %ld) ", bytes, lines);
+                       "\"7BIT\" %ld %ld)", bytes, lines);
 
                return;
        }
 
        /* For messages already stored in RFC822 format, we have to parse. */
-       /* FIXME do this! */
-
-
+       cprintf("BODYSTRUCTURE ");
+       mime_parser(msg->cm_fields['M'],
+                       NULL,
+                       *imap_fetch_bodystructure_part, /* part */
+                       *imap_fetch_bodystructure_pre,  /* pre-multi */
+                       *imap_fetch_bodystructure_post, /* post-multi */
+                       NULL,
+                       0);
 }
 
 
@@ -669,6 +829,10 @@ void imap_pick_range(char *range, int is_uid) {
        int s;
        char setstr[1024], lostr[1024], histr[1024];
        int lo, hi;
+       char *actual_range;
+
+       actual_range = range;
+       if (!strcasecmp(range, "ALL")) actual_range = "1:*";
 
        /*
         * Clear out the IMAP_FETCHED flags for all messages.
@@ -680,9 +844,9 @@ void imap_pick_range(char *range, int is_uid) {
        /*
         * Now set it for all specified messages.
         */
-       num_sets = num_tokens(range, ',');
+       num_sets = num_tokens(actual_range, ',');
        for (s=0; s<num_sets; ++s) {
-               extract_token(setstr, range, s, ',');
+               extract_token(setstr, actual_range, s, ',');
 
                extract_token(lostr, setstr, 0, ':');
                if (num_tokens(setstr, ':') >= 2) {
@@ -721,7 +885,7 @@ void imap_pick_range(char *range, int is_uid) {
  */
 void imap_fetch(int num_parms, char *parms[]) {
        char items[1024];
-       char *itemlist[256];
+       char *itemlist[SIZ];
        int num_items;
        int i;
 
@@ -753,7 +917,7 @@ void imap_fetch(int num_parms, char *parms[]) {
  */
 void imap_uidfetch(int num_parms, char *parms[]) {
        char items[1024];
-       char *itemlist[256];
+       char *itemlist[SIZ];
        int num_items;
        int i;
        int have_uid_item = 0;