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 "mime_parser.h"
40 #include "serv_imap.h"
41 #include "imap_tools.h"
42 #include "imap_fetch.h"
48 * Individual field functions for imap_do_fetch_msg() ...
53 void imap_fetch_uid(int seq) {
54 cprintf("UID %ld", IMAP->msgids[seq-1]);
57 void imap_fetch_flags(struct CtdlMessage *msg) {
58 cprintf("FLAGS ()"); /* FIXME do something here */
61 void imap_fetch_internaldate(struct CtdlMessage *msg) {
65 if (msg->cm_fields['T'] != NULL) {
66 msgdate = atol(msg->cm_fields['T']);
72 datestring(buf, msgdate, DATESTRING_IMAP);
73 cprintf("INTERNALDATE \"%s\"", buf);
78 * Fetch RFC822-formatted messages.
80 * 'whichfmt' should be set to one of:
81 * "RFC822" entire message
82 * "RFC822.HEADER" headers only (with trailing blank line)
83 * "RFC822.SIZE" size of translated message
84 * "RFC822.TEXT" body only (without leading blank line)
86 void imap_fetch_rfc822(int msgnum, char *whichfmt) {
90 long headers_size, text_size, total_size;
91 long bytes_remaining = 0;
96 lprintf(1, "Cannot open temp file: %s\n", strerror(errno));
101 * Load the message into a temp file for translation and measurement
103 CtdlRedirectOutput(tmp, -1);
104 CtdlOutputMsg(msgnum, MT_RFC822, 0, 0, 1);
105 CtdlRedirectOutput(NULL, -1);
108 * Now figure out where the headers/text break is. IMAP considers the
109 * intervening blank line to be part of the headers, not the text.
114 ptr = fgets(buf, sizeof buf, tmp);
117 if (strlen(buf) == 0) headers_size = ftell(tmp);
119 } while ( (headers_size == 0L) && (ptr != NULL) );
120 fseek(tmp, 0L, SEEK_END);
121 total_size = ftell(tmp);
122 text_size = total_size - headers_size;
124 if (!strcasecmp(whichfmt, "RFC822.SIZE")) {
125 cprintf("RFC822.SIZE %ld", total_size);
130 else if (!strcasecmp(whichfmt, "RFC822")) {
131 bytes_remaining = total_size;
135 else if (!strcasecmp(whichfmt, "RFC822.HEADER")) {
136 bytes_remaining = headers_size;
140 else if (!strcasecmp(whichfmt, "RFC822.TEXT")) {
141 bytes_remaining = text_size;
142 fseek(tmp, headers_size, SEEK_SET);
145 cprintf("%s {%ld}\r\n", whichfmt, bytes_remaining);
146 blocksize = sizeof(buf);
147 while (bytes_remaining > 0L) {
148 if (blocksize > bytes_remaining) blocksize = bytes_remaining;
149 fread(buf, blocksize, 1, tmp);
150 client_write(buf, blocksize);
151 bytes_remaining = bytes_remaining - blocksize;
160 * FIXME this is TOTALLY BROKEN!!!
162 void imap_fetch_part(char *name, char *filename, char *partnum, char *disp,
163 void *content, char *cbtype, size_t length)
166 if (!strcasecmp(partnum, IMAP->desired_part)) {
167 cprintf("part=%s|%s|%s|%s|%s|%d\r\n",
168 name, filename, partnum, disp, cbtype, length);
177 * Implements the BODY and BODY.PEEK fetch items
179 void imap_fetch_body(long msgnum, char *item, int is_peek,
180 struct CtdlMessage *msg) {
187 long bytes_remaining = 0;
191 /* extract section */
192 strcpy(section, item);
193 for (i=0; i<strlen(section); ++i) {
194 if (section[i]=='[') strcpy(section, §ion[i+1]);
196 for (i=0; i<strlen(section); ++i) {
197 if (section[i]==']') section[i] = 0;
199 lprintf(9, "Section is %s\n", section);
201 /* extract partial */
202 strcpy(partial, item);
203 for (i=0; i<strlen(partial); ++i) {
204 if (partial[i]=='<') {
205 strcpy(partial, &partial[i+1]);
209 for (i=0; i<strlen(partial); ++i) {
210 if (partial[i]=='>') partial[i] = 0;
212 lprintf(9, "Partial is %s\n", partial);
216 lprintf(1, "Cannot open temp file: %s\n", strerror(errno));
220 /* Now figure out what the client wants, and get it */
222 if (!strcmp(section, "")) { /* the whole thing */
223 CtdlRedirectOutput(tmp, -1);
224 CtdlOutputMsg(msgnum, MT_RFC822, 0, 0, 1);
225 CtdlRedirectOutput(NULL, -1);
229 * Be obnoxious and send the entire header, even if the client only
230 * asks for certain fields. FIXME this shortcut later.
232 else if (!strncasecmp(section, "HEADER", 6)) {
233 CtdlRedirectOutput(tmp, -1);
234 CtdlOutputMsg(msgnum, MT_RFC822, 1, 0, 1);
235 CtdlRedirectOutput(NULL, -1);
236 fprintf(tmp, "\r\n"); /* add the trailing newline */
240 * Anything else must be a part specifier.
243 safestrncpy(IMAP->desired_part, section,
244 sizeof(IMAP->desired_part));
245 mime_parser(msg->cm_fields['M'], NULL, *imap_fetch_part);
249 fseek(tmp, 0L, SEEK_END);
250 bytes_remaining = ftell(tmp);
252 if (is_partial == 0) {
254 cprintf("BODY[%s] {%ld}\r\n", section, bytes_remaining);
257 sscanf(partial, "%ld.%ld", &pstart, &pbytes);
258 if ((bytes_remaining - pstart) < pbytes) {
259 pbytes = bytes_remaining - pstart;
261 fseek(tmp, pstart, SEEK_SET);
262 bytes_remaining = pbytes;
263 cprintf("BODY[%s] {%ld}<%ld>\r\n",
264 section, bytes_remaining, pstart);
267 blocksize = sizeof(buf);
268 while (bytes_remaining > 0L) {
269 if (blocksize > bytes_remaining) blocksize = bytes_remaining;
270 fread(buf, blocksize, 1, tmp);
271 client_write(buf, blocksize);
272 bytes_remaining = bytes_remaining - blocksize;
278 /* FIXME set the last read pointer or something */
285 * imap_do_fetch() calls imap_do_fetch_msg() to output the deta of an
286 * individual message, once it has been successfully loaded from disk.
288 void imap_do_fetch_msg(int seq, struct CtdlMessage *msg,
289 int num_items, char **itemlist) {
292 cprintf("* %d FETCH (", seq);
294 for (i=0; i<num_items; ++i) {
296 if (!strncasecmp(itemlist[i], "BODY[", 5)) {
297 imap_fetch_body(IMAP->msgids[seq-1], itemlist[i], 0, msg);
299 else if (!strncasecmp(itemlist[i], "BODY.PEEK[", 10)) {
300 imap_fetch_body(IMAP->msgids[seq-1], itemlist[i], 1, msg);
302 else if (!strcasecmp(itemlist[i], "BODYSTRUCTURE")) {
303 /* FIXME do something here */
305 else if (!strcasecmp(itemlist[i], "ENVELOPE")) {
306 /* FIXME do something here */
308 else if (!strcasecmp(itemlist[i], "FLAGS")) {
309 imap_fetch_flags(msg);
311 else if (!strcasecmp(itemlist[i], "INTERNALDATE")) {
312 imap_fetch_internaldate(msg);
314 else if (!strcasecmp(itemlist[i], "RFC822")) {
315 imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i]);
317 else if (!strcasecmp(itemlist[i], "RFC822.HEADER")) {
318 imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i]);
320 else if (!strcasecmp(itemlist[i], "RFC822.SIZE")) {
321 imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i]);
323 else if (!strcasecmp(itemlist[i], "RFC822.TEXT")) {
324 imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i]);
326 else if (!strcasecmp(itemlist[i], "UID")) {
330 if (i != num_items-1) cprintf(" ");
339 * imap_fetch() calls imap_do_fetch() to do its actual work, once it's
340 * validated and boiled down the request a bit.
342 void imap_do_fetch(int num_items, char **itemlist) {
344 struct CtdlMessage *msg;
346 if (IMAP->num_msgs > 0)
347 for (i = 0; i < IMAP->num_msgs; ++i)
348 if (IMAP->flags[i] && IMAP_FETCHED) {
349 msg = CtdlFetchMessage(IMAP->msgids[i]);
351 imap_do_fetch_msg(i+1, msg, num_items, itemlist);
352 CtdlFreeMessage(msg);
355 cprintf("* %d FETCH <internal error>\r\n", i+1);
363 * Back end for imap_handle_macros()
364 * Note that this function *only* looks at the beginning of the string. It
365 * is not a generic search-and-replace function.
367 void imap_macro_replace(char *str, char *find, char *replace) {
370 if (!strncasecmp(str, find, strlen(find))) {
371 if (str[strlen(find)]==' ') {
372 strcpy(holdbuf, &str[strlen(find)+1]);
373 strcpy(str, replace);
375 strcat(str, holdbuf);
377 if (str[strlen(find)]==0) {
378 strcpy(holdbuf, &str[strlen(find)+1]);
379 strcpy(str, replace);
387 * Handle macros embedded in FETCH data items.
388 * (What the heck are macros doing in a wire protocol? Are we trying to save
389 * the computer at the other end the trouble of typing a lot of characters?)
391 void imap_handle_macros(char *str) {
395 for (i=0; i<strlen(str); ++i) {
396 if (str[i]=='(') ++nest;
397 if (str[i]=='[') ++nest;
398 if (str[i]=='<') ++nest;
399 if (str[i]=='{') ++nest;
400 if (str[i]==')') --nest;
401 if (str[i]==']') --nest;
402 if (str[i]=='>') --nest;
403 if (str[i]=='}') --nest;
406 imap_macro_replace(&str[i],
408 "FLAGS INTERNALDATE RFC822.SIZE ENVELOPE"
410 imap_macro_replace(&str[i],
414 imap_macro_replace(&str[i],
416 "FLAGS INTERNALDATE RFC822.SIZE"
418 imap_macro_replace(&str[i],
420 "FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODY"
428 * Break out the data items requested, possibly a parenthesized list.
429 * Returns the number of data items, or -1 if the list is invalid.
430 * NOTE: this function alters the string it is fed, and uses it as a buffer
431 * to hold the data for the pointers it returns.
433 int imap_extract_data_items(char **argv, char *items) {
439 /* Convert all whitespace to ordinary space characters. */
440 for (i=0; i<strlen(items); ++i) {
441 if (isspace(items[i])) items[i]=' ';
444 /* Strip leading and trailing whitespace, then strip leading and
445 * trailing parentheses if it's a list
448 if ( (items[0]=='(') && (items[strlen(items)-1]==')') ) {
449 items[strlen(items)-1] = 0;
450 strcpy(items, &items[1]);
454 /* Parse any macro data items */
455 imap_handle_macros(items);
458 * Now break out the data items. We throw in one trailing space in
459 * order to avoid having to break out the last one manually.
463 initial_len = strlen(items);
464 for (i=0; i<initial_len; ++i) {
465 if (items[i]=='(') ++nest;
466 if (items[i]=='[') ++nest;
467 if (items[i]=='<') ++nest;
468 if (items[i]=='{') ++nest;
469 if (items[i]==')') --nest;
470 if (items[i]==']') --nest;
471 if (items[i]=='>') --nest;
472 if (items[i]=='}') --nest;
474 if (nest <= 0) if (items[i]==' ') {
476 argv[num_items++] = start;
487 * One particularly hideous aspect of IMAP is that we have to allow the client
488 * to specify arbitrary ranges and/or sets of messages to fetch. Citadel IMAP
489 * handles this by setting the IMAP_FETCHED flag for each message specified in
490 * the ranges/sets, then looping through the message array, outputting messages
491 * with the flag set. We don't bother returning an error if an out-of-range
492 * number is specified (we just return quietly) because any client braindead
493 * enough to request a bogus message number isn't going to notice the
496 * This function clears out the IMAP_FETCHED bits, then sets that bit for each
497 * message included in the specified range.
499 * Set is_uid to 1 to fetch by UID instead of sequence number.
501 void imap_pick_range(char *range, int is_uid) {
505 char setstr[1024], lostr[1024], histr[1024];
509 * Clear out the IMAP_FETCHED flags for all messages.
511 for (i = 1; i <= IMAP->num_msgs; ++i) {
512 IMAP->flags[i-1] = IMAP->flags[i-1] & ~IMAP_FETCHED;
516 * Now set it for all specified messages.
518 num_sets = num_tokens(range, ',');
519 for (s=0; s<num_sets; ++s) {
520 extract_token(setstr, range, s, ',');
522 extract_token(lostr, setstr, 0, ':');
523 if (num_tokens(setstr, ':') >= 2) {
524 extract_token(histr, setstr, 1, ':');
525 if (!strcmp(histr, "*")) sprintf(histr, "%d", INT_MAX);
528 strcpy(histr, lostr);
533 /* Loop through the array, flipping bits where appropriate */
534 for (i = 1; i <= IMAP->num_msgs; ++i) {
535 if (is_uid) { /* fetch by sequence number */
536 if ( (IMAP->msgids[i-1]>=lo)
537 && (IMAP->msgids[i-1]<=hi)) {
539 IMAP->flags[i-1] | IMAP_FETCHED;
542 else { /* fetch by uid */
543 if ( (i>=lo) && (i<=hi)) {
545 IMAP->flags[i-1] | IMAP_FETCHED;
555 * This function is called by the main command loop.
557 void imap_fetch(int num_parms, char *parms[]) {
564 cprintf("%s BAD invalid parameters\r\n", parms[0]);
568 imap_pick_range(parms[2], 0);
571 for (i=3; i<num_parms; ++i) {
572 strcat(items, parms[i]);
573 if (i < (num_parms-1)) strcat(items, " ");
576 num_items = imap_extract_data_items(itemlist, items);
578 cprintf("%s BAD invalid data item list\r\n", parms[0]);
582 imap_do_fetch(num_items, itemlist);
583 cprintf("%s OK FETCH completed\r\n", parms[0]);
587 * This function is called by the main command loop.
589 void imap_uidfetch(int num_parms, char *parms[]) {
594 int have_uid_item = 0;
597 cprintf("%s BAD invalid parameters\r\n", parms[0]);
601 imap_pick_range(parms[3], 1);
604 for (i=4; i<num_parms; ++i) {
605 strcat(items, parms[i]);
606 if (i < (num_parms-1)) strcat(items, " ");
609 num_items = imap_extract_data_items(itemlist, items);
611 cprintf("%s BAD invalid data item list\r\n", parms[0]);
615 /* If the "UID" item was not included, we include it implicitly
616 * because this is a UID FETCH command
618 for (i=0; i<num_items; ++i) {
619 if (!strcasecmp(itemlist[i], "UID")) ++have_uid_item;
621 if (have_uid_item == 0) itemlist[num_items++] = "UID";
623 imap_do_fetch(num_items, itemlist);
624 cprintf("%s OK UID FETCH completed\r\n", parms[0]);