2 * Implements the FETCH command in IMAP.
3 * This is a good example of the protocol's gratuitous complexity.
5 * Copyright (c) 2001-2020 by the citadel.org team
7 * This program is open source software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
19 #include "../../sysdep.h"
27 #include <sys/types.h>
33 #include <libcitadel.h>
34 #include "../../citadel.h"
35 #include "../../server.h"
36 #include "../../sysdep_decls.h"
37 #include "../../citserver.h"
38 #include "../../support.h"
39 #include "../../config.h"
40 #include "../../user_ops.h"
41 #include "../../database.h"
42 #include "../../msgbase.h"
43 #include "../../internet_addressing.h"
44 #include "serv_imap.h"
45 #include "imap_tools.h"
46 #include "imap_fetch.h"
47 #include "../../genstamp.h"
48 #include "../../ctdl_module.h"
53 * Individual field functions for imap_do_fetch_msg() ...
56 void imap_fetch_uid(int seq) {
57 IAPrintf("UID %ld", IMAP->msgids[seq-1]);
60 void imap_fetch_flags(int seq)
63 int num_flags_printed = 0;
65 if (Imap->flags[seq] & IMAP_DELETED) {
66 if (num_flags_printed > 0)
71 if (Imap->flags[seq] & IMAP_SEEN) {
72 if (num_flags_printed > 0)
77 if (Imap->flags[seq] & IMAP_ANSWERED) {
78 if (num_flags_printed > 0)
83 if (Imap->flags[seq] & IMAP_RECENT) {
84 if (num_flags_printed > 0)
93 void imap_fetch_internaldate(struct CtdlMessage *msg) {
98 if (!CM_IsEmpty(msg, eTimestamp)) {
99 msgdate = atol(msg->cm_fields[eTimestamp]);
102 msgdate = time(NULL);
105 datestring(datebuf, sizeof datebuf, msgdate, DATESTRING_IMAP);
106 IAPrintf( "INTERNALDATE \"%s\"", datebuf);
111 * Fetch RFC822-formatted messages.
113 * 'whichfmt' should be set to one of:
114 * "RFC822" entire message
115 * "RFC822.HEADER" headers only (with trailing blank line)
116 * "RFC822.SIZE" size of translated message
117 * "RFC822.TEXT" body only (without leading blank line)
119 void imap_fetch_rfc822(long msgnum, const char *whichfmt) {
120 CitContext *CCC = CC;
121 citimap *Imap = CCCIMAP;
122 const char *ptr = NULL;
123 size_t headers_size, text_size, total_size;
124 size_t bytes_to_send = 0;
126 int need_to_rewrite_metadata = 0;
129 /* Determine whether this particular fetch operation requires
130 * us to fetch the message body from disk. If not, we can save
131 * on some disk operations...
133 if ( (!strcasecmp(whichfmt, "RFC822")) || (!strcasecmp(whichfmt, "RFC822.TEXT")) ) {
137 /* If this is an RFC822.SIZE fetch, first look in the message's
138 * metadata record to see if we've saved that information.
140 if (!strcasecmp(whichfmt, "RFC822.SIZE")) {
141 GetMetaData(&smi, msgnum);
142 if (smi.meta_rfc822_length > 0L) {
143 IAPrintf("RFC822.SIZE %ld", smi.meta_rfc822_length);
146 need_to_rewrite_metadata = 1;
150 /* Cache the most recent RFC822 FETCH because some clients like to
151 * fetch in pieces, and we don't want to have to go back to the
152 * message store for each piece. We also burn the cache if the
153 * client requests something that involves reading the message
154 * body, but we haven't fetched the body yet.
156 if ((Imap->cached_rfc822 != NULL)
157 && (Imap->cached_rfc822_msgnum == msgnum)
158 && (Imap->cached_rfc822_withbody || (!need_body)) ) {
161 else if (Imap->cached_rfc822 != NULL) {
162 /* Some other message is cached -- free it */
163 FreeStrBuf(&Imap->cached_rfc822);
164 Imap->cached_rfc822_msgnum = (-1);
167 /* At this point, we now can fetch and convert the message iff it's not
168 * the one we had cached.
170 if (Imap->cached_rfc822 == NULL) {
172 * Load the message into memory for translation & measurement
174 CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
175 CtdlOutputMsg(msgnum, MT_RFC822,
176 (need_body ? HEADERS_ALL : HEADERS_FAST),
177 0, 1, NULL, SUPPRESS_ENV_TO, NULL, NULL, NULL
180 client_write(HKEY("\r\n")); // extra trailing newline -- to the redirect_buffer, *not* to the client
182 Imap->cached_rfc822 = CCC->redirect_buffer;
183 CCC->redirect_buffer = NULL;
184 Imap->cached_rfc822_msgnum = msgnum;
185 Imap->cached_rfc822_withbody = need_body;
186 if ( (need_to_rewrite_metadata) &&
187 (StrLength(Imap->cached_rfc822) > 0) ) {
188 smi.meta_rfc822_length = StrLength(Imap->cached_rfc822);
194 * Now figure out where the headers/text break is. IMAP considers the
195 * intervening blank line to be part of the headers, not the text.
200 StrBuf *Line = NewStrBuf();
203 StrBufSipLine(Line, Imap->cached_rfc822, &ptr);
205 if ((StrLength(Line) != 0) && (ptr != StrBufNOTNULL))
208 if ((StrLength(Line) != 0) &&
209 (ptr != StrBufNOTNULL) )
211 headers_size = ptr - ChrPtr(Imap->cached_rfc822);
214 } while ( (headers_size == 0) &&
215 (ptr != StrBufNOTNULL) );
217 total_size = StrLength(Imap->cached_rfc822);
218 text_size = total_size - headers_size;
222 headers_size = total_size = StrLength(Imap->cached_rfc822);
226 syslog(LOG_DEBUG, "imap: RFC822 headers=" SIZE_T_FMT ", text=" SIZE_T_FMT ", total=" SIZE_T_FMT, headers_size, text_size, total_size);
228 if (!strcasecmp(whichfmt, "RFC822.SIZE")) {
229 IAPrintf("RFC822.SIZE " SIZE_T_FMT, total_size);
233 else if (!strcasecmp(whichfmt, "RFC822")) {
234 ptr = ChrPtr(Imap->cached_rfc822);
235 bytes_to_send = total_size;
238 else if (!strcasecmp(whichfmt, "RFC822.HEADER")) {
239 ptr = ChrPtr(Imap->cached_rfc822);
240 bytes_to_send = headers_size;
243 else if (!strcasecmp(whichfmt, "RFC822.TEXT")) {
244 ptr = &ChrPtr(Imap->cached_rfc822)[headers_size];
245 bytes_to_send = text_size;
248 IAPrintf("%s {" SIZE_T_FMT "}\r\n", whichfmt, bytes_to_send);
249 iaputs(ptr, bytes_to_send);
254 * Load a specific part of a message into the temp file to be output to a
255 * client. FIXME we can handle parts like "2" and "2.1" and even "2.MIME"
256 * but we still can't handle "2.HEADER" (which might not be a problem).
258 * Note: mime_parser() was called with dont_decode set to 1, so we have the
259 * luxury of simply spewing without having to re-encode.
261 void imap_load_part(char *name, char *filename, char *partnum, char *disp,
262 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
263 char *cbid, void *cbuserdata)
266 StrBuf *desired_section;
268 desired_section = (StrBuf *)cbuserdata;
269 syslog(LOG_DEBUG, "imap: imap_load_part() looking for %s, found %s", ChrPtr(desired_section), partnum);
271 if (!strcasecmp(partnum, ChrPtr(desired_section))) {
272 client_write(content, length);
275 snprintf(mimebuf2, sizeof mimebuf2, "%s.MIME", partnum);
277 if (!strcasecmp(ChrPtr(desired_section), mimebuf2)) {
278 client_write(HKEY("Content-type: "));
279 client_write(cbtype, strlen(cbtype));
280 if (!IsEmptyStr(cbcharset)) {
281 client_write(HKEY("; charset=\""));
282 client_write(cbcharset, strlen(cbcharset));
283 client_write(HKEY("\""));
285 if (!IsEmptyStr(name)) {
286 client_write(HKEY("; name=\""));
287 client_write(name, strlen(name));
288 client_write(HKEY("\""));
290 client_write(HKEY("\r\n"));
291 if (!IsEmptyStr(encoding)) {
292 client_write(HKEY("Content-Transfer-Encoding: "));
293 client_write(encoding, strlen(encoding));
294 client_write(HKEY("\r\n"));
296 if (!IsEmptyStr(encoding)) {
297 client_write(HKEY("Content-Disposition: "));
298 client_write(disp, strlen(disp));
300 if (!IsEmptyStr(filename)) {
301 client_write(HKEY("; filename=\""));
302 client_write(filename, strlen(filename));
303 client_write(HKEY("\""));
305 client_write(HKEY("\r\n"));
307 cprintf("Content-Length: %ld\r\n\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 IAPuts("((\"----\" NIL \"x\" \"x.org\")) ");
327 if (!is_room_aide() && (msg->cm_anon_type == MES_ANONOPT)) {
328 IAPuts("((\"anonymous\" NIL \"x\" \"x.org\")) ");
332 /* For everything else, we do stuff. */
333 IAPuts("(("); // open double-parens
334 IPutMsgField(eAuthor); // display name
335 IAPuts(" NIL "); // source route (not used)
337 if (!CM_IsEmpty(msg, erFc822Addr)) {
338 process_rfc822_addr(msg->cm_fields[erFc822Addr], user, node, name);
339 IPutStr(user, strlen(user)); /* mailbox name (user id) */
341 IPutStr(node, strlen(node)); /* host name */
344 IPutMsgField(eAuthor); /* Make up a synthetic address */
346 IPutStr(CtdlGetConfigStr("c_fqdn"), strlen(CtdlGetConfigStr("c_fqdn")));
349 IAPuts(")) "); /* close double-parens */
354 * Output an envelope address (or set of addresses) in the official,
355 * convoluted, braindead format. (Note that we can't use this for
356 * the "From" address because its data may come from a number of different
357 * fields. But we can use it for "To" and possibly others.
359 void imap_output_envelope_addr(char *addr) {
360 char individual_addr[256];
372 if (IsEmptyStr(addr)) {
379 /* How many addresses are listed here? */
380 num_addrs = num_tokens(addr, ',');
382 /* Output them one by one. */
383 for (i=0; i<num_addrs; ++i) {
384 extract_token(individual_addr, addr, i, ',', sizeof individual_addr);
385 string_trim(individual_addr);
386 process_rfc822_addr(individual_addr, user, node, name);
388 IPutStr(name, strlen(name));
390 IPutStr(user, strlen(user));
392 IPutStr(node, strlen(node));
394 if (i < (num_addrs-1))
403 * Implements the ENVELOPE fetch item
405 * Note that the imap_strout() function can cleverly output NULL fields as NIL,
406 * so we don't have to check for that condition like we do elsewhere.
408 void imap_fetch_envelope(struct CtdlMessage *msg) {
409 char datestringbuf[SIZ];
411 char *fieldptr = NULL;
416 /* Parse the message date into an IMAP-format date string */
417 if (!CM_IsEmpty(msg, eTimestamp)) {
418 msgdate = atol(msg->cm_fields[eTimestamp]);
421 msgdate = time(NULL);
423 len = datestring(datestringbuf, sizeof datestringbuf, msgdate, DATESTRING_IMAP);
425 /* Now start spewing data fields. The order is important, as it is
426 * defined by the protocol specification. Nonexistent fields must
427 * be output as NIL, existent fields must be quoted or literalled.
428 * The imap_strout() function conveniently does all this for us.
430 IAPuts("ENVELOPE (");
433 IPutStr(datestringbuf, len);
437 IPutMsgField(eMsgSubject);
441 imap_output_envelope_from(msg);
443 /* Sender (default to same as 'From' if not present) */
444 fieldptr = rfc822_fetch_field(msg->cm_fields[eMesageText], "Sender");
445 if (fieldptr != NULL) {
446 imap_output_envelope_addr(fieldptr);
450 imap_output_envelope_from(msg);
454 fieldptr = rfc822_fetch_field(msg->cm_fields[eMesageText], "Reply-to");
455 if (fieldptr != NULL) {
456 imap_output_envelope_addr(fieldptr);
460 imap_output_envelope_from(msg);
464 imap_output_envelope_addr(msg->cm_fields[eRecipient]);
466 /* Cc (we do it this way because there might be a legacy non-Citadel Cc: field present) */
467 fieldptr = msg->cm_fields[eCarbonCopY];
468 if (fieldptr != NULL) {
469 imap_output_envelope_addr(fieldptr);
472 fieldptr = rfc822_fetch_field(msg->cm_fields[eMesageText], "Cc");
473 imap_output_envelope_addr(fieldptr);
474 if (fieldptr != NULL) free(fieldptr);
478 fieldptr = rfc822_fetch_field(msg->cm_fields[eMesageText], "Bcc");
479 imap_output_envelope_addr(fieldptr);
480 if (fieldptr != NULL) free(fieldptr);
483 fieldptr = rfc822_fetch_field(msg->cm_fields[eMesageText], "In-reply-to");
484 IPutStr(fieldptr, (fieldptr)?strlen(fieldptr):0);
486 if (fieldptr != NULL) free(fieldptr);
489 len = msg->cm_lengths[emessageId];
492 (msg->cm_fields[emessageId][0] == '<') &&
493 (msg->cm_fields[emessageId][len - 1] == '>'))
495 IPutMsgField(emessageId);
499 char *Buf = malloc(len + 3);
502 if (msg->cm_fields[emessageId][0] != '<')
507 memcpy(&Buf[pos], msg->cm_fields[emessageId], len);
509 if (msg->cm_fields[emessageId][len] != '>')
523 * This function is called only when CC->redirect_buffer contains a set of
524 * RFC822 headers with no body attached. Its job is to strip that set of
525 * headers down to *only* the ones we're interested in.
527 void imap_strip_headers(StrBuf *section) {
529 StrBuf *which_fields = NULL;
530 int doing_headers = 0;
534 StrBuf *boiled_headers = NULL;
537 int done_headers = 0;
538 const char *Ptr = NULL;
539 CitContext *CCC = CC;
541 if (CCC->redirect_buffer == NULL) return;
543 which_fields = NewStrBufDup(section);
545 if (!strncasecmp(ChrPtr(which_fields), "HEADER.FIELDS", 13))
548 !strncasecmp(ChrPtr(which_fields), "HEADER.FIELDS.NOT", 17))
551 for (i=0; i < StrLength(which_fields); ++i) {
552 if (ChrPtr(which_fields)[i]=='(')
553 StrBufReplaceToken(which_fields, i, 1, HKEY(""));
555 for (i=0; i < StrLength(which_fields); ++i) {
556 if (ChrPtr(which_fields)[i]==')') {
557 StrBufCutAt(which_fields, i, NULL);
561 memset(&Cmd, 0, sizeof(citimap_command));
562 Cmd.CmdBuf = which_fields;
563 num_parms = imap_parameterize(&Cmd);
565 boiled_headers = NewStrBufPlain(NULL, StrLength(CCC->redirect_buffer));
566 Line = NewStrBufPlain(NULL, SIZ);
570 StrBufSipLine(Line, CCC->redirect_buffer, &Ptr);
572 if (!isspace(ChrPtr(Line)[0])) {
574 if (doing_headers == 0) ok = 1;
576 /* we're supposed to print all headers that are not matching the filter list */
577 if (headers_not) for (i=0, ok = 1; (i < num_parms) && (ok == 1); ++i) {
578 if ( (!strncasecmp(ChrPtr(Line),
580 Cmd.Params[i].len)) &&
581 (ChrPtr(Line)[Cmd.Params[i].len]==':') ) {
585 /* we're supposed to print all headers matching the filterlist */
586 else for (i=0, ok = 0; ((i < num_parms) && (ok == 0)); ++i) {
587 if ( (!strncasecmp(ChrPtr(Line),
589 Cmd.Params[i].len)) &&
590 (ChrPtr(Line)[Cmd.Params[i].len]==':') ) {
598 StrBufAppendBuf(boiled_headers, Line, 0);
599 StrBufAppendBufPlain(boiled_headers, HKEY("\r\n"), 0);
602 if ((Ptr == StrBufNOTNULL) ||
603 (StrLength(Line) == 0) ||
604 (ChrPtr(Line)[0]=='\r') ||
605 (ChrPtr(Line)[0]=='\n') ) done_headers = 1;
606 } while (!done_headers);
608 StrBufAppendBufPlain(boiled_headers, HKEY("\r\n"), 0);
610 /* Now save it back (it'll always be smaller) */
611 FreeStrBuf(&CCC->redirect_buffer);
612 CCC->redirect_buffer = boiled_headers;
615 FreeStrBuf(&which_fields);
621 * Implements the BODY and BODY.PEEK fetch items
623 void imap_fetch_body(long msgnum, ConstStr item, int is_peek) {
624 struct CtdlMessage *msg = NULL;
628 size_t pstart, pbytes;
629 int loading_body_now = 0;
631 int burn_the_cache = 0;
632 CitContext *CCC = CC;
633 citimap *Imap = CCCIMAP;
635 /* extract section */
636 section = NewStrBufPlain(CKEY(item));
638 if (strchr(ChrPtr(section), '[') != NULL) {
639 StrBufStripAllBut(section, '[', ']');
641 syslog(LOG_DEBUG, "imap: selected section is [%s]", (StrLength(section) == 0) ? "(empty)" : ChrPtr(section));
643 /* Burn the cache if we don't have the same section of the
644 * same message again.
646 if (Imap->cached_body != NULL) {
647 if (Imap->cached_bodymsgnum != msgnum) {
650 else if ( (!Imap->cached_body_withbody) && (need_body) ) {
653 else if (strcasecmp(Imap->cached_bodypart, ChrPtr(section))) {
656 if (burn_the_cache) {
657 /* Yup, go ahead and burn the cache. */
658 free(Imap->cached_body);
659 Imap->cached_body_len = 0;
660 Imap->cached_body = NULL;
661 Imap->cached_bodymsgnum = (-1);
662 strcpy(Imap->cached_bodypart, "");
666 /* extract partial */
667 partial = NewStrBufPlain(CKEY(item));
668 if (strchr(ChrPtr(partial), '<') != NULL) {
669 StrBufStripAllBut(partial, '<', '>');
672 if ( (is_partial == 1) && (StrLength(partial) > 0) ) {
673 syslog(LOG_DEBUG, "imap: selected partial is <%s>", ChrPtr(partial));
676 if (Imap->cached_body == NULL) {
677 CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
678 loading_body_now = 1;
679 msg = CtdlFetchMessage(msgnum, (need_body ? 1 : 0));
682 /* Now figure out what the client wants, and get it */
684 if (!loading_body_now) {
685 /* What we want is already in memory */
688 else if ( (!strcmp(ChrPtr(section), "1")) && (msg->cm_format_type != 4) ) {
689 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_NONE, 0, 1, SUPPRESS_ENV_TO);
692 else if (StrLength(section) == 0) {
693 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, SUPPRESS_ENV_TO);
697 * If the client asked for just headers, or just particular header
698 * fields, strip it down.
700 else if (!strncasecmp(ChrPtr(section), "HEADER", 6)) {
701 /* This used to work with HEADERS_FAST, but then Apple got stupid with their
702 * IMAP library and this broke Mail.App and iPhone Mail, so we had to change it
703 * to HEADERS_ONLY so the trendy hipsters with their iPhones can read mail.
705 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ONLY, 0, 1, SUPPRESS_ENV_TO);
706 imap_strip_headers(section);
710 * Strip it down if the client asked for everything _except_ headers.
712 else if (!strncasecmp(ChrPtr(section), "TEXT", 4)) {
713 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_NONE, 0, 1, SUPPRESS_ENV_TO);
717 * Anything else must be a part specifier.
718 * (Note value of 1 passed as 'dont_decode' so client gets it encoded)
721 mime_parser(CM_RANGE(msg, eMesageText),
722 *imap_load_part, NULL, NULL,
728 if (loading_body_now) {
729 Imap->cached_body_len = StrLength(CCC->redirect_buffer);
730 Imap->cached_body = SmashStrBuf(&CCC->redirect_buffer);
731 Imap->cached_bodymsgnum = msgnum;
732 Imap->cached_body_withbody = need_body;
733 strcpy(Imap->cached_bodypart, ChrPtr(section));
736 if (is_partial == 0) {
738 iaputs(SKEY(section));
739 IAPrintf("] {" SIZE_T_FMT "}\r\n", Imap->cached_body_len);
741 pbytes = Imap->cached_body_len;
744 sscanf(ChrPtr(partial), SIZE_T_FMT "." SIZE_T_FMT, &pstart, &pbytes);
745 if (pbytes > (Imap->cached_body_len - pstart)) {
746 pbytes = Imap->cached_body_len - pstart;
749 iaputs(SKEY(section));
750 IAPrintf("]<" SIZE_T_FMT "> {" SIZE_T_FMT "}\r\n", pstart, pbytes);
753 FreeStrBuf(&partial);
755 /* Here we go -- output it */
756 iaputs(&Imap->cached_body[pstart], pbytes);
762 /* Mark this message as "seen" *unless* this is a "peek" operation */
764 CtdlSetSeen(&msgnum, 1, 1, ctdlsetseen_seen, NULL, NULL);
766 FreeStrBuf(§ion);
770 * Called immediately before outputting a multipart bodystructure
772 void imap_fetch_bodystructure_pre(
773 char *name, char *filename, char *partnum, char *disp,
774 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
775 char *cbid, void *cbuserdata
784 * Called immediately after outputting a multipart bodystructure
786 void imap_fetch_bodystructure_post(
787 char *name, char *filename, char *partnum, char *disp,
788 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
789 char *cbid, void *cbuserdata
797 len = extract_token(subtype, cbtype, 1, '/', sizeof subtype);
798 IPutStr(subtype, len);
801 /* IAPuts(" NIL"); We thought we needed this at one point, but maybe we don't... */
809 * Output the info for a MIME part in the format required by BODYSTRUCTURE.
812 void imap_fetch_bodystructure_part(
813 char *name, char *filename, char *partnum, char *disp,
814 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
815 char *cbid, void *cbuserdata
819 int have_encoding = 0;
822 char cbmaintype[128];
827 if (cbtype != NULL) if (!IsEmptyStr(cbtype)) have_cbtype = 1;
829 cbmaintype_len = extract_token(cbmaintype, cbtype, 0, '/', sizeof cbmaintype);
830 cbsubtype_len = extract_token(cbsubtype, cbtype, 1, '/', sizeof cbsubtype);
833 strcpy(cbmaintype, "TEXT");
835 strcpy(cbsubtype, "PLAIN");
840 IPutStr(cbmaintype, cbmaintype_len); /* body type */
842 IPutStr(cbsubtype, cbsubtype_len); /* body subtype */
845 IAPuts("("); /* begin body parameter list */
847 /* "NAME" must appear as the first parameter. This is not required by IMAP,
848 * but the Asterisk voicemail application blindly assumes that NAME will be in
849 * the first position. If it isn't, it rejects the message.
851 if ((name != NULL) && (!IsEmptyStr(name))) {
853 IPutStr(name, strlen(name));
857 IAPuts("\"CHARSET\" ");
858 if ((cbcharset == NULL) || (cbcharset[0] == 0)){
859 IPutStr(HKEY("US-ASCII"));
862 IPutStr(cbcharset, strlen(cbcharset));
864 IAPuts(") "); /* end body parameter list */
866 IAPuts("NIL "); /* Body ID */
867 IAPuts("NIL "); /* Body description */
869 if ((encoding != NULL) && (encoding[0] != 0)) have_encoding = 1;
871 IPutStr(encoding, strlen(encoding));
874 IPutStr(HKEY("7BIT"));
878 /* The next field is the size of the part in bytes. */
879 IAPrintf("%ld ", (long)length); /* bytes */
881 /* The next field is the number of lines in the part, if and only
882 * if the part is TEXT. More gratuitous complexity.
884 if (!strcasecmp(cbmaintype, "TEXT")) {
885 if (length) for (i=0; i<length; ++i) {
886 if (((char *)content)[i] == '\n') ++lines;
888 IAPrintf("%d ", lines);
891 /* More gratuitous complexity */
892 if ((!strcasecmp(cbmaintype, "MESSAGE"))
893 && (!strcasecmp(cbsubtype, "RFC822"))) {
894 /* FIXME: message/rfc822 also needs to output the envelope structure,
895 * body structure, and line count of the encapsulated message. Fortunately
896 * there are not yet any clients depending on this, so we can get away
897 * with not implementing it for now.
901 /* MD5 value of body part; we can get away with NIL'ing this */
905 if ((disp == NULL) || IsEmptyStr(disp)) {
910 IPutStr(disp, strlen(disp));
911 if ((filename != NULL) && (!IsEmptyStr(filename))) {
912 IAPuts(" (\"FILENAME\" ");
913 IPutStr(filename, strlen(filename));
919 /* Body language (not defined yet) */
926 * Spew the BODYSTRUCTURE data for a message.
929 void imap_fetch_bodystructure (long msgnum, const char *item,
930 struct CtdlMessage *msg) {
931 const char *rfc822 = NULL;
932 const char *rfc822_body = NULL;
934 size_t rfc822_headers_len;
935 size_t rfc822_body_len;
936 const char *ptr = NULL;
941 /* Handle NULL message gracefully */
943 IAPuts("BODYSTRUCTURE (\"TEXT\" \"PLAIN\" "
944 "(\"CHARSET\" \"US-ASCII\") NIL NIL "
949 /* For non-RFC822 (ordinary Citadel) messages, this is short and
952 if (msg->cm_format_type != FMT_RFC822) {
954 /* *sigh* We have to RFC822-format the message just to be able
955 * to measure it. FIXME use smi cached fields if possible
958 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
959 CtdlOutputPreLoadedMsg(msg, MT_RFC822, 0, 0, 1, SUPPRESS_ENV_TO);
960 rfc822_len = StrLength(CC->redirect_buffer);
961 rfc822 = pch = SmashStrBuf(&CC->redirect_buffer);
965 ptr = cmemreadline(ptr, buf, sizeof buf);
967 if ((IsEmptyStr(buf)) && (rfc822_body == NULL)) {
972 rfc822_headers_len = rfc822_body - rfc822;
973 rfc822_body_len = rfc822_len - rfc822_headers_len;
976 IAPuts("BODYSTRUCTURE (\"TEXT\" \"PLAIN\" "
977 "(\"CHARSET\" \"US-ASCII\") NIL NIL "
979 IAPrintf(SIZE_T_FMT " %d)", rfc822_body_len, lines);
984 /* For messages already stored in RFC822 format, we have to parse. */
985 IAPuts("BODYSTRUCTURE ");
986 mime_parser(CM_RANGE(msg, eMesageText),
987 *imap_fetch_bodystructure_part, /* part */
988 *imap_fetch_bodystructure_pre, /* pre-multi */
989 *imap_fetch_bodystructure_post, /* post-multi */
991 1); /* don't decode -- we want it as-is */
996 * imap_do_fetch() calls imap_do_fetch_msg() to output the data of an
997 * individual message, once it has been selected for output.
999 void imap_do_fetch_msg(int seq, citimap_command *Cmd) {
1001 citimap *Imap = IMAP;
1002 struct CtdlMessage *msg = NULL;
1003 int body_loaded = 0;
1005 /* Don't attempt to fetch bogus messages or UID's */
1006 if (seq < 1) return;
1007 if (Imap->msgids[seq-1] < 1L) return;
1010 IAPrintf("* %d FETCH (", seq);
1012 for (i=0; i<Cmd->num_parms; ++i) {
1014 /* Fetchable without going to the message store at all */
1015 if (!strcasecmp(Cmd->Params[i].Key, "UID")) {
1016 imap_fetch_uid(seq);
1018 else if (!strcasecmp(Cmd->Params[i].Key, "FLAGS")) {
1019 imap_fetch_flags(seq-1);
1022 /* Potentially fetchable from cache, if the client requests
1023 * stuff from the same message several times in a row.
1025 else if (!strcasecmp(Cmd->Params[i].Key, "RFC822")) {
1026 imap_fetch_rfc822(Imap->msgids[seq-1], Cmd->Params[i].Key);
1028 else if (!strcasecmp(Cmd->Params[i].Key, "RFC822.HEADER")) {
1029 imap_fetch_rfc822(Imap->msgids[seq-1], Cmd->Params[i].Key);
1031 else if (!strcasecmp(Cmd->Params[i].Key, "RFC822.SIZE")) {
1032 imap_fetch_rfc822(Imap->msgids[seq-1], Cmd->Params[i].Key);
1034 else if (!strcasecmp(Cmd->Params[i].Key, "RFC822.TEXT")) {
1035 imap_fetch_rfc822(Imap->msgids[seq-1], Cmd->Params[i].Key);
1038 /* BODY fetches do their own fetching and caching too. */
1039 else if (!strncasecmp(Cmd->Params[i].Key, "BODY[", 5)) {
1040 imap_fetch_body(Imap->msgids[seq-1], Cmd->Params[i], 0);
1042 else if (!strncasecmp(Cmd->Params[i].Key, "BODY.PEEK[", 10)) {
1043 imap_fetch_body(Imap->msgids[seq-1], Cmd->Params[i], 1);
1046 /* Otherwise, load the message into memory.
1048 else if (!strcasecmp(Cmd->Params[i].Key, "BODYSTRUCTURE")) {
1049 if ((msg != NULL) && (!body_loaded)) {
1050 CM_Free(msg); /* need the whole thing */
1054 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
1057 imap_fetch_bodystructure(Imap->msgids[seq-1],
1058 Cmd->Params[i].Key, msg);
1060 else if (!strcasecmp(Cmd->Params[i].Key, "ENVELOPE")) {
1062 msg = CtdlFetchMessage(Imap->msgids[seq-1], 0);
1065 imap_fetch_envelope(msg);
1067 else if (!strcasecmp(Cmd->Params[i].Key, "INTERNALDATE")) {
1069 msg = CtdlFetchMessage(Imap->msgids[seq-1], 0);
1072 imap_fetch_internaldate(msg);
1075 if (i != Cmd->num_parms-1) IAPuts(" ");
1088 * imap_fetch() calls imap_do_fetch() to do its actual work, once it's
1089 * validated and boiled down the request a bit.
1091 void imap_do_fetch(citimap_command *Cmd) {
1092 citimap *Imap = IMAP;
1095 /* debug output the parsed vector */
1098 syslog(LOG_DEBUG, "imap: ----- %ld params", Cmd->num_parms);
1100 for (i=0; i < Cmd->num_parms; i++) {
1101 if (Cmd->Params[i].len != strlen(Cmd->Params[i].Key))
1102 syslog(LOG_DEBUG, "imap: *********** %ld != %ld : %s",
1104 strlen(Cmd->Params[i].Key),
1105 Cmd->Params[i].Key);
1107 syslog(LOG_DEBUG, "imap: %ld : %s",
1109 Cmd->Params[i].Key);
1114 if (Imap->num_msgs > 0) {
1115 for (i = 0; i < Imap->num_msgs; ++i) {
1117 /* Abort the fetch loop if the session breaks.
1118 * This is important for users who keep mailboxes
1119 * that are too big *and* are too impatient to
1120 * let them finish loading. :)
1122 if (CC->kill_me) return;
1124 /* Get any message marked for fetch. */
1125 if (Imap->flags[i] & IMAP_SELECTED) {
1126 imap_do_fetch_msg(i+1, Cmd);
1135 * Back end for imap_handle_macros()
1136 * Note that this function *only* looks at the beginning of the string. It
1137 * is not a generic search-and-replace function.
1139 void imap_macro_replace(StrBuf *Buf, long where,
1141 char *find, long findlen,
1142 char *replace, long replacelen)
1145 if (StrLength(Buf) - where > findlen)
1148 if (!strncasecmp(ChrPtr(Buf) + where, find, findlen)) {
1149 if (ChrPtr(Buf)[where + findlen] == ' ') {
1150 StrBufPlain(TmpBuf, replace, replacelen);
1151 StrBufAppendBufPlain(TmpBuf, HKEY(" "), 0);
1152 StrBufReplaceToken(Buf, where, findlen,
1155 if (where + findlen == StrLength(Buf)) {
1156 StrBufReplaceToken(Buf, where, findlen,
1157 replace, replacelen);
1165 * Handle macros embedded in FETCH data items.
1166 * (What the heck are macros doing in a wire protocol? Are we trying to save
1167 * the computer at the other end the trouble of typing a lot of characters?)
1169 void imap_handle_macros(citimap_command *Cmd) {
1172 StrBuf *Tmp = NewStrBuf();
1174 for (i=0; i < StrLength(Cmd->CmdBuf); ++i) {
1175 char ch = ChrPtr(Cmd->CmdBuf)[i];
1180 else if ((ch==')') ||
1186 imap_macro_replace(Cmd->CmdBuf, i,
1189 HKEY("FLAGS INTERNALDATE RFC822.SIZE ENVELOPE")
1191 imap_macro_replace(Cmd->CmdBuf, i,
1194 HKEY("BODYSTRUCTURE")
1196 imap_macro_replace(Cmd->CmdBuf, i,
1199 HKEY("FLAGS INTERNALDATE RFC822.SIZE")
1201 imap_macro_replace(Cmd->CmdBuf, i,
1204 HKEY("FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODY")
1213 * Break out the data items requested, possibly a parenthesized list.
1214 * Returns the number of data items, or -1 if the list is invalid.
1215 * NOTE: this function alters the string it is fed, and uses it as a buffer
1216 * to hold the data for the pointers it returns.
1218 int imap_extract_data_items(citimap_command *Cmd)
1222 const char *pch, *end;
1224 /* Convert all whitespace to ordinary space characters. */
1225 pch = ChrPtr(Cmd->CmdBuf);
1226 end = pch + StrLength(Cmd->CmdBuf);
1231 StrBufPeek(Cmd->CmdBuf, pch, 0, ' ');
1235 /* Strip leading and trailing whitespace, then strip leading and
1236 * trailing parentheses if it's a list
1238 StrBufTrim(Cmd->CmdBuf);
1239 pch = ChrPtr(Cmd->CmdBuf);
1240 if ( (pch[0]=='(') &&
1241 (pch[StrLength(Cmd->CmdBuf)-1]==')') )
1243 StrBufCutRight(Cmd->CmdBuf, 1);
1244 StrBufCutLeft(Cmd->CmdBuf, 1);
1245 StrBufTrim(Cmd->CmdBuf);
1248 /* Parse any macro data items */
1249 imap_handle_macros(Cmd);
1252 * Now break out the data items. We throw in one trailing space in
1253 * order to avoid having to break out the last one manually.
1255 nArgs = StrLength(Cmd->CmdBuf) / 10 + 10;
1256 nArgs = CmdAdjust(Cmd, nArgs, 0);
1258 Cmd->Params[Cmd->num_parms].Key = pch = ChrPtr(Cmd->CmdBuf);
1259 end = Cmd->Params[Cmd->num_parms].Key + StrLength(Cmd->CmdBuf);
1269 else if ((*pch==')') ||
1275 if ((nest <= 0) && (*pch==' ')) {
1276 StrBufPeek(Cmd->CmdBuf, pch, 0, '\0');
1277 Cmd->Params[Cmd->num_parms].len =
1278 pch - Cmd->Params[Cmd->num_parms].Key;
1280 if (Cmd->num_parms + 1 >= Cmd->avail_parms) {
1281 nArgs = CmdAdjust(Cmd, nArgs * 2, 1);
1284 Cmd->Params[Cmd->num_parms].Key = ++pch;
1286 else if (pch + 1 == end) {
1287 Cmd->Params[Cmd->num_parms].len =
1288 pch - Cmd->Params[Cmd->num_parms].Key + 1;
1294 return Cmd->num_parms;
1300 * One particularly hideous aspect of IMAP is that we have to allow the client
1301 * to specify arbitrary ranges and/or sets of messages to fetch. Citadel IMAP
1302 * handles this by setting the IMAP_SELECTED flag for each message specified in
1303 * the ranges/sets, then looping through the message array, outputting messages
1304 * with the flag set. We don't bother returning an error if an out-of-range
1305 * number is specified (we just return quietly) because any client braindead
1306 * enough to request a bogus message number isn't going to notice the
1307 * difference anyway.
1309 * This function clears out the IMAP_SELECTED bits, then sets that bit for each
1310 * message included in the specified range.
1312 * Set is_uid to 1 to fetch by UID instead of sequence number.
1314 void imap_pick_range(const char *supplied_range, int is_uid) {
1315 citimap *Imap = IMAP;
1319 char setstr[SIZ], lostr[SIZ], histr[SIZ];
1321 char actual_range[SIZ];
1324 * Handle the "ALL" macro
1326 if (!strcasecmp(supplied_range, "ALL")) {
1327 safestrncpy(actual_range, "1:*", sizeof actual_range);
1330 safestrncpy(actual_range, supplied_range, sizeof actual_range);
1334 * Clear out the IMAP_SELECTED flags for all messages.
1336 for (i = 0; i < Imap->num_msgs; ++i) {
1337 Imap->flags[i] = Imap->flags[i] & ~IMAP_SELECTED;
1341 * Now set it for all specified messages.
1343 num_sets = num_tokens(actual_range, ',');
1344 for (s=0; s<num_sets; ++s) {
1345 extract_token(setstr, actual_range, s, ',', sizeof setstr);
1347 extract_token(lostr, setstr, 0, ':', sizeof lostr);
1348 if (num_tokens(setstr, ':') >= 2) {
1349 extract_token(histr, setstr, 1, ':', sizeof histr);
1350 if (!strcmp(histr, "*")) snprintf(histr, sizeof histr, "%ld", LONG_MAX);
1353 safestrncpy(histr, lostr, sizeof histr);
1358 /* Loop through the array, flipping bits where appropriate */
1359 for (i = 1; i <= Imap->num_msgs; ++i) {
1360 if (is_uid) { /* fetch by sequence number */
1361 if ( (Imap->msgids[i-1]>=lo)
1362 && (Imap->msgids[i-1]<=hi)) {
1363 Imap->flags[i-1] |= IMAP_SELECTED;
1366 else { /* fetch by uid */
1367 if ( (i>=lo) && (i<=hi)) {
1368 Imap->flags[i-1] |= IMAP_SELECTED;
1378 * This function is called by the main command loop.
1380 void imap_fetch(int num_parms, ConstStr *Params) {
1381 citimap_command Cmd;
1384 if (num_parms < 4) {
1385 IReply("BAD invalid parameters");
1389 imap_pick_range(Params[2].Key, 0);
1391 memset(&Cmd, 0, sizeof(citimap_command));
1392 Cmd.CmdBuf = NewStrBufPlain(NULL, StrLength(IMAP->Cmd.CmdBuf));
1393 MakeStringOf(Cmd.CmdBuf, 3);
1395 num_items = imap_extract_data_items(&Cmd);
1396 if (num_items < 1) {
1397 IReply("BAD invalid data item list");
1398 FreeStrBuf(&Cmd.CmdBuf);
1403 imap_do_fetch(&Cmd);
1404 IReply("OK FETCH completed");
1405 FreeStrBuf(&Cmd.CmdBuf);
1410 * This function is called by the main command loop.
1412 void imap_uidfetch(int num_parms, ConstStr *Params) {
1413 citimap_command Cmd;
1416 int have_uid_item = 0;
1418 if (num_parms < 5) {
1419 IReply("BAD invalid parameters");
1423 imap_pick_range(Params[3].Key, 1);
1425 memset(&Cmd, 0, sizeof(citimap_command));
1426 Cmd.CmdBuf = NewStrBufPlain(NULL, StrLength(IMAP->Cmd.CmdBuf));
1428 MakeStringOf(Cmd.CmdBuf, 4);
1430 syslog(LOG_DEBUG, "imap: -------%s--------", ChrPtr(Cmd.CmdBuf));
1432 num_items = imap_extract_data_items(&Cmd);
1433 if (num_items < 1) {
1434 IReply("BAD invalid data item list");
1435 FreeStrBuf(&Cmd.CmdBuf);
1440 /* If the "UID" item was not included, we include it implicitly
1441 * (at the beginning) because this is a UID FETCH command
1443 for (i=0; i<num_items; ++i) {
1444 if (!strcasecmp(Cmd.Params[i].Key, "UID")) ++have_uid_item;
1446 if (have_uid_item == 0) {
1447 if (Cmd.num_parms + 1 >= Cmd.avail_parms)
1448 CmdAdjust(&Cmd, Cmd.avail_parms + 1, 1);
1449 memmove(&Cmd.Params[1],
1451 sizeof(ConstStr) * Cmd.num_parms);
1454 Cmd.Params[0] = (ConstStr){HKEY("UID")};
1457 imap_do_fetch(&Cmd);
1458 IReply("OK UID FETCH completed");
1459 FreeStrBuf(&Cmd.CmdBuf);