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(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, struct CtdlMessage *msg) {
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 CtdlOutputPreLoadedMsg(msg, msgnum, MT_RFC822, 0, 0, 1);
110 CtdlRedirectOutput(NULL, -1);
111 if (!is_valid_message(msg)) {
112 lprintf(1, "WARNING: output clobbered the message!\n");
116 * Now figure out where the headers/text break is. IMAP considers the
117 * intervening blank line to be part of the headers, not the text.
122 ptr = fgets(buf, sizeof buf, tmp);
125 if (strlen(buf) == 0) headers_size = ftell(tmp);
127 } while ( (headers_size == 0L) && (ptr != NULL) );
128 fseek(tmp, 0L, SEEK_END);
129 total_size = ftell(tmp);
130 text_size = total_size - headers_size;
132 if (!strcasecmp(whichfmt, "RFC822.SIZE")) {
133 cprintf("RFC822.SIZE %ld", total_size);
138 else if (!strcasecmp(whichfmt, "RFC822")) {
139 bytes_remaining = total_size;
143 else if (!strcasecmp(whichfmt, "RFC822.HEADER")) {
144 bytes_remaining = headers_size;
148 else if (!strcasecmp(whichfmt, "RFC822.TEXT")) {
149 bytes_remaining = text_size;
150 fseek(tmp, headers_size, SEEK_SET);
153 cprintf("%s {%ld}\r\n", whichfmt, bytes_remaining);
154 blocksize = sizeof(buf);
155 while (bytes_remaining > 0L) {
156 if (blocksize > bytes_remaining) blocksize = bytes_remaining;
157 fread(buf, blocksize, 1, tmp);
158 client_write(buf, blocksize);
159 bytes_remaining = bytes_remaining - blocksize;
168 * Load a specific part of a message into the temp file to be output to a
169 * client. FIXME we can handle parts like "2" and "2.1" and even "2.MIME"
170 * but we still can't handle "2.HEADER" (which might not be a problem, because
171 * we currently don't have the ability to break out nested RFC822's anyway).
173 * Note: mime_parser() was called with dont_decode set to 1, so we have the
174 * luxury of simply spewing without having to re-encode.
176 void imap_load_part(char *name, char *filename, char *partnum, char *disp,
177 void *content, char *cbtype, size_t length, char *encoding,
180 struct imap_fetch_part *imfp;
183 imfp = (struct imap_fetch_part *)cbuserdata;
185 if (!strcasecmp(partnum, imfp->desired_section)) {
186 fwrite(content, length, 1, imfp->output_fp);
189 sprintf(mbuf2, "%s.MIME", partnum);
191 if (!strcasecmp(imfp->desired_section, mbuf2)) {
192 fprintf(imfp->output_fp, "Content-type: %s", cbtype);
193 if (strlen(name) > 0)
194 fprintf(imfp->output_fp, "; name=\"%s\"", name);
195 fprintf(imfp->output_fp, "\r\n");
196 if (strlen(encoding) > 0)
197 fprintf(imfp->output_fp,
198 "Content-Transfer-Encoding: %s\r\n", encoding);
199 if (strlen(encoding) > 0) {
200 fprintf(imfp->output_fp, "Content-Disposition: %s",
202 if (strlen(filename) > 0) {
203 fprintf(imfp->output_fp, "; filename=\"%s\"",
206 fprintf(imfp->output_fp, "\r\n");
208 fprintf(imfp->output_fp, "Content-Length: %d\r\n", length);
209 fprintf(imfp->output_fp, "\r\n");
217 * Called by imap_fetch_envelope() to output the "From" field.
218 * This is in its own function because its logic is kind of complex. We
219 * really need to make this suck less.
221 void imap_output_envelope_from(struct CtdlMessage *msg) {
222 char user[1024], node[1024], name[1024];
224 cprintf("(("); /* open double-parens */
225 imap_strout(msg->cm_fields['A']); /* personal name */
226 cprintf(" NIL "); /* source route (not used) */
228 if (msg->cm_fields['F'] != NULL) {
229 process_rfc822_addr(msg->cm_fields['F'], user, node, name);
230 imap_strout(user); /* mailbox name (user id) */
232 if (!strcasecmp(node, config.c_nodename)) {
233 imap_strout(config.c_fqdn);
236 imap_strout(node); /* host name */
240 imap_strout(msg->cm_fields['A']); /* mailbox name (user id) */
242 imap_strout(msg->cm_fields['N']); /* host name */
245 cprintf(")) "); /* close double-parens */
250 * Implements the ENVELOPE fetch item
252 * FIXME ... we only output some of the fields right now. Definitely need
253 * to do all of them. Accurately, too.
255 * Note that the imap_strout() function can cleverly output NULL fields as NIL,
256 * so we don't have to check for that condition like we do elsewhere.
258 void imap_fetch_envelope(long msgnum, struct CtdlMessage *msg) {
259 char datestringbuf[SIZ];
261 char *fieldptr = NULL;
263 /* Parse the message date into an IMAP-format date string */
264 if (msg->cm_fields['T'] != NULL) {
265 msgdate = atol(msg->cm_fields['T']);
268 msgdate = time(NULL);
270 datestring(datestringbuf, msgdate, DATESTRING_IMAP);
272 /* Now start spewing data fields. The order is important, as it is
273 * defined by the protocol specification. Nonexistent fields must
274 * be output as NIL, existent fields must be quoted or literalled.
275 * The imap_strout() function conveniently does all this for us.
277 cprintf("ENVELOPE (");
280 imap_strout(datestringbuf);
284 imap_strout(msg->cm_fields['U']);
288 imap_output_envelope_from(msg);
292 /* FIXME ... check for a *real* Sender: field */
295 imap_output_envelope_from(msg);
300 /* FIXME ... check for a *real* Reply-to: field */
303 imap_output_envelope_from(msg);
306 cprintf("NIL "); /* to */
308 cprintf("NIL "); /* cc */
310 cprintf("NIL "); /* bcc */
313 fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "In-reply-to");
314 imap_strout(fieldptr);
316 if (fieldptr != NULL) phree(fieldptr);
319 imap_strout(msg->cm_fields['I']);
326 * Strip any non header information out of a chunk of RFC822 data on disk
328 void imap_strip_headers(FILE *fp) {
332 while (fgets(buf, sizeof buf, fp) != NULL) {
334 if (strlen(buf) == 0) {
336 ftruncate(fileno(fp), ftell(fp));
340 fprintf(fp, "\r\n"); /* add the trailing newline */
346 * Implements the BODY and BODY.PEEK fetch items
348 void imap_fetch_body(long msgnum, char *item, int is_peek,
349 struct CtdlMessage *msg) {
356 long bytes_remaining = 0;
359 struct imap_fetch_part imfp;
361 /* extract section */
362 strcpy(section, item);
363 for (i=0; i<strlen(section); ++i) {
364 if (section[i]=='[') strcpy(section, §ion[i+1]);
366 for (i=0; i<strlen(section); ++i) {
367 if (section[i]==']') section[i] = 0;
369 lprintf(9, "Section is %s\n", section);
371 /* extract partial */
372 strcpy(partial, item);
373 for (i=0; i<strlen(partial); ++i) {
374 if (partial[i]=='<') {
375 strcpy(partial, &partial[i+1]);
379 for (i=0; i<strlen(partial); ++i) {
380 if (partial[i]=='>') partial[i] = 0;
382 if (is_partial == 0) strcpy(partial, "");
383 if (strlen(partial) > 0) lprintf(9, "Partial is %s\n", partial);
387 lprintf(1, "Cannot open temp file: %s\n", strerror(errno));
391 /* Now figure out what the client wants, and get it */
393 if (!strcmp(section, "")) { /* the whole thing */
394 CtdlRedirectOutput(tmp, -1);
395 CtdlOutputPreLoadedMsg(msg, msgnum, MT_RFC822, 0, 0, 1);
396 CtdlRedirectOutput(NULL, -1);
400 * Be obnoxious and send the entire header, even if the client only
401 * asks for certain fields. FIXME this shortcut later.
403 else if (!strncasecmp(section, "HEADER", 6)) {
404 CtdlRedirectOutput(tmp, -1);
405 CtdlOutputPreLoadedMsg(msg, msgnum, MT_RFC822, 1, 0, 1);
406 CtdlRedirectOutput(NULL, -1);
407 imap_strip_headers(tmp);
411 * Anything else must be a part specifier.
412 * (Note value of 1 passed as 'dont_decode' so client gets it encoded)
415 safestrncpy(imfp.desired_section, section,
416 sizeof(imfp.desired_section));
417 imfp.output_fp = tmp;
419 mime_parser(msg->cm_fields['M'], NULL,
420 *imap_load_part, NULL, NULL,
426 fseek(tmp, 0L, SEEK_END);
427 bytes_remaining = ftell(tmp);
429 if (is_partial == 0) {
431 cprintf("BODY[%s] {%ld}\r\n", section, bytes_remaining);
434 sscanf(partial, "%ld.%ld", &pstart, &pbytes);
435 if ((bytes_remaining - pstart) < pbytes) {
436 pbytes = bytes_remaining - pstart;
438 fseek(tmp, pstart, SEEK_SET);
439 bytes_remaining = pbytes;
440 cprintf("BODY[%s] {%ld}<%ld>\r\n",
441 section, bytes_remaining, pstart);
444 blocksize = sizeof(buf);
445 while (bytes_remaining > 0L) {
446 if (blocksize > bytes_remaining) blocksize = bytes_remaining;
447 fread(buf, blocksize, 1, tmp);
448 client_write(buf, blocksize);
449 bytes_remaining = bytes_remaining - blocksize;
455 /* FIXME set the last read pointer or something */
460 * Called immediately before outputting a multipart bodystructure
462 void imap_fetch_bodystructure_pre(
463 char *name, char *filename, char *partnum, char *disp,
464 void *content, char *cbtype, size_t length, char *encoding,
474 * Called immediately after outputting a multipart bodystructure
476 void imap_fetch_bodystructure_post(
477 char *name, char *filename, char *partnum, char *disp,
478 void *content, char *cbtype, size_t length, char *encoding,
484 extract_token(subtype, cbtype, 1, '/');
485 imap_strout(subtype);
492 * Output the info for a MIME part in the format required by BODYSTRUCTURE.
495 void imap_fetch_bodystructure_part(
496 char *name, char *filename, char *partnum, char *disp,
497 void *content, char *cbtype, size_t length, char *encoding,
503 int have_encoding = 0;
507 if (cbtype != NULL) if (strlen(cbtype)>0) have_cbtype = 1;
510 extract_token(buf, cbtype, 0, '/');
513 extract_token(buf, cbtype, 1, '/');
518 cprintf("\"TEXT\" \"PLAIN\" ");
521 cprintf("(\"CHARSET\" \"US-ASCII\"");
523 if (name != NULL) if (strlen(name)>0) {
524 cprintf(" \"NAME\" ");
528 if (filename != NULL) if (strlen(filename)>0) {
529 cprintf(" \"FILENAME\" ");
537 if (encoding != NULL) if (strlen(encoding) > 0) have_encoding = 1;
540 imap_strout(encoding);
547 cprintf("%ld ", length); /* bytes */
548 cprintf("NIL) "); /* lines */
554 * Spew the BODYSTRUCTURE data for a message. (Do you need a silencer if
555 * you're going to shoot a MIME? Do you need a reason to shoot Mark Crispin?
559 void imap_fetch_bodystructure (long msgnum, char *item,
560 struct CtdlMessage *msg) {
566 /* For non-RFC822 (ordinary Citadel) messages, this is short and
569 if (msg->cm_format_type != FMT_RFC822) {
571 /* *sigh* We have to RFC822-format the message just to be able
575 if (tmp == NULL) return;
576 CtdlRedirectOutput(tmp, -1);
577 CtdlOutputPreLoadedMsg(msg, msgnum, MT_RFC822, 0, 0, 1);
578 CtdlRedirectOutput(NULL, -1);
581 while (fgets(buf, sizeof buf, tmp) != NULL) ++lines;
585 cprintf("BODYSTRUCTURE (\"TEXT\" \"PLAIN\" "
586 "(\"CHARSET\" \"US-ASCII\") NIL NIL "
587 "\"7BIT\" %ld %ld)", bytes, lines);
592 /* For messages already stored in RFC822 format, we have to parse. */
593 cprintf("BODYSTRUCTURE ");
594 mime_parser(msg->cm_fields['M'],
596 *imap_fetch_bodystructure_part, /* part */
597 *imap_fetch_bodystructure_pre, /* pre-multi */
598 *imap_fetch_bodystructure_post, /* post-multi */
609 * imap_do_fetch() calls imap_do_fetch_msg() to output the deta of an
610 * individual message, once it has been successfully loaded from disk.
612 void imap_do_fetch_msg(int seq, struct CtdlMessage *msg,
613 int num_items, char **itemlist) {
616 cprintf("* %d FETCH (", seq);
618 for (i=0; i<num_items; ++i) {
620 if (!strncasecmp(itemlist[i], "BODY[", 5)) {
621 imap_fetch_body(IMAP->msgids[seq-1], itemlist[i],
624 else if (!strncasecmp(itemlist[i], "BODY.PEEK[", 10)) {
625 imap_fetch_body(IMAP->msgids[seq-1], itemlist[i],
628 else if (!strcasecmp(itemlist[i], "BODYSTRUCTURE")) {
629 imap_fetch_bodystructure(IMAP->msgids[seq-1],
632 else if (!strcasecmp(itemlist[i], "ENVELOPE")) {
633 imap_fetch_envelope(IMAP->msgids[seq-1], msg);
635 else if (!strcasecmp(itemlist[i], "FLAGS")) {
636 imap_fetch_flags(msg);
638 else if (!strcasecmp(itemlist[i], "INTERNALDATE")) {
639 imap_fetch_internaldate(msg);
641 else if (!strcasecmp(itemlist[i], "RFC822")) {
642 imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i], msg);
644 else if (!strcasecmp(itemlist[i], "RFC822.HEADER")) {
645 imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i], msg);
647 else if (!strcasecmp(itemlist[i], "RFC822.SIZE")) {
648 imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i], msg);
650 else if (!strcasecmp(itemlist[i], "RFC822.TEXT")) {
651 imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i], msg);
653 else if (!strcasecmp(itemlist[i], "UID")) {
657 if (i != num_items-1) cprintf(" ");
666 * imap_fetch() calls imap_do_fetch() to do its actual work, once it's
667 * validated and boiled down the request a bit.
669 void imap_do_fetch(int num_items, char **itemlist) {
671 struct CtdlMessage *msg;
673 if (IMAP->num_msgs > 0)
674 for (i = 0; i < IMAP->num_msgs; ++i)
675 if (IMAP->flags[i] & IMAP_FETCHED) {
676 msg = CtdlFetchMessage(IMAP->msgids[i]);
678 imap_do_fetch_msg(i+1, msg, num_items, itemlist);
679 CtdlFreeMessage(msg);
682 cprintf("* %d FETCH <internal error>\r\n", i+1);
690 * Back end for imap_handle_macros()
691 * Note that this function *only* looks at the beginning of the string. It
692 * is not a generic search-and-replace function.
694 void imap_macro_replace(char *str, char *find, char *replace) {
697 if (!strncasecmp(str, find, strlen(find))) {
698 if (str[strlen(find)]==' ') {
699 strcpy(holdbuf, &str[strlen(find)+1]);
700 strcpy(str, replace);
702 strcat(str, holdbuf);
704 if (str[strlen(find)]==0) {
705 strcpy(holdbuf, &str[strlen(find)+1]);
706 strcpy(str, replace);
714 * Handle macros embedded in FETCH data items.
715 * (What the heck are macros doing in a wire protocol? Are we trying to save
716 * the computer at the other end the trouble of typing a lot of characters?)
718 void imap_handle_macros(char *str) {
722 for (i=0; i<strlen(str); ++i) {
723 if (str[i]=='(') ++nest;
724 if (str[i]=='[') ++nest;
725 if (str[i]=='<') ++nest;
726 if (str[i]=='{') ++nest;
727 if (str[i]==')') --nest;
728 if (str[i]==']') --nest;
729 if (str[i]=='>') --nest;
730 if (str[i]=='}') --nest;
733 imap_macro_replace(&str[i],
735 "FLAGS INTERNALDATE RFC822.SIZE ENVELOPE"
737 imap_macro_replace(&str[i],
741 imap_macro_replace(&str[i],
743 "FLAGS INTERNALDATE RFC822.SIZE"
745 imap_macro_replace(&str[i],
747 "FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODY"
755 * Break out the data items requested, possibly a parenthesized list.
756 * Returns the number of data items, or -1 if the list is invalid.
757 * NOTE: this function alters the string it is fed, and uses it as a buffer
758 * to hold the data for the pointers it returns.
760 int imap_extract_data_items(char **argv, char *items) {
766 /* Convert all whitespace to ordinary space characters. */
767 for (i=0; i<strlen(items); ++i) {
768 if (isspace(items[i])) items[i]=' ';
771 /* Strip leading and trailing whitespace, then strip leading and
772 * trailing parentheses if it's a list
775 if ( (items[0]=='(') && (items[strlen(items)-1]==')') ) {
776 items[strlen(items)-1] = 0;
777 strcpy(items, &items[1]);
781 /* Parse any macro data items */
782 imap_handle_macros(items);
785 * Now break out the data items. We throw in one trailing space in
786 * order to avoid having to break out the last one manually.
790 initial_len = strlen(items);
791 for (i=0; i<initial_len; ++i) {
792 if (items[i]=='(') ++nest;
793 if (items[i]=='[') ++nest;
794 if (items[i]=='<') ++nest;
795 if (items[i]=='{') ++nest;
796 if (items[i]==')') --nest;
797 if (items[i]==']') --nest;
798 if (items[i]=='>') --nest;
799 if (items[i]=='}') --nest;
801 if (nest <= 0) if (items[i]==' ') {
803 argv[num_items++] = start;
814 * One particularly hideous aspect of IMAP is that we have to allow the client
815 * to specify arbitrary ranges and/or sets of messages to fetch. Citadel IMAP
816 * handles this by setting the IMAP_FETCHED flag for each message specified in
817 * the ranges/sets, then looping through the message array, outputting messages
818 * with the flag set. We don't bother returning an error if an out-of-range
819 * number is specified (we just return quietly) because any client braindead
820 * enough to request a bogus message number isn't going to notice the
823 * This function clears out the IMAP_FETCHED bits, then sets that bit for each
824 * message included in the specified range.
826 * Set is_uid to 1 to fetch by UID instead of sequence number.
828 void imap_pick_range(char *range, int is_uid) {
832 char setstr[SIZ], lostr[SIZ], histr[SIZ]; /* was 1024 */
836 actual_range = range;
837 if (!strcasecmp(range, "ALL")) actual_range = "1:*";
840 * Clear out the IMAP_FETCHED flags for all messages.
842 for (i = 0; i < IMAP->num_msgs; ++i) {
843 IMAP->flags[i] = IMAP->flags[i] & ~IMAP_FETCHED;
847 * Now set it for all specified messages.
849 num_sets = num_tokens(actual_range, ',');
850 for (s=0; s<num_sets; ++s) {
851 extract_token(setstr, actual_range, s, ',');
853 extract_token(lostr, setstr, 0, ':');
854 if (num_tokens(setstr, ':') >= 2) {
855 extract_token(histr, setstr, 1, ':');
856 if (!strcmp(histr, "*")) sprintf(histr, "%d", INT_MAX);
859 strcpy(histr, lostr);
864 /* Loop through the array, flipping bits where appropriate */
865 for (i = 1; i <= IMAP->num_msgs; ++i) {
866 if (is_uid) { /* fetch by sequence number */
867 if ( (IMAP->msgids[i-1]>=lo)
868 && (IMAP->msgids[i-1]<=hi)) {
870 IMAP->flags[i-1] | IMAP_FETCHED;
873 else { /* fetch by uid */
874 if ( (i>=lo) && (i<=hi)) {
876 IMAP->flags[i-1] | IMAP_FETCHED;
883 * Make sure we didn't select any expunged messages.
885 for (i = 0; i < IMAP->num_msgs; ++i) {
886 if (IMAP->flags[i] & IMAP_EXPUNGED) {
887 lprintf(9, "eliminating %d because expunged\n", i);
888 IMAP->flags[i] = IMAP->flags[i] & ~IMAP_FETCHED;
897 * This function is called by the main command loop.
899 void imap_fetch(int num_parms, char *parms[]) {
900 char items[SIZ]; /* was 1024 */
906 cprintf("%s BAD invalid parameters\r\n", parms[0]);
910 imap_pick_range(parms[2], 0);
913 for (i=3; i<num_parms; ++i) {
914 strcat(items, parms[i]);
915 if (i < (num_parms-1)) strcat(items, " ");
918 num_items = imap_extract_data_items(itemlist, items);
920 cprintf("%s BAD invalid data item list\r\n", parms[0]);
924 imap_do_fetch(num_items, itemlist);
925 cprintf("%s OK FETCH completed\r\n", parms[0]);
929 * This function is called by the main command loop.
931 void imap_uidfetch(int num_parms, char *parms[]) {
932 char items[SIZ]; /* was 1024 */
936 int have_uid_item = 0;
939 cprintf("%s BAD invalid parameters\r\n", parms[0]);
943 imap_pick_range(parms[3], 1);
946 for (i=4; i<num_parms; ++i) {
947 strcat(items, parms[i]);
948 if (i < (num_parms-1)) strcat(items, " ");
951 num_items = imap_extract_data_items(itemlist, items);
953 cprintf("%s BAD invalid data item list\r\n", parms[0]);
957 /* If the "UID" item was not included, we include it implicitly
958 * because this is a UID FETCH command
960 for (i=0; i<num_items; ++i) {
961 if (!strcasecmp(itemlist[i], "UID")) ++have_uid_item;
963 if (have_uid_item == 0) itemlist[num_items++] = "UID";
965 imap_do_fetch(num_items, itemlist);
966 cprintf("%s OK UID FETCH completed\r\n", parms[0]);