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