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);
219 fseek(tmp, 0L, SEEK_END);
220 bytes_remaining = ftell(tmp);
222 if (is_partial == 0) {
224 cprintf("BODY[%s] {%ld}\r\n", section, bytes_remaining);
227 sscanf(partial, "%ld.%ld", &pstart, &pbytes);
228 if ((bytes_remaining - pstart) < pbytes) {
229 pbytes = bytes_remaining - pstart;
231 fseek(tmp, pstart, SEEK_SET);
232 bytes_remaining = pbytes;
233 cprintf("BODY[%s] {%ld}<%ld>\r\n",
234 section, bytes_remaining, pstart);
237 blocksize = sizeof(buf);
238 while (bytes_remaining > 0L) {
239 if (blocksize > bytes_remaining) blocksize = bytes_remaining;
240 fread(buf, blocksize, 1, tmp);
241 client_write(buf, blocksize);
242 bytes_remaining = bytes_remaining - blocksize;
248 /* FIXME set the last read pointer or something */
255 * imap_do_fetch() calls imap_do_fetch_msg() to output the deta of an
256 * individual message, once it has been successfully loaded from disk.
258 void imap_do_fetch_msg(int seq, struct CtdlMessage *msg,
259 int num_items, char **itemlist) {
262 cprintf("* %d FETCH (", seq);
264 for (i=0; i<num_items; ++i) {
266 if (!strncasecmp(itemlist[i], "BODY[", 5)) {
267 imap_fetch_body(IMAP->msgids[seq-1], itemlist[i], 0);
269 else if (!strncasecmp(itemlist[i], "BODY.PEEK[", 10)) {
270 imap_fetch_body(IMAP->msgids[seq-1], itemlist[i], 1);
272 else if (!strcasecmp(itemlist[i], "BODYSTRUCTURE")) {
273 /* FIXME do something here */
275 else if (!strcasecmp(itemlist[i], "ENVELOPE")) {
276 /* FIXME do something here */
278 else if (!strcasecmp(itemlist[i], "FLAGS")) {
279 imap_fetch_flags(msg);
281 else if (!strcasecmp(itemlist[i], "INTERNALDATE")) {
282 imap_fetch_internaldate(msg);
284 else if (!strcasecmp(itemlist[i], "RFC822")) {
285 imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i]);
287 else if (!strcasecmp(itemlist[i], "RFC822.HEADER")) {
288 imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i]);
290 else if (!strcasecmp(itemlist[i], "RFC822.SIZE")) {
291 imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i]);
293 else if (!strcasecmp(itemlist[i], "RFC822.TEXT")) {
294 imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i]);
296 else if (!strcasecmp(itemlist[i], "UID")) {
300 if (i != num_items-1) cprintf(" ");
309 * imap_fetch() calls imap_do_fetch() to do its actual work, once it's
310 * validated and boiled down the request a bit.
312 void imap_do_fetch(int num_items, char **itemlist) {
314 struct CtdlMessage *msg;
316 if (IMAP->num_msgs > 0)
317 for (i = 0; i < IMAP->num_msgs; ++i)
318 if (IMAP->flags[i] && IMAP_FETCHED) {
319 msg = CtdlFetchMessage(IMAP->msgids[i]);
321 imap_do_fetch_msg(i+1, msg, num_items, itemlist);
322 CtdlFreeMessage(msg);
325 cprintf("* %d FETCH <internal error>\r\n", i+1);
333 * Back end for imap_handle_macros()
334 * Note that this function *only* looks at the beginning of the string. It
335 * is not a generic search-and-replace function.
337 void imap_macro_replace(char *str, char *find, char *replace) {
340 if (!strncasecmp(str, find, strlen(find))) {
341 if (str[strlen(find)]==' ') {
342 strcpy(holdbuf, &str[strlen(find)+1]);
343 strcpy(str, replace);
345 strcat(str, holdbuf);
347 if (str[strlen(find)]==0) {
348 strcpy(holdbuf, &str[strlen(find)+1]);
349 strcpy(str, replace);
357 * Handle macros embedded in FETCH data items.
358 * (What the heck are macros doing in a wire protocol? Are we trying to save
359 * the computer at the other end the trouble of typing a lot of characters?)
361 void imap_handle_macros(char *str) {
365 for (i=0; i<strlen(str); ++i) {
366 if (str[i]=='(') ++nest;
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;
376 imap_macro_replace(&str[i],
378 "FLAGS INTERNALDATE RFC822.SIZE ENVELOPE"
380 imap_macro_replace(&str[i],
384 imap_macro_replace(&str[i],
386 "FLAGS INTERNALDATE RFC822.SIZE"
388 imap_macro_replace(&str[i],
390 "FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODY"
398 * Break out the data items requested, possibly a parenthesized list.
399 * Returns the number of data items, or -1 if the list is invalid.
400 * NOTE: this function alters the string it is fed, and uses it as a buffer
401 * to hold the data for the pointers it returns.
403 int imap_extract_data_items(char **argv, char *items) {
409 /* Convert all whitespace to ordinary space characters. */
410 for (i=0; i<strlen(items); ++i) {
411 if (isspace(items[i])) items[i]=' ';
414 /* Strip leading and trailing whitespace, then strip leading and
415 * trailing parentheses if it's a list
418 if ( (items[0]=='(') && (items[strlen(items)-1]==')') ) {
419 items[strlen(items)-1] = 0;
420 strcpy(items, &items[1]);
424 /* Parse any macro data items */
425 imap_handle_macros(items);
428 * Now break out the data items. We throw in one trailing space in
429 * order to avoid having to break out the last one manually.
433 initial_len = strlen(items);
434 for (i=0; i<initial_len; ++i) {
435 if (items[i]=='(') ++nest;
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;
444 if (nest <= 0) if (items[i]==' ') {
446 argv[num_items++] = start;
457 * One particularly hideous aspect of IMAP is that we have to allow the client
458 * to specify arbitrary ranges and/or sets of messages to fetch. Citadel IMAP
459 * handles this by setting the IMAP_FETCHED flag for each message specified in
460 * the ranges/sets, then looping through the message array, outputting messages
461 * with the flag set. We don't bother returning an error if an out-of-range
462 * number is specified (we just return quietly) because any client braindead
463 * enough to request a bogus message number isn't going to notice the
466 * This function clears out the IMAP_FETCHED bits, then sets that bit for each
467 * message included in the specified range.
469 * Set is_uid to 1 to fetch by UID instead of sequence number.
471 void imap_pick_range(char *range, int is_uid) {
475 char setstr[1024], lostr[1024], histr[1024];
479 * Clear out the IMAP_FETCHED flags for all messages.
481 for (i = 1; i <= IMAP->num_msgs; ++i) {
482 IMAP->flags[i-1] = IMAP->flags[i-1] & ~IMAP_FETCHED;
486 * Now set it for all specified messages.
488 num_sets = num_tokens(range, ',');
489 for (s=0; s<num_sets; ++s) {
490 extract_token(setstr, range, s, ',');
492 extract_token(lostr, setstr, 0, ':');
493 if (num_tokens(setstr, ':') >= 2) {
494 extract_token(histr, setstr, 1, ':');
495 if (!strcmp(histr, "*")) sprintf(histr, "%d", INT_MAX);
498 strcpy(histr, lostr);
503 /* Loop through the array, flipping bits where appropriate */
504 for (i = 1; i <= IMAP->num_msgs; ++i) {
505 if (is_uid) { /* fetch by sequence number */
506 if ( (IMAP->msgids[i-1]>=lo)
507 && (IMAP->msgids[i-1]<=hi)) {
509 IMAP->flags[i-1] | IMAP_FETCHED;
512 else { /* fetch by uid */
513 if ( (i>=lo) && (i<=hi)) {
515 IMAP->flags[i-1] | IMAP_FETCHED;
525 * This function is called by the main command loop.
527 void imap_fetch(int num_parms, char *parms[]) {
534 cprintf("%s BAD invalid parameters\r\n", parms[0]);
538 imap_pick_range(parms[2], 0);
541 for (i=3; i<num_parms; ++i) {
542 strcat(items, parms[i]);
543 if (i < (num_parms-1)) strcat(items, " ");
546 num_items = imap_extract_data_items(itemlist, items);
548 cprintf("%s BAD invalid data item list\r\n", parms[0]);
552 imap_do_fetch(num_items, itemlist);
553 cprintf("%s OK FETCH completed\r\n", parms[0]);
557 * This function is called by the main command loop.
559 void imap_uidfetch(int num_parms, char *parms[]) {
564 int have_uid_item = 0;
567 cprintf("%s BAD invalid parameters\r\n", parms[0]);
571 imap_pick_range(parms[3], 1);
574 for (i=4; i<num_parms; ++i) {
575 strcat(items, parms[i]);
576 if (i < (num_parms-1)) strcat(items, " ");
579 num_items = imap_extract_data_items(itemlist, items);
581 cprintf("%s BAD invalid data item list\r\n", parms[0]);
585 /* If the "UID" item was not included, we include it implicitly
586 * because this is a UID FETCH command
588 for (i=0; i<num_items; ++i) {
589 if (!strcasecmp(itemlist[i], "UID")) ++have_uid_item;
591 if (have_uid_item == 0) itemlist[num_items++] = "UID";
593 imap_do_fetch(num_items, itemlist);
594 cprintf("%s OK UID FETCH completed\r\n", parms[0]);