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 * Fetch RFC822-formatted messages.
79 * 'whichfmt' should be set to one of:
80 * "RFC822" entire message
81 * "RFC822.HEADER" headers only (with trailing blank line)
82 * "RFC822.SIZE" size of translated message
83 * "RFC822.TEXT" body only (without leading blank line)
85 void imap_fetch_rfc822(int msgnum, char *whichfmt) {
89 long headers_size, text_size, total_size;
90 long bytes_remaining = 0;
95 lprintf(1, "Cannot open temp file: %s\n", strerror(errno));
100 * Load the message into a temp file for translation and measurement
102 CtdlRedirectOutput(tmp, -1);
103 CtdlOutputMsg(msgnum, MT_RFC822, 0, 0, 1);
104 CtdlRedirectOutput(NULL, -1);
107 * Now figure out where the headers/text break is. IMAP considers the
108 * intervening blank line to be part of the headers, not the text.
113 ptr = fgets(buf, sizeof buf, tmp);
116 if (strlen(buf) == 0) headers_size = ftell(tmp);
118 } while ( (headers_size == 0L) && (ptr != NULL) );
119 fseek(tmp, 0L, SEEK_END);
120 total_size = ftell(tmp);
121 text_size = total_size - headers_size;
123 if (!strcasecmp(whichfmt, "RFC822.SIZE")) {
124 cprintf("RFC822.SIZE %ld", total_size);
129 else if (!strcasecmp(whichfmt, "RFC822")) {
130 bytes_remaining = total_size;
134 else if (!strcasecmp(whichfmt, "RFC822.HEADER")) {
135 bytes_remaining = headers_size;
139 else if (!strcasecmp(whichfmt, "RFC822.TEXT")) {
140 bytes_remaining = text_size;
141 fseek(tmp, headers_size, SEEK_SET);
144 cprintf("%s {%ld}\r\n", whichfmt, bytes_remaining);
145 blocksize = sizeof(buf);
146 while (bytes_remaining > 0L) {
147 if (blocksize > bytes_remaining) blocksize = bytes_remaining;
148 fread(buf, blocksize, 1, tmp);
149 client_write(buf, blocksize);
150 bytes_remaining = bytes_remaining - blocksize;
158 * Implements the BODY and BODY.PEEK fetch items
160 void imap_fetch_body(long msgnum, char *item, int is_peek) {
167 long bytes_remaining = 0;
171 /* extract section */
172 strcpy(section, item);
173 for (i=0; i<strlen(section); ++i) {
174 if (section[i]=='[') strcpy(section, §ion[i+1]);
176 for (i=0; i<strlen(section); ++i) {
177 if (section[i]==']') section[i] = 0;
179 lprintf(9, "Section is %s\n", section);
181 /* extract partial */
182 strcpy(partial, item);
183 for (i=0; i<strlen(partial); ++i) {
184 if (partial[i]=='<') {
185 strcpy(partial, &partial[i+1]);
189 for (i=0; i<strlen(partial); ++i) {
190 if (partial[i]=='>') partial[i] = 0;
192 lprintf(9, "Partial is %s\n", partial);
196 lprintf(1, "Cannot open temp file: %s\n", strerror(errno));
200 /* Now figure out what the client wants, and get it */
202 if (!strcmp(section, "")) { /* the whole thing */
203 CtdlRedirectOutput(tmp, -1);
204 CtdlOutputMsg(msgnum, MT_RFC822, 0, 0, 1);
205 CtdlRedirectOutput(NULL, -1);
209 * Be obnoxious and send the entire header, even if the client only
210 * asks for certain fields. FIXME this shortcut later.
212 else if (!strncasecmp(section, "HEADER", 6)) {
213 CtdlRedirectOutput(tmp, -1);
214 CtdlOutputMsg(msgnum, MT_RFC822, 1, 0, 1);
215 CtdlRedirectOutput(NULL, -1);
216 fprintf(tmp, "\r\n"); /* add the trailing newline */
220 fseek(tmp, 0L, SEEK_END);
221 bytes_remaining = ftell(tmp);
223 if (is_partial == 0) {
225 cprintf("BODY[%s] {%ld}\r\n", section, bytes_remaining);
228 sscanf(partial, "%ld.%ld", &pstart, &pbytes);
229 if ((bytes_remaining - pstart) < pbytes) {
230 pbytes = bytes_remaining - pstart;
232 fseek(tmp, pstart, SEEK_SET);
233 bytes_remaining = pbytes;
234 cprintf("BODY[%s] {%ld}<%ld>\r\n",
235 section, bytes_remaining, pstart);
238 blocksize = sizeof(buf);
239 while (bytes_remaining > 0L) {
240 if (blocksize > bytes_remaining) blocksize = bytes_remaining;
241 fread(buf, blocksize, 1, tmp);
242 client_write(buf, blocksize);
243 bytes_remaining = bytes_remaining - blocksize;
249 /* FIXME set the last read pointer or something */
256 * imap_do_fetch() calls imap_do_fetch_msg() to output the deta of an
257 * individual message, once it has been successfully loaded from disk.
259 void imap_do_fetch_msg(int seq, struct CtdlMessage *msg,
260 int num_items, char **itemlist) {
263 cprintf("* %d FETCH (", seq);
265 for (i=0; i<num_items; ++i) {
267 if (!strncasecmp(itemlist[i], "BODY[", 5)) {
268 imap_fetch_body(IMAP->msgids[seq-1], itemlist[i], 0);
270 else if (!strncasecmp(itemlist[i], "BODY.PEEK[", 10)) {
271 imap_fetch_body(IMAP->msgids[seq-1], itemlist[i], 1);
273 else if (!strcasecmp(itemlist[i], "BODYSTRUCTURE")) {
274 /* FIXME do something here */
276 else if (!strcasecmp(itemlist[i], "ENVELOPE")) {
277 /* FIXME do something here */
279 else if (!strcasecmp(itemlist[i], "FLAGS")) {
280 imap_fetch_flags(msg);
282 else if (!strcasecmp(itemlist[i], "INTERNALDATE")) {
283 imap_fetch_internaldate(msg);
285 else if (!strcasecmp(itemlist[i], "RFC822")) {
286 imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i]);
288 else if (!strcasecmp(itemlist[i], "RFC822.HEADER")) {
289 imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i]);
291 else if (!strcasecmp(itemlist[i], "RFC822.SIZE")) {
292 imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i]);
294 else if (!strcasecmp(itemlist[i], "RFC822.TEXT")) {
295 imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i]);
297 else if (!strcasecmp(itemlist[i], "UID")) {
301 if (i != num_items-1) cprintf(" ");
310 * imap_fetch() calls imap_do_fetch() to do its actual work, once it's
311 * validated and boiled down the request a bit.
313 void imap_do_fetch(int num_items, char **itemlist) {
315 struct CtdlMessage *msg;
317 if (IMAP->num_msgs > 0)
318 for (i = 0; i < IMAP->num_msgs; ++i)
319 if (IMAP->flags[i] && IMAP_FETCHED) {
320 msg = CtdlFetchMessage(IMAP->msgids[i]);
322 imap_do_fetch_msg(i+1, msg, num_items, itemlist);
323 CtdlFreeMessage(msg);
326 cprintf("* %d FETCH <internal error>\r\n", i+1);
334 * Back end for imap_handle_macros()
335 * Note that this function *only* looks at the beginning of the string. It
336 * is not a generic search-and-replace function.
338 void imap_macro_replace(char *str, char *find, char *replace) {
341 if (!strncasecmp(str, find, strlen(find))) {
342 if (str[strlen(find)]==' ') {
343 strcpy(holdbuf, &str[strlen(find)+1]);
344 strcpy(str, replace);
346 strcat(str, holdbuf);
348 if (str[strlen(find)]==0) {
349 strcpy(holdbuf, &str[strlen(find)+1]);
350 strcpy(str, replace);
358 * Handle macros embedded in FETCH data items.
359 * (What the heck are macros doing in a wire protocol? Are we trying to save
360 * the computer at the other end the trouble of typing a lot of characters?)
362 void imap_handle_macros(char *str) {
366 for (i=0; i<strlen(str); ++i) {
367 if (str[i]=='(') ++nest;
368 if (str[i]=='[') ++nest;
369 if (str[i]=='<') ++nest;
370 if (str[i]=='{') ++nest;
371 if (str[i]==')') --nest;
372 if (str[i]==']') --nest;
373 if (str[i]=='>') --nest;
374 if (str[i]=='}') --nest;
377 imap_macro_replace(&str[i],
379 "FLAGS INTERNALDATE RFC822.SIZE ENVELOPE"
381 imap_macro_replace(&str[i],
385 imap_macro_replace(&str[i],
387 "FLAGS INTERNALDATE RFC822.SIZE"
389 imap_macro_replace(&str[i],
391 "FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODY"
399 * Break out the data items requested, possibly a parenthesized list.
400 * Returns the number of data items, or -1 if the list is invalid.
401 * NOTE: this function alters the string it is fed, and uses it as a buffer
402 * to hold the data for the pointers it returns.
404 int imap_extract_data_items(char **argv, char *items) {
410 /* Convert all whitespace to ordinary space characters. */
411 for (i=0; i<strlen(items); ++i) {
412 if (isspace(items[i])) items[i]=' ';
415 /* Strip leading and trailing whitespace, then strip leading and
416 * trailing parentheses if it's a list
419 if ( (items[0]=='(') && (items[strlen(items)-1]==')') ) {
420 items[strlen(items)-1] = 0;
421 strcpy(items, &items[1]);
425 /* Parse any macro data items */
426 imap_handle_macros(items);
429 * Now break out the data items. We throw in one trailing space in
430 * order to avoid having to break out the last one manually.
434 initial_len = strlen(items);
435 for (i=0; i<initial_len; ++i) {
436 if (items[i]=='(') ++nest;
437 if (items[i]=='[') ++nest;
438 if (items[i]=='<') ++nest;
439 if (items[i]=='{') ++nest;
440 if (items[i]==')') --nest;
441 if (items[i]==']') --nest;
442 if (items[i]=='>') --nest;
443 if (items[i]=='}') --nest;
445 if (nest <= 0) if (items[i]==' ') {
447 argv[num_items++] = start;
458 * One particularly hideous aspect of IMAP is that we have to allow the client
459 * to specify arbitrary ranges and/or sets of messages to fetch. Citadel IMAP
460 * handles this by setting the IMAP_FETCHED flag for each message specified in
461 * the ranges/sets, then looping through the message array, outputting messages
462 * with the flag set. We don't bother returning an error if an out-of-range
463 * number is specified (we just return quietly) because any client braindead
464 * enough to request a bogus message number isn't going to notice the
467 * This function clears out the IMAP_FETCHED bits, then sets that bit for each
468 * message included in the specified range.
470 * Set is_uid to 1 to fetch by UID instead of sequence number.
472 void imap_pick_range(char *range, int is_uid) {
476 char setstr[1024], lostr[1024], histr[1024];
480 * Clear out the IMAP_FETCHED flags for all messages.
482 for (i = 1; i <= IMAP->num_msgs; ++i) {
483 IMAP->flags[i-1] = IMAP->flags[i-1] & ~IMAP_FETCHED;
487 * Now set it for all specified messages.
489 num_sets = num_tokens(range, ',');
490 for (s=0; s<num_sets; ++s) {
491 extract_token(setstr, range, s, ',');
493 extract_token(lostr, setstr, 0, ':');
494 if (num_tokens(setstr, ':') >= 2) {
495 extract_token(histr, setstr, 1, ':');
496 if (!strcmp(histr, "*")) sprintf(histr, "%d", INT_MAX);
499 strcpy(histr, lostr);
504 /* Loop through the array, flipping bits where appropriate */
505 for (i = 1; i <= IMAP->num_msgs; ++i) {
506 if (is_uid) { /* fetch by sequence number */
507 if ( (IMAP->msgids[i-1]>=lo)
508 && (IMAP->msgids[i-1]<=hi)) {
510 IMAP->flags[i-1] | IMAP_FETCHED;
513 else { /* fetch by uid */
514 if ( (i>=lo) && (i<=hi)) {
516 IMAP->flags[i-1] | IMAP_FETCHED;
526 * This function is called by the main command loop.
528 void imap_fetch(int num_parms, char *parms[]) {
535 cprintf("%s BAD invalid parameters\r\n", parms[0]);
539 imap_pick_range(parms[2], 0);
542 for (i=3; i<num_parms; ++i) {
543 strcat(items, parms[i]);
544 if (i < (num_parms-1)) strcat(items, " ");
547 num_items = imap_extract_data_items(itemlist, items);
549 cprintf("%s BAD invalid data item list\r\n", parms[0]);
553 imap_do_fetch(num_items, itemlist);
554 cprintf("%s OK FETCH completed\r\n", parms[0]);
558 * This function is called by the main command loop.
560 void imap_uidfetch(int num_parms, char *parms[]) {
565 int have_uid_item = 0;
568 cprintf("%s BAD invalid parameters\r\n", parms[0]);
572 imap_pick_range(parms[3], 1);
575 for (i=4; i<num_parms; ++i) {
576 strcat(items, parms[i]);
577 if (i < (num_parms-1)) strcat(items, " ");
580 num_items = imap_extract_data_items(itemlist, items);
582 cprintf("%s BAD invalid data item list\r\n", parms[0]);
586 /* If the "UID" item was not included, we include it implicitly
587 * because this is a UID FETCH command
589 for (i=0; i<num_items; ++i) {
590 if (!strcasecmp(itemlist[i], "UID")) ++have_uid_item;
592 if (have_uid_item == 0) itemlist[num_items++] = "UID";
594 imap_do_fetch(num_items, itemlist);
595 cprintf("%s OK UID FETCH completed\r\n", parms[0]);