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"
47 * Individual field functions for imap_do_fetch_msg() ...
52 void imap_fetch_uid(int seq) {
53 cprintf("UID %ld", IMAP->msgids[seq-1]);
56 void imap_fetch_flags(struct CtdlMessage *msg) {
57 cprintf("FLAGS ()"); /* FIXME do something here */
60 void imap_fetch_internaldate(struct CtdlMessage *msg) {
64 if (msg->cm_fields['T'] != NULL) {
65 msgdate = atol(msg->cm_fields['T']);
71 datestring(buf, msgdate, DATESTRING_IMAP);
72 cprintf("INTERNALDATE \"%s\"", buf);
77 * imap_do_fetch() calls imap_do_fetch_msg() to output the deta of an
78 * individual message, once it has been successfully loaded from disk.
80 void imap_do_fetch_msg(int seq, struct CtdlMessage *msg,
81 int num_items, char **itemlist) {
84 cprintf("* %d FETCH (", seq);
86 for (i=0; i<num_items; ++i) {
88 if (!strncasecmp(itemlist[i], "BODY[", 5)) {
89 /* FIXME do something here */
91 else if (!strncasecmp(itemlist[i], "BODY.PEEK[", 10)) {
92 /* FIXME do something here */
94 else if (!strcasecmp(itemlist[i], "BODYSTRUCTURE")) {
95 /* FIXME do something here */
97 else if (!strcasecmp(itemlist[i], "ENVELOPE")) {
98 /* FIXME do something here */
100 else if (!strcasecmp(itemlist[i], "FLAGS")) {
101 imap_fetch_flags(msg);
103 else if (!strcasecmp(itemlist[i], "INTERNALDATE")) {
104 imap_fetch_internaldate(msg);
106 else if (!strcasecmp(itemlist[i], "RFC822")) {
107 /* FIXME do something here */
109 else if (!strcasecmp(itemlist[i], "RFC822.HEADER")) {
110 /* FIXME do something here */
112 else if (!strcasecmp(itemlist[i], "RFC822.SIZE")) {
113 /* FIXME do something here */
115 else if (!strcasecmp(itemlist[i], "RFC822.TEXT")) {
116 /* FIXME do something here */
118 else if (!strcasecmp(itemlist[i], "UID")) {
122 if (i != num_items-1) cprintf(" ");
131 * imap_fetch() calls imap_do_fetch() to do its actual work, once it's
132 * validated and boiled down the request a bit.
134 void imap_do_fetch(int lo, int hi, int num_items, char **itemlist) {
136 struct CtdlMessage *msg;
138 for (i=0; i<num_items; ++i) {
139 lprintf(9, "* item[%d] = <%s>\r\n", i, itemlist[i]);
142 for (i = lo; i <= hi; ++i) {
143 msg = CtdlFetchMessage(IMAP->msgids[i-1]);
145 imap_do_fetch_msg(i, msg, num_items, itemlist);
146 CtdlFreeMessage(msg);
149 cprintf("* %d FETCH <internal error>\r\n", i);
157 * Back end for imap_handle_macros()
158 * Note that this function *only* looks at the beginning of the string. It
159 * is not a generic search-and-replace function.
161 void imap_macro_replace(char *str, char *find, char *replace) {
164 if (!strncasecmp(str, find, strlen(find))) {
165 if (str[strlen(find)]==' ') {
166 strcpy(holdbuf, &str[strlen(find)+1]);
167 strcpy(str, replace);
169 strcat(str, holdbuf);
171 if (str[strlen(find)]==0) {
172 strcpy(holdbuf, &str[strlen(find)+1]);
173 strcpy(str, replace);
181 * Handle macros embedded in FETCH data items.
182 * (What the heck are macros doing in a wire protocol? Are we trying to save
183 * the computer at the other end the trouble of typing a lot of characters?)
185 void imap_handle_macros(char *str) {
189 for (i=0; i<strlen(str); ++i) {
190 if (str[i]=='(') ++nest;
191 if (str[i]=='[') ++nest;
192 if (str[i]=='<') ++nest;
193 if (str[i]=='{') ++nest;
194 if (str[i]==')') --nest;
195 if (str[i]==']') --nest;
196 if (str[i]=='>') --nest;
197 if (str[i]=='}') --nest;
200 imap_macro_replace(&str[i],
202 "FLAGS INTERNALDATE RFC822.SIZE ENVELOPE"
204 imap_macro_replace(&str[i],
208 imap_macro_replace(&str[i],
210 "FLAGS INTERNALDATE RFC822.SIZE"
212 imap_macro_replace(&str[i],
214 "FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODY"
222 * Break out the data items requested, possibly a parenthesized list.
223 * Returns the number of data items, or -1 if the list is invalid.
224 * NOTE: this function alters the string it is fed, and uses it as a buffer
225 * to hold the data for the pointers it returns.
227 int imap_extract_data_items(char **argv, char *items) {
233 /* Convert all whitespace to ordinary space characters. */
234 for (i=0; i<strlen(items); ++i) {
235 if (isspace(items[i])) items[i]=' ';
238 /* Strip leading and trailing whitespace, then strip leading and
239 * trailing parentheses if it's a list
242 if ( (items[0]=='(') && (items[strlen(items)-1]==')') ) {
243 items[strlen(items)-1] = 0;
244 strcpy(items, &items[1]);
248 /* Parse any macro data items */
249 imap_handle_macros(items);
252 * Now break out the data items. We throw in one trailing space in
253 * order to avoid having to break out the last one manually.
257 initial_len = strlen(items);
258 for (i=0; i<initial_len; ++i) {
259 if (items[i]=='(') ++nest;
260 if (items[i]=='[') ++nest;
261 if (items[i]=='<') ++nest;
262 if (items[i]=='{') ++nest;
263 if (items[i]==')') --nest;
264 if (items[i]==']') --nest;
265 if (items[i]=='>') --nest;
266 if (items[i]=='}') --nest;
268 if (nest <= 0) if (items[i]==' ') {
270 argv[num_items++] = start;
282 * This function is called by the main command loop.
284 void imap_fetch(int num_parms, char *parms[]) {
287 char lostr[1024], histr[1024], items[1024];
293 cprintf("%s BAD invalid parameters\r\n", parms[0]);
297 extract_token(lostr, parms[2], 0, ':');
299 extract_token(histr, parms[2], 1, ':');
302 if ( (lo < 1) || (hi < 1) || (lo > hi) || (hi > IMAP->num_msgs) ) {
303 cprintf("%s BAD invalid sequence numbers %d:%d\r\n",
309 for (i=3; i<num_parms; ++i) {
310 strcat(items, parms[i]);
311 if (i < (num_parms-1)) strcat(items, " ");
314 num_items = imap_extract_data_items(itemlist, items);
316 cprintf("%s BAD invalid data item list\r\n", parms[0]);
320 imap_do_fetch(lo, hi, num_items, itemlist);
321 cprintf("%s OK FETCH completed\r\n", parms[0]);