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