]> code.citadel.org Git - citadel.git/blobdiff - citadel/imap_fetch.c
* More IMAP tweaks
[citadel.git] / citadel / imap_fetch.c
index 9b653b85f6073e0f7ed23d20144b62a9075bb70c..5bfc50520eaa31101c884c8d98a108456466a49c 100644 (file)
@@ -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[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);
 
-       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']);