]> code.citadel.org Git - citadel.git/blobdiff - citadel/imap_fetch.c
* Backed out the variable-length string changes.
[citadel.git] / citadel / imap_fetch.c
index e97704866eba13cc99a811255b931e19295ca73e..09a22bf1516849bb08888f89292fd74879bf6860 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) {
@@ -256,7 +256,7 @@ void imap_output_envelope_from(struct CtdlMessage *msg) {
  * 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];
+       char datestringbuf[SIZ];
        time_t msgdate;
        char *fieldptr = NULL;
 
@@ -380,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) {
@@ -417,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);
        }
@@ -456,13 +456,105 @@ 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.
+ *
+ */
+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];
+       int have_cbtype = 0;
+       int have_encoding = 0;
+
+       cprintf("(");
+
+       if (cbtype != NULL) if (strlen(cbtype)>0) have_cbtype = 1;
+
+       if (have_cbtype) {
+               extract_token(buf, cbtype, 0, '/');
+               imap_strout(buf);
+               cprintf(" ");
+               extract_token(buf, cbtype, 1, '/');
+               imap_strout(buf);
+               cprintf(" ");
+       }
+       else {
+               cprintf("\"TEXT\" \"PLAIN\" ");
+       }
+
+       cprintf("(\"CHARSET\" \"US-ASCII\"");
+
+       if (name != NULL) if (strlen(name)>0) {
+               cprintf(" \"NAME\" ");
+               imap_strout(name);
+       }
+
+       if (filename != NULL) if (strlen(filename)>0) {
+               cprintf(" \"FILENAME\" ");
+               imap_strout(name);
+       }
+
+       cprintf(") ");
+
+       cprintf("NIL NIL ");
+
+       if (encoding != NULL) if (strlen(encoding) > 0)  have_encoding = 1;
+
+       if (have_encoding) {
+               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
  * 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) {
@@ -471,7 +563,6 @@ void imap_fetch_bodystructure (long msgnum, char *item,
        long lines = 0L;
        long bytes = 0L;
 
-
        /* For non-RFC822 (ordinary Citadel) messages, this is short and
         * sweet...
         */
@@ -493,15 +584,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);
 }
 
 
@@ -576,7 +672,7 @@ void imap_do_fetch(int num_items, char **itemlist) {
 
        if (IMAP->num_msgs > 0)
         for (i = 0; i < IMAP->num_msgs; ++i)
-         if (IMAP->flags[i] && IMAP_FETCHED) {
+         if (IMAP->flags[i] & IMAP_FETCHED) {
                msg = CtdlFetchMessage(IMAP->msgids[i]);
                if (msg != NULL) {
                        imap_do_fetch_msg(i+1, msg, num_items, itemlist);
@@ -729,22 +825,29 @@ int imap_extract_data_items(char **argv, char *items) {
  *
  * Set is_uid to 1 to fetch by UID instead of sequence number.
  */
-void imap_pick_range(char *range, int is_uid) {
+void imap_pick_range(char *supplied_range, int is_uid) {
        int i;
        int num_sets;
        int s;
-       char setstr[1024], lostr[1024], histr[1024];
+       char setstr[SIZ], lostr[SIZ], histr[SIZ];       /* was 1024 */
        int lo, hi;
-       char *actual_range;
+       char actual_range[SIZ];
 
-       actual_range = range;
-       if (!strcasecmp(range, "ALL")) actual_range = "1:*";
+       /* 
+        * Handle the "ALL" macro
+        */
+       if (!strcasecmp(supplied_range, "ALL")) {
+               safestrncpy(actual_range, "1:*", sizeof actual_range);
+       }
+       else {
+               safestrncpy(actual_range, supplied_range, sizeof actual_range);
+       }
 
        /*
         * Clear out the IMAP_FETCHED flags for all messages.
         */
-       for (i = 1; i <= IMAP->num_msgs; ++i) {
-               IMAP->flags[i-1] = IMAP->flags[i-1] & ~IMAP_FETCHED;
+       for (i = 0; i < IMAP->num_msgs; ++i) {
+               IMAP->flags[i] = IMAP->flags[i] & ~IMAP_FETCHED;
        }
 
        /*
@@ -782,6 +885,17 @@ void imap_pick_range(char *range, int is_uid) {
                        }
                }
        }
+
+       /*
+        * Make sure we didn't select any expunged messages.
+        */
+       for (i = 0; i < IMAP->num_msgs; ++i) {
+               if (IMAP->flags[i] & IMAP_EXPUNGED) {
+                       lprintf(9, "eliminating %d because expunged\n", i);
+                       IMAP->flags[i] = IMAP->flags[i] & ~IMAP_FETCHED;
+               }
+       }
+
 }
 
 
@@ -790,8 +904,8 @@ void imap_pick_range(char *range, int is_uid) {
  * This function is called by the main command loop.
  */
 void imap_fetch(int num_parms, char *parms[]) {
-       char items[1024];
-       char *itemlist[256];
+       char items[SIZ];        /* was 1024 */
+       char *itemlist[SIZ];
        int num_items;
        int i;
 
@@ -822,8 +936,8 @@ void imap_fetch(int num_parms, char *parms[]) {
  * This function is called by the main command loop.
  */
 void imap_uidfetch(int num_parms, char *parms[]) {
-       char items[1024];
-       char *itemlist[256];
+       char items[SIZ];        /* was 1024 */
+       char *itemlist[SIZ];
        int num_items;
        int i;
        int have_uid_item = 0;