91cb398323268b149f82a3ead81abb0026adf31e
[citadel.git] / citadel / modules / imap / imap_fetch.c
1 /*
2  * Implements the FETCH command in IMAP.
3  * This is a good example of the protocol's gratuitous complexity.
4  *
5  * Copyright (c) 2001-2017 by the citadel.org team
6  *
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.
11  *
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.
16  */
17
18
19 #include "sysdep.h"
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <fcntl.h>
24 #include <signal.h>
25 #include <pwd.h>
26 #include <errno.h>
27 #include <sys/types.h>
28
29 #if TIME_WITH_SYS_TIME
30 # include <sys/time.h>
31 # include <time.h>
32 #else
33 # if HAVE_SYS_TIME_H
34 #  include <sys/time.h>
35 # else
36 #  include <time.h>
37 # endif
38 #endif
39
40 #include <sys/wait.h>
41 #include <ctype.h>
42 #include <string.h>
43 #include <limits.h>
44 #include <libcitadel.h>
45 #include "citadel.h"
46 #include "server.h"
47 #include "sysdep_decls.h"
48 #include "citserver.h"
49 #include "support.h"
50 #include "config.h"
51 #include "user_ops.h"
52 #include "database.h"
53 #include "msgbase.h"
54 #include "internet_addressing.h"
55 #include "serv_imap.h"
56 #include "imap_tools.h"
57 #include "imap_fetch.h"
58 #include "genstamp.h"
59 #include "ctdl_module.h"
60
61
62
63 /*
64  * Individual field functions for imap_do_fetch_msg() ...
65  */
66
67 void imap_fetch_uid(int seq) {
68         IAPrintf("UID %ld", IMAP->msgids[seq-1]);
69 }
70
71 void imap_fetch_flags(int seq) 
72 {
73         citimap *Imap = IMAP;
74         int num_flags_printed = 0;
75         IAPuts("FLAGS (");
76         if (Imap->flags[seq] & IMAP_DELETED) {
77                 if (num_flags_printed > 0) 
78                         IAPuts(" ");
79                 IAPuts("\\Deleted");
80                 ++num_flags_printed;
81         }
82         if (Imap->flags[seq] & IMAP_SEEN) {
83                 if (num_flags_printed > 0) 
84                         IAPuts(" ");
85                 IAPuts("\\Seen");
86                 ++num_flags_printed;
87         }
88         if (Imap->flags[seq] & IMAP_ANSWERED) {
89                 if (num_flags_printed > 0) 
90                         IAPuts(" ");
91                 IAPuts("\\Answered");
92                 ++num_flags_printed;
93         }
94         if (Imap->flags[seq] & IMAP_RECENT) {
95                 if (num_flags_printed > 0) 
96                         IAPuts(" ");
97                 IAPuts("\\Recent");
98                 ++num_flags_printed;
99         }
100         IAPuts(")");
101 }
102
103
104 void imap_fetch_internaldate(struct CtdlMessage *msg) {
105         char datebuf[64];
106         time_t msgdate;
107
108         if (!msg) return;
109         if (!CM_IsEmpty(msg, eTimestamp)) {
110                 msgdate = atol(msg->cm_fields[eTimestamp]);
111         }
112         else {
113                 msgdate = time(NULL);
114         }
115
116         datestring(datebuf, sizeof datebuf, msgdate, DATESTRING_IMAP);
117         IAPrintf( "INTERNALDATE \"%s\"", datebuf);
118 }
119
120
121 /*
122  * Fetch RFC822-formatted messages.
123  *
124  * 'whichfmt' should be set to one of:
125  *      "RFC822"        entire message
126  *      "RFC822.HEADER" headers only (with trailing blank line)
127  *      "RFC822.SIZE"   size of translated message
128  *      "RFC822.TEXT"   body only (without leading blank line)
129  */
130 void imap_fetch_rfc822(long msgnum, const char *whichfmt) {
131         CitContext *CCC = CC;
132         citimap *Imap = CCCIMAP;
133         const char *ptr = NULL;
134         size_t headers_size, text_size, total_size;
135         size_t bytes_to_send = 0;
136         struct MetaData smi;
137         int need_to_rewrite_metadata = 0;
138         int need_body = 0;
139
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...
143          */
144         if ( (!strcasecmp(whichfmt, "RFC822")) || (!strcasecmp(whichfmt, "RFC822.TEXT")) ) {
145                 need_body = 1;
146         }
147
148         /* If this is an RFC822.SIZE fetch, first look in the message's
149          * metadata record to see if we've saved that information.
150          */
151         if (!strcasecmp(whichfmt, "RFC822.SIZE")) {
152                 GetMetaData(&smi, msgnum);
153                 if (smi.meta_rfc822_length > 0L) {
154                         IAPrintf("RFC822.SIZE %ld", smi.meta_rfc822_length);
155                         return;
156                 }
157                 need_to_rewrite_metadata = 1;
158                 need_body = 1;
159         }
160         
161         /* Cache the most recent RFC822 FETCH because some clients like to
162          * fetch in pieces, and we don't want to have to go back to the
163          * message store for each piece.  We also burn the cache if the
164          * client requests something that involves reading the message
165          * body, but we haven't fetched the body yet.
166          */
167         if ((Imap->cached_rfc822 != NULL)
168            && (Imap->cached_rfc822_msgnum == msgnum)
169            && (Imap->cached_rfc822_withbody || (!need_body)) ) {
170                 /* Good to go! */
171         }
172         else if (Imap->cached_rfc822 != NULL) {
173                 /* Some other message is cached -- free it */
174                 FreeStrBuf(&Imap->cached_rfc822);
175                 Imap->cached_rfc822_msgnum = (-1);
176         }
177
178         /* At this point, we now can fetch and convert the message iff it's not
179          * the one we had cached.
180          */
181         if (Imap->cached_rfc822 == NULL) {
182                 /*
183                  * Load the message into memory for translation & measurement
184                  */
185                 CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
186                 CtdlOutputMsg(msgnum, MT_RFC822,
187                         (need_body ? HEADERS_ALL : HEADERS_FAST),
188                         0, 1, NULL, SUPPRESS_ENV_TO, NULL, NULL, NULL
189                 );
190                 if (!need_body) {
191                         client_write(HKEY("\r\n"));             // extra trailing newline -- to the redirect_buffer, *not* to the client
192                 }
193                 Imap->cached_rfc822 = CCC->redirect_buffer;
194                 CCC->redirect_buffer = NULL;
195                 Imap->cached_rfc822_msgnum = msgnum;
196                 Imap->cached_rfc822_withbody = need_body;
197                 if ( (need_to_rewrite_metadata) && 
198                      (StrLength(Imap->cached_rfc822) > 0) ) {
199                         smi.meta_rfc822_length = StrLength(Imap->cached_rfc822);
200                         PutMetaData(&smi);
201                 }
202         }
203
204         /*
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.
207          */
208         headers_size = 0;
209
210         if (need_body) {
211                 StrBuf *Line = NewStrBuf();
212                 ptr = NULL;
213                 do {
214                         StrBufSipLine(Line, Imap->cached_rfc822, &ptr);
215
216                         if ((StrLength(Line) != 0)  && (ptr != StrBufNOTNULL))
217                         {
218                                 StrBufTrim(Line);
219                                 if ((StrLength(Line) != 0) && 
220                                     (ptr != StrBufNOTNULL)    )
221                                 {
222                                         headers_size = ptr - ChrPtr(Imap->cached_rfc822);
223                                 }
224                         }
225                 } while ( (headers_size == 0)    && 
226                           (ptr != StrBufNOTNULL) );
227
228                 total_size = StrLength(Imap->cached_rfc822);
229                 text_size = total_size - headers_size;
230                 FreeStrBuf(&Line);
231         }
232         else {
233                 headers_size = total_size = StrLength(Imap->cached_rfc822);
234                 text_size = 0;
235         }
236
237         syslog(LOG_DEBUG, "imap: RFC822 headers=" SIZE_T_FMT ", text=" SIZE_T_FMT ", total=" SIZE_T_FMT, headers_size, text_size, total_size);
238
239         if (!strcasecmp(whichfmt, "RFC822.SIZE")) {
240                 IAPrintf("RFC822.SIZE " SIZE_T_FMT, total_size);
241                 return;
242         }
243
244         else if (!strcasecmp(whichfmt, "RFC822")) {
245                 ptr = ChrPtr(Imap->cached_rfc822);
246                 bytes_to_send = total_size;
247         }
248
249         else if (!strcasecmp(whichfmt, "RFC822.HEADER")) {
250                 ptr = ChrPtr(Imap->cached_rfc822);
251                 bytes_to_send = headers_size;
252         }
253
254         else if (!strcasecmp(whichfmt, "RFC822.TEXT")) {
255                 ptr = &ChrPtr(Imap->cached_rfc822)[headers_size];
256                 bytes_to_send = text_size;
257         }
258
259         IAPrintf("%s {" SIZE_T_FMT "}\r\n", whichfmt, bytes_to_send);
260         iaputs(ptr, bytes_to_send);
261 }
262
263
264 /*
265  * Load a specific part of a message into the temp file to be output to a
266  * client.  FIXME we can handle parts like "2" and "2.1" and even "2.MIME"
267  * but we still can't handle "2.HEADER" (which might not be a problem).
268  *
269  * Note: mime_parser() was called with dont_decode set to 1, so we have the
270  * luxury of simply spewing without having to re-encode.
271  */
272 void imap_load_part(char *name, char *filename, char *partnum, char *disp,
273                     void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
274                     char *cbid, void *cbuserdata)
275 {
276         char mimebuf2[SIZ];
277         StrBuf *desired_section;
278
279         desired_section = (StrBuf *)cbuserdata;
280         syslog(LOG_DEBUG, "imap: imap_load_part() looking for %s, found %s", ChrPtr(desired_section), partnum);
281
282         if (!strcasecmp(partnum, ChrPtr(desired_section))) {
283                 client_write(content, length);
284         }
285
286         snprintf(mimebuf2, sizeof mimebuf2, "%s.MIME", partnum);
287
288         if (!strcasecmp(ChrPtr(desired_section), mimebuf2)) {
289                 client_write(HKEY("Content-type: "));
290                 client_write(cbtype, strlen(cbtype));
291                 if (!IsEmptyStr(cbcharset)) {
292                         client_write(HKEY("; charset=\""));
293                         client_write(cbcharset, strlen(cbcharset));
294                         client_write(HKEY("\""));
295                 }
296                 if (!IsEmptyStr(name)) {
297                         client_write(HKEY("; name=\""));
298                         client_write(name, strlen(name));
299                         client_write(HKEY("\""));
300                 }
301                 client_write(HKEY("\r\n"));
302                 if (!IsEmptyStr(encoding)) {
303                         client_write(HKEY("Content-Transfer-Encoding: "));
304                         client_write(encoding, strlen(encoding));
305                         client_write(HKEY("\r\n"));
306                 }
307                 if (!IsEmptyStr(encoding)) {
308                         client_write(HKEY("Content-Disposition: "));
309                         client_write(disp, strlen(disp));
310                 
311                         if (!IsEmptyStr(filename)) {
312                                 client_write(HKEY("; filename=\""));
313                                 client_write(filename, strlen(filename));
314                                 client_write(HKEY("\""));
315                         }
316                         client_write(HKEY("\r\n"));
317                 }
318                 cprintf("Content-Length: %ld\r\n\r\n", (long)length);
319         }
320 }
321
322
323 /* 
324  * Called by imap_fetch_envelope() to output the "From" field.
325  * This is in its own function because its logic is kind of complex.  We
326  * really need to make this suck less.
327  */
328 void imap_output_envelope_from(struct CtdlMessage *msg) {
329         char user[SIZ], node[SIZ], name[SIZ];
330
331         if (!msg) return;
332
333         /* For anonymous messages, it's so easy! */
334         if (!is_room_aide() && (msg->cm_anon_type == MES_ANONONLY)) {
335                 IAPuts("((\"----\" NIL \"x\" \"x.org\")) ");
336                 return;
337         }
338         if (!is_room_aide() && (msg->cm_anon_type == MES_ANONOPT)) {
339                 IAPuts("((\"anonymous\" NIL \"x\" \"x.org\")) ");
340                 return;
341         }
342
343         /* For everything else, we do stuff. */
344         IAPuts("((");                           // open double-parens
345         IPutMsgField(eAuthor);                  // display name
346         IAPuts(" NIL ");                        // source route (not used)
347
348         if (!CM_IsEmpty(msg, erFc822Addr)) {
349                 process_rfc822_addr(msg->cm_fields[erFc822Addr], user, node, name);
350                 IPutStr(user, strlen(user));            /* mailbox name (user id) */
351                 IAPuts(" ");
352                 if (!strcasecmp(node, CtdlGetConfigStr("c_nodename"))) {
353                         IPutStr(CtdlGetConfigStr("c_fqdn"), strlen(CtdlGetConfigStr("c_fqdn")));
354                 }
355                 else {
356                         IPutStr(node, strlen(node));            /* host name */
357                 }
358         }
359         else {
360                 IPutMsgField(eAuthor); /* mailbox name (user id) */
361                 IAPuts(" ");
362                 IPutMsgField(eNodeName);        /* host name */
363         }
364         
365         IAPuts(")) "); /* close double-parens */
366 }
367
368
369 /*
370  * Output an envelope address (or set of addresses) in the official,
371  * convoluted, braindead format.  (Note that we can't use this for
372  * the "From" address because its data may come from a number of different
373  * fields.  But we can use it for "To" and possibly others.
374  */
375 void imap_output_envelope_addr(char *addr) {
376         char individual_addr[256];
377         int num_addrs;
378         int i;
379         char user[256];
380         char node[256];
381         char name[256];
382
383         if (addr == NULL) {
384                 IAPuts("NIL ");
385                 return;
386         }
387
388         if (IsEmptyStr(addr)) {
389                 IAPuts("NIL ");
390                 return;
391         }
392
393         IAPuts("(");
394
395         /* How many addresses are listed here? */
396         num_addrs = num_tokens(addr, ',');
397
398         /* Output them one by one. */
399         for (i=0; i<num_addrs; ++i) {
400                 extract_token(individual_addr, addr, i, ',', sizeof individual_addr);
401                 striplt(individual_addr);
402                 process_rfc822_addr(individual_addr, user, node, name);
403                 IAPuts("(");
404                 IPutStr(name, strlen(name));
405                 IAPuts(" NIL ");
406                 IPutStr(user, strlen(user));
407                 IAPuts(" ");
408                 IPutStr(node, strlen(node));
409                 IAPuts(")");
410                 if (i < (num_addrs-1)) 
411                         IAPuts(" ");
412         }
413
414         IAPuts(") ");
415 }
416
417
418 /*
419  * Implements the ENVELOPE fetch item
420  * 
421  * Note that the imap_strout() function can cleverly output NULL fields as NIL,
422  * so we don't have to check for that condition like we do elsewhere.
423  */
424 void imap_fetch_envelope(struct CtdlMessage *msg) {
425         char datestringbuf[SIZ];
426         time_t msgdate;
427         char *fieldptr = NULL;
428         long len;
429
430         if (!msg) return;
431
432         /* Parse the message date into an IMAP-format date string */
433         if (!CM_IsEmpty(msg, eTimestamp)) {
434                 msgdate = atol(msg->cm_fields[eTimestamp]);
435         }
436         else {
437                 msgdate = time(NULL);
438         }
439         len = datestring(datestringbuf, sizeof datestringbuf, msgdate, DATESTRING_IMAP);
440
441         /* Now start spewing data fields.  The order is important, as it is
442          * defined by the protocol specification.  Nonexistent fields must
443          * be output as NIL, existent fields must be quoted or literalled.
444          * The imap_strout() function conveniently does all this for us.
445          */
446         IAPuts("ENVELOPE (");
447
448         /* Date */
449         IPutStr(datestringbuf, len);
450         IAPuts(" ");
451
452         /* Subject */
453         IPutMsgField(eMsgSubject);
454         IAPuts(" ");
455
456         /* From */
457         imap_output_envelope_from(msg);
458
459         /* Sender (default to same as 'From' if not present) */
460         fieldptr = rfc822_fetch_field(msg->cm_fields[eMesageText], "Sender");
461         if (fieldptr != NULL) {
462                 imap_output_envelope_addr(fieldptr);
463                 free(fieldptr);
464         }
465         else {
466                 imap_output_envelope_from(msg);
467         }
468
469         /* Reply-to */
470         fieldptr = rfc822_fetch_field(msg->cm_fields[eMesageText], "Reply-to");
471         if (fieldptr != NULL) {
472                 imap_output_envelope_addr(fieldptr);
473                 free(fieldptr);
474         }
475         else {
476                 imap_output_envelope_from(msg);
477         }
478
479         /* To */
480         imap_output_envelope_addr(msg->cm_fields[eRecipient]);
481
482         /* Cc (we do it this way because there might be a legacy non-Citadel Cc: field present) */
483         fieldptr = msg->cm_fields[eCarbonCopY];
484         if (fieldptr != NULL) {
485                 imap_output_envelope_addr(fieldptr);
486         }
487         else {
488                 fieldptr = rfc822_fetch_field(msg->cm_fields[eMesageText], "Cc");
489                 imap_output_envelope_addr(fieldptr);
490                 if (fieldptr != NULL) free(fieldptr);
491         }
492
493         /* Bcc */
494         fieldptr = rfc822_fetch_field(msg->cm_fields[eMesageText], "Bcc");
495         imap_output_envelope_addr(fieldptr);
496         if (fieldptr != NULL) free(fieldptr);
497
498         /* In-reply-to */
499         fieldptr = rfc822_fetch_field(msg->cm_fields[eMesageText], "In-reply-to");
500         IPutStr(fieldptr, (fieldptr)?strlen(fieldptr):0);
501         IAPuts(" ");
502         if (fieldptr != NULL) free(fieldptr);
503
504         /* message ID */
505         len = msg->cm_lengths[emessageId];
506         
507         if ((len == 0) || (
508                 (msg->cm_fields[emessageId][0] == '<') && 
509                 (msg->cm_fields[emessageId][len - 1] == '>'))
510         ) {
511                 IPutMsgField(emessageId);
512         }
513         else 
514         {
515                 char *Buf = malloc(len + 3);
516                 long pos = 0;
517                 
518                 if (msg->cm_fields[emessageId][0] != '<')
519                 {
520                         Buf[pos] = '<';
521                         pos ++;
522                 }
523                 memcpy(&Buf[pos], msg->cm_fields[emessageId], len);
524                 pos += len;
525                 if (msg->cm_fields[emessageId][len] != '>')
526                 {
527                         Buf[pos] = '>';
528                         pos++;
529                 }
530                 Buf[pos] = '\0';
531                 IPutStr(Buf, pos);
532                 free(Buf);
533         }
534         IAPuts(")");
535 }
536
537
538 /*
539  * This function is called only when CC->redirect_buffer contains a set of
540  * RFC822 headers with no body attached.  Its job is to strip that set of
541  * headers down to *only* the ones we're interested in.
542  */
543 void imap_strip_headers(StrBuf *section) {
544         citimap_command Cmd;
545         StrBuf *which_fields = NULL;
546         int doing_headers = 0;
547         int headers_not = 0;
548         int num_parms = 0;
549         int i;
550         StrBuf *boiled_headers = NULL;
551         StrBuf *Line;
552         int ok = 0;
553         int done_headers = 0;
554         const char *Ptr = NULL;
555         CitContext *CCC = CC;
556
557         if (CCC->redirect_buffer == NULL) return;
558
559         which_fields = NewStrBufDup(section);
560
561         if (!strncasecmp(ChrPtr(which_fields), "HEADER.FIELDS", 13))
562                 doing_headers = 1;
563         if (doing_headers && 
564             !strncasecmp(ChrPtr(which_fields), "HEADER.FIELDS.NOT", 17))
565                 headers_not = 1;
566
567         for (i=0; i < StrLength(which_fields); ++i) {
568                 if (ChrPtr(which_fields)[i]=='(')
569                         StrBufReplaceToken(which_fields, i, 1, HKEY(""));
570         }
571         for (i=0; i < StrLength(which_fields); ++i) {
572                 if (ChrPtr(which_fields)[i]==')') {
573                         StrBufCutAt(which_fields, i, NULL);
574                         break;
575                 }
576         }
577         memset(&Cmd, 0, sizeof(citimap_command));
578         Cmd.CmdBuf = which_fields;
579         num_parms = imap_parameterize(&Cmd);
580
581         boiled_headers = NewStrBufPlain(NULL, StrLength(CCC->redirect_buffer));
582         Line = NewStrBufPlain(NULL, SIZ);
583         Ptr = NULL;
584         ok = 0;
585         do {
586                 StrBufSipLine(Line, CCC->redirect_buffer, &Ptr);
587
588                 if (!isspace(ChrPtr(Line)[0])) {
589
590                         if (doing_headers == 0) ok = 1;
591                         else {
592                                 /* we're supposed to print all headers that are not matching the filter list */
593                                 if (headers_not) for (i=0, ok = 1; (i < num_parms) && (ok == 1); ++i) {
594                                                 if ( (!strncasecmp(ChrPtr(Line), 
595                                                                    Cmd.Params[i].Key,
596                                                                    Cmd.Params[i].len)) &&
597                                                      (ChrPtr(Line)[Cmd.Params[i].len]==':') ) {
598                                                         ok = 0;
599                                                 }
600                                 }
601                                 /* we're supposed to print all headers matching the filterlist */
602                                 else for (i=0, ok = 0; ((i < num_parms) && (ok == 0)); ++i) {
603                                                 if ( (!strncasecmp(ChrPtr(Line), 
604                                                                    Cmd.Params[i].Key,
605                                                                    Cmd.Params[i].len)) &&
606                                                      (ChrPtr(Line)[Cmd.Params[i].len]==':') ) {
607                                                         ok = 1;
608                                         }
609                                 }
610                         }
611                 }
612
613                 if (ok) {
614                         StrBufAppendBuf(boiled_headers, Line, 0);
615                         StrBufAppendBufPlain(boiled_headers, HKEY("\r\n"), 0);
616                 }
617
618                 if ((Ptr == StrBufNOTNULL)  ||
619                     (StrLength(Line) == 0)  ||
620                     (ChrPtr(Line)[0]=='\r') ||
621                     (ChrPtr(Line)[0]=='\n')   ) done_headers = 1;
622         } while (!done_headers);
623
624         StrBufAppendBufPlain(boiled_headers, HKEY("\r\n"), 0);
625
626         /* Now save it back (it'll always be smaller) */
627         FreeStrBuf(&CCC->redirect_buffer);
628         CCC->redirect_buffer = boiled_headers;
629
630         free(Cmd.Params);
631         FreeStrBuf(&which_fields);
632         FreeStrBuf(&Line);
633 }
634
635
636 /*
637  * Implements the BODY and BODY.PEEK fetch items
638  */
639 void imap_fetch_body(long msgnum, ConstStr item, int is_peek) {
640         struct CtdlMessage *msg = NULL;
641         StrBuf *section;
642         StrBuf *partial;
643         int is_partial = 0;
644         size_t pstart, pbytes;
645         int loading_body_now = 0;
646         int need_body = 1;
647         int burn_the_cache = 0;
648         CitContext *CCC = CC;
649         citimap *Imap = CCCIMAP;
650
651         /* extract section */
652         section = NewStrBufPlain(CKEY(item));
653         
654         if (strchr(ChrPtr(section), '[') != NULL) {
655                 StrBufStripAllBut(section, '[', ']');
656         }
657         syslog(LOG_DEBUG, "imap: selected section is [%s]", (StrLength(section) == 0) ? "(empty)" : ChrPtr(section));
658
659         /* Burn the cache if we don't have the same section of the 
660          * same message again.
661          */
662         if (Imap->cached_body != NULL) {
663                 if (Imap->cached_bodymsgnum != msgnum) {
664                         burn_the_cache = 1;
665                 }
666                 else if ( (!Imap->cached_body_withbody) && (need_body) ) {
667                         burn_the_cache = 1;
668                 }
669                 else if (strcasecmp(Imap->cached_bodypart, ChrPtr(section))) {
670                         burn_the_cache = 1;
671                 }
672                 if (burn_the_cache) {
673                         /* Yup, go ahead and burn the cache. */
674                         free(Imap->cached_body);
675                         Imap->cached_body_len = 0;
676                         Imap->cached_body = NULL;
677                         Imap->cached_bodymsgnum = (-1);
678                         strcpy(Imap->cached_bodypart, "");
679                 }
680         }
681
682         /* extract partial */
683         partial = NewStrBufPlain(CKEY(item));
684         if (strchr(ChrPtr(partial), '<') != NULL) {
685                 StrBufStripAllBut(partial, '<', '>');
686                 is_partial = 1;
687         }
688         if ( (is_partial == 1) && (StrLength(partial) > 0) ) {
689                 syslog(LOG_DEBUG, "imap: selected partial is <%s>", ChrPtr(partial));
690         }
691
692         if (Imap->cached_body == NULL) {
693                 CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
694                 loading_body_now = 1;
695                 msg = CtdlFetchMessage(msgnum, (need_body ? 1 : 0), 1);
696         }
697
698         /* Now figure out what the client wants, and get it */
699
700         if (!loading_body_now) {
701                 /* What we want is already in memory */
702         }
703
704         else if ( (!strcmp(ChrPtr(section), "1")) && (msg->cm_format_type != 4) ) {
705                 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_NONE, 0, 1, SUPPRESS_ENV_TO);
706         }
707
708         else if (StrLength(section) == 0) {
709                 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, SUPPRESS_ENV_TO);
710         }
711
712         /*
713          * If the client asked for just headers, or just particular header
714          * fields, strip it down.
715          */
716         else if (!strncasecmp(ChrPtr(section), "HEADER", 6)) {
717                 /* This used to work with HEADERS_FAST, but then Apple got stupid with their
718                  * IMAP library and this broke Mail.App and iPhone Mail, so we had to change it
719                  * to HEADERS_ONLY so the trendy hipsters with their iPhones can read mail.
720                  */
721                 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ONLY, 0, 1, SUPPRESS_ENV_TO);
722                 imap_strip_headers(section);
723         }
724
725         /*
726          * Strip it down if the client asked for everything _except_ headers.
727          */
728         else if (!strncasecmp(ChrPtr(section), "TEXT", 4)) {
729                 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_NONE, 0, 1, SUPPRESS_ENV_TO);
730         }
731
732         /*
733          * Anything else must be a part specifier.
734          * (Note value of 1 passed as 'dont_decode' so client gets it encoded)
735          */
736         else {
737                 mime_parser(CM_RANGE(msg, eMesageText),
738                             *imap_load_part, NULL, NULL,
739                             section,
740                             1
741                         );
742         }
743
744         if (loading_body_now) {
745                 Imap->cached_body_len = StrLength(CCC->redirect_buffer);
746                 Imap->cached_body = SmashStrBuf(&CCC->redirect_buffer);
747                 Imap->cached_bodymsgnum = msgnum;
748                 Imap->cached_body_withbody = need_body;
749                 strcpy(Imap->cached_bodypart, ChrPtr(section));
750         }
751
752         if (is_partial == 0) {
753                 IAPuts("BODY[");
754                 iaputs(SKEY(section));
755                 IAPrintf("] {" SIZE_T_FMT "}\r\n", Imap->cached_body_len);
756                 pstart = 0;
757                 pbytes = Imap->cached_body_len;
758         }
759         else {
760                 sscanf(ChrPtr(partial), SIZE_T_FMT "." SIZE_T_FMT, &pstart, &pbytes);
761                 if (pbytes > (Imap->cached_body_len - pstart)) {
762                         pbytes = Imap->cached_body_len - pstart;
763                 }
764                 IAPuts("BODY[");
765                 iaputs(SKEY(section));
766                 IAPrintf("]<" SIZE_T_FMT "> {" SIZE_T_FMT "}\r\n", pstart, pbytes);
767         }
768
769         FreeStrBuf(&partial);
770
771         /* Here we go -- output it */
772         iaputs(&Imap->cached_body[pstart], pbytes);
773
774         if (msg != NULL) {
775                 CM_Free(msg);
776         }
777
778         /* Mark this message as "seen" *unless* this is a "peek" operation */
779         if (is_peek == 0) {
780                 CtdlSetSeen(&msgnum, 1, 1, ctdlsetseen_seen, NULL, NULL);
781         }
782         FreeStrBuf(&section);
783 }
784
785 /*
786  * Called immediately before outputting a multipart bodystructure
787  */
788 void imap_fetch_bodystructure_pre(
789                 char *name, char *filename, char *partnum, char *disp,
790                 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
791                 char *cbid, void *cbuserdata
792                 ) {
793
794         IAPuts("(");
795 }
796
797
798
799 /*
800  * Called immediately after outputting a multipart bodystructure
801  */
802 void imap_fetch_bodystructure_post(
803                 char *name, char *filename, char *partnum, char *disp,
804                 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
805                 char *cbid, void *cbuserdata
806                 ) {
807         long len;
808         char subtype[128];
809
810         IAPuts(" ");
811
812         /* disposition */
813         len = extract_token(subtype, cbtype, 1, '/', sizeof subtype);
814         IPutStr(subtype, len);
815
816         /* body language */
817         /* IAPuts(" NIL"); We thought we needed this at one point, but maybe we don't... */
818
819         IAPuts(")");
820 }
821
822
823
824 /*
825  * Output the info for a MIME part in the format required by BODYSTRUCTURE.
826  *
827  */
828 void imap_fetch_bodystructure_part(
829                 char *name, char *filename, char *partnum, char *disp,
830                 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
831                 char *cbid, void *cbuserdata
832                 ) {
833
834         int have_cbtype = 0;
835         int have_encoding = 0;
836         int lines = 0;
837         size_t i;
838         char cbmaintype[128];
839         char cbsubtype[128];
840         long cbmaintype_len;
841         long cbsubtype_len;
842
843         if (cbtype != NULL) if (!IsEmptyStr(cbtype)) have_cbtype = 1;
844         if (have_cbtype) {
845                 cbmaintype_len = extract_token(cbmaintype, cbtype, 0, '/', sizeof cbmaintype);
846                 cbsubtype_len = extract_token(cbsubtype, cbtype, 1, '/', sizeof cbsubtype);
847         }
848         else {
849                 strcpy(cbmaintype, "TEXT");
850                 cbmaintype_len = 4;
851                 strcpy(cbsubtype, "PLAIN");
852                 cbsubtype_len = 5;
853         }
854
855         IAPuts("(");
856         IPutStr(cbmaintype, cbmaintype_len);                    /* body type */
857         IAPuts(" ");
858         IPutStr(cbsubtype, cbsubtype_len);                      /* body subtype */
859         IAPuts(" ");
860
861         IAPuts("(");                                            /* begin body parameter list */
862
863         /* "NAME" must appear as the first parameter.  This is not required by IMAP,
864          * but the Asterisk voicemail application blindly assumes that NAME will be in
865          * the first position.  If it isn't, it rejects the message.
866          */
867         if ((name != NULL) && (!IsEmptyStr(name))) {
868                 IAPuts("\"NAME\" ");
869                 IPutStr(name, strlen(name));
870                 IAPuts(" ");
871         }
872
873         IAPuts("\"CHARSET\" ");
874         if ((cbcharset == NULL) || (cbcharset[0] == 0)){
875                 IPutStr(HKEY("US-ASCII"));
876         }
877         else {
878                 IPutStr(cbcharset, strlen(cbcharset));
879         }
880         IAPuts(") ");                                           /* end body parameter list */
881
882         IAPuts("NIL ");                                         /* Body ID */
883         IAPuts("NIL ");                                         /* Body description */
884
885         if ((encoding != NULL) && (encoding[0] != 0))  have_encoding = 1;
886         if (have_encoding) {
887                 IPutStr(encoding, strlen(encoding));
888         }
889         else {
890                 IPutStr(HKEY("7BIT"));
891         }
892         IAPuts(" ");
893
894         /* The next field is the size of the part in bytes. */
895         IAPrintf("%ld ", (long)length); /* bytes */
896
897         /* The next field is the number of lines in the part, if and only
898          * if the part is TEXT.  More gratuitous complexity.
899          */
900         if (!strcasecmp(cbmaintype, "TEXT")) {
901                 if (length) for (i=0; i<length; ++i) {
902                         if (((char *)content)[i] == '\n') ++lines;
903                 }
904                 IAPrintf("%d ", lines);
905         }
906
907         /* More gratuitous complexity */
908         if ((!strcasecmp(cbmaintype, "MESSAGE"))
909            && (!strcasecmp(cbsubtype, "RFC822"))) {
910                 /* FIXME: message/rfc822 also needs to output the envelope structure,
911                  * body structure, and line count of the encapsulated message.  Fortunately
912                  * there are not yet any clients depending on this, so we can get away
913                  * with not implementing it for now.
914                  */
915         }
916
917         /* MD5 value of body part; we can get away with NIL'ing this */
918         IAPuts("NIL ");
919
920         /* Disposition */
921         if ((disp == NULL) || IsEmptyStr(disp)) {
922                 IAPuts("NIL");
923         }
924         else {
925                 IAPuts("(");
926                 IPutStr(disp, strlen(disp));
927                 if ((filename != NULL) && (!IsEmptyStr(filename))) {
928                         IAPuts(" (\"FILENAME\" ");
929                         IPutStr(filename, strlen(filename));
930                         IAPuts(")");
931                 }
932                 IAPuts(")");
933         }
934
935         /* Body language (not defined yet) */
936         IAPuts(" NIL)");
937 }
938
939
940
941 /*
942  * Spew the BODYSTRUCTURE data for a message.
943  *
944  */
945 void imap_fetch_bodystructure (long msgnum, const char *item,
946                 struct CtdlMessage *msg) {
947         const char *rfc822 = NULL;
948         const char *rfc822_body = NULL;
949         size_t rfc822_len;
950         size_t rfc822_headers_len;
951         size_t rfc822_body_len;
952         const char *ptr = NULL;
953         char *pch;
954         char buf[SIZ];
955         int lines = 0;
956
957         /* Handle NULL message gracefully */
958         if (msg == NULL) {
959                 IAPuts("BODYSTRUCTURE (\"TEXT\" \"PLAIN\" "
960                         "(\"CHARSET\" \"US-ASCII\") NIL NIL "
961                         "\"7BIT\" 0 0)");
962                 return;
963         }
964
965         /* For non-RFC822 (ordinary Citadel) messages, this is short and
966          * sweet...
967          */
968         if (msg->cm_format_type != FMT_RFC822) {
969
970                 /* *sigh* We have to RFC822-format the message just to be able
971                  * to measure it.  FIXME use smi cached fields if possible
972                  */
973
974                 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
975                 CtdlOutputPreLoadedMsg(msg, MT_RFC822, 0, 0, 1, SUPPRESS_ENV_TO);
976                 rfc822_len = StrLength(CC->redirect_buffer);
977                 rfc822 = pch = SmashStrBuf(&CC->redirect_buffer);
978
979                 ptr = rfc822;
980                 do {
981                         ptr = cmemreadline(ptr, buf, sizeof buf);
982                         ++lines;
983                         if ((IsEmptyStr(buf)) && (rfc822_body == NULL)) {
984                                 rfc822_body = ptr;
985                         }
986                 } while (*ptr != 0);
987
988                 rfc822_headers_len = rfc822_body - rfc822;
989                 rfc822_body_len = rfc822_len - rfc822_headers_len;
990                 free(pch);
991
992                 IAPuts("BODYSTRUCTURE (\"TEXT\" \"PLAIN\" "
993                        "(\"CHARSET\" \"US-ASCII\") NIL NIL "
994                        "\"7BIT\" ");
995                 IAPrintf(SIZE_T_FMT " %d)", rfc822_body_len, lines);
996
997                 return;
998         }
999
1000         /* For messages already stored in RFC822 format, we have to parse. */
1001         IAPuts("BODYSTRUCTURE ");
1002         mime_parser(CM_RANGE(msg, eMesageText),
1003                     *imap_fetch_bodystructure_part,     /* part */
1004                     *imap_fetch_bodystructure_pre,      /* pre-multi */
1005                     *imap_fetch_bodystructure_post,     /* post-multi */
1006                     NULL,
1007                     1); /* don't decode -- we want it as-is */
1008 }
1009
1010
1011 /*
1012  * imap_do_fetch() calls imap_do_fetch_msg() to output the data of an
1013  * individual message, once it has been selected for output.
1014  */
1015 void imap_do_fetch_msg(int seq, citimap_command *Cmd) {
1016         int i;
1017         citimap *Imap = IMAP;
1018         struct CtdlMessage *msg = NULL;
1019         int body_loaded = 0;
1020
1021         /* Don't attempt to fetch bogus messages or UID's */
1022         if (seq < 1) return;
1023         if (Imap->msgids[seq-1] < 1L) return;
1024
1025         buffer_output();
1026         IAPrintf("* %d FETCH (", seq);
1027
1028         for (i=0; i<Cmd->num_parms; ++i) {
1029
1030                 /* Fetchable without going to the message store at all */
1031                 if (!strcasecmp(Cmd->Params[i].Key, "UID")) {
1032                         imap_fetch_uid(seq);
1033                 }
1034                 else if (!strcasecmp(Cmd->Params[i].Key, "FLAGS")) {
1035                         imap_fetch_flags(seq-1);
1036                 }
1037
1038                 /* Potentially fetchable from cache, if the client requests
1039                  * stuff from the same message several times in a row.
1040                  */
1041                 else if (!strcasecmp(Cmd->Params[i].Key, "RFC822")) {
1042                         imap_fetch_rfc822(Imap->msgids[seq-1], Cmd->Params[i].Key);
1043                 }
1044                 else if (!strcasecmp(Cmd->Params[i].Key, "RFC822.HEADER")) {
1045                         imap_fetch_rfc822(Imap->msgids[seq-1], Cmd->Params[i].Key);
1046                 }
1047                 else if (!strcasecmp(Cmd->Params[i].Key, "RFC822.SIZE")) {
1048                         imap_fetch_rfc822(Imap->msgids[seq-1], Cmd->Params[i].Key);
1049                 }
1050                 else if (!strcasecmp(Cmd->Params[i].Key, "RFC822.TEXT")) {
1051                         imap_fetch_rfc822(Imap->msgids[seq-1], Cmd->Params[i].Key);
1052                 }
1053
1054                 /* BODY fetches do their own fetching and caching too. */
1055                 else if (!strncasecmp(Cmd->Params[i].Key, "BODY[", 5)) {
1056                         imap_fetch_body(Imap->msgids[seq-1], Cmd->Params[i], 0);
1057                 }
1058                 else if (!strncasecmp(Cmd->Params[i].Key, "BODY.PEEK[", 10)) {
1059                         imap_fetch_body(Imap->msgids[seq-1], Cmd->Params[i], 1);
1060                 }
1061
1062                 /* Otherwise, load the message into memory.
1063                  */
1064                 else if (!strcasecmp(Cmd->Params[i].Key, "BODYSTRUCTURE")) {
1065                         if ((msg != NULL) && (!body_loaded)) {
1066                                 CM_Free(msg);   /* need the whole thing */
1067                                 msg = NULL;
1068                         }
1069                         if (msg == NULL) {
1070                                 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1, 1);
1071                                 body_loaded = 1;
1072                         }
1073                         imap_fetch_bodystructure(Imap->msgids[seq-1],
1074                                         Cmd->Params[i].Key, msg);
1075                 }
1076                 else if (!strcasecmp(Cmd->Params[i].Key, "ENVELOPE")) {
1077                         if (msg == NULL) {
1078                                 msg = CtdlFetchMessage(Imap->msgids[seq-1], 0, 1);
1079                                 body_loaded = 0;
1080                         }
1081                         imap_fetch_envelope(msg);
1082                 }
1083                 else if (!strcasecmp(Cmd->Params[i].Key, "INTERNALDATE")) {
1084                         if (msg == NULL) {
1085                                 msg = CtdlFetchMessage(Imap->msgids[seq-1], 0, 1);
1086                                 body_loaded = 0;
1087                         }
1088                         imap_fetch_internaldate(msg);
1089                 }
1090
1091                 if (i != Cmd->num_parms-1) IAPuts(" ");
1092         }
1093
1094         IAPuts(")\r\n");
1095         unbuffer_output();
1096         if (msg != NULL) {
1097                 CM_Free(msg);
1098         }
1099 }
1100
1101
1102
1103 /*
1104  * imap_fetch() calls imap_do_fetch() to do its actual work, once it's
1105  * validated and boiled down the request a bit.
1106  */
1107 void imap_do_fetch(citimap_command *Cmd) {
1108         citimap *Imap = IMAP;
1109         int i;
1110 #if 0
1111 /* debug output the parsed vector */
1112         {
1113                 int i;
1114                 syslog(LOG_DEBUG, "imap: ----- %ld params", Cmd->num_parms);
1115
1116         for (i=0; i < Cmd->num_parms; i++) {
1117                 if (Cmd->Params[i].len != strlen(Cmd->Params[i].Key))
1118                         syslog(LOG_DEBUG, "imap: *********** %ld != %ld : %s",
1119                                     Cmd->Params[i].len, 
1120                                     strlen(Cmd->Params[i].Key),
1121                                     Cmd->Params[i].Key);
1122                 else
1123                         syslog(LOG_DEBUG, "imap: %ld : %s",
1124                                     Cmd->Params[i].len, 
1125                                     Cmd->Params[i].Key);
1126         }}
1127
1128 #endif
1129
1130         if (Imap->num_msgs > 0) {
1131                 for (i = 0; i < Imap->num_msgs; ++i) {
1132
1133                         /* Abort the fetch loop if the session breaks.
1134                          * This is important for users who keep mailboxes
1135                          * that are too big *and* are too impatient to
1136                          * let them finish loading.  :)
1137                          */
1138                         if (CC->kill_me) return;
1139
1140                         /* Get any message marked for fetch. */
1141                         if (Imap->flags[i] & IMAP_SELECTED) {
1142                                 imap_do_fetch_msg(i+1, Cmd);
1143                         }
1144                 }
1145         }
1146 }
1147
1148
1149
1150 /*
1151  * Back end for imap_handle_macros()
1152  * Note that this function *only* looks at the beginning of the string.  It
1153  * is not a generic search-and-replace function.
1154  */
1155 void imap_macro_replace(StrBuf *Buf, long where, 
1156                         StrBuf *TmpBuf,
1157                         char *find, long findlen, 
1158                         char *replace, long replacelen) 
1159 {
1160
1161         if (StrLength(Buf) - where > findlen)
1162                 return;
1163
1164         if (!strncasecmp(ChrPtr(Buf) + where, find, findlen)) {
1165                 if (ChrPtr(Buf)[where + findlen] == ' ') {
1166                         StrBufPlain(TmpBuf, replace, replacelen);
1167                         StrBufAppendBufPlain(TmpBuf, HKEY(" "), 0);
1168                         StrBufReplaceToken(Buf, where, findlen, 
1169                                            SKEY(TmpBuf));
1170                 }
1171                 if (where + findlen == StrLength(Buf)) {
1172                         StrBufReplaceToken(Buf, where, findlen, 
1173                                            replace, replacelen);
1174                 }
1175         }
1176 }
1177
1178
1179
1180 /*
1181  * Handle macros embedded in FETCH data items.
1182  * (What the heck are macros doing in a wire protocol?  Are we trying to save
1183  * the computer at the other end the trouble of typing a lot of characters?)
1184  */
1185 void imap_handle_macros(citimap_command *Cmd) {
1186         long i;
1187         int nest = 0;
1188         StrBuf *Tmp = NewStrBuf();
1189
1190         for (i=0; i < StrLength(Cmd->CmdBuf); ++i) {
1191                 char ch = ChrPtr(Cmd->CmdBuf)[i];
1192                 if ((ch=='(') ||
1193                     (ch=='[') ||
1194                     (ch=='<') ||
1195                     (ch=='{')) ++nest;
1196                 else if ((ch==')') ||
1197                          (ch==']') ||
1198                          (ch=='>') ||
1199                          (ch=='}')) --nest;
1200
1201                 if (nest <= 0) {
1202                         imap_macro_replace(Cmd->CmdBuf, i,
1203                                            Tmp, 
1204                                            HKEY("ALL"),
1205                                            HKEY("FLAGS INTERNALDATE RFC822.SIZE ENVELOPE")
1206                         );
1207                         imap_macro_replace(Cmd->CmdBuf, i,
1208                                            Tmp, 
1209                                            HKEY("BODY"),
1210                                            HKEY("BODYSTRUCTURE")
1211                         );
1212                         imap_macro_replace(Cmd->CmdBuf, i,
1213                                            Tmp, 
1214                                            HKEY("FAST"),
1215                                            HKEY("FLAGS INTERNALDATE RFC822.SIZE")
1216                         );
1217                         imap_macro_replace(Cmd->CmdBuf, i,
1218                                            Tmp, 
1219                                            HKEY("FULL"),
1220                                            HKEY("FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODY")
1221                         );
1222                 }
1223         }
1224         FreeStrBuf(&Tmp);
1225 }
1226
1227
1228 /*
1229  * Break out the data items requested, possibly a parenthesized list.
1230  * Returns the number of data items, or -1 if the list is invalid.
1231  * NOTE: this function alters the string it is fed, and uses it as a buffer
1232  * to hold the data for the pointers it returns.
1233  */
1234 int imap_extract_data_items(citimap_command *Cmd) 
1235 {
1236         int nArgs;
1237         int nest = 0;
1238         const char *pch, *end;
1239
1240         /* Convert all whitespace to ordinary space characters. */
1241         pch = ChrPtr(Cmd->CmdBuf);
1242         end = pch + StrLength(Cmd->CmdBuf);
1243
1244         while (pch < end)
1245         {
1246                 if (isspace(*pch)) 
1247                         StrBufPeek(Cmd->CmdBuf, pch, 0, ' ');
1248                 pch++;
1249         }
1250
1251         /* Strip leading and trailing whitespace, then strip leading and
1252          * trailing parentheses if it's a list
1253          */
1254         StrBufTrim(Cmd->CmdBuf);
1255         pch = ChrPtr(Cmd->CmdBuf);
1256         if ( (pch[0]=='(') && 
1257              (pch[StrLength(Cmd->CmdBuf)-1]==')') ) 
1258         {
1259                 StrBufCutRight(Cmd->CmdBuf, 1);
1260                 StrBufCutLeft(Cmd->CmdBuf, 1);
1261                 StrBufTrim(Cmd->CmdBuf);
1262         }
1263
1264         /* Parse any macro data items */
1265         imap_handle_macros(Cmd);
1266
1267         /*
1268          * Now break out the data items.  We throw in one trailing space in
1269          * order to avoid having to break out the last one manually.
1270          */
1271         nArgs = StrLength(Cmd->CmdBuf) / 10 + 10;
1272         nArgs = CmdAdjust(Cmd, nArgs, 0);
1273         Cmd->num_parms = 0;
1274         Cmd->Params[Cmd->num_parms].Key = pch = ChrPtr(Cmd->CmdBuf);
1275         end = Cmd->Params[Cmd->num_parms].Key + StrLength(Cmd->CmdBuf);
1276
1277         while (pch < end) 
1278         {
1279                 if ((*pch=='(') ||
1280                     (*pch=='[') ||
1281                     (*pch=='<') ||
1282                     (*pch=='{'))
1283                         ++nest;
1284
1285                 else if ((*pch==')') ||
1286                          (*pch==']') ||
1287                          (*pch=='>') ||
1288                          (*pch=='}'))
1289                         --nest;
1290
1291                 if ((nest <= 0) && (*pch==' ')) {
1292                         StrBufPeek(Cmd->CmdBuf, pch, 0, '\0');
1293                         Cmd->Params[Cmd->num_parms].len = 
1294                                 pch - Cmd->Params[Cmd->num_parms].Key;
1295
1296                         if (Cmd->num_parms + 1 >= Cmd->avail_parms) {
1297                                 nArgs = CmdAdjust(Cmd, nArgs * 2, 1);
1298                         }
1299                         Cmd->num_parms++;                       
1300                         Cmd->Params[Cmd->num_parms].Key = ++pch;
1301                 }
1302                 else if (pch + 1 == end) {
1303                         Cmd->Params[Cmd->num_parms].len = 
1304                                 pch - Cmd->Params[Cmd->num_parms].Key + 1;
1305
1306                         Cmd->num_parms++;                       
1307                 }
1308                 pch ++;
1309         }
1310         return Cmd->num_parms;
1311
1312 }
1313
1314
1315 /*
1316  * One particularly hideous aspect of IMAP is that we have to allow the client
1317  * to specify arbitrary ranges and/or sets of messages to fetch.  Citadel IMAP
1318  * handles this by setting the IMAP_SELECTED flag for each message specified in
1319  * the ranges/sets, then looping through the message array, outputting messages
1320  * with the flag set.  We don't bother returning an error if an out-of-range
1321  * number is specified (we just return quietly) because any client braindead
1322  * enough to request a bogus message number isn't going to notice the
1323  * difference anyway.
1324  *
1325  * This function clears out the IMAP_SELECTED bits, then sets that bit for each
1326  * message included in the specified range.
1327  *
1328  * Set is_uid to 1 to fetch by UID instead of sequence number.
1329  */
1330 void imap_pick_range(const char *supplied_range, int is_uid) {
1331         citimap *Imap = IMAP;
1332         int i;
1333         int num_sets;
1334         int s;
1335         char setstr[SIZ], lostr[SIZ], histr[SIZ];
1336         long lo, hi;
1337         char actual_range[SIZ];
1338
1339         /* 
1340          * Handle the "ALL" macro
1341          */
1342         if (!strcasecmp(supplied_range, "ALL")) {
1343                 safestrncpy(actual_range, "1:*", sizeof actual_range);
1344         }
1345         else {
1346                 safestrncpy(actual_range, supplied_range, sizeof actual_range);
1347         }
1348
1349         /*
1350          * Clear out the IMAP_SELECTED flags for all messages.
1351          */
1352         for (i = 0; i < Imap->num_msgs; ++i) {
1353                 Imap->flags[i] = Imap->flags[i] & ~IMAP_SELECTED;
1354         }
1355
1356         /*
1357          * Now set it for all specified messages.
1358          */
1359         num_sets = num_tokens(actual_range, ',');
1360         for (s=0; s<num_sets; ++s) {
1361                 extract_token(setstr, actual_range, s, ',', sizeof setstr);
1362
1363                 extract_token(lostr, setstr, 0, ':', sizeof lostr);
1364                 if (num_tokens(setstr, ':') >= 2) {
1365                         extract_token(histr, setstr, 1, ':', sizeof histr);
1366                         if (!strcmp(histr, "*")) snprintf(histr, sizeof histr, "%ld", LONG_MAX);
1367                 } 
1368                 else {
1369                         safestrncpy(histr, lostr, sizeof histr);
1370                 }
1371                 lo = atol(lostr);
1372                 hi = atol(histr);
1373
1374                 /* Loop through the array, flipping bits where appropriate */
1375                 for (i = 1; i <= Imap->num_msgs; ++i) {
1376                         if (is_uid) {   /* fetch by sequence number */
1377                                 if ( (Imap->msgids[i-1]>=lo)
1378                                    && (Imap->msgids[i-1]<=hi)) {
1379                                         Imap->flags[i-1] |= IMAP_SELECTED;
1380                                 }
1381                         }
1382                         else {          /* fetch by uid */
1383                                 if ( (i>=lo) && (i<=hi)) {
1384                                         Imap->flags[i-1] |= IMAP_SELECTED;
1385                                 }
1386                         }
1387                 }
1388         }
1389 }
1390
1391
1392
1393 /*
1394  * This function is called by the main command loop.
1395  */
1396 void imap_fetch(int num_parms, ConstStr *Params) {
1397         citimap_command Cmd;
1398         int num_items;
1399         
1400         if (num_parms < 4) {
1401                 IReply("BAD invalid parameters");
1402                 return;
1403         }
1404
1405         imap_pick_range(Params[2].Key, 0);
1406
1407         memset(&Cmd, 0, sizeof(citimap_command));
1408         Cmd.CmdBuf = NewStrBufPlain(NULL, StrLength(IMAP->Cmd.CmdBuf));
1409         MakeStringOf(Cmd.CmdBuf, 3);
1410
1411         num_items = imap_extract_data_items(&Cmd);
1412         if (num_items < 1) {
1413                 IReply("BAD invalid data item list");
1414                 FreeStrBuf(&Cmd.CmdBuf);
1415                 free(Cmd.Params);
1416                 return;
1417         }
1418
1419         imap_do_fetch(&Cmd);
1420         IReply("OK FETCH completed");
1421         FreeStrBuf(&Cmd.CmdBuf);
1422         free(Cmd.Params);
1423 }
1424
1425 /*
1426  * This function is called by the main command loop.
1427  */
1428 void imap_uidfetch(int num_parms, ConstStr *Params) {
1429         citimap_command Cmd;
1430         int num_items;
1431         int i;
1432         int have_uid_item = 0;
1433
1434         if (num_parms < 5) {
1435                 IReply("BAD invalid parameters");
1436                 return;
1437         }
1438
1439         imap_pick_range(Params[3].Key, 1);
1440
1441         memset(&Cmd, 0, sizeof(citimap_command));
1442         Cmd.CmdBuf = NewStrBufPlain(NULL, StrLength(IMAP->Cmd.CmdBuf));
1443
1444         MakeStringOf(Cmd.CmdBuf, 4);
1445 #if 0
1446         syslog(LOG_DEBUG, "imap: -------%s--------", ChrPtr(Cmd.CmdBuf));
1447 #endif
1448         num_items = imap_extract_data_items(&Cmd);
1449         if (num_items < 1) {
1450                 IReply("BAD invalid data item list");
1451                 FreeStrBuf(&Cmd.CmdBuf);
1452                 free(Cmd.Params);
1453                 return;
1454         }
1455
1456         /* If the "UID" item was not included, we include it implicitly
1457          * (at the beginning) because this is a UID FETCH command
1458          */
1459         for (i=0; i<num_items; ++i) {
1460                 if (!strcasecmp(Cmd.Params[i].Key, "UID")) ++have_uid_item;
1461         }
1462         if (have_uid_item == 0) {
1463                 if (Cmd.num_parms + 1 >= Cmd.avail_parms)
1464                         CmdAdjust(&Cmd, Cmd.avail_parms + 1, 1);
1465                 memmove(&Cmd.Params[1], 
1466                         &Cmd.Params[0], 
1467                         sizeof(ConstStr) * Cmd.num_parms);
1468
1469                 Cmd.num_parms++;
1470                 Cmd.Params[0] = (ConstStr){HKEY("UID")};
1471         }
1472
1473         imap_do_fetch(&Cmd);
1474         IReply("OK UID FETCH completed");
1475         FreeStrBuf(&Cmd.CmdBuf);
1476         free(Cmd.Params);
1477 }
1478
1479