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[SIZ];
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(int seq) {
64 if (IMAP->flags[seq] & IMAP_DELETED) cprintf("\\Deleted ");
68 void imap_fetch_internaldate(struct CtdlMessage *msg) {
72 if (msg->cm_fields['T'] != NULL) {
73 msgdate = atol(msg->cm_fields['T']);
79 datestring(buf, msgdate, DATESTRING_IMAP);
80 cprintf("INTERNALDATE \"%s\"", buf);
85 * Fetch RFC822-formatted messages.
87 * 'whichfmt' should be set to one of:
88 * "RFC822" entire message
89 * "RFC822.HEADER" headers only (with trailing blank line)
90 * "RFC822.SIZE" size of translated message
91 * "RFC822.TEXT" body only (without leading blank line)
93 void imap_fetch_rfc822(int msgnum, char *whichfmt, struct CtdlMessage *msg) {
97 long headers_size, text_size, total_size;
98 long bytes_remaining = 0;
103 lprintf(1, "Cannot open temp file: %s\n", strerror(errno));
108 * Load the message into a temp file for translation and measurement
110 CtdlRedirectOutput(tmp, -1);
111 CtdlOutputPreLoadedMsg(msg, msgnum, MT_RFC822, 0, 0, 1);
112 CtdlRedirectOutput(NULL, -1);
113 if (!is_valid_message(msg)) {
114 lprintf(1, "WARNING: output clobbered the message!\n");
118 * Now figure out where the headers/text break is. IMAP considers the
119 * intervening blank line to be part of the headers, not the text.
124 ptr = fgets(buf, sizeof buf, tmp);
127 if (strlen(buf) == 0) headers_size = ftell(tmp);
129 } while ( (headers_size == 0L) && (ptr != NULL) );
130 fseek(tmp, 0L, SEEK_END);
131 total_size = ftell(tmp);
132 text_size = total_size - headers_size;
134 if (!strcasecmp(whichfmt, "RFC822.SIZE")) {
135 cprintf("RFC822.SIZE %ld", total_size);
140 else if (!strcasecmp(whichfmt, "RFC822")) {
141 bytes_remaining = total_size;
145 else if (!strcasecmp(whichfmt, "RFC822.HEADER")) {
146 bytes_remaining = headers_size;
150 else if (!strcasecmp(whichfmt, "RFC822.TEXT")) {
151 bytes_remaining = text_size;
152 fseek(tmp, headers_size, SEEK_SET);
155 cprintf("%s {%ld}\r\n", whichfmt, bytes_remaining);
156 blocksize = sizeof(buf);
157 while (bytes_remaining > 0L) {
158 if (blocksize > bytes_remaining) blocksize = bytes_remaining;
159 fread(buf, blocksize, 1, tmp);
160 client_write(buf, blocksize);
161 bytes_remaining = bytes_remaining - blocksize;
170 * Load a specific part of a message into the temp file to be output to a
171 * client. FIXME we can handle parts like "2" and "2.1" and even "2.MIME"
172 * but we still can't handle "2.HEADER" (which might not be a problem, because
173 * we currently don't have the ability to break out nested RFC822's anyway).
175 * Note: mime_parser() was called with dont_decode set to 1, so we have the
176 * luxury of simply spewing without having to re-encode.
178 void imap_load_part(char *name, char *filename, char *partnum, char *disp,
179 void *content, char *cbtype, size_t length, char *encoding,
182 struct imap_fetch_part *imfp;
185 imfp = (struct imap_fetch_part *)cbuserdata;
187 if (!strcasecmp(partnum, imfp->desired_section)) {
188 fwrite(content, length, 1, imfp->output_fp);
191 sprintf(mbuf2, "%s.MIME", partnum);
193 if (!strcasecmp(imfp->desired_section, mbuf2)) {
194 fprintf(imfp->output_fp, "Content-type: %s", cbtype);
195 if (strlen(name) > 0)
196 fprintf(imfp->output_fp, "; name=\"%s\"", name);
197 fprintf(imfp->output_fp, "\r\n");
198 if (strlen(encoding) > 0)
199 fprintf(imfp->output_fp,
200 "Content-Transfer-Encoding: %s\r\n", encoding);
201 if (strlen(encoding) > 0) {
202 fprintf(imfp->output_fp, "Content-Disposition: %s",
204 if (strlen(filename) > 0) {
205 fprintf(imfp->output_fp, "; filename=\"%s\"",
208 fprintf(imfp->output_fp, "\r\n");
210 fprintf(imfp->output_fp, "Content-Length: %d\r\n", length);
211 fprintf(imfp->output_fp, "\r\n");
219 * Called by imap_fetch_envelope() to output the "From" field.
220 * This is in its own function because its logic is kind of complex. We
221 * really need to make this suck less.
223 void imap_output_envelope_from(struct CtdlMessage *msg) {
224 char user[1024], node[1024], name[1024];
226 cprintf("(("); /* open double-parens */
227 imap_strout(msg->cm_fields['A']); /* personal name */
228 cprintf(" NIL "); /* source route (not used) */
230 if (msg->cm_fields['F'] != NULL) {
231 process_rfc822_addr(msg->cm_fields['F'], user, node, name);
232 imap_strout(user); /* mailbox name (user id) */
234 if (!strcasecmp(node, config.c_nodename)) {
235 imap_strout(config.c_fqdn);
238 imap_strout(node); /* host name */
242 imap_strout(msg->cm_fields['A']); /* mailbox name (user id) */
244 imap_strout(msg->cm_fields['N']); /* host name */
247 cprintf(")) "); /* close double-parens */
252 * Implements the ENVELOPE fetch item
254 * FIXME ... we only output some of the fields right now. Definitely need
255 * to do all of them. Accurately, too.
257 * Note that the imap_strout() function can cleverly output NULL fields as NIL,
258 * so we don't have to check for that condition like we do elsewhere.
260 void imap_fetch_envelope(long msgnum, struct CtdlMessage *msg) {
261 char datestringbuf[SIZ];
263 char *fieldptr = NULL;
265 /* Parse the message date into an IMAP-format date string */
266 if (msg->cm_fields['T'] != NULL) {
267 msgdate = atol(msg->cm_fields['T']);
270 msgdate = time(NULL);
272 datestring(datestringbuf, msgdate, DATESTRING_IMAP);
274 /* Now start spewing data fields. The order is important, as it is
275 * defined by the protocol specification. Nonexistent fields must
276 * be output as NIL, existent fields must be quoted or literalled.
277 * The imap_strout() function conveniently does all this for us.
279 cprintf("ENVELOPE (");
282 imap_strout(datestringbuf);
286 imap_strout(msg->cm_fields['U']);
290 imap_output_envelope_from(msg);
294 /* FIXME ... check for a *real* Sender: field */
297 imap_output_envelope_from(msg);
302 /* FIXME ... check for a *real* Reply-to: field */
305 imap_output_envelope_from(msg);
308 cprintf("NIL "); /* to */
310 cprintf("NIL "); /* cc */
312 cprintf("NIL "); /* bcc */
315 fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "In-reply-to");
316 imap_strout(fieldptr);
318 if (fieldptr != NULL) phree(fieldptr);
321 imap_strout(msg->cm_fields['I']);
328 * Strip any non header information out of a chunk of RFC822 data on disk,
329 * then boil it down to just the fields we want.
331 void imap_strip_headers(FILE *fp, char *section) {
333 char *which_fields = NULL;
334 int doing_headers = 0;
340 if (!strncasecmp(which_fields, "HEADER.FIELDS", 13))
342 if (!strncasecmp(which_fields, "HEADER.FIELDS.NOT", 17))
345 which_fields = strdoop(section);
346 for (i=0; i<strlen(which_fields); ++i) {
347 if (which_fields[i]=='(')
348 strcpy(which_fields, &which_fields[i+1]);
350 for (i=0; i<strlen(which_fields); ++i) {
351 if (which_fields[i]==')')
354 num_parms = imap_parameterize(parms, which_fields);
355 for (i=0; i<num_parms; ++i) {
356 lprintf(9, "parm[%d] = <%s>\n", i, parms[i]);
357 } /* FIXME do something here! */
362 while (fgets(buf, sizeof buf, fp) != NULL) {
364 if (strlen(buf) == 0) {
366 ftruncate(fileno(fp), ftell(fp));
370 fprintf(fp, "\r\n"); /* add the trailing newline */
376 * Implements the BODY and BODY.PEEK fetch items
378 void imap_fetch_body(long msgnum, char *item, int is_peek,
379 struct CtdlMessage *msg) {
386 long bytes_remaining = 0;
389 struct imap_fetch_part imfp;
391 /* extract section */
392 strcpy(section, item);
393 for (i=0; i<strlen(section); ++i) {
394 if (section[i]=='[') strcpy(section, §ion[i+1]);
396 for (i=0; i<strlen(section); ++i) {
397 if (section[i]==']') section[i] = 0;
399 lprintf(9, "Section is %s\n", section);
401 /* extract partial */
402 strcpy(partial, item);
403 for (i=0; i<strlen(partial); ++i) {
404 if (partial[i]=='<') {
405 strcpy(partial, &partial[i+1]);
409 for (i=0; i<strlen(partial); ++i) {
410 if (partial[i]=='>') partial[i] = 0;
412 if (is_partial == 0) strcpy(partial, "");
413 if (strlen(partial) > 0) lprintf(9, "Partial is %s\n", partial);
417 lprintf(1, "Cannot open temp file: %s\n", strerror(errno));
421 /* Now figure out what the client wants, and get it */
423 if (!strcmp(section, "")) { /* the whole thing */
424 CtdlRedirectOutput(tmp, -1);
425 CtdlOutputPreLoadedMsg(msg, msgnum, MT_RFC822, 0, 0, 1);
426 CtdlRedirectOutput(NULL, -1);
430 * Be obnoxious and send the entire header, even if the client only
431 * asks for certain fields. FIXME this shortcut later.
433 else if (!strncasecmp(section, "HEADER", 6)) {
434 CtdlRedirectOutput(tmp, -1);
435 CtdlOutputPreLoadedMsg(msg, msgnum, MT_RFC822, 1, 0, 1);
436 CtdlRedirectOutput(NULL, -1);
437 imap_strip_headers(tmp, section);
441 * Anything else must be a part specifier.
442 * (Note value of 1 passed as 'dont_decode' so client gets it encoded)
445 safestrncpy(imfp.desired_section, section,
446 sizeof(imfp.desired_section));
447 imfp.output_fp = tmp;
449 mime_parser(msg->cm_fields['M'], NULL,
450 *imap_load_part, NULL, NULL,
456 fseek(tmp, 0L, SEEK_END);
457 bytes_remaining = ftell(tmp);
459 if (is_partial == 0) {
461 cprintf("BODY[%s] {%ld}\r\n", section, bytes_remaining);
464 sscanf(partial, "%ld.%ld", &pstart, &pbytes);
465 if ((bytes_remaining - pstart) < pbytes) {
466 pbytes = bytes_remaining - pstart;
468 fseek(tmp, pstart, SEEK_SET);
469 bytes_remaining = pbytes;
470 cprintf("BODY[%s] {%ld}<%ld>\r\n",
471 section, bytes_remaining, pstart);
474 blocksize = sizeof(buf);
475 while (bytes_remaining > 0L) {
476 if (blocksize > bytes_remaining) blocksize = bytes_remaining;
477 fread(buf, blocksize, 1, tmp);
478 client_write(buf, blocksize);
479 bytes_remaining = bytes_remaining - blocksize;
485 /* FIXME set the last read pointer or something */
490 * Called immediately before outputting a multipart bodystructure
492 void imap_fetch_bodystructure_pre(
493 char *name, char *filename, char *partnum, char *disp,
494 void *content, char *cbtype, size_t length, char *encoding,
504 * Called immediately after outputting a multipart bodystructure
506 void imap_fetch_bodystructure_post(
507 char *name, char *filename, char *partnum, char *disp,
508 void *content, char *cbtype, size_t length, char *encoding,
514 extract_token(subtype, cbtype, 1, '/');
515 imap_strout(subtype);
522 * Output the info for a MIME part in the format required by BODYSTRUCTURE.
525 void imap_fetch_bodystructure_part(
526 char *name, char *filename, char *partnum, char *disp,
527 void *content, char *cbtype, size_t length, char *encoding,
533 int have_encoding = 0;
537 if (cbtype != NULL) if (strlen(cbtype)>0) have_cbtype = 1;
540 extract_token(buf, cbtype, 0, '/');
543 extract_token(buf, cbtype, 1, '/');
548 cprintf("\"TEXT\" \"PLAIN\" ");
551 cprintf("(\"CHARSET\" \"US-ASCII\"");
553 if (name != NULL) if (strlen(name)>0) {
554 cprintf(" \"NAME\" ");
558 if (filename != NULL) if (strlen(filename)>0) {
559 cprintf(" \"FILENAME\" ");
567 if (encoding != NULL) if (strlen(encoding) > 0) have_encoding = 1;
570 imap_strout(encoding);
577 cprintf("%ld ", length); /* bytes */
578 cprintf("NIL) "); /* lines */
584 * Spew the BODYSTRUCTURE data for a message. (Do you need a silencer if
585 * you're going to shoot a MIME? Do you need a reason to shoot Mark Crispin?
589 void imap_fetch_bodystructure (long msgnum, char *item,
590 struct CtdlMessage *msg) {
596 /* For non-RFC822 (ordinary Citadel) messages, this is short and
599 if (msg->cm_format_type != FMT_RFC822) {
601 /* *sigh* We have to RFC822-format the message just to be able
605 if (tmp == NULL) return;
606 CtdlRedirectOutput(tmp, -1);
607 CtdlOutputPreLoadedMsg(msg, msgnum, MT_RFC822, 0, 0, 1);
608 CtdlRedirectOutput(NULL, -1);
611 while (fgets(buf, sizeof buf, tmp) != NULL) ++lines;
615 cprintf("BODYSTRUCTURE (\"TEXT\" \"PLAIN\" "
616 "(\"CHARSET\" \"US-ASCII\") NIL NIL "
617 "\"7BIT\" %ld %ld)", bytes, lines);
622 /* For messages already stored in RFC822 format, we have to parse. */
623 cprintf("BODYSTRUCTURE ");
624 mime_parser(msg->cm_fields['M'],
626 *imap_fetch_bodystructure_part, /* part */
627 *imap_fetch_bodystructure_pre, /* pre-multi */
628 *imap_fetch_bodystructure_post, /* post-multi */
639 * imap_do_fetch() calls imap_do_fetch_msg() to output the deta of an
640 * individual message, once it has been successfully loaded from disk.
642 void imap_do_fetch_msg(int seq, struct CtdlMessage *msg,
643 int num_items, char **itemlist) {
646 cprintf("* %d FETCH (", seq);
648 for (i=0; i<num_items; ++i) {
650 if (!strncasecmp(itemlist[i], "BODY[", 5)) {
651 imap_fetch_body(IMAP->msgids[seq-1], itemlist[i],
654 else if (!strncasecmp(itemlist[i], "BODY.PEEK[", 10)) {
655 imap_fetch_body(IMAP->msgids[seq-1], itemlist[i],
658 else if (!strcasecmp(itemlist[i], "BODYSTRUCTURE")) {
659 imap_fetch_bodystructure(IMAP->msgids[seq-1],
662 else if (!strcasecmp(itemlist[i], "ENVELOPE")) {
663 imap_fetch_envelope(IMAP->msgids[seq-1], msg);
665 else if (!strcasecmp(itemlist[i], "FLAGS")) {
666 imap_fetch_flags(seq-1);
668 else if (!strcasecmp(itemlist[i], "INTERNALDATE")) {
669 imap_fetch_internaldate(msg);
671 else if (!strcasecmp(itemlist[i], "RFC822")) {
672 imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i], msg);
674 else if (!strcasecmp(itemlist[i], "RFC822.HEADER")) {
675 imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i], msg);
677 else if (!strcasecmp(itemlist[i], "RFC822.SIZE")) {
678 imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i], msg);
680 else if (!strcasecmp(itemlist[i], "RFC822.TEXT")) {
681 imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i], msg);
683 else if (!strcasecmp(itemlist[i], "UID")) {
687 if (i != num_items-1) cprintf(" ");
696 * imap_fetch() calls imap_do_fetch() to do its actual work, once it's
697 * validated and boiled down the request a bit.
699 void imap_do_fetch(int num_items, char **itemlist) {
701 struct CtdlMessage *msg;
703 if (IMAP->num_msgs > 0)
704 for (i = 0; i < IMAP->num_msgs; ++i)
705 if (IMAP->flags[i] & IMAP_SELECTED) {
706 msg = CtdlFetchMessage(IMAP->msgids[i]);
708 imap_do_fetch_msg(i+1, msg, num_items, itemlist);
709 CtdlFreeMessage(msg);
712 cprintf("* %d FETCH <internal error>\r\n", i+1);
720 * Back end for imap_handle_macros()
721 * Note that this function *only* looks at the beginning of the string. It
722 * is not a generic search-and-replace function.
724 void imap_macro_replace(char *str, char *find, char *replace) {
727 if (!strncasecmp(str, find, strlen(find))) {
728 if (str[strlen(find)]==' ') {
729 strcpy(holdbuf, &str[strlen(find)+1]);
730 strcpy(str, replace);
732 strcat(str, holdbuf);
734 if (str[strlen(find)]==0) {
735 strcpy(holdbuf, &str[strlen(find)+1]);
736 strcpy(str, replace);
744 * Handle macros embedded in FETCH data items.
745 * (What the heck are macros doing in a wire protocol? Are we trying to save
746 * the computer at the other end the trouble of typing a lot of characters?)
748 void imap_handle_macros(char *str) {
752 for (i=0; i<strlen(str); ++i) {
753 if (str[i]=='(') ++nest;
754 if (str[i]=='[') ++nest;
755 if (str[i]=='<') ++nest;
756 if (str[i]=='{') ++nest;
757 if (str[i]==')') --nest;
758 if (str[i]==']') --nest;
759 if (str[i]=='>') --nest;
760 if (str[i]=='}') --nest;
763 imap_macro_replace(&str[i],
765 "FLAGS INTERNALDATE RFC822.SIZE ENVELOPE"
767 imap_macro_replace(&str[i],
771 imap_macro_replace(&str[i],
773 "FLAGS INTERNALDATE RFC822.SIZE"
775 imap_macro_replace(&str[i],
777 "FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODY"
785 * Break out the data items requested, possibly a parenthesized list.
786 * Returns the number of data items, or -1 if the list is invalid.
787 * NOTE: this function alters the string it is fed, and uses it as a buffer
788 * to hold the data for the pointers it returns.
790 int imap_extract_data_items(char **argv, char *items) {
796 /* Convert all whitespace to ordinary space characters. */
797 for (i=0; i<strlen(items); ++i) {
798 if (isspace(items[i])) items[i]=' ';
801 /* Strip leading and trailing whitespace, then strip leading and
802 * trailing parentheses if it's a list
805 if ( (items[0]=='(') && (items[strlen(items)-1]==')') ) {
806 items[strlen(items)-1] = 0;
807 strcpy(items, &items[1]);
811 /* Parse any macro data items */
812 imap_handle_macros(items);
815 * Now break out the data items. We throw in one trailing space in
816 * order to avoid having to break out the last one manually.
820 initial_len = strlen(items);
821 for (i=0; i<initial_len; ++i) {
822 if (items[i]=='(') ++nest;
823 if (items[i]=='[') ++nest;
824 if (items[i]=='<') ++nest;
825 if (items[i]=='{') ++nest;
826 if (items[i]==')') --nest;
827 if (items[i]==']') --nest;
828 if (items[i]=='>') --nest;
829 if (items[i]=='}') --nest;
831 if (nest <= 0) if (items[i]==' ') {
833 argv[num_items++] = start;
844 * One particularly hideous aspect of IMAP is that we have to allow the client
845 * to specify arbitrary ranges and/or sets of messages to fetch. Citadel IMAP
846 * handles this by setting the IMAP_SELECTED flag for each message specified in
847 * the ranges/sets, then looping through the message array, outputting messages
848 * with the flag set. We don't bother returning an error if an out-of-range
849 * number is specified (we just return quietly) because any client braindead
850 * enough to request a bogus message number isn't going to notice the
853 * This function clears out the IMAP_SELECTED bits, then sets that bit for each
854 * message included in the specified range.
856 * Set is_uid to 1 to fetch by UID instead of sequence number.
858 void imap_pick_range(char *supplied_range, int is_uid) {
862 char setstr[SIZ], lostr[SIZ], histr[SIZ]; /* was 1024 */
864 char actual_range[SIZ];
867 * Handle the "ALL" macro
869 if (!strcasecmp(supplied_range, "ALL")) {
870 safestrncpy(actual_range, "1:*", sizeof actual_range);
873 safestrncpy(actual_range, supplied_range, sizeof actual_range);
877 * Clear out the IMAP_SELECTED flags for all messages.
879 for (i = 0; i < IMAP->num_msgs; ++i) {
880 IMAP->flags[i] = IMAP->flags[i] & ~IMAP_SELECTED;
884 * Now set it for all specified messages.
886 num_sets = num_tokens(actual_range, ',');
887 for (s=0; s<num_sets; ++s) {
888 extract_token(setstr, actual_range, s, ',');
890 extract_token(lostr, setstr, 0, ':');
891 if (num_tokens(setstr, ':') >= 2) {
892 extract_token(histr, setstr, 1, ':');
893 if (!strcmp(histr, "*")) sprintf(histr, "%d", INT_MAX);
896 strcpy(histr, lostr);
901 /* Loop through the array, flipping bits where appropriate */
902 for (i = 1; i <= IMAP->num_msgs; ++i) {
903 if (is_uid) { /* fetch by sequence number */
904 if ( (IMAP->msgids[i-1]>=lo)
905 && (IMAP->msgids[i-1]<=hi)) {
907 IMAP->flags[i-1] | IMAP_SELECTED;
910 else { /* fetch by uid */
911 if ( (i>=lo) && (i<=hi)) {
913 IMAP->flags[i-1] | IMAP_SELECTED;
924 * This function is called by the main command loop.
926 void imap_fetch(int num_parms, char *parms[]) {
927 char items[SIZ]; /* was 1024 */
933 cprintf("%s BAD invalid parameters\r\n", parms[0]);
937 imap_pick_range(parms[2], 0);
940 for (i=3; i<num_parms; ++i) {
941 strcat(items, parms[i]);
942 if (i < (num_parms-1)) strcat(items, " ");
945 num_items = imap_extract_data_items(itemlist, items);
947 cprintf("%s BAD invalid data item list\r\n", parms[0]);
951 imap_do_fetch(num_items, itemlist);
952 cprintf("%s OK FETCH completed\r\n", parms[0]);
956 * This function is called by the main command loop.
958 void imap_uidfetch(int num_parms, char *parms[]) {
959 char items[SIZ]; /* was 1024 */
963 int have_uid_item = 0;
966 cprintf("%s BAD invalid parameters\r\n", parms[0]);
970 imap_pick_range(parms[3], 1);
973 for (i=4; i<num_parms; ++i) {
974 strcat(items, parms[i]);
975 if (i < (num_parms-1)) strcat(items, " ");
978 num_items = imap_extract_data_items(itemlist, items);
980 cprintf("%s BAD invalid data item list\r\n", parms[0]);
984 /* If the "UID" item was not included, we include it implicitly
985 * because this is a UID FETCH command
987 for (i=0; i<num_items; ++i) {
988 if (!strcasecmp(itemlist[i], "UID")) ++have_uid_item;
990 if (have_uid_item == 0) itemlist[num_items++] = "UID";
992 imap_do_fetch(num_items, itemlist);
993 cprintf("%s OK UID FETCH completed\r\n", parms[0]);