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