]> code.citadel.org Git - citadel.git/blob - citadel/imap_fetch.c
* HEADER.FIELDS...
[citadel.git] / citadel / imap_fetch.c
1 /*
2  * $Id$
3  *
4  * Implements the FETCH command in IMAP.
5  * This command is way too convoluted.  Marc Crispin is a fscking idiot.
6  *
7  */
8
9
10 #include "sysdep.h"
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <stdio.h>
14 #include <fcntl.h>
15 #include <signal.h>
16 #include <pwd.h>
17 #include <errno.h>
18 #include <sys/types.h>
19 #include <sys/time.h>
20 #include <sys/wait.h>
21 #include <ctype.h>
22 #include <string.h>
23 #include <limits.h>
24 #include "citadel.h"
25 #include "server.h"
26 #include <time.h>
27 #include "sysdep_decls.h"
28 #include "citserver.h"
29 #include "support.h"
30 #include "config.h"
31 #include "dynloader.h"
32 #include "room_ops.h"
33 #include "user_ops.h"
34 #include "policy.h"
35 #include "database.h"
36 #include "msgbase.h"
37 #include "tools.h"
38 #include "internet_addressing.h"
39 #include "mime_parser.h"
40 #include "serv_imap.h"
41 #include "imap_tools.h"
42 #include "imap_fetch.h"
43 #include "genstamp.h"
44
45
46
47 struct imap_fetch_part {
48         char desired_section[SIZ];
49         FILE *output_fp;
50 };
51
52 /*
53  * Individual field functions for imap_do_fetch_msg() ...
54  */
55
56
57
58 void imap_fetch_uid(int seq) {
59         cprintf("UID %ld", IMAP->msgids[seq-1]);
60 }
61
62 void imap_fetch_flags(int seq) {
63         cprintf("FLAGS (");
64         if (IMAP->flags[seq] & IMAP_DELETED) cprintf("\\Deleted ");
65         cprintf(")");
66 }
67
68 void imap_fetch_internaldate(struct CtdlMessage *msg) {
69         char buf[SIZ];
70         time_t msgdate;
71
72         if (msg->cm_fields['T'] != NULL) {
73                 msgdate = atol(msg->cm_fields['T']);
74         }
75         else {
76                 msgdate = time(NULL);
77         }
78
79         datestring(buf, msgdate, DATESTRING_IMAP);
80         cprintf("INTERNALDATE \"%s\"", buf);
81 }
82
83
84 /*
85  * Fetch RFC822-formatted messages.
86  *
87  * 'whichfmt' should be set to one of:
88  *      "RFC822"        entire message
89  *      "RFC822.HEADER" headers only (with trailing blank line)
90  *      "RFC822.SIZE"   size of translated message
91  *      "RFC822.TEXT"   body only (without leading blank line)
92  */
93 void imap_fetch_rfc822(int msgnum, char *whichfmt, struct CtdlMessage *msg) {
94         FILE *tmp;
95         char buf[1024];
96         char *ptr;
97         long headers_size, text_size, total_size;
98         long bytes_remaining = 0;
99         long blocksize;
100
101         tmp = tmpfile();
102         if (tmp == NULL) {
103                 lprintf(1, "Cannot open temp file: %s\n", strerror(errno));
104                 return;
105         }
106
107         /*
108          * Load the message into a temp file for translation and measurement
109          */ 
110         CtdlRedirectOutput(tmp, -1);
111         CtdlOutputPreLoadedMsg(msg, msgnum, MT_RFC822, 0, 0, 1);
112         CtdlRedirectOutput(NULL, -1);
113         if (!is_valid_message(msg)) {
114                 lprintf(1, "WARNING: output clobbered the message!\n");
115         }
116
117         /*
118          * Now figure out where the headers/text break is.  IMAP considers the
119          * intervening blank line to be part of the headers, not the text.
120          */
121         rewind(tmp);
122         headers_size = 0L;
123         do {
124                 ptr = fgets(buf, sizeof buf, tmp);
125                 if (ptr != NULL) {
126                         striplt(buf);
127                         if (strlen(buf) == 0) headers_size = ftell(tmp);
128                 }
129         } while ( (headers_size == 0L) && (ptr != NULL) );
130         fseek(tmp, 0L, SEEK_END);
131         total_size = ftell(tmp);
132         text_size = total_size - headers_size;
133
134         if (!strcasecmp(whichfmt, "RFC822.SIZE")) {
135                 cprintf("RFC822.SIZE %ld", total_size);
136                 fclose(tmp);
137                 return;
138         }
139
140         else if (!strcasecmp(whichfmt, "RFC822")) {
141                 bytes_remaining = total_size;
142                 rewind(tmp);
143         }
144
145         else if (!strcasecmp(whichfmt, "RFC822.HEADER")) {
146                 bytes_remaining = headers_size;
147                 rewind(tmp);
148         }
149
150         else if (!strcasecmp(whichfmt, "RFC822.TEXT")) {
151                 bytes_remaining = text_size;
152                 fseek(tmp, headers_size, SEEK_SET);
153         }
154
155         cprintf("%s {%ld}\r\n", whichfmt, bytes_remaining);
156         blocksize = sizeof(buf);
157         while (bytes_remaining > 0L) {
158                 if (blocksize > bytes_remaining) blocksize = bytes_remaining;
159                 fread(buf, blocksize, 1, tmp);
160                 client_write(buf, blocksize);
161                 bytes_remaining = bytes_remaining - blocksize;
162         }
163
164         fclose(tmp);
165 }
166
167
168
169 /*
170  * Load a specific part of a message into the temp file to be output to a
171  * client.  FIXME we can handle parts like "2" and "2.1" and even "2.MIME"
172  * but we still can't handle "2.HEADER" (which might not be a problem, because
173  * we currently don't have the ability to break out nested RFC822's anyway).
174  *
175  * Note: mime_parser() was called with dont_decode set to 1, so we have the
176  * luxury of simply spewing without having to re-encode.
177  */
178 void imap_load_part(char *name, char *filename, char *partnum, char *disp,
179                     void *content, char *cbtype, size_t length, char *encoding,
180                     void *cbuserdata)
181 {
182         struct imap_fetch_part *imfp;
183         char mbuf2[1024];
184
185         imfp = (struct imap_fetch_part *)cbuserdata;
186
187         if (!strcasecmp(partnum, imfp->desired_section)) {
188                 fwrite(content, length, 1, imfp->output_fp);
189         }
190
191         sprintf(mbuf2, "%s.MIME", partnum);
192
193         if (!strcasecmp(imfp->desired_section, mbuf2)) {
194                 fprintf(imfp->output_fp, "Content-type: %s", cbtype);
195                 if (strlen(name) > 0)
196                         fprintf(imfp->output_fp, "; name=\"%s\"", name);
197                 fprintf(imfp->output_fp, "\r\n");
198                 if (strlen(encoding) > 0)
199                         fprintf(imfp->output_fp,
200                                 "Content-Transfer-Encoding: %s\r\n", encoding);
201                 if (strlen(encoding) > 0) {
202                         fprintf(imfp->output_fp, "Content-Disposition: %s",
203                                         disp);
204                         if (strlen(filename) > 0) {
205                                 fprintf(imfp->output_fp, "; filename=\"%s\"",
206                                         filename);
207                         }
208                         fprintf(imfp->output_fp, "\r\n");
209                 }
210                 fprintf(imfp->output_fp, "Content-Length: %d\r\n", length);
211                 fprintf(imfp->output_fp, "\r\n");
212         }
213                         
214
215 }
216
217
218 /* 
219  * Called by imap_fetch_envelope() to output the "From" field.
220  * This is in its own function because its logic is kind of complex.  We
221  * really need to make this suck less.
222  */
223 void imap_output_envelope_from(struct CtdlMessage *msg) {
224         char user[1024], node[1024], name[1024];
225
226         cprintf("((");                          /* open double-parens */
227         imap_strout(msg->cm_fields['A']);       /* personal name */
228         cprintf(" NIL ");                       /* source route (not used) */
229
230         if (msg->cm_fields['F'] != NULL) {
231                 process_rfc822_addr(msg->cm_fields['F'], user, node, name);
232                 imap_strout(user);              /* mailbox name (user id) */
233                 cprintf(" ");
234                 if (!strcasecmp(node, config.c_nodename)) {
235                         imap_strout(config.c_fqdn);
236                 }
237                 else {
238                         imap_strout(node);              /* host name */
239                 }
240         }
241         else {
242                 imap_strout(msg->cm_fields['A']); /* mailbox name (user id) */
243                 cprintf(" ");
244                 imap_strout(msg->cm_fields['N']);       /* host name */
245         }
246         
247         cprintf(")) ");                         /* close double-parens */
248 }
249
250
251 /*
252  * Implements the ENVELOPE fetch item
253  * 
254  * FIXME ... we only output some of the fields right now.  Definitely need
255  *           to do all of them.  Accurately, too.
256  *
257  * Note that the imap_strout() function can cleverly output NULL fields as NIL,
258  * so we don't have to check for that condition like we do elsewhere.
259  */
260 void imap_fetch_envelope(long msgnum, struct CtdlMessage *msg) {
261         char datestringbuf[SIZ];
262         time_t msgdate;
263         char *fieldptr = NULL;
264
265         /* Parse the message date into an IMAP-format date string */
266         if (msg->cm_fields['T'] != NULL) {
267                 msgdate = atol(msg->cm_fields['T']);
268         }
269         else {
270                 msgdate = time(NULL);
271         }
272         datestring(datestringbuf, msgdate, DATESTRING_IMAP);
273
274         /* Now start spewing data fields.  The order is important, as it is
275          * defined by the protocol specification.  Nonexistent fields must
276          * be output as NIL, existent fields must be quoted or literalled.
277          * The imap_strout() function conveniently does all this for us.
278          */
279         cprintf("ENVELOPE (");
280
281         /* Date */
282         imap_strout(datestringbuf);
283         cprintf(" ");
284
285         /* Subject */
286         imap_strout(msg->cm_fields['U']);
287         cprintf(" ");
288
289         /* From */
290         imap_output_envelope_from(msg);
291
292         /* Sender */
293         if (0) {
294                 /* FIXME ... check for a *real* Sender: field */
295         }
296         else {
297                 imap_output_envelope_from(msg);
298         }
299
300         /* Reply-to */
301         if (0) {
302                 /* FIXME ... check for a *real* Reply-to: field */
303         }
304         else {
305                 imap_output_envelope_from(msg);
306         }
307
308         cprintf("NIL ");        /* to */
309
310         cprintf("NIL ");        /* cc */
311
312         cprintf("NIL ");        /* bcc */
313
314         /* In-reply-to */
315         fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "In-reply-to");
316         imap_strout(fieldptr);
317         cprintf(" ");
318         if (fieldptr != NULL) phree(fieldptr);
319
320         /* message ID */
321         imap_strout(msg->cm_fields['I']);
322
323         cprintf(") ");
324 }
325
326
327 /*
328  * Strip any non header information out of a chunk of RFC822 data on disk,
329  * then boil it down to just the fields we want.
330  */
331 void imap_strip_headers(FILE *fp, char *section) {
332         char buf[1024];
333         char *which_fields = NULL;
334         int doing_headers = 0;
335         int headers_not = 0;
336         char *parms[SIZ];
337         int num_parms = 0;
338         int i;
339
340         if (!strncasecmp(which_fields, "HEADER.FIELDS", 13))
341                 doing_headers = 1;
342         if (!strncasecmp(which_fields, "HEADER.FIELDS.NOT", 17))
343                 headers_not = 1;
344
345         which_fields = strdoop(section);
346         for (i=0; i<strlen(which_fields); ++i) {
347                 if (which_fields[i]=='(')
348                         strcpy(which_fields, &which_fields[i+1]);
349         }
350         for (i=0; i<strlen(which_fields); ++i) {
351                 if (which_fields[i]==')')
352                         which_fields[i] = 0;
353         }
354         num_parms = imap_parameterize(parms, which_fields);
355         for (i=0; i<num_parms; ++i) {
356                 lprintf(9, "parm[%d] = <%s>\n", i, parms[i]);
357         }       /* FIXME do something here! */
358
359         phree(which_fields);
360
361         rewind(fp);
362         while (fgets(buf, sizeof buf, fp) != NULL) {
363                 striplt(buf);
364                 if (strlen(buf) == 0) {
365                         fflush(fp);
366                         ftruncate(fileno(fp), ftell(fp));
367                 }
368         }
369         fflush(fp);
370         fprintf(fp, "\r\n");    /* add the trailing newline */
371         rewind(fp);
372 }
373
374
375 /*
376  * Implements the BODY and BODY.PEEK fetch items
377  */
378 void imap_fetch_body(long msgnum, char *item, int is_peek,
379                 struct CtdlMessage *msg) {
380         char section[1024];
381         char partial[1024];
382         int is_partial = 0;
383         char buf[1024];
384         int i;
385         FILE *tmp;
386         long bytes_remaining = 0;
387         long blocksize;
388         long pstart, pbytes;
389         struct imap_fetch_part imfp;
390
391         /* extract section */
392         strcpy(section, item);
393         for (i=0; i<strlen(section); ++i) {
394                 if (section[i]=='[') strcpy(section, &section[i+1]);
395         }
396         for (i=0; i<strlen(section); ++i) {
397                 if (section[i]==']') section[i] = 0;
398         }
399         lprintf(9, "Section is %s\n", section);
400
401         /* extract partial */
402         strcpy(partial, item);
403         for (i=0; i<strlen(partial); ++i) {
404                 if (partial[i]=='<') {
405                         strcpy(partial, &partial[i+1]);
406                         is_partial = 1;
407                 }
408         }
409         for (i=0; i<strlen(partial); ++i) {
410                 if (partial[i]=='>') partial[i] = 0;
411         }
412         if (is_partial == 0) strcpy(partial, "");
413         if (strlen(partial) > 0) lprintf(9, "Partial is %s\n", partial);
414
415         tmp = tmpfile();
416         if (tmp == NULL) {
417                 lprintf(1, "Cannot open temp file: %s\n", strerror(errno));
418                 return;
419         }
420
421         /* Now figure out what the client wants, and get it */
422
423         if (!strcmp(section, "")) {             /* the whole thing */
424                 CtdlRedirectOutput(tmp, -1);
425                 CtdlOutputPreLoadedMsg(msg, msgnum, MT_RFC822, 0, 0, 1);
426                 CtdlRedirectOutput(NULL, -1);
427         }
428
429         /*
430          * Be obnoxious and send the entire header, even if the client only
431          * asks for certain fields.  FIXME this shortcut later.
432          */
433         else if (!strncasecmp(section, "HEADER", 6)) {
434                 CtdlRedirectOutput(tmp, -1);
435                 CtdlOutputPreLoadedMsg(msg, msgnum, MT_RFC822, 1, 0, 1);
436                 CtdlRedirectOutput(NULL, -1);
437                 imap_strip_headers(tmp, section);
438         }
439
440         /*
441          * Anything else must be a part specifier.
442          * (Note value of 1 passed as 'dont_decode' so client gets it encoded)
443          */
444         else {
445                 safestrncpy(imfp.desired_section, section,
446                                 sizeof(imfp.desired_section));
447                 imfp.output_fp = tmp;
448
449                 mime_parser(msg->cm_fields['M'], NULL,
450                                 *imap_load_part, NULL, NULL,
451                                 (void *)&imfp,
452                                 1);
453         }
454
455
456         fseek(tmp, 0L, SEEK_END);
457         bytes_remaining = ftell(tmp);
458
459         if (is_partial == 0) {
460                 rewind(tmp);
461                 cprintf("BODY[%s] {%ld}\r\n", section, bytes_remaining);
462         }
463         else {
464                 sscanf(partial, "%ld.%ld", &pstart, &pbytes);
465                 if ((bytes_remaining - pstart) < pbytes) {
466                         pbytes = bytes_remaining - pstart;
467                 }
468                 fseek(tmp, pstart, SEEK_SET);
469                 bytes_remaining = pbytes;
470                 cprintf("BODY[%s] {%ld}<%ld>\r\n",
471                         section, bytes_remaining, pstart);
472         }
473
474         blocksize = sizeof(buf);
475         while (bytes_remaining > 0L) {
476                 if (blocksize > bytes_remaining) blocksize = bytes_remaining;
477                 fread(buf, blocksize, 1, tmp);
478                 client_write(buf, blocksize);
479                 bytes_remaining = bytes_remaining - blocksize;
480         }
481
482         fclose(tmp);
483
484         if (is_peek) {
485                 /* FIXME set the last read pointer or something */
486         }
487 }
488
489 /*
490  * Called immediately before outputting a multipart bodystructure
491  */
492 void imap_fetch_bodystructure_pre(
493                 char *name, char *filename, char *partnum, char *disp,
494                 void *content, char *cbtype, size_t length, char *encoding,
495                 void *cbuserdata
496                 ) {
497
498         cprintf("(");
499 }
500
501
502
503 /*
504  * Called immediately after outputting a multipart bodystructure
505  */
506 void imap_fetch_bodystructure_post(
507                 char *name, char *filename, char *partnum, char *disp,
508                 void *content, char *cbtype, size_t length, char *encoding,
509                 void *cbuserdata
510                 ) {
511
512         char subtype[SIZ];
513
514         extract_token(subtype, cbtype, 1, '/');
515         imap_strout(subtype);
516         cprintf(")");
517 }
518
519
520
521 /*
522  * Output the info for a MIME part in the format required by BODYSTRUCTURE.
523  *
524  */
525 void imap_fetch_bodystructure_part(
526                 char *name, char *filename, char *partnum, char *disp,
527                 void *content, char *cbtype, size_t length, char *encoding,
528                 void *cbuserdata
529                 ) {
530
531         char buf[SIZ];
532         int have_cbtype = 0;
533         int have_encoding = 0;
534
535         cprintf("(");
536
537         if (cbtype != NULL) if (strlen(cbtype)>0) have_cbtype = 1;
538
539         if (have_cbtype) {
540                 extract_token(buf, cbtype, 0, '/');
541                 imap_strout(buf);
542                 cprintf(" ");
543                 extract_token(buf, cbtype, 1, '/');
544                 imap_strout(buf);
545                 cprintf(" ");
546         }
547         else {
548                 cprintf("\"TEXT\" \"PLAIN\" ");
549         }
550
551         cprintf("(\"CHARSET\" \"US-ASCII\"");
552
553         if (name != NULL) if (strlen(name)>0) {
554                 cprintf(" \"NAME\" ");
555                 imap_strout(name);
556         }
557
558         if (filename != NULL) if (strlen(filename)>0) {
559                 cprintf(" \"FILENAME\" ");
560                 imap_strout(name);
561         }
562
563         cprintf(") ");
564
565         cprintf("NIL NIL ");
566
567         if (encoding != NULL) if (strlen(encoding) > 0)  have_encoding = 1;
568
569         if (have_encoding) {
570                 imap_strout(encoding);
571         }
572         else {
573                 imap_strout("7BIT");
574         }
575         cprintf(" ");
576
577         cprintf("%ld ", length);        /* bytes */
578         cprintf("NIL) ");               /* lines */
579 }
580
581
582
583 /*
584  * Spew the BODYSTRUCTURE data for a message.  (Do you need a silencer if
585  * you're going to shoot a MIME?  Do you need a reason to shoot Mark Crispin?
586  * No, and no.)
587  *
588  */
589 void imap_fetch_bodystructure (long msgnum, char *item,
590                 struct CtdlMessage *msg) {
591         FILE *tmp;
592         char buf[1024];
593         long lines = 0L;
594         long bytes = 0L;
595
596         /* For non-RFC822 (ordinary Citadel) messages, this is short and
597          * sweet...
598          */
599         if (msg->cm_format_type != FMT_RFC822) {
600
601                 /* *sigh* We have to RFC822-format the message just to be able
602                  * to measure it.
603                  */
604                 tmp = tmpfile();
605                 if (tmp == NULL) return;
606                 CtdlRedirectOutput(tmp, -1);
607                 CtdlOutputPreLoadedMsg(msg, msgnum, MT_RFC822, 0, 0, 1);
608                 CtdlRedirectOutput(NULL, -1);
609
610                 rewind(tmp);
611                 while (fgets(buf, sizeof buf, tmp) != NULL) ++lines;
612                 bytes = ftell(tmp);
613                 fclose(tmp);
614
615                 cprintf("BODYSTRUCTURE (\"TEXT\" \"PLAIN\" "
616                         "(\"CHARSET\" \"US-ASCII\") NIL NIL "
617                         "\"7BIT\" %ld %ld)", bytes, lines);
618
619                 return;
620         }
621
622         /* For messages already stored in RFC822 format, we have to parse. */
623         cprintf("BODYSTRUCTURE ");
624         mime_parser(msg->cm_fields['M'],
625                         NULL,
626                         *imap_fetch_bodystructure_part, /* part */
627                         *imap_fetch_bodystructure_pre,  /* pre-multi */
628                         *imap_fetch_bodystructure_post, /* post-multi */
629                         NULL,
630                         0);
631 }
632
633
634
635
636
637
638 /*
639  * imap_do_fetch() calls imap_do_fetch_msg() to output the deta of an
640  * individual message, once it has been successfully loaded from disk.
641  */
642 void imap_do_fetch_msg(int seq, struct CtdlMessage *msg,
643                         int num_items, char **itemlist) {
644         int i;
645
646         cprintf("* %d FETCH (", seq);
647
648         for (i=0; i<num_items; ++i) {
649
650                 if (!strncasecmp(itemlist[i], "BODY[", 5)) {
651                         imap_fetch_body(IMAP->msgids[seq-1], itemlist[i],
652                                         0, msg);
653                 }
654                 else if (!strncasecmp(itemlist[i], "BODY.PEEK[", 10)) {
655                         imap_fetch_body(IMAP->msgids[seq-1], itemlist[i],
656                                         1, msg);
657                 }
658                 else if (!strcasecmp(itemlist[i], "BODYSTRUCTURE")) {
659                         imap_fetch_bodystructure(IMAP->msgids[seq-1],
660                                         itemlist[i], msg);
661                 }
662                 else if (!strcasecmp(itemlist[i], "ENVELOPE")) {
663                         imap_fetch_envelope(IMAP->msgids[seq-1], msg);
664                 }
665                 else if (!strcasecmp(itemlist[i], "FLAGS")) {
666                         imap_fetch_flags(seq-1);
667                 }
668                 else if (!strcasecmp(itemlist[i], "INTERNALDATE")) {
669                         imap_fetch_internaldate(msg);
670                 }
671                 else if (!strcasecmp(itemlist[i], "RFC822")) {
672                         imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i], msg);
673                 }
674                 else if (!strcasecmp(itemlist[i], "RFC822.HEADER")) {
675                         imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i], msg);
676                 }
677                 else if (!strcasecmp(itemlist[i], "RFC822.SIZE")) {
678                         imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i], msg);
679                 }
680                 else if (!strcasecmp(itemlist[i], "RFC822.TEXT")) {
681                         imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i], msg);
682                 }
683                 else if (!strcasecmp(itemlist[i], "UID")) {
684                         imap_fetch_uid(seq);
685                 }
686
687                 if (i != num_items-1) cprintf(" ");
688         }
689
690         cprintf(")\r\n");
691 }
692
693
694
695 /*
696  * imap_fetch() calls imap_do_fetch() to do its actual work, once it's
697  * validated and boiled down the request a bit.
698  */
699 void imap_do_fetch(int num_items, char **itemlist) {
700         int i;
701         struct CtdlMessage *msg;
702
703         if (IMAP->num_msgs > 0)
704          for (i = 0; i < IMAP->num_msgs; ++i)
705           if (IMAP->flags[i] & IMAP_SELECTED) {
706                 msg = CtdlFetchMessage(IMAP->msgids[i]);
707                 if (msg != NULL) {
708                         imap_do_fetch_msg(i+1, msg, num_items, itemlist);
709                         CtdlFreeMessage(msg);
710                 }
711                 else {
712                         cprintf("* %d FETCH <internal error>\r\n", i+1);
713                 }
714         }
715 }
716
717
718
719 /*
720  * Back end for imap_handle_macros()
721  * Note that this function *only* looks at the beginning of the string.  It
722  * is not a generic search-and-replace function.
723  */
724 void imap_macro_replace(char *str, char *find, char *replace) {
725         char holdbuf[1024];
726
727         if (!strncasecmp(str, find, strlen(find))) {
728                 if (str[strlen(find)]==' ') {
729                         strcpy(holdbuf, &str[strlen(find)+1]);
730                         strcpy(str, replace);
731                         strcat(str, " ");
732                         strcat(str, holdbuf);
733                 }
734                 if (str[strlen(find)]==0) {
735                         strcpy(holdbuf, &str[strlen(find)+1]);
736                         strcpy(str, replace);
737                 }
738         }
739 }
740
741
742
743 /*
744  * Handle macros embedded in FETCH data items.
745  * (What the heck are macros doing in a wire protocol?  Are we trying to save
746  * the computer at the other end the trouble of typing a lot of characters?)
747  */
748 void imap_handle_macros(char *str) {
749         int i;
750         int nest = 0;
751
752         for (i=0; i<strlen(str); ++i) {
753                 if (str[i]=='(') ++nest;
754                 if (str[i]=='[') ++nest;
755                 if (str[i]=='<') ++nest;
756                 if (str[i]=='{') ++nest;
757                 if (str[i]==')') --nest;
758                 if (str[i]==']') --nest;
759                 if (str[i]=='>') --nest;
760                 if (str[i]=='}') --nest;
761
762                 if (nest <= 0) {
763                         imap_macro_replace(&str[i],
764                                 "ALL",
765                                 "FLAGS INTERNALDATE RFC822.SIZE ENVELOPE"
766                         );
767                         imap_macro_replace(&str[i],
768                                 "BODY",
769                                 "BODYSTRUCTURE"
770                         );
771                         imap_macro_replace(&str[i],
772                                 "FAST",
773                                 "FLAGS INTERNALDATE RFC822.SIZE"
774                         );
775                         imap_macro_replace(&str[i],
776                                 "FULL",
777                                 "FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODY"
778                         );
779                 }
780         }
781 }
782
783
784 /*
785  * Break out the data items requested, possibly a parenthesized list.
786  * Returns the number of data items, or -1 if the list is invalid.
787  * NOTE: this function alters the string it is fed, and uses it as a buffer
788  * to hold the data for the pointers it returns.
789  */
790 int imap_extract_data_items(char **argv, char *items) {
791         int num_items = 0;
792         int nest = 0;
793         int i, initial_len;
794         char *start;
795
796         /* Convert all whitespace to ordinary space characters. */
797         for (i=0; i<strlen(items); ++i) {
798                 if (isspace(items[i])) items[i]=' ';
799         }
800
801         /* Strip leading and trailing whitespace, then strip leading and
802          * trailing parentheses if it's a list
803          */
804         striplt(items);
805         if ( (items[0]=='(') && (items[strlen(items)-1]==')') ) {
806                 items[strlen(items)-1] = 0;
807                 strcpy(items, &items[1]);
808                 striplt(items);
809         }
810
811         /* Parse any macro data items */
812         imap_handle_macros(items);
813
814         /*
815          * Now break out the data items.  We throw in one trailing space in
816          * order to avoid having to break out the last one manually.
817          */
818         strcat(items, " ");
819         start = items;
820         initial_len = strlen(items);
821         for (i=0; i<initial_len; ++i) {
822                 if (items[i]=='(') ++nest;
823                 if (items[i]=='[') ++nest;
824                 if (items[i]=='<') ++nest;
825                 if (items[i]=='{') ++nest;
826                 if (items[i]==')') --nest;
827                 if (items[i]==']') --nest;
828                 if (items[i]=='>') --nest;
829                 if (items[i]=='}') --nest;
830
831                 if (nest <= 0) if (items[i]==' ') {
832                         items[i] = 0;
833                         argv[num_items++] = start;
834                         start = &items[i+1];
835                 }
836         }
837
838         return(num_items);
839
840 }
841
842
843 /*
844  * One particularly hideous aspect of IMAP is that we have to allow the client
845  * to specify arbitrary ranges and/or sets of messages to fetch.  Citadel IMAP
846  * handles this by setting the IMAP_SELECTED flag for each message specified in
847  * the ranges/sets, then looping through the message array, outputting messages
848  * with the flag set.  We don't bother returning an error if an out-of-range
849  * number is specified (we just return quietly) because any client braindead
850  * enough to request a bogus message number isn't going to notice the
851  * difference anyway.
852  *
853  * This function clears out the IMAP_SELECTED bits, then sets that bit for each
854  * message included in the specified range.
855  *
856  * Set is_uid to 1 to fetch by UID instead of sequence number.
857  */
858 void imap_pick_range(char *supplied_range, int is_uid) {
859         int i;
860         int num_sets;
861         int s;
862         char setstr[SIZ], lostr[SIZ], histr[SIZ];       /* was 1024 */
863         int lo, hi;
864         char actual_range[SIZ];
865
866         /* 
867          * Handle the "ALL" macro
868          */
869         if (!strcasecmp(supplied_range, "ALL")) {
870                 safestrncpy(actual_range, "1:*", sizeof actual_range);
871         }
872         else {
873                 safestrncpy(actual_range, supplied_range, sizeof actual_range);
874         }
875
876         /*
877          * Clear out the IMAP_SELECTED flags for all messages.
878          */
879         for (i = 0; i < IMAP->num_msgs; ++i) {
880                 IMAP->flags[i] = IMAP->flags[i] & ~IMAP_SELECTED;
881         }
882
883         /*
884          * Now set it for all specified messages.
885          */
886         num_sets = num_tokens(actual_range, ',');
887         for (s=0; s<num_sets; ++s) {
888                 extract_token(setstr, actual_range, s, ',');
889
890                 extract_token(lostr, setstr, 0, ':');
891                 if (num_tokens(setstr, ':') >= 2) {
892                         extract_token(histr, setstr, 1, ':');
893                         if (!strcmp(histr, "*")) sprintf(histr, "%d", INT_MAX);
894                 } 
895                 else {
896                         strcpy(histr, lostr);
897                 }
898                 lo = atoi(lostr);
899                 hi = atoi(histr);
900
901                 /* Loop through the array, flipping bits where appropriate */
902                 for (i = 1; i <= IMAP->num_msgs; ++i) {
903                         if (is_uid) {   /* fetch by sequence number */
904                                 if ( (IMAP->msgids[i-1]>=lo)
905                                    && (IMAP->msgids[i-1]<=hi)) {
906                                         IMAP->flags[i-1] =
907                                                 IMAP->flags[i-1] | IMAP_SELECTED;
908                                 }
909                         }
910                         else {          /* fetch by uid */
911                                 if ( (i>=lo) && (i<=hi)) {
912                                         IMAP->flags[i-1] =
913                                                 IMAP->flags[i-1] | IMAP_SELECTED;
914                                 }
915                         }
916                 }
917         }
918
919 }
920
921
922
923 /*
924  * This function is called by the main command loop.
925  */
926 void imap_fetch(int num_parms, char *parms[]) {
927         char items[SIZ];        /* was 1024 */
928         char *itemlist[SIZ];
929         int num_items;
930         int i;
931
932         if (num_parms < 4) {
933                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
934                 return;
935         }
936
937         imap_pick_range(parms[2], 0);
938
939         strcpy(items, "");
940         for (i=3; i<num_parms; ++i) {
941                 strcat(items, parms[i]);
942                 if (i < (num_parms-1)) strcat(items, " ");
943         }
944
945         num_items = imap_extract_data_items(itemlist, items);
946         if (num_items < 1) {
947                 cprintf("%s BAD invalid data item list\r\n", parms[0]);
948                 return;
949         }
950
951         imap_do_fetch(num_items, itemlist);
952         cprintf("%s OK FETCH completed\r\n", parms[0]);
953 }
954
955 /*
956  * This function is called by the main command loop.
957  */
958 void imap_uidfetch(int num_parms, char *parms[]) {
959         char items[SIZ];        /* was 1024 */
960         char *itemlist[SIZ];
961         int num_items;
962         int i;
963         int have_uid_item = 0;
964
965         if (num_parms < 5) {
966                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
967                 return;
968         }
969
970         imap_pick_range(parms[3], 1);
971
972         strcpy(items, "");
973         for (i=4; i<num_parms; ++i) {
974                 strcat(items, parms[i]);
975                 if (i < (num_parms-1)) strcat(items, " ");
976         }
977
978         num_items = imap_extract_data_items(itemlist, items);
979         if (num_items < 1) {
980                 cprintf("%s BAD invalid data item list\r\n", parms[0]);
981                 return;
982         }
983
984         /* If the "UID" item was not included, we include it implicitly
985          * because this is a UID FETCH command
986          */
987         for (i=0; i<num_items; ++i) {
988                 if (!strcasecmp(itemlist[i], "UID")) ++have_uid_item;
989         }
990         if (have_uid_item == 0) itemlist[num_items++] = "UID";
991
992         imap_do_fetch(num_items, itemlist);
993         cprintf("%s OK UID FETCH completed\r\n", parms[0]);
994 }
995
996