4 * Implements the FETCH command in IMAP.
5 * This command is way too convoluted. Marc Crispin is a fscking idiot.
18 #include <sys/types.h>
27 #include "sysdep_decls.h"
28 #include "citserver.h"
31 #include "dynloader.h"
38 #include "internet_addressing.h"
39 #include "serv_imap.h"
40 #include "imap_tools.h"
41 #include "imap_fetch.h"
48 void imap_do_fetch(int lo, int hi, int num_items, char **itemlist) {
51 cprintf("* imap_do_fetch() lo=%d hi=%d num_items=%d\r\n",
54 for (i=0; i<num_items; ++i) {
55 cprintf("* item[%d] = <%s>\r\n", i, itemlist[i]);
64 * Break out the data items requested, possibly a parenthesized list.
65 * Returns the number of data items, or -1 if the list is invalid.
66 * NOTE: this function alters the string it is fed, and uses it as a buffer
67 * to hold the data for the pointers it returns.
69 int imap_extract_data_items(char **argv, char *items) {
75 /* Convert all whitespace to ordinary space characters. */
76 for (i=0; i<strlen(items); ++i) {
77 if (isspace(items[i])) items[i]=' ';
80 /* Strip leading and trailing whitespace, then strip leading and
81 * trailing parentheses if it's a list
84 if ( (items[0]=='(') && (items[strlen(items)-1]==')') ) {
85 items[strlen(items)-1] = 0;
86 strcpy(items, &items[1]);
91 * Now break out the data items. We throw in one trailing space in
92 * order to avoid having to break out the last one manually.
96 initial_len = strlen(items);
97 for (i=0; i<initial_len; ++i) {
98 if (items[i]=='(') ++nest;
99 if (items[i]=='[') ++nest;
100 if (items[i]=='<') ++nest;
101 if (items[i]=='{') ++nest;
102 if (items[i]==')') --nest;
103 if (items[i]==']') --nest;
104 if (items[i]=='>') --nest;
105 if (items[i]=='}') --nest;
107 if (nest <= 0) if (items[i]==' ') {
109 argv[num_items++] = start;
123 * This function is called by the main command loop.
125 void imap_fetch(int num_parms, char *parms[]) {
128 char lostr[1024], histr[1024], items[1024];
134 cprintf("%s BAD invalid parameters\r\n", parms[0]);
138 extract_token(lostr, parms[2], 0, ':');
140 extract_token(histr, parms[2], 1, ':');
143 if ( (lo < 1) || (hi < 1) || (lo > hi) || (hi > IMAP->num_msgs) ) {
144 cprintf("%s BAD invalid sequence numbers %d:%d\r\n",
150 for (i=3; i<num_parms; ++i) {
151 strcat(items, parms[i]);
152 if (i < (num_parms-1)) strcat(items, " ");
155 num_items = imap_extract_data_items(itemlist, items);
157 cprintf("%s BAD invalid data item list\r\n", parms[0]);
161 imap_do_fetch(lo, hi, num_items, itemlist);
162 cprintf("%s OK FETCH completed\r\n", parms[0]);