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"
45 * imap_do_fetch() calls imap_do_fetch_msg() to output the deta of an
46 * individual message, once it has been successfully loaded from disk.
48 void imap_do_fetch_msg(int seq, struct CtdlMessage *msg,
49 int num_items, char **itemlist) {
51 cprintf("* %d FETCH ", seq);
52 /* FIXME obviously we must do something here */
59 * imap_fetch() calls imap_do_fetch() to do its actual work, once it's
60 * validated and boiled down the request a bit.
62 void imap_do_fetch(int lo, int hi, int num_items, char **itemlist) {
64 struct CtdlMessage *msg;
66 for (i=0; i<num_items; ++i) {
67 lprintf(9, "* item[%d] = <%s>\r\n", i, itemlist[i]);
70 for (i = lo; i <= hi; ++i) {
71 msg = CtdlFetchMessage(IMAP->msgids[i-1]);
73 imap_do_fetch_msg(i, msg, num_items, itemlist);
77 cprintf("* %d FETCH <internal error>\r\n", i);
85 * Back end for imap_handle_macros()
86 * Note that this function *only* looks at the beginning of the string. It
87 * is not a generic search-and-replace function.
89 void imap_macro_replace(char *str, char *find, char *replace) {
92 if (!strncasecmp(str, find, strlen(find))) {
93 if (str[strlen(find)]==' ') {
94 lprintf(9, "WAS: %s\n", str);
95 strcpy(holdbuf, &str[strlen(find)+1]);
99 lprintf(9, "NOW: %s\n", str);
107 * Handle macros embedded in FETCH data items.
108 * (What the heck are macros doing in a wire protocol? Are we trying to save
109 * the computer at the other end the trouble of typing a lot of characters?)
111 void imap_handle_macros(char *str) {
112 imap_macro_replace(str, "meta", "foo bar baz");
117 * Break out the data items requested, possibly a parenthesized list.
118 * Returns the number of data items, or -1 if the list is invalid.
119 * NOTE: this function alters the string it is fed, and uses it as a buffer
120 * to hold the data for the pointers it returns.
122 int imap_extract_data_items(char **argv, char *items) {
128 /* Convert all whitespace to ordinary space characters. */
129 for (i=0; i<strlen(items); ++i) {
130 if (isspace(items[i])) items[i]=' ';
133 /* Strip leading and trailing whitespace, then strip leading and
134 * trailing parentheses if it's a list
137 if ( (items[0]=='(') && (items[strlen(items)-1]==')') ) {
138 items[strlen(items)-1] = 0;
139 strcpy(items, &items[1]);
144 * Now break out the data items. We throw in one trailing space in
145 * order to avoid having to break out the last one manually.
149 initial_len = strlen(items);
150 imap_handle_macros(start);
151 for (i=0; i<initial_len; ++i) {
152 if (items[i]=='(') ++nest;
153 if (items[i]=='[') ++nest;
154 if (items[i]=='<') ++nest;
155 if (items[i]=='{') ++nest;
156 if (items[i]==')') --nest;
157 if (items[i]==']') --nest;
158 if (items[i]=='>') --nest;
159 if (items[i]=='}') --nest;
161 if (nest <= 0) if (items[i]==' ') {
163 argv[num_items++] = start;
165 imap_handle_macros(start);
176 * This function is called by the main command loop.
178 void imap_fetch(int num_parms, char *parms[]) {
181 char lostr[1024], histr[1024], items[1024];
187 cprintf("%s BAD invalid parameters\r\n", parms[0]);
191 extract_token(lostr, parms[2], 0, ':');
193 extract_token(histr, parms[2], 1, ':');
196 if ( (lo < 1) || (hi < 1) || (lo > hi) || (hi > IMAP->num_msgs) ) {
197 cprintf("%s BAD invalid sequence numbers %d:%d\r\n",
203 for (i=3; i<num_parms; ++i) {
204 strcat(items, parms[i]);
205 if (i < (num_parms-1)) strcat(items, " ");
208 num_items = imap_extract_data_items(itemlist, items);
210 cprintf("%s BAD invalid data item list\r\n", parms[0]);
214 imap_do_fetch(lo, hi, num_items, itemlist);
215 cprintf("%s OK FETCH completed\r\n", parms[0]);