#include "serv_imap.h"
#include "imap_tools.h"
#include "imap_fetch.h"
+#include "genstamp.h"
/*
- * Back end function.
+ * Individual field functions for imap_do_fetch_msg() ...
*/
-void imap_do_fetch(int lo, int hi, int num_items, char **itemlist) {
+
+
+
+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("* imap_do_fetch() lo=%d hi=%d num_items=%d\r\n",
- lo, hi, num_items);
+ 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) {
- cprintf("* item[%d] = <%s>\r\n", i, itemlist[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"
+ );
+ }
+ }
+}
/*
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.
-
-
/*
* This function is called by the main command loop.
*/