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"
47 struct imap_fetch_part {
48 char desired_section[256];
53 * Individual field functions for imap_do_fetch_msg() ...
58 void imap_fetch_uid(int seq) {
59 cprintf("UID %ld", IMAP->msgids[seq-1]);
62 void imap_fetch_flags(struct CtdlMessage *msg) {
63 cprintf("FLAGS ()"); /* FIXME do something here */
66 void imap_fetch_internaldate(struct CtdlMessage *msg) {
70 if (msg->cm_fields['T'] != NULL) {
71 msgdate = atol(msg->cm_fields['T']);
77 datestring(buf, msgdate, DATESTRING_IMAP);
78 cprintf("INTERNALDATE \"%s\"", buf);
83 * Fetch RFC822-formatted messages.
85 * 'whichfmt' should be set to one of:
86 * "RFC822" entire message
87 * "RFC822.HEADER" headers only (with trailing blank line)
88 * "RFC822.SIZE" size of translated message
89 * "RFC822.TEXT" body only (without leading blank line)
91 void imap_fetch_rfc822(int msgnum, char *whichfmt) {
95 long headers_size, text_size, total_size;
96 long bytes_remaining = 0;
101 lprintf(1, "Cannot open temp file: %s\n", strerror(errno));
106 * Load the message into a temp file for translation and measurement
108 CtdlRedirectOutput(tmp, -1);
109 CtdlOutputMsg(msgnum, MT_RFC822, 0, 0, 1);
110 CtdlRedirectOutput(NULL, -1);
113 * Now figure out where the headers/text break is. IMAP considers the
114 * intervening blank line to be part of the headers, not the text.
119 ptr = fgets(buf, sizeof buf, tmp);
122 if (strlen(buf) == 0) headers_size = ftell(tmp);
124 } while ( (headers_size == 0L) && (ptr != NULL) );
125 fseek(tmp, 0L, SEEK_END);
126 total_size = ftell(tmp);
127 text_size = total_size - headers_size;
129 if (!strcasecmp(whichfmt, "RFC822.SIZE")) {
130 cprintf("RFC822.SIZE %ld", total_size);
135 else if (!strcasecmp(whichfmt, "RFC822")) {
136 bytes_remaining = total_size;
140 else if (!strcasecmp(whichfmt, "RFC822.HEADER")) {
141 bytes_remaining = headers_size;
145 else if (!strcasecmp(whichfmt, "RFC822.TEXT")) {
146 bytes_remaining = text_size;
147 fseek(tmp, headers_size, SEEK_SET);
150 cprintf("%s {%ld}\r\n", whichfmt, bytes_remaining);
151 blocksize = sizeof(buf);
152 while (bytes_remaining > 0L) {
153 if (blocksize > bytes_remaining) blocksize = bytes_remaining;
154 fread(buf, blocksize, 1, tmp);
155 client_write(buf, blocksize);
156 bytes_remaining = bytes_remaining - blocksize;
165 * Load a specific part of a message into the temp file to be output to a
166 * client. FIXME we can handle parts like "2" and "2.1" and even "2.MIME"
167 * but we still can't handle "2.HEADER" (which might not be a problem, because
168 * we currently don't have the ability to break out nested RFC822's anyway).
170 * Note: mime_parser() was called with dont_decode set to 1, so we have the
171 * luxury of simply spewing without having to re-encode.
173 void imap_load_part(char *name, char *filename, char *partnum, char *disp,
174 void *content, char *cbtype, size_t length, char *encoding,
177 struct imap_fetch_part *imfp;
180 imfp = (struct imap_fetch_part *)cbuserdata;
182 if (!strcasecmp(partnum, imfp->desired_section)) {
183 fwrite(content, length, 1, imfp->output_fp);
186 sprintf(mbuf2, "%s.MIME", partnum);
188 if (!strcasecmp(imfp->desired_section, mbuf2)) {
189 fprintf(imfp->output_fp, "Content-type: %s", cbtype);
190 if (strlen(name) > 0)
191 fprintf(imfp->output_fp, "; name=\"%s\"", name);
192 fprintf(imfp->output_fp, "\r\n");
193 if (strlen(encoding) > 0)
194 fprintf(imfp->output_fp,
195 "Content-Transfer-Encoding: %s\r\n", encoding);
196 if (strlen(encoding) > 0) {
197 fprintf(imfp->output_fp, "Content-Disposition: %s",
199 if (strlen(filename) > 0) {
200 fprintf(imfp->output_fp, "; filename=\"%s\"",
203 fprintf(imfp->output_fp, "\r\n");
205 fprintf(imfp->output_fp, "Content-Length: %d\r\n", length);
206 fprintf(imfp->output_fp, "\r\n");
214 * Implements the ENVELOPE fetch item
216 * FIXME ... we have to actually do something useful here.
218 void imap_fetch_envelope(long msgnum, struct CtdlMessage *msg) {
219 cprintf("ENVELOPE (");
220 cprintf("NIL "); /* date */
221 cprintf("NIL "); /* subject */
222 cprintf("NIL "); /* from */
223 cprintf("NIL "); /* sender */
224 cprintf("NIL "); /* reply-to */
225 cprintf("NIL "); /* to */
226 cprintf("NIL "); /* cc */
227 cprintf("NIL "); /* bcc */
228 cprintf("NIL "); /* in-reply-to */
229 cprintf("NIL"); /* message-id */
236 * Implements the BODY and BODY.PEEK fetch items
238 void imap_fetch_body(long msgnum, char *item, int is_peek,
239 struct CtdlMessage *msg) {
246 long bytes_remaining = 0;
249 struct imap_fetch_part imfp;
251 /* extract section */
252 strcpy(section, item);
253 for (i=0; i<strlen(section); ++i) {
254 if (section[i]=='[') strcpy(section, §ion[i+1]);
256 for (i=0; i<strlen(section); ++i) {
257 if (section[i]==']') section[i] = 0;
259 lprintf(9, "Section is %s\n", section);
261 /* extract partial */
262 strcpy(partial, item);
263 for (i=0; i<strlen(partial); ++i) {
264 if (partial[i]=='<') {
265 strcpy(partial, &partial[i+1]);
269 for (i=0; i<strlen(partial); ++i) {
270 if (partial[i]=='>') partial[i] = 0;
272 lprintf(9, "Partial is %s\n", partial);
276 lprintf(1, "Cannot open temp file: %s\n", strerror(errno));
280 /* Now figure out what the client wants, and get it */
282 if (!strcmp(section, "")) { /* the whole thing */
283 CtdlRedirectOutput(tmp, -1);
284 CtdlOutputMsg(msgnum, MT_RFC822, 0, 0, 1);
285 CtdlRedirectOutput(NULL, -1);
289 * Be obnoxious and send the entire header, even if the client only
290 * asks for certain fields. FIXME this shortcut later.
292 else if (!strncasecmp(section, "HEADER", 6)) {
293 CtdlRedirectOutput(tmp, -1);
294 CtdlOutputMsg(msgnum, MT_RFC822, 1, 0, 1);
295 CtdlRedirectOutput(NULL, -1);
296 fprintf(tmp, "\r\n"); /* add the trailing newline */
300 * Anything else must be a part specifier.
301 * (Note value of 1 passed as 'dont_decode' so client gets it encoded)
304 safestrncpy(imfp.desired_section, section,
305 sizeof(imfp.desired_section));
306 imfp.output_fp = tmp;
308 mime_parser(msg->cm_fields['M'], NULL,
315 fseek(tmp, 0L, SEEK_END);
316 bytes_remaining = ftell(tmp);
318 if (is_partial == 0) {
320 cprintf("BODY[%s] {%ld}\r\n", section, bytes_remaining);
323 sscanf(partial, "%ld.%ld", &pstart, &pbytes);
324 if ((bytes_remaining - pstart) < pbytes) {
325 pbytes = bytes_remaining - pstart;
327 fseek(tmp, pstart, SEEK_SET);
328 bytes_remaining = pbytes;
329 cprintf("BODY[%s] {%ld}<%ld>\r\n",
330 section, bytes_remaining, pstart);
333 blocksize = sizeof(buf);
334 while (bytes_remaining > 0L) {
335 if (blocksize > bytes_remaining) blocksize = bytes_remaining;
336 fread(buf, blocksize, 1, tmp);
337 client_write(buf, blocksize);
338 bytes_remaining = bytes_remaining - blocksize;
344 /* FIXME set the last read pointer or something */
351 * imap_do_fetch() calls imap_do_fetch_msg() to output the deta of an
352 * individual message, once it has been successfully loaded from disk.
354 void imap_do_fetch_msg(int seq, struct CtdlMessage *msg,
355 int num_items, char **itemlist) {
358 cprintf("* %d FETCH (", seq);
360 for (i=0; i<num_items; ++i) {
362 if (!strncasecmp(itemlist[i], "BODY[", 5)) {
363 imap_fetch_body(IMAP->msgids[seq-1], itemlist[i],
366 else if (!strncasecmp(itemlist[i], "BODY.PEEK[", 10)) {
367 imap_fetch_body(IMAP->msgids[seq-1], itemlist[i],
370 else if (!strcasecmp(itemlist[i], "BODYSTRUCTURE")) {
371 /* FIXME do something here */
373 else if (!strcasecmp(itemlist[i], "ENVELOPE")) {
374 imap_fetch_envelope(IMAP->msgids[seq-1], msg);
376 else if (!strcasecmp(itemlist[i], "FLAGS")) {
377 imap_fetch_flags(msg);
379 else if (!strcasecmp(itemlist[i], "INTERNALDATE")) {
380 imap_fetch_internaldate(msg);
382 else if (!strcasecmp(itemlist[i], "RFC822")) {
383 imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i]);
385 else if (!strcasecmp(itemlist[i], "RFC822.HEADER")) {
386 imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i]);
388 else if (!strcasecmp(itemlist[i], "RFC822.SIZE")) {
389 imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i]);
391 else if (!strcasecmp(itemlist[i], "RFC822.TEXT")) {
392 imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i]);
394 else if (!strcasecmp(itemlist[i], "UID")) {
398 if (i != num_items-1) cprintf(" ");
407 * imap_fetch() calls imap_do_fetch() to do its actual work, once it's
408 * validated and boiled down the request a bit.
410 void imap_do_fetch(int num_items, char **itemlist) {
412 struct CtdlMessage *msg;
414 if (IMAP->num_msgs > 0)
415 for (i = 0; i < IMAP->num_msgs; ++i)
416 if (IMAP->flags[i] && IMAP_FETCHED) {
417 msg = CtdlFetchMessage(IMAP->msgids[i]);
419 imap_do_fetch_msg(i+1, msg, num_items, itemlist);
420 CtdlFreeMessage(msg);
423 cprintf("* %d FETCH <internal error>\r\n", i+1);
431 * Back end for imap_handle_macros()
432 * Note that this function *only* looks at the beginning of the string. It
433 * is not a generic search-and-replace function.
435 void imap_macro_replace(char *str, char *find, char *replace) {
438 if (!strncasecmp(str, find, strlen(find))) {
439 if (str[strlen(find)]==' ') {
440 strcpy(holdbuf, &str[strlen(find)+1]);
441 strcpy(str, replace);
443 strcat(str, holdbuf);
445 if (str[strlen(find)]==0) {
446 strcpy(holdbuf, &str[strlen(find)+1]);
447 strcpy(str, replace);
455 * Handle macros embedded in FETCH data items.
456 * (What the heck are macros doing in a wire protocol? Are we trying to save
457 * the computer at the other end the trouble of typing a lot of characters?)
459 void imap_handle_macros(char *str) {
463 for (i=0; i<strlen(str); ++i) {
464 if (str[i]=='(') ++nest;
465 if (str[i]=='[') ++nest;
466 if (str[i]=='<') ++nest;
467 if (str[i]=='{') ++nest;
468 if (str[i]==')') --nest;
469 if (str[i]==']') --nest;
470 if (str[i]=='>') --nest;
471 if (str[i]=='}') --nest;
474 imap_macro_replace(&str[i],
476 "FLAGS INTERNALDATE RFC822.SIZE ENVELOPE"
478 imap_macro_replace(&str[i],
482 imap_macro_replace(&str[i],
484 "FLAGS INTERNALDATE RFC822.SIZE"
486 imap_macro_replace(&str[i],
488 "FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODY"
496 * Break out the data items requested, possibly a parenthesized list.
497 * Returns the number of data items, or -1 if the list is invalid.
498 * NOTE: this function alters the string it is fed, and uses it as a buffer
499 * to hold the data for the pointers it returns.
501 int imap_extract_data_items(char **argv, char *items) {
507 /* Convert all whitespace to ordinary space characters. */
508 for (i=0; i<strlen(items); ++i) {
509 if (isspace(items[i])) items[i]=' ';
512 /* Strip leading and trailing whitespace, then strip leading and
513 * trailing parentheses if it's a list
516 if ( (items[0]=='(') && (items[strlen(items)-1]==')') ) {
517 items[strlen(items)-1] = 0;
518 strcpy(items, &items[1]);
522 /* Parse any macro data items */
523 imap_handle_macros(items);
526 * Now break out the data items. We throw in one trailing space in
527 * order to avoid having to break out the last one manually.
531 initial_len = strlen(items);
532 for (i=0; i<initial_len; ++i) {
533 if (items[i]=='(') ++nest;
534 if (items[i]=='[') ++nest;
535 if (items[i]=='<') ++nest;
536 if (items[i]=='{') ++nest;
537 if (items[i]==')') --nest;
538 if (items[i]==']') --nest;
539 if (items[i]=='>') --nest;
540 if (items[i]=='}') --nest;
542 if (nest <= 0) if (items[i]==' ') {
544 argv[num_items++] = start;
555 * One particularly hideous aspect of IMAP is that we have to allow the client
556 * to specify arbitrary ranges and/or sets of messages to fetch. Citadel IMAP
557 * handles this by setting the IMAP_FETCHED flag for each message specified in
558 * the ranges/sets, then looping through the message array, outputting messages
559 * with the flag set. We don't bother returning an error if an out-of-range
560 * number is specified (we just return quietly) because any client braindead
561 * enough to request a bogus message number isn't going to notice the
564 * This function clears out the IMAP_FETCHED bits, then sets that bit for each
565 * message included in the specified range.
567 * Set is_uid to 1 to fetch by UID instead of sequence number.
569 void imap_pick_range(char *range, int is_uid) {
573 char setstr[1024], lostr[1024], histr[1024];
577 * Clear out the IMAP_FETCHED flags for all messages.
579 for (i = 1; i <= IMAP->num_msgs; ++i) {
580 IMAP->flags[i-1] = IMAP->flags[i-1] & ~IMAP_FETCHED;
584 * Now set it for all specified messages.
586 num_sets = num_tokens(range, ',');
587 for (s=0; s<num_sets; ++s) {
588 extract_token(setstr, range, s, ',');
590 extract_token(lostr, setstr, 0, ':');
591 if (num_tokens(setstr, ':') >= 2) {
592 extract_token(histr, setstr, 1, ':');
593 if (!strcmp(histr, "*")) sprintf(histr, "%d", INT_MAX);
596 strcpy(histr, lostr);
601 /* Loop through the array, flipping bits where appropriate */
602 for (i = 1; i <= IMAP->num_msgs; ++i) {
603 if (is_uid) { /* fetch by sequence number */
604 if ( (IMAP->msgids[i-1]>=lo)
605 && (IMAP->msgids[i-1]<=hi)) {
607 IMAP->flags[i-1] | IMAP_FETCHED;
610 else { /* fetch by uid */
611 if ( (i>=lo) && (i<=hi)) {
613 IMAP->flags[i-1] | IMAP_FETCHED;
623 * This function is called by the main command loop.
625 void imap_fetch(int num_parms, char *parms[]) {
632 cprintf("%s BAD invalid parameters\r\n", parms[0]);
636 imap_pick_range(parms[2], 0);
639 for (i=3; i<num_parms; ++i) {
640 strcat(items, parms[i]);
641 if (i < (num_parms-1)) strcat(items, " ");
644 num_items = imap_extract_data_items(itemlist, items);
646 cprintf("%s BAD invalid data item list\r\n", parms[0]);
650 imap_do_fetch(num_items, itemlist);
651 cprintf("%s OK FETCH completed\r\n", parms[0]);
655 * This function is called by the main command loop.
657 void imap_uidfetch(int num_parms, char *parms[]) {
662 int have_uid_item = 0;
665 cprintf("%s BAD invalid parameters\r\n", parms[0]);
669 imap_pick_range(parms[3], 1);
672 for (i=4; i<num_parms; ++i) {
673 strcat(items, parms[i]);
674 if (i < (num_parms-1)) strcat(items, " ");
677 num_items = imap_extract_data_items(itemlist, items);
679 cprintf("%s BAD invalid data item list\r\n", parms[0]);
683 /* If the "UID" item was not included, we include it implicitly
684 * because this is a UID FETCH command
686 for (i=0; i<num_items; ++i) {
687 if (!strcasecmp(itemlist[i], "UID")) ++have_uid_item;
689 if (have_uid_item == 0) itemlist[num_items++] = "UID";
691 imap_do_fetch(num_items, itemlist);
692 cprintf("%s OK UID FETCH completed\r\n", parms[0]);