4 * Implements the FETCH command in IMAP.
5 * This is a good example of the protocol's gratuitous complexity.
7 * Copyright (c) 2001-2009 by the citadel.org team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #include <sys/types.h>
35 #if TIME_WITH_SYS_TIME
36 # include <sys/time.h>
40 # include <sys/time.h>
50 #include <libcitadel.h>
53 #include "sysdep_decls.h"
54 #include "citserver.h"
61 #include "internet_addressing.h"
62 #include "imap_tools.h"
63 #include "serv_imap.h"
64 #include "imap_fetch.h"
66 #include "ctdl_module.h"
71 * Individual field functions for imap_do_fetch_msg() ...
74 void imap_fetch_uid(int seq) {
75 cprintf("UID %ld", IMAP->msgids[seq-1]);
78 void imap_fetch_flags(int seq) {
79 int num_flags_printed = 0;
81 if (IMAP->flags[seq] & IMAP_DELETED) {
82 if (num_flags_printed > 0) cprintf(" ");
86 if (IMAP->flags[seq] & IMAP_SEEN) {
87 if (num_flags_printed > 0) cprintf(" ");
91 if (IMAP->flags[seq] & IMAP_ANSWERED) {
92 if (num_flags_printed > 0) cprintf(" ");
93 cprintf("\\Answered");
96 if (IMAP->flags[seq] & IMAP_RECENT) {
97 if (num_flags_printed > 0) cprintf(" ");
105 void imap_fetch_internaldate(struct CtdlMessage *msg) {
110 if (msg->cm_fields['T'] != NULL) {
111 msgdate = atol(msg->cm_fields['T']);
114 msgdate = time(NULL);
117 datestring(buf, sizeof buf, msgdate, DATESTRING_IMAP);
118 cprintf("INTERNALDATE \"%s\"", buf);
123 * Fetch RFC822-formatted messages.
125 * 'whichfmt' should be set to one of:
126 * "RFC822" entire message
127 * "RFC822.HEADER" headers only (with trailing blank line)
128 * "RFC822.SIZE" size of translated message
129 * "RFC822.TEXT" body only (without leading blank line)
131 void imap_fetch_rfc822(long msgnum, const char *whichfmt) {
133 const char *ptr = NULL;
134 size_t headers_size, text_size, total_size;
135 size_t bytes_to_send = 0;
137 int need_to_rewrite_metadata = 0;
140 /* Determine whether this particular fetch operation requires
141 * us to fetch the message body from disk. If not, we can save
142 * on some disk operations...
144 if ( (!strcasecmp(whichfmt, "RFC822"))
145 || (!strcasecmp(whichfmt, "RFC822.TEXT")) ) {
149 /* If this is an RFC822.SIZE fetch, first look in the message's
150 * metadata record to see if we've saved that information.
152 if (!strcasecmp(whichfmt, "RFC822.SIZE")) {
153 GetMetaData(&smi, msgnum);
154 if (smi.meta_rfc822_length > 0L) {
155 cprintf("RFC822.SIZE %ld", smi.meta_rfc822_length);
158 need_to_rewrite_metadata = 1;
162 /* Cache the most recent RFC822 FETCH because some clients like to
163 * fetch in pieces, and we don't want to have to go back to the
164 * message store for each piece. We also burn the cache if the
165 * client requests something that involves reading the message
166 * body, but we haven't fetched the body yet.
168 if ((IMAP->cached_rfc822_data != NULL)
169 && (IMAP->cached_rfc822_msgnum == msgnum)
170 && (IMAP->cached_rfc822_withbody || (!need_body)) ) {
173 else if (IMAP->cached_rfc822_data != NULL) {
174 /* Some other message is cached -- free it */
175 free(IMAP->cached_rfc822_data);
176 IMAP->cached_rfc822_data = NULL;
177 IMAP->cached_rfc822_msgnum = (-1);
178 IMAP->cached_rfc822_len = 0;
181 /* At this point, we now can fetch and convert the message iff it's not
182 * the one we had cached.
184 if (IMAP->cached_rfc822_data == NULL) {
186 * Load the message into memory for translation & measurement
188 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
189 CtdlOutputMsg(msgnum, MT_RFC822,
190 (need_body ? HEADERS_ALL : HEADERS_FAST),
191 0, 1, NULL, SUPPRESS_ENV_TO
193 if (!need_body) cprintf("\r\n"); /* extra trailing newline */
194 IMAP->cached_rfc822_len = StrLength(CC->redirect_buffer);
195 IMAP->cached_rfc822_data = SmashStrBuf(&CC->redirect_buffer);
196 IMAP->cached_rfc822_msgnum = msgnum;
197 IMAP->cached_rfc822_withbody = need_body;
198 if ( (need_to_rewrite_metadata) && (IMAP->cached_rfc822_len > 0) ) {
199 smi.meta_rfc822_length = (long)IMAP->cached_rfc822_len;
205 * Now figure out where the headers/text break is. IMAP considers the
206 * intervening blank line to be part of the headers, not the text.
213 ptr = IMAP->cached_rfc822_data;
215 ptr = memreadline(ptr, buf, sizeof buf);
218 if (IsEmptyStr(buf)) {
219 headers_size = ptr - IMAP->cached_rfc822_data;
222 } while ( (headers_size == 0) && (*ptr != 0) );
224 total_size = IMAP->cached_rfc822_len;
225 text_size = total_size - headers_size;
228 headers_size = IMAP->cached_rfc822_len;
229 total_size = IMAP->cached_rfc822_len;
233 CtdlLogPrintf(CTDL_DEBUG,
234 "RFC822: headers=" SIZE_T_FMT
236 ", total=" SIZE_T_FMT "\n",
237 headers_size, text_size, total_size);
239 if (!strcasecmp(whichfmt, "RFC822.SIZE")) {
240 cprintf("RFC822.SIZE " SIZE_T_FMT, total_size);
244 else if (!strcasecmp(whichfmt, "RFC822")) {
245 ptr = IMAP->cached_rfc822_data;
246 bytes_to_send = total_size;
249 else if (!strcasecmp(whichfmt, "RFC822.HEADER")) {
250 ptr = IMAP->cached_rfc822_data;
251 bytes_to_send = headers_size;
254 else if (!strcasecmp(whichfmt, "RFC822.TEXT")) {
255 ptr = &IMAP->cached_rfc822_data[headers_size];
256 bytes_to_send = text_size;
259 cprintf("%s {" SIZE_T_FMT "}\r\n", whichfmt, bytes_to_send);
260 client_write(ptr, bytes_to_send);
266 * Load a specific part of a message into the temp file to be output to a
267 * client. FIXME we can handle parts like "2" and "2.1" and even "2.MIME"
268 * but we still can't handle "2.HEADER" (which might not be a problem).
270 * Note: mime_parser() was called with dont_decode set to 1, so we have the
271 * luxury of simply spewing without having to re-encode.
273 void imap_load_part(char *name, char *filename, char *partnum, char *disp,
274 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
275 char *cbid, void *cbuserdata)
278 char *desired_section;
280 desired_section = (char *)cbuserdata;
282 if (!strcasecmp(partnum, desired_section)) {
283 client_write(content, length);
286 snprintf(mbuf2, sizeof mbuf2, "%s.MIME", partnum);
288 if (!strcasecmp(desired_section, mbuf2)) {
289 cprintf("Content-type: %s", cbtype);
290 if (!IsEmptyStr(cbcharset))
291 cprintf("; charset=\"%s\"", cbcharset);
292 if (!IsEmptyStr(name))
293 cprintf("; name=\"%s\"", name);
295 if (!IsEmptyStr(encoding))
296 cprintf("Content-Transfer-Encoding: %s\r\n", encoding);
297 if (!IsEmptyStr(encoding)) {
298 cprintf("Content-Disposition: %s", disp);
299 if (!IsEmptyStr(filename)) {
300 cprintf("; filename=\"%s\"", filename);
304 cprintf("Content-Length: %ld\r\n", (long)length);
313 * Called by imap_fetch_envelope() to output the "From" field.
314 * This is in its own function because its logic is kind of complex. We
315 * really need to make this suck less.
317 void imap_output_envelope_from(struct CtdlMessage *msg) {
318 char user[SIZ], node[SIZ], name[SIZ];
322 /* For anonymous messages, it's so easy! */
323 if (!is_room_aide() && (msg->cm_anon_type == MES_ANONONLY)) {
324 cprintf("((\"----\" NIL \"x\" \"x.org\")) ");
327 if (!is_room_aide() && (msg->cm_anon_type == MES_ANONOPT)) {
328 cprintf("((\"anonymous\" NIL \"x\" \"x.org\")) ");
332 /* For everything else, we do stuff. */
333 cprintf("(("); /* open double-parens */
334 plain_imap_strout(msg->cm_fields['A']); /* personal name */
335 cprintf(" NIL "); /* source route (not used) */
338 if (msg->cm_fields['F'] != NULL) {
339 process_rfc822_addr(msg->cm_fields['F'], user, node, name);
340 plain_imap_strout(user); /* mailbox name (user id) */
342 if (!strcasecmp(node, config.c_nodename)) {
343 plain_imap_strout(config.c_fqdn);
346 plain_imap_strout(node); /* host name */
350 plain_imap_strout(msg->cm_fields['A']); /* mailbox name (user id) */
352 plain_imap_strout(msg->cm_fields['N']); /* host name */
355 cprintf(")) "); /* close double-parens */
361 * Output an envelope address (or set of addresses) in the official,
362 * convoluted, braindead format. (Note that we can't use this for
363 * the "From" address because its data may come from a number of different
364 * fields. But we can use it for "To" and possibly others.
366 void imap_output_envelope_addr(char *addr) {
367 char individual_addr[256];
379 if (IsEmptyStr(addr)) {
386 /* How many addresses are listed here? */
387 num_addrs = num_tokens(addr, ',');
389 /* Output them one by one. */
390 for (i=0; i<num_addrs; ++i) {
391 extract_token(individual_addr, addr, i, ',', sizeof individual_addr);
392 striplt(individual_addr);
393 process_rfc822_addr(individual_addr, user, node, name);
395 plain_imap_strout(name);
397 plain_imap_strout(user);
399 plain_imap_strout(node);
401 if (i < (num_addrs-1)) cprintf(" ");
409 * Implements the ENVELOPE fetch item
411 * Note that the imap_strout() function can cleverly output NULL fields as NIL,
412 * so we don't have to check for that condition like we do elsewhere.
414 void imap_fetch_envelope(struct CtdlMessage *msg) {
415 char datestringbuf[SIZ];
417 char *fieldptr = NULL;
421 /* Parse the message date into an IMAP-format date string */
422 if (msg->cm_fields['T'] != NULL) {
423 msgdate = atol(msg->cm_fields['T']);
426 msgdate = time(NULL);
428 datestring(datestringbuf, sizeof datestringbuf,
429 msgdate, DATESTRING_IMAP);
431 /* Now start spewing data fields. The order is important, as it is
432 * defined by the protocol specification. Nonexistent fields must
433 * be output as NIL, existent fields must be quoted or literalled.
434 * The imap_strout() function conveniently does all this for us.
436 cprintf("ENVELOPE (");
439 plain_imap_strout(datestringbuf);
443 plain_imap_strout(msg->cm_fields['U']);
447 imap_output_envelope_from(msg);
449 /* Sender (default to same as 'From' if not present) */
450 fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Sender");
451 if (fieldptr != NULL) {
452 imap_output_envelope_addr(fieldptr);
456 imap_output_envelope_from(msg);
460 fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Reply-to");
461 if (fieldptr != NULL) {
462 imap_output_envelope_addr(fieldptr);
466 imap_output_envelope_from(msg);
470 imap_output_envelope_addr(msg->cm_fields['R']);
472 /* Cc (we do it this way because there might be a legacy non-Citadel Cc: field present) */
473 fieldptr = msg->cm_fields['Y'];
474 if (fieldptr != NULL) {
475 imap_output_envelope_addr(fieldptr);
478 fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Cc");
479 imap_output_envelope_addr(fieldptr);
480 if (fieldptr != NULL) free(fieldptr);
484 fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Bcc");
485 imap_output_envelope_addr(fieldptr);
486 if (fieldptr != NULL) free(fieldptr);
489 fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "In-reply-to");
490 plain_imap_strout(fieldptr);
492 if (fieldptr != NULL) free(fieldptr);
495 plain_imap_strout(msg->cm_fields['I']);
501 * This function is called only when CC->redirect_buffer contains a set of
502 * RFC822 headers with no body attached. Its job is to strip that set of
503 * headers down to *only* the ones we're interested in.
505 void imap_strip_headers(StrBuf *section) {
507 StrBuf *which_fields = NULL;
508 int doing_headers = 0;
512 StrBuf *boiled_headers = NULL;
515 int done_headers = 0;
516 const char *Ptr = NULL;
517 CitContext *CCC = CC;
519 if (CCC->redirect_buffer == NULL) return;
521 which_fields = NewStrBufDup(section);
523 if (!strncasecmp(ChrPtr(which_fields), "HEADER.FIELDS", 13))
526 !strncasecmp(ChrPtr(which_fields), "HEADER.FIELDS.NOT", 17))
529 for (i=0; i < StrLength(which_fields); ++i) {
530 if (ChrPtr(which_fields)[i]=='(')
531 StrBufReplaceToken(which_fields, i, 1, HKEY(""));
533 for (i=0; i < StrLength(which_fields); ++i) {
534 if (ChrPtr(which_fields)[i]==')') {
535 StrBufCutAt(which_fields, i, NULL);
539 memset(&Cmd, 0, sizeof(citimap_command));
540 Cmd.CmdBuf = which_fields;
541 num_parms = imap_parameterize(&Cmd);
543 boiled_headers = NewStrBufPlain(NULL, StrLength(CCC->redirect_buffer));
544 Line = NewStrBufPlain(NULL, SIZ);
548 StrBufSipLine(Line, CCC->redirect_buffer, &Ptr);
550 if (!isspace(ChrPtr(Line)[0])) {
552 if (doing_headers == 0) ok = 1;
554 /* we're supposed to print all headers that are not matching the filter list */
555 if (headers_not) for (i=0, ok = 1; (i < num_parms) && (ok == 1); ++i) {
556 if ( (!strncasecmp(ChrPtr(Line),
558 Cmd.Params[i].len)) &&
559 (ChrPtr(Line)[Cmd.Params[i].len]==':') ) {
563 /* we're supposed to print all headers matching the filterlist */
564 else for (i=0, ok = 0; ((i < num_parms) && (ok == 0)); ++i) {
565 if ( (!strncasecmp(ChrPtr(Line),
567 Cmd.Params[i].len)) &&
568 (ChrPtr(Line)[Cmd.Params[i].len]==':') ) {
576 StrBufAppendBuf(boiled_headers, Line, 0);
577 StrBufAppendBufPlain(boiled_headers, HKEY("\r\n"), 0);
580 if ((Ptr == StrBufNOTNULL) ||
581 (StrLength(Line) == 0) ||
582 (ChrPtr(Line)[0]=='\r') ||
583 (ChrPtr(Line)[0]=='\n') ) done_headers = 1;
584 } while (!done_headers);
586 StrBufAppendBufPlain(boiled_headers, HKEY("\r\n"), 0);
588 /* Now save it back (it'll always be smaller) */
589 FreeStrBuf(&CCC->redirect_buffer);
590 CCC->redirect_buffer = boiled_headers;
593 FreeStrBuf(&which_fields);
599 * Implements the BODY and BODY.PEEK fetch items
601 void imap_fetch_body(long msgnum, ConstStr item, int is_peek) {
602 struct CtdlMessage *msg = NULL;
606 size_t pstart, pbytes;
607 int loading_body_now = 0;
609 int burn_the_cache = 0;
610 CitContext *CCC = CC;
612 /* extract section */
613 section = NewStrBufPlain(CKEY(item));
615 if (strchr(ChrPtr(section), '[') != NULL) {
616 StrBufStripAllBut(section, '[', ']');
618 CtdlLogPrintf(CTDL_DEBUG, "Section is: %s%s\n",
620 (StrLength(section) == 0) ? "(empty)" : ""
623 /* Burn the cache if we don't have the same section of the
624 * same message again.
626 if (IMAP->cached_body != NULL) {
627 if (IMAP->cached_bodymsgnum != msgnum) {
630 else if ( (!IMAP->cached_body_withbody) && (need_body) ) {
633 else if (strcasecmp(IMAP->cached_bodypart, ChrPtr(section))) {
636 if (burn_the_cache) {
637 /* Yup, go ahead and burn the cache. */
638 free(IMAP->cached_body);
639 IMAP->cached_body_len = 0;
640 IMAP->cached_body = NULL;
641 IMAP->cached_bodymsgnum = (-1);
642 strcpy(IMAP->cached_bodypart, "");
646 /* extract partial */
647 partial = NewStrBufPlain(CKEY(item));
648 if (strchr(ChrPtr(partial), '<') != NULL) {
649 StrBufStripAllBut(partial, '<', '>');
653 if (StrLength(partial) > 0)
654 CtdlLogPrintf(CTDL_DEBUG,
658 if (IMAP->cached_body == NULL) {
659 CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
660 loading_body_now = 1;
661 msg = CtdlFetchMessage(msgnum, (need_body ? 1 : 0));
664 /* Now figure out what the client wants, and get it */
666 if (!loading_body_now) {
667 /* What we want is already in memory */
670 else if ( (!strcmp(ChrPtr(section), "1")) && (msg->cm_format_type != 4) ) {
671 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_NONE, 0, 1, SUPPRESS_ENV_TO);
674 else if (StrLength(section) == 0) {
675 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, SUPPRESS_ENV_TO);
679 * If the client asked for just headers, or just particular header
680 * fields, strip it down.
682 else if (!strncasecmp(ChrPtr(section), "HEADER", 6)) {
683 /* This used to work with HEADERS_FAST, but then Apple got stupid with their
684 * IMAP library and this broke Mail.App and iPhone Mail, so we had to change it
685 * to HEADERS_ONLY so the trendy hipsters with their iPhones can read mail.
687 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ONLY, 0, 1, SUPPRESS_ENV_TO);
688 imap_strip_headers(section);
692 * Strip it down if the client asked for everything _except_ headers.
694 else if (!strncasecmp(ChrPtr(section), "TEXT", 4)) {
695 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_NONE, 0, 1, SUPPRESS_ENV_TO);
699 * Anything else must be a part specifier.
700 * (Note value of 1 passed as 'dont_decode' so client gets it encoded)
703 mime_parser(msg->cm_fields['M'], NULL,
704 *imap_load_part, NULL, NULL,
709 if (loading_body_now) {
710 IMAP->cached_body_len = StrLength(CCC->redirect_buffer);
711 IMAP->cached_body = SmashStrBuf(&CCC->redirect_buffer);
712 IMAP->cached_bodymsgnum = msgnum;
713 IMAP->cached_body_withbody = need_body;
714 strcpy(IMAP->cached_bodypart, ChrPtr(section));
717 if (is_partial == 0) {
718 cprintf("BODY[%s] {" SIZE_T_FMT "}\r\n", ChrPtr(section), IMAP->cached_body_len);
720 pbytes = IMAP->cached_body_len;
723 sscanf(ChrPtr(partial), SIZE_T_FMT "." SIZE_T_FMT, &pstart, &pbytes);
724 if (pbytes > (IMAP->cached_body_len - pstart)) {
725 pbytes = IMAP->cached_body_len - pstart;
727 cprintf("BODY[%s]<" SIZE_T_FMT "> {" SIZE_T_FMT "}\r\n", ChrPtr(section), pstart, pbytes);
730 FreeStrBuf(&partial);
732 /* Here we go -- output it */
733 client_write(&IMAP->cached_body[pstart], pbytes);
736 CtdlFreeMessage(msg);
739 /* Mark this message as "seen" *unless* this is a "peek" operation */
741 CtdlSetSeen(&msgnum, 1, 1, ctdlsetseen_seen, NULL, NULL);
743 FreeStrBuf(§ion);
747 * Called immediately before outputting a multipart bodystructure
749 void imap_fetch_bodystructure_pre(
750 char *name, char *filename, char *partnum, char *disp,
751 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
752 char *cbid, void *cbuserdata
761 * Called immediately after outputting a multipart bodystructure
763 void imap_fetch_bodystructure_post(
764 char *name, char *filename, char *partnum, char *disp,
765 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
766 char *cbid, void *cbuserdata
774 extract_token(subtype, cbtype, 1, '/', sizeof subtype);
775 plain_imap_strout(subtype);
778 /* cprintf(" NIL"); We thought we needed this at one point, but maybe we don't... */
786 * Output the info for a MIME part in the format required by BODYSTRUCTURE.
789 void imap_fetch_bodystructure_part(
790 char *name, char *filename, char *partnum, char *disp,
791 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
792 char *cbid, void *cbuserdata
796 int have_encoding = 0;
799 char cbmaintype[128];
802 if (cbtype != NULL) if (!IsEmptyStr(cbtype)) have_cbtype = 1;
804 extract_token(cbmaintype, cbtype, 0, '/', sizeof cbmaintype);
805 extract_token(cbsubtype, cbtype, 1, '/', sizeof cbsubtype);
808 strcpy(cbmaintype, "TEXT");
809 strcpy(cbsubtype, "PLAIN");
813 plain_imap_strout(cbmaintype); /* body type */
815 plain_imap_strout(cbsubtype); /* body subtype */
818 cprintf("("); /* begin body parameter list */
820 /* "NAME" must appear as the first parameter. This is not required by IMAP,
821 * but the Asterisk voicemail application blindly assumes that NAME will be in
822 * the first position. If it isn't, it rejects the message.
824 if (name != NULL) if (!IsEmptyStr(name)) {
825 cprintf("\"NAME\" ");
826 plain_imap_strout(name);
830 cprintf("\"CHARSET\" ");
831 if (cbcharset == NULL) {
832 plain_imap_strout("US-ASCII");
834 else if (cbcharset[0] == 0) {
835 plain_imap_strout("US-ASCII");
838 plain_imap_strout(cbcharset);
840 cprintf(") "); /* end body parameter list */
842 cprintf("NIL "); /* Body ID */
843 cprintf("NIL "); /* Body description */
845 if (encoding != NULL) if (encoding[0] != 0) have_encoding = 1;
847 plain_imap_strout(encoding);
850 plain_imap_strout("7BIT");
854 /* The next field is the size of the part in bytes. */
855 cprintf("%ld ", (long)length); /* bytes */
857 /* The next field is the number of lines in the part, if and only
858 * if the part is TEXT. More gratuitous complexity.
860 if (!strcasecmp(cbmaintype, "TEXT")) {
861 if (length) for (i=0; i<length; ++i) {
862 if (((char *)content)[i] == '\n') ++lines;
864 cprintf("%d ", lines);
867 /* More gratuitous complexity */
868 if ((!strcasecmp(cbmaintype, "MESSAGE"))
869 && (!strcasecmp(cbsubtype, "RFC822"))) {
870 /* FIXME: message/rfc822 also needs to output the envelope structure,
871 * body structure, and line count of the encapsulated message. Fortunately
872 * there are not yet any clients depending on this, so we can get away
873 * with not implementing it for now.
877 /* MD5 value of body part; we can get away with NIL'ing this */
884 else if (IsEmptyStr(disp)) {
889 plain_imap_strout(disp);
890 if (filename != NULL) if (!IsEmptyStr(filename)) {
891 cprintf(" (\"FILENAME\" ");
892 plain_imap_strout(filename);
898 /* Body language (not defined yet) */
905 * Spew the BODYSTRUCTURE data for a message.
908 void imap_fetch_bodystructure (long msgnum, const char *item,
909 struct CtdlMessage *msg) {
910 const char *rfc822 = NULL;
911 const char *rfc822_body = NULL;
913 size_t rfc822_headers_len;
914 size_t rfc822_body_len;
915 const char *ptr = NULL;
920 /* Handle NULL message gracefully */
922 cprintf("BODYSTRUCTURE (\"TEXT\" \"PLAIN\" "
923 "(\"CHARSET\" \"US-ASCII\") NIL NIL "
928 /* For non-RFC822 (ordinary Citadel) messages, this is short and
931 if (msg->cm_format_type != FMT_RFC822) {
933 /* *sigh* We have to RFC822-format the message just to be able
934 * to measure it. FIXME use smi cached fields if possible
937 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
938 CtdlOutputPreLoadedMsg(msg, MT_RFC822, 0, 0, 1, SUPPRESS_ENV_TO);
939 rfc822_len = StrLength(CC->redirect_buffer);
940 rfc822 = pch = SmashStrBuf(&CC->redirect_buffer);
944 ptr = memreadline(ptr, buf, sizeof buf);
946 if ((IsEmptyStr(buf)) && (rfc822_body == NULL)) {
951 rfc822_headers_len = rfc822_body - rfc822;
952 rfc822_body_len = rfc822_len - rfc822_headers_len;
955 cprintf("BODYSTRUCTURE (\"TEXT\" \"PLAIN\" "
956 "(\"CHARSET\" \"US-ASCII\") NIL NIL "
957 "\"7BIT\" " SIZE_T_FMT " %d)", rfc822_body_len, lines);
962 /* For messages already stored in RFC822 format, we have to parse. */
963 cprintf("BODYSTRUCTURE ");
964 mime_parser(msg->cm_fields['M'],
966 *imap_fetch_bodystructure_part, /* part */
967 *imap_fetch_bodystructure_pre, /* pre-multi */
968 *imap_fetch_bodystructure_post, /* post-multi */
970 1); /* don't decode -- we want it as-is */
975 * imap_do_fetch() calls imap_do_fetch_msg() to output the data of an
976 * individual message, once it has been selected for output.
978 void imap_do_fetch_msg(int seq, citimap_command *Cmd) {
980 citimap *Imap = IMAP;
981 struct CtdlMessage *msg = NULL;
984 /* Don't attempt to fetch bogus messages or UID's */
986 if (Imap->msgids[seq-1] < 1L) return;
989 cprintf("* %d FETCH (", seq);
991 for (i=0; i<Cmd->num_parms; ++i) {
993 /* Fetchable without going to the message store at all */
994 if (!strcasecmp(Cmd->Params[i].Key, "UID")) {
997 else if (!strcasecmp(Cmd->Params[i].Key, "FLAGS")) {
998 imap_fetch_flags(seq-1);
1001 /* Potentially fetchable from cache, if the client requests
1002 * stuff from the same message several times in a row.
1004 else if (!strcasecmp(Cmd->Params[i].Key, "RFC822")) {
1005 imap_fetch_rfc822(Imap->msgids[seq-1], Cmd->Params[i].Key);
1007 else if (!strcasecmp(Cmd->Params[i].Key, "RFC822.HEADER")) {
1008 imap_fetch_rfc822(Imap->msgids[seq-1], Cmd->Params[i].Key);
1010 else if (!strcasecmp(Cmd->Params[i].Key, "RFC822.SIZE")) {
1011 imap_fetch_rfc822(Imap->msgids[seq-1], Cmd->Params[i].Key);
1013 else if (!strcasecmp(Cmd->Params[i].Key, "RFC822.TEXT")) {
1014 imap_fetch_rfc822(Imap->msgids[seq-1], Cmd->Params[i].Key);
1017 /* BODY fetches do their own fetching and caching too. */
1018 else if (!strncasecmp(Cmd->Params[i].Key, "BODY[", 5)) {
1019 imap_fetch_body(Imap->msgids[seq-1], Cmd->Params[i], 0);
1021 else if (!strncasecmp(Cmd->Params[i].Key, "BODY.PEEK[", 10)) {
1022 imap_fetch_body(Imap->msgids[seq-1], Cmd->Params[i], 1);
1025 /* Otherwise, load the message into memory.
1027 else if (!strcasecmp(Cmd->Params[i].Key, "BODYSTRUCTURE")) {
1028 if ((msg != NULL) && (!body_loaded)) {
1029 CtdlFreeMessage(msg); /* need the whole thing */
1033 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
1036 imap_fetch_bodystructure(Imap->msgids[seq-1],
1037 Cmd->Params[i].Key, msg);
1039 else if (!strcasecmp(Cmd->Params[i].Key, "ENVELOPE")) {
1041 msg = CtdlFetchMessage(Imap->msgids[seq-1], 0);
1044 imap_fetch_envelope(msg);
1046 else if (!strcasecmp(Cmd->Params[i].Key, "INTERNALDATE")) {
1048 msg = CtdlFetchMessage(Imap->msgids[seq-1], 0);
1051 imap_fetch_internaldate(msg);
1054 if (i != Cmd->num_parms-1) cprintf(" ");
1060 CtdlFreeMessage(msg);
1067 * imap_fetch() calls imap_do_fetch() to do its actual work, once it's
1068 * validated and boiled down the request a bit.
1070 void imap_do_fetch(citimap_command *Cmd) {
1073 /* debug output the parsed vector */
1076 CtdlLogPrintf(CTDL_DEBUG, "----- %ld params \n",
1079 for (i=0; i < Cmd->num_parms; i++) {
1080 if (Cmd->Params[i].len != strlen(Cmd->Params[i].Key))
1081 CtdlLogPrintf(CTDL_DEBUG, "*********** %ld != %ld : %s\n",
1083 strlen(Cmd->Params[i].Key),
1084 Cmd->Params[i].Key);
1086 CtdlLogPrintf(CTDL_DEBUG, "%ld : %s\n",
1088 Cmd->Params[i].Key);
1093 if (IMAP->num_msgs > 0) {
1094 for (i = 0; i < IMAP->num_msgs; ++i) {
1096 /* Abort the fetch loop if the session breaks.
1097 * This is important for users who keep mailboxes
1098 * that are too big *and* are too impatient to
1099 * let them finish loading. :)
1101 if (CC->kill_me) return;
1103 /* Get any message marked for fetch. */
1104 if (IMAP->flags[i] & IMAP_SELECTED) {
1105 imap_do_fetch_msg(i+1, Cmd);
1114 * Back end for imap_handle_macros()
1115 * Note that this function *only* looks at the beginning of the string. It
1116 * is not a generic search-and-replace function.
1118 void imap_macro_replace(StrBuf *Buf, long where,
1120 char *find, long findlen,
1121 char *replace, long replacelen)
1124 if (StrLength(Buf) - where > findlen)
1127 if (!strncasecmp(ChrPtr(Buf) + where, find, findlen)) {
1128 if (ChrPtr(Buf)[where + findlen] == ' ') {
1129 StrBufPlain(TmpBuf, replace, replacelen);
1130 StrBufAppendBufPlain(TmpBuf, HKEY(" "), 0);
1131 StrBufReplaceToken(Buf, where, findlen,
1134 if (where + findlen == StrLength(Buf)) {
1135 StrBufReplaceToken(Buf, where, findlen,
1136 replace, replacelen);
1144 * Handle macros embedded in FETCH data items.
1145 * (What the heck are macros doing in a wire protocol? Are we trying to save
1146 * the computer at the other end the trouble of typing a lot of characters?)
1148 void imap_handle_macros(citimap_command *Cmd) {
1151 StrBuf *Tmp = NewStrBuf();
1153 for (i=0; i < StrLength(Cmd->CmdBuf); ++i) {
1154 char ch = ChrPtr(Cmd->CmdBuf)[i];
1159 else if ((ch==')') ||
1165 imap_macro_replace(Cmd->CmdBuf, i,
1168 HKEY("FLAGS INTERNALDATE RFC822.SIZE ENVELOPE")
1170 imap_macro_replace(Cmd->CmdBuf, i,
1173 HKEY("BODYSTRUCTURE")
1175 imap_macro_replace(Cmd->CmdBuf, i,
1178 HKEY("FLAGS INTERNALDATE RFC822.SIZE")
1180 imap_macro_replace(Cmd->CmdBuf, i,
1183 HKEY("FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODY")
1192 * Break out the data items requested, possibly a parenthesized list.
1193 * Returns the number of data items, or -1 if the list is invalid.
1194 * NOTE: this function alters the string it is fed, and uses it as a buffer
1195 * to hold the data for the pointers it returns.
1197 int imap_extract_data_items(citimap_command *Cmd)
1201 const char *pch, *end;
1204 /* Convert all whitespace to ordinary space characters. */
1205 pch = ChrPtr(Cmd->CmdBuf);
1206 end = pch + StrLength(Cmd->CmdBuf);
1211 StrBufPeek(Cmd->CmdBuf, pch, 0, ' ');
1215 /* Strip leading and trailing whitespace, then strip leading and
1216 * trailing parentheses if it's a list
1218 StrBufTrim(Cmd->CmdBuf);
1219 pch = ChrPtr(Cmd->CmdBuf);
1220 if ( (pch[0]=='(') &&
1221 (pch[StrLength(Cmd->CmdBuf)-1]==')') )
1223 StrBufCutRight(Cmd->CmdBuf, 1);
1224 StrBufCutLeft(Cmd->CmdBuf, 1);
1225 StrBufTrim(Cmd->CmdBuf);
1228 /* Parse any macro data items */
1229 imap_handle_macros(Cmd);
1232 * Now break out the data items. We throw in one trailing space in
1233 * order to avoid having to break out the last one manually.
1235 nArgs = StrLength(Cmd->CmdBuf) / 10 + 10;
1236 nArgs = CmdAdjust(Cmd, nArgs, 0);
1237 initial_len = StrLength(Cmd->CmdBuf);
1239 Cmd->Params[Cmd->num_parms].Key = pch = ChrPtr(Cmd->CmdBuf);
1240 end = Cmd->Params[Cmd->num_parms].Key + StrLength(Cmd->CmdBuf);
1250 else if ((*pch==')') ||
1256 if ((nest <= 0) && (*pch==' ')) {
1257 StrBufPeek(Cmd->CmdBuf, pch, 0, '\0');
1258 Cmd->Params[Cmd->num_parms].len =
1259 pch - Cmd->Params[Cmd->num_parms].Key;
1261 if (Cmd->num_parms + 1 >= Cmd->avail_parms) {
1262 nArgs = CmdAdjust(Cmd, nArgs * 2, 1);
1265 Cmd->Params[Cmd->num_parms].Key = ++pch;
1267 else if (pch + 1 == end) {
1268 Cmd->Params[Cmd->num_parms].len =
1269 pch - Cmd->Params[Cmd->num_parms].Key + 1;
1275 return Cmd->num_parms;
1281 * One particularly hideous aspect of IMAP is that we have to allow the client
1282 * to specify arbitrary ranges and/or sets of messages to fetch. Citadel IMAP
1283 * handles this by setting the IMAP_SELECTED flag for each message specified in
1284 * the ranges/sets, then looping through the message array, outputting messages
1285 * with the flag set. We don't bother returning an error if an out-of-range
1286 * number is specified (we just return quietly) because any client braindead
1287 * enough to request a bogus message number isn't going to notice the
1288 * difference anyway.
1290 * This function clears out the IMAP_SELECTED bits, then sets that bit for each
1291 * message included in the specified range.
1293 * Set is_uid to 1 to fetch by UID instead of sequence number.
1295 void imap_pick_range(const char *supplied_range, int is_uid) {
1299 char setstr[SIZ], lostr[SIZ], histr[SIZ];
1301 char actual_range[SIZ];
1305 * Handle the "ALL" macro
1307 if (!strcasecmp(supplied_range, "ALL")) {
1308 safestrncpy(actual_range, "1:*", sizeof actual_range);
1311 safestrncpy(actual_range, supplied_range, sizeof actual_range);
1316 * Clear out the IMAP_SELECTED flags for all messages.
1318 for (i = 0; i < Imap->num_msgs; ++i) {
1319 Imap->flags[i] = Imap->flags[i] & ~IMAP_SELECTED;
1323 * Now set it for all specified messages.
1325 num_sets = num_tokens(actual_range, ',');
1326 for (s=0; s<num_sets; ++s) {
1327 extract_token(setstr, actual_range, s, ',', sizeof setstr);
1329 extract_token(lostr, setstr, 0, ':', sizeof lostr);
1330 if (num_tokens(setstr, ':') >= 2) {
1331 extract_token(histr, setstr, 1, ':', sizeof histr);
1332 if (!strcmp(histr, "*")) snprintf(histr, sizeof histr, "%ld", LONG_MAX);
1335 safestrncpy(histr, lostr, sizeof histr);
1340 /* Loop through the array, flipping bits where appropriate */
1341 for (i = 1; i <= Imap->num_msgs; ++i) {
1342 if (is_uid) { /* fetch by sequence number */
1343 if ( (Imap->msgids[i-1]>=lo)
1344 && (Imap->msgids[i-1]<=hi)) {
1345 Imap->flags[i-1] |= IMAP_SELECTED;
1348 else { /* fetch by uid */
1349 if ( (i>=lo) && (i<=hi)) {
1350 Imap->flags[i-1] |= IMAP_SELECTED;
1360 * This function is called by the main command loop.
1362 void imap_fetch(int num_parms, ConstStr *Params) {
1363 citimap_command Cmd;
1366 if (num_parms < 4) {
1367 cprintf("%s BAD invalid parameters\r\n", Params[0].Key);
1371 imap_pick_range(Params[2].Key, 0);
1373 memset(&Cmd, 0, sizeof(citimap_command));
1374 Cmd.CmdBuf = NewStrBufPlain(NULL, StrLength(IMAP->Cmd.CmdBuf));
1375 MakeStringOf(Cmd.CmdBuf, 3);
1377 num_items = imap_extract_data_items(&Cmd);
1378 if (num_items < 1) {
1379 cprintf("%s BAD invalid data item list\r\n", Params[0].Key);
1380 FreeStrBuf(&Cmd.CmdBuf);
1385 imap_do_fetch(&Cmd);
1386 cprintf("%s OK FETCH completed\r\n", Params[0].Key);
1387 FreeStrBuf(&Cmd.CmdBuf);
1392 * This function is called by the main command loop.
1394 void imap_uidfetch(int num_parms, ConstStr *Params) {
1395 citimap_command Cmd;
1398 int have_uid_item = 0;
1400 if (num_parms < 5) {
1401 cprintf("%s BAD invalid parameters\r\n", Params[0].Key);
1405 imap_pick_range(Params[3].Key, 1);
1407 memset(&Cmd, 0, sizeof(citimap_command));
1408 Cmd.CmdBuf = NewStrBufPlain(NULL, StrLength(IMAP->Cmd.CmdBuf));
1410 MakeStringOf(Cmd.CmdBuf, 4);
1412 CtdlLogPrintf(CTDL_DEBUG, "-------%s--------\n", ChrPtr(Cmd.CmdBuf));
1414 num_items = imap_extract_data_items(&Cmd);
1415 if (num_items < 1) {
1416 cprintf("%s BAD invalid data item list\r\n", Params[0].Key);
1417 FreeStrBuf(&Cmd.CmdBuf);
1422 /* If the "UID" item was not included, we include it implicitly
1423 * (at the beginning) because this is a UID FETCH command
1425 for (i=0; i<num_items; ++i) {
1426 if (!strcasecmp(Cmd.Params[i].Key, "UID")) ++have_uid_item;
1428 if (have_uid_item == 0) {
1429 if (Cmd.num_parms + 1 >= Cmd.avail_parms)
1430 CmdAdjust(&Cmd, Cmd.avail_parms + 1, 1);
1431 memmove(&Cmd.Params[1],
1433 sizeof(ConstStr) * Cmd.num_parms);
1436 Cmd.Params[0] = (ConstStr){HKEY("UID")};
1439 imap_do_fetch(&Cmd);
1440 cprintf("%s OK UID FETCH completed\r\n", Params[0].Key);
1441 FreeStrBuf(&Cmd.CmdBuf);