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