]> code.citadel.org Git - citadel.git/blobdiff - citadel/imap_fetch.c
* IMAP date strings. More stupidity.
[citadel.git] / citadel / imap_fetch.c
index ccdaaf1ddbd6caab57c694d1aaff5ce32fd2da8f..a85eba79ffd28f3254379413d0a51bb46dcb4505 100644 (file)
 #include "serv_imap.h"
 #include "imap_tools.h"
 #include "imap_fetch.h"
+#include "genstamp.h"
 
 
 
-void imap_do_fetch(int lo, int hi, char *items) {
+/*
+ * Individual field functions for imap_do_fetch_msg() ...
+ */
+
+
+
+void imap_fetch_uid(int seq) {
+       cprintf("UID %ld", IMAP->msgids[seq-1]);
 }
 
+void imap_fetch_internaldate(struct CtdlMessage *msg) {
+       char buf[256];
+       time_t msgdate;
+
+       if (msg->cm_fields['T'] != NULL) {
+               msgdate = atol(msg->cm_fields['T']);
+       }
+       else {
+               msgdate = time(NULL);
+       }
+
+       datestring(buf, msgdate, DATESTRING_IMAP);
+       cprintf("INTERNALDATE \"%s\"", buf);
+}
+
+
+/*
+ * imap_do_fetch() calls imap_do_fetch_msg() to output the deta of an
+ * individual message, once it has been successfully loaded from disk.
+ */
+void imap_do_fetch_msg(int seq, struct CtdlMessage *msg,
+                       int num_items, char **itemlist) {
+       int i;
+
+       cprintf("* %d FETCH (", seq);
+
+       for (i=0; i<num_items; ++i) {
+
+               if (!strncasecmp(itemlist[i], "BODY[", 5)) {
+                       /* FIXME do something here */
+               }
+               else if (!strncasecmp(itemlist[i], "BODY.PEEK[", 10)) {
+                       /* FIXME do something here */
+               }
+               else if (!strcasecmp(itemlist[i], "BODYSTRUCTURE")) {
+                       /* FIXME do something here */
+               }
+               else if (!strcasecmp(itemlist[i], "ENVELOPE")) {
+                       /* FIXME do something here */
+               }
+               else if (!strcasecmp(itemlist[i], "FLAGS")) {
+                       /* FIXME do something here */
+               }
+               else if (!strcasecmp(itemlist[i], "INTERNALDATE")) {
+                       imap_fetch_internaldate(msg);
+               }
+               else if (!strcasecmp(itemlist[i], "RFC822")) {
+                       /* FIXME do something here */
+               }
+               else if (!strcasecmp(itemlist[i], "RFC822.HEADER")) {
+                       /* FIXME do something here */
+               }
+               else if (!strcasecmp(itemlist[i], "RFC822.SIZE")) {
+                       /* FIXME do something here */
+               }
+               else if (!strcasecmp(itemlist[i], "RFC822.TEXT")) {
+                       /* FIXME do something here */
+               }
+               else if (!strcasecmp(itemlist[i], "UID")) {
+                       imap_fetch_uid(seq);
+               }
+
+               if (i != num_items-1) cprintf(" ");
+       }
+
+       cprintf(")\r\n");
+}
+
+
+
+/*
+ * imap_fetch() calls imap_do_fetch() to do its actual work, once it's
+ * validated and boiled down the request a bit.
+ */
+void imap_do_fetch(int lo, int hi, int num_items, char **itemlist) {
+       int i;
+       struct CtdlMessage *msg;
+
+       for (i=0; i<num_items; ++i) {
+               lprintf(9, "* item[%d] = <%s>\r\n", i, itemlist[i]);
+       }
+
+       for (i = lo; i <= hi; ++i) {
+               msg = CtdlFetchMessage(IMAP->msgids[i-1]);
+               if (msg != NULL) {
+                       imap_do_fetch_msg(i, msg, num_items, itemlist);
+                       CtdlFreeMessage(msg);
+               }
+               else {
+                       cprintf("* %d FETCH <internal error>\r\n", i);
+               }
+       }
+}
+
+
+
+/*
+ * Back end for imap_handle_macros()
+ * Note that this function *only* looks at the beginning of the string.  It
+ * is not a generic search-and-replace function.
+ */
+void imap_macro_replace(char *str, char *find, char *replace) {
+       char holdbuf[1024];
+
+       if (!strncasecmp(str, find, strlen(find))) {
+               if (str[strlen(find)]==' ') {
+                       strcpy(holdbuf, &str[strlen(find)+1]);
+                       strcpy(str, replace);
+                       strcat(str, " ");
+                       strcat(str, holdbuf);
+               }
+               if (str[strlen(find)]==0) {
+                       strcpy(holdbuf, &str[strlen(find)+1]);
+                       strcpy(str, replace);
+               }
+       }
+}
+
+
+
+/*
+ * Handle macros embedded in FETCH data items.
+ * (What the heck are macros doing in a wire protocol?  Are we trying to save
+ * the computer at the other end the trouble of typing a lot of characters?)
+ */
+void imap_handle_macros(char *str) {
+       int i;
+       int nest = 0;
+
+       for (i=0; i<strlen(str); ++i) {
+               if (str[i]=='(') ++nest;
+               if (str[i]=='[') ++nest;
+               if (str[i]=='<') ++nest;
+               if (str[i]=='{') ++nest;
+               if (str[i]==')') --nest;
+               if (str[i]==']') --nest;
+               if (str[i]=='>') --nest;
+               if (str[i]=='}') --nest;
+
+               if (nest <= 0) {
+                       imap_macro_replace(&str[i],
+                               "ALL",
+                               "FLAGS INTERNALDATE RFC822.SIZE ENVELOPE"
+                       );
+                       imap_macro_replace(&str[i],
+                               "BODY",
+                               "BODYSTRUCTURE"
+                       );
+                       imap_macro_replace(&str[i],
+                               "FAST",
+                               "FLAGS INTERNALDATE RFC822.SIZE"
+                       );
+                       imap_macro_replace(&str[i],
+                               "FULL",
+                               "FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODY"
+                       );
+               }
+       }
+}
+
+
+/*
+ * Break out the data items requested, possibly a parenthesized list.
+ * Returns the number of data items, or -1 if the list is invalid.
+ * NOTE: this function alters the string it is fed, and uses it as a buffer
+ * to hold the data for the pointers it returns.
+ */
+int imap_extract_data_items(char **argv, char *items) {
+       int num_items = 0;
+       int nest = 0;
+       int i, initial_len;
+       char *start;
+
+       /* Convert all whitespace to ordinary space characters. */
+       for (i=0; i<strlen(items); ++i) {
+               if (isspace(items[i])) items[i]=' ';
+       }
+
+       /* Strip leading and trailing whitespace, then strip leading and
+        * trailing parentheses if it's a list
+        */
+       striplt(items);
+       if ( (items[0]=='(') && (items[strlen(items)-1]==')') ) {
+               items[strlen(items)-1] = 0;
+               strcpy(items, &items[1]);
+               striplt(items);
+       }
+
+       /* Parse any macro data items */
+       imap_handle_macros(items);
+
+       /*
+        * Now break out the data items.  We throw in one trailing space in
+        * order to avoid having to break out the last one manually.
+        */
+       strcat(items, " ");
+       start = items;
+       initial_len = strlen(items);
+       for (i=0; i<initial_len; ++i) {
+               if (items[i]=='(') ++nest;
+               if (items[i]=='[') ++nest;
+               if (items[i]=='<') ++nest;
+               if (items[i]=='{') ++nest;
+               if (items[i]==')') --nest;
+               if (items[i]==']') --nest;
+               if (items[i]=='>') --nest;
+               if (items[i]=='}') --nest;
+
+               if (nest <= 0) if (items[i]==' ') {
+                       items[i] = 0;
+                       argv[num_items++] = start;
+                       start = &items[i+1];
+               }
+       }
+
+       return(num_items);
+
+}
 
 
 
@@ -55,6 +281,8 @@ void imap_fetch(int num_parms, char *parms[]) {
        int lo = 0;
        int hi = 0;
        char lostr[1024], histr[1024], items[1024];
+       char *itemlist[256];
+       int num_items;
        int i;
 
        if (num_parms < 4) {
@@ -79,7 +307,13 @@ void imap_fetch(int num_parms, char *parms[]) {
                if (i < (num_parms-1)) strcat(items, " ");
        }
 
-       imap_do_fetch(lo, hi, items);
+       num_items = imap_extract_data_items(itemlist, items);
+       if (num_items < 1) {
+               cprintf("%s BAD invalid data item list\r\n", parms[0]);
+               return;
+       }
+
+       imap_do_fetch(lo, hi, num_items, itemlist);
        cprintf("%s OK FETCH completed\r\n", parms[0]);
 }