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