]> code.citadel.org Git - citadel.git/blob - citadel/imap_fetch.c
* msgbase.c: Added new API function CtdlOutputPreLoadedMsg(), and
[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[256];
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(struct CtdlMessage *msg) {
63         cprintf("FLAGS ()");    /* FIXME do something here */
64 }
65
66 void imap_fetch_internaldate(struct CtdlMessage *msg) {
67         char buf[256];
68         time_t msgdate;
69
70         if (msg->cm_fields['T'] != NULL) {
71                 msgdate = atol(msg->cm_fields['T']);
72         }
73         else {
74                 msgdate = time(NULL);
75         }
76
77         datestring(buf, msgdate, DATESTRING_IMAP);
78         cprintf("INTERNALDATE \"%s\"", buf);
79 }
80
81
82 /*
83  * Fetch RFC822-formatted messages.
84  *
85  * 'whichfmt' should be set to one of:
86  *      "RFC822"        entire message
87  *      "RFC822.HEADER" headers only (with trailing blank line)
88  *      "RFC822.SIZE"   size of translated message
89  *      "RFC822.TEXT"   body only (without leading blank line)
90  */
91 void imap_fetch_rfc822(int msgnum, char *whichfmt, struct CtdlMessage *msg) {
92         FILE *tmp;
93         char buf[1024];
94         char *ptr;
95         long headers_size, text_size, total_size;
96         long bytes_remaining = 0;
97         long blocksize;
98
99         tmp = tmpfile();
100         if (tmp == NULL) {
101                 lprintf(1, "Cannot open temp file: %s\n", strerror(errno));
102                 return;
103         }
104
105         /*
106          * Load the message into a temp file for translation and measurement
107          */ 
108         CtdlRedirectOutput(tmp, -1);
109         CtdlOutputPreLoadedMsg(msg, msgnum, MT_RFC822, 0, 0, 1);
110         CtdlRedirectOutput(NULL, -1);
111
112         /*
113          * Now figure out where the headers/text break is.  IMAP considers the
114          * intervening blank line to be part of the headers, not the text.
115          */
116         rewind(tmp);
117         headers_size = 0L;
118         do {
119                 ptr = fgets(buf, sizeof buf, tmp);
120                 if (ptr != NULL) {
121                         striplt(buf);
122                         if (strlen(buf) == 0) headers_size = ftell(tmp);
123                 }
124         } while ( (headers_size == 0L) && (ptr != NULL) );
125         fseek(tmp, 0L, SEEK_END);
126         total_size = ftell(tmp);
127         text_size = total_size - headers_size;
128
129         if (!strcasecmp(whichfmt, "RFC822.SIZE")) {
130                 cprintf("RFC822.SIZE %ld", total_size);
131                 fclose(tmp);
132                 return;
133         }
134
135         else if (!strcasecmp(whichfmt, "RFC822")) {
136                 bytes_remaining = total_size;
137                 rewind(tmp);
138         }
139
140         else if (!strcasecmp(whichfmt, "RFC822.HEADER")) {
141                 bytes_remaining = headers_size;
142                 rewind(tmp);
143         }
144
145         else if (!strcasecmp(whichfmt, "RFC822.TEXT")) {
146                 bytes_remaining = text_size;
147                 fseek(tmp, headers_size, SEEK_SET);
148         }
149
150         cprintf("%s {%ld}\r\n", whichfmt, bytes_remaining);
151         blocksize = sizeof(buf);
152         while (bytes_remaining > 0L) {
153                 if (blocksize > bytes_remaining) blocksize = bytes_remaining;
154                 fread(buf, blocksize, 1, tmp);
155                 client_write(buf, blocksize);
156                 bytes_remaining = bytes_remaining - blocksize;
157         }
158
159         fclose(tmp);
160 }
161
162
163
164 /*
165  * Load a specific part of a message into the temp file to be output to a
166  * client.  FIXME we can handle parts like "2" and "2.1" and even "2.MIME"
167  * but we still can't handle "2.HEADER" (which might not be a problem, because
168  * we currently don't have the ability to break out nested RFC822's anyway).
169  *
170  * Note: mime_parser() was called with dont_decode set to 1, so we have the
171  * luxury of simply spewing without having to re-encode.
172  */
173 void imap_load_part(char *name, char *filename, char *partnum, char *disp,
174                     void *content, char *cbtype, size_t length, char *encoding,
175                     void *cbuserdata)
176 {
177         struct imap_fetch_part *imfp;
178         char mbuf2[1024];
179
180         imfp = (struct imap_fetch_part *)cbuserdata;
181
182         if (!strcasecmp(partnum, imfp->desired_section)) {
183                 fwrite(content, length, 1, imfp->output_fp);
184         }
185
186         sprintf(mbuf2, "%s.MIME", partnum);
187
188         if (!strcasecmp(imfp->desired_section, mbuf2)) {
189                 fprintf(imfp->output_fp, "Content-type: %s", cbtype);
190                 if (strlen(name) > 0)
191                         fprintf(imfp->output_fp, "; name=\"%s\"", name);
192                 fprintf(imfp->output_fp, "\r\n");
193                 if (strlen(encoding) > 0)
194                         fprintf(imfp->output_fp,
195                                 "Content-Transfer-Encoding: %s\r\n", encoding);
196                 if (strlen(encoding) > 0) {
197                         fprintf(imfp->output_fp, "Content-Disposition: %s",
198                                         disp);
199                         if (strlen(filename) > 0) {
200                                 fprintf(imfp->output_fp, "; filename=\"%s\"",
201                                         filename);
202                         }
203                         fprintf(imfp->output_fp, "\r\n");
204                 }
205                 fprintf(imfp->output_fp, "Content-Length: %d\r\n", length);
206                 fprintf(imfp->output_fp, "\r\n");
207         }
208                         
209
210 }
211
212
213 /*
214  * Implements the ENVELOPE fetch item
215  * 
216  * FIXME ... we only output some of the fields right now.  Definitely need
217  *           to do all of them.  Accurately, too.
218  *
219  * Note that the imap_strout() function can cleverly output NULL fields as NIL,
220  * so we don't have to check for that condition like we do elsewhere.
221  */
222 void imap_fetch_envelope(long msgnum, struct CtdlMessage *msg) {
223         char buf[256];
224         time_t msgdate;
225
226         if (msg->cm_fields['T'] != NULL) {
227                 msgdate = atol(msg->cm_fields['T']);
228         }
229         else {
230                 msgdate = time(NULL);
231         }
232
233         datestring(buf, msgdate, DATESTRING_IMAP);
234
235         cprintf("ENVELOPE (");
236
237         /* date */
238         cprintf("\"%s\" ", buf);
239
240         /* subject */
241         imap_strout(msg->cm_fields['U']);
242         cprintf(" ");
243
244         cprintf("NIL ");        /* from */
245         cprintf("NIL ");        /* sender */
246         cprintf("NIL ");        /* reply-to */
247         cprintf("NIL ");        /* to */
248         cprintf("NIL ");        /* cc */
249         cprintf("NIL ");        /* bcc */
250         cprintf("NIL ");        /* in-reply-to */
251
252         /* message ID */
253         imap_strout(msg->cm_fields['I']);
254
255         cprintf(") ");
256 }
257
258
259 /*
260  * Strip any non header information out of a chunk of RFC822 data on disk
261  */
262 void imap_strip_headers(FILE *fp) {
263         char buf[1024];
264
265         rewind(fp);
266         while (fgets(buf, sizeof buf, fp) != NULL) {
267                 striplt(buf);
268                 if (strlen(buf) == 0) {
269                         fflush(fp);
270                         ftruncate(fileno(fp), ftell(fp));
271                 }
272         }
273         fflush(fp);
274         fprintf(fp, "\r\n");    /* add the trailing newline */
275         rewind(fp);
276 }
277
278
279 /*
280  * Implements the BODY and BODY.PEEK fetch items
281  */
282 void imap_fetch_body(long msgnum, char *item, int is_peek,
283                 struct CtdlMessage *msg) {
284         char section[1024];
285         char partial[1024];
286         int is_partial = 0;
287         char buf[1024];
288         int i;
289         FILE *tmp;
290         long bytes_remaining = 0;
291         long blocksize;
292         long pstart, pbytes;
293         struct imap_fetch_part imfp;
294
295         /* extract section */
296         strcpy(section, item);
297         for (i=0; i<strlen(section); ++i) {
298                 if (section[i]=='[') strcpy(section, &section[i+1]);
299         }
300         for (i=0; i<strlen(section); ++i) {
301                 if (section[i]==']') section[i] = 0;
302         }
303         lprintf(9, "Section is %s\n", section);
304
305         /* extract partial */
306         strcpy(partial, item);
307         for (i=0; i<strlen(partial); ++i) {
308                 if (partial[i]=='<') {
309                         strcpy(partial, &partial[i+1]);
310                         is_partial = 1;
311                 }
312         }
313         for (i=0; i<strlen(partial); ++i) {
314                 if (partial[i]=='>') partial[i] = 0;
315         }
316         if (is_partial == 0) strcpy(partial, "");
317         lprintf(9, "Partial is %s\n", partial);
318
319         tmp = tmpfile();
320         if (tmp == NULL) {
321                 lprintf(1, "Cannot open temp file: %s\n", strerror(errno));
322                 return;
323         }
324
325         /* Now figure out what the client wants, and get it */
326
327         if (!strcmp(section, "")) {             /* the whole thing */
328                 CtdlRedirectOutput(tmp, -1);
329                 CtdlOutputPreLoadedMsg(msg, msgnum, MT_RFC822, 0, 0, 1);
330                 CtdlRedirectOutput(NULL, -1);
331         }
332
333         /*
334          * Be obnoxious and send the entire header, even if the client only
335          * asks for certain fields.  FIXME this shortcut later.
336          */
337         else if (!strncasecmp(section, "HEADER", 6)) {
338                 CtdlRedirectOutput(tmp, -1);
339                 CtdlOutputPreLoadedMsg(msg, msgnum, MT_RFC822, 1, 0, 1);
340                 CtdlRedirectOutput(NULL, -1);
341                 imap_strip_headers(tmp);
342         }
343
344         /*
345          * Anything else must be a part specifier.
346          * (Note value of 1 passed as 'dont_decode' so client gets it encoded)
347          */
348         else {
349                 safestrncpy(imfp.desired_section, section,
350                                 sizeof(imfp.desired_section));
351                 imfp.output_fp = tmp;
352
353                 mime_parser(msg->cm_fields['M'], NULL,
354                                 *imap_load_part,
355                                 (void *)&imfp,
356                                 1);
357         }
358
359
360         fseek(tmp, 0L, SEEK_END);
361         bytes_remaining = ftell(tmp);
362
363         if (is_partial == 0) {
364                 rewind(tmp);
365                 cprintf("BODY[%s] {%ld}\r\n", section, bytes_remaining);
366         }
367         else {
368                 sscanf(partial, "%ld.%ld", &pstart, &pbytes);
369                 if ((bytes_remaining - pstart) < pbytes) {
370                         pbytes = bytes_remaining - pstart;
371                 }
372                 fseek(tmp, pstart, SEEK_SET);
373                 bytes_remaining = pbytes;
374                 cprintf("BODY[%s] {%ld}<%ld>\r\n",
375                         section, bytes_remaining, pstart);
376         }
377
378         blocksize = sizeof(buf);
379         while (bytes_remaining > 0L) {
380                 if (blocksize > bytes_remaining) blocksize = bytes_remaining;
381                 fread(buf, blocksize, 1, tmp);
382                 client_write(buf, blocksize);
383                 bytes_remaining = bytes_remaining - blocksize;
384         }
385
386         fclose(tmp);
387
388         if (is_peek) {
389                 /* FIXME set the last read pointer or something */
390         }
391 }
392
393
394 /*
395  * Spew the BODYSTRUCTURE data for a message.  (Do you need a silencer if
396  * you're going to shoot a MIME?  Do you need a reason to shoot Mark Crispin?
397  * No, and no.)
398  *
399  * FIXME finish the implementation
400  */
401 void imap_fetch_bodystructure (long msgnum, char *item,
402                 struct CtdlMessage *msg) {
403         FILE *tmp;
404         char buf[1024];
405         long lines = 0L;
406         long bytes = 0L;
407
408
409         /* For non-RFC822 (ordinary Citadel) messages, this is short and
410          * sweet...
411          */
412         if (msg->cm_format_type != FMT_RFC822) {
413
414                 /* *sigh* We have to RFC822-format the message just to be able
415                  * to measure it.
416                  */
417                 tmp = tmpfile();
418                 if (tmp == NULL) return;
419                 CtdlRedirectOutput(tmp, -1);
420                 CtdlOutputPreLoadedMsg(msg, msgnum, MT_RFC822, 0, 0, 1);
421                 CtdlRedirectOutput(NULL, -1);
422
423                 rewind(tmp);
424                 while (fgets(buf, sizeof buf, tmp) != NULL) ++lines;
425                 bytes = ftell(tmp);
426                 fclose(tmp);
427
428                 cprintf("BODYSTRUCTURE (\"TEXT\" \"PLAIN\" "
429                         "(\"CHARSET\" \"US-ASCII\") NIL NIL "
430                         "\"7BIT\" %ld %ld) ", bytes, lines);
431
432                 return;
433         }
434
435         /* For messages already stored in RFC822 format, we have to parse. */
436         /* FIXME do this! */
437
438
439 }
440
441
442
443
444
445
446 /*
447  * imap_do_fetch() calls imap_do_fetch_msg() to output the deta of an
448  * individual message, once it has been successfully loaded from disk.
449  */
450 void imap_do_fetch_msg(int seq, struct CtdlMessage *msg,
451                         int num_items, char **itemlist) {
452         int i;
453
454         cprintf("* %d FETCH (", seq);
455
456         for (i=0; i<num_items; ++i) {
457
458                 if (!strncasecmp(itemlist[i], "BODY[", 5)) {
459                         imap_fetch_body(IMAP->msgids[seq-1], itemlist[i],
460                                         0, msg);
461                 }
462                 else if (!strncasecmp(itemlist[i], "BODY.PEEK[", 10)) {
463                         imap_fetch_body(IMAP->msgids[seq-1], itemlist[i],
464                                         1, msg);
465                 }
466                 else if (!strcasecmp(itemlist[i], "BODYSTRUCTURE")) {
467                         imap_fetch_bodystructure(IMAP->msgids[seq-1],
468                                         itemlist[i], msg);
469                 }
470                 else if (!strcasecmp(itemlist[i], "ENVELOPE")) {
471                         imap_fetch_envelope(IMAP->msgids[seq-1], msg);
472                 }
473                 else if (!strcasecmp(itemlist[i], "FLAGS")) {
474                         imap_fetch_flags(msg);
475                 }
476                 else if (!strcasecmp(itemlist[i], "INTERNALDATE")) {
477                         imap_fetch_internaldate(msg);
478                 }
479                 else if (!strcasecmp(itemlist[i], "RFC822")) {
480                         imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i], msg);
481                 }
482                 else if (!strcasecmp(itemlist[i], "RFC822.HEADER")) {
483                         imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i], msg);
484                 }
485                 else if (!strcasecmp(itemlist[i], "RFC822.SIZE")) {
486                         imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i], msg);
487                 }
488                 else if (!strcasecmp(itemlist[i], "RFC822.TEXT")) {
489                         imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i], msg);
490                 }
491                 else if (!strcasecmp(itemlist[i], "UID")) {
492                         imap_fetch_uid(seq);
493                 }
494
495                 if (i != num_items-1) cprintf(" ");
496         }
497
498         cprintf(")\r\n");
499 }
500
501
502
503 /*
504  * imap_fetch() calls imap_do_fetch() to do its actual work, once it's
505  * validated and boiled down the request a bit.
506  */
507 void imap_do_fetch(int num_items, char **itemlist) {
508         int i;
509         struct CtdlMessage *msg;
510
511         if (IMAP->num_msgs > 0)
512          for (i = 0; i < IMAP->num_msgs; ++i)
513           if (IMAP->flags[i] && IMAP_FETCHED) {
514                 msg = CtdlFetchMessage(IMAP->msgids[i]);
515                 if (msg != NULL) {
516                         imap_do_fetch_msg(i+1, msg, num_items, itemlist);
517                         CtdlFreeMessage(msg);
518                 }
519                 else {
520                         cprintf("* %d FETCH <internal error>\r\n", i+1);
521                 }
522         }
523 }
524
525
526
527 /*
528  * Back end for imap_handle_macros()
529  * Note that this function *only* looks at the beginning of the string.  It
530  * is not a generic search-and-replace function.
531  */
532 void imap_macro_replace(char *str, char *find, char *replace) {
533         char holdbuf[1024];
534
535         if (!strncasecmp(str, find, strlen(find))) {
536                 if (str[strlen(find)]==' ') {
537                         strcpy(holdbuf, &str[strlen(find)+1]);
538                         strcpy(str, replace);
539                         strcat(str, " ");
540                         strcat(str, holdbuf);
541                 }
542                 if (str[strlen(find)]==0) {
543                         strcpy(holdbuf, &str[strlen(find)+1]);
544                         strcpy(str, replace);
545                 }
546         }
547 }
548
549
550
551 /*
552  * Handle macros embedded in FETCH data items.
553  * (What the heck are macros doing in a wire protocol?  Are we trying to save
554  * the computer at the other end the trouble of typing a lot of characters?)
555  */
556 void imap_handle_macros(char *str) {
557         int i;
558         int nest = 0;
559
560         for (i=0; i<strlen(str); ++i) {
561                 if (str[i]=='(') ++nest;
562                 if (str[i]=='[') ++nest;
563                 if (str[i]=='<') ++nest;
564                 if (str[i]=='{') ++nest;
565                 if (str[i]==')') --nest;
566                 if (str[i]==']') --nest;
567                 if (str[i]=='>') --nest;
568                 if (str[i]=='}') --nest;
569
570                 if (nest <= 0) {
571                         imap_macro_replace(&str[i],
572                                 "ALL",
573                                 "FLAGS INTERNALDATE RFC822.SIZE ENVELOPE"
574                         );
575                         imap_macro_replace(&str[i],
576                                 "BODY",
577                                 "BODYSTRUCTURE"
578                         );
579                         imap_macro_replace(&str[i],
580                                 "FAST",
581                                 "FLAGS INTERNALDATE RFC822.SIZE"
582                         );
583                         imap_macro_replace(&str[i],
584                                 "FULL",
585                                 "FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODY"
586                         );
587                 }
588         }
589 }
590
591
592 /*
593  * Break out the data items requested, possibly a parenthesized list.
594  * Returns the number of data items, or -1 if the list is invalid.
595  * NOTE: this function alters the string it is fed, and uses it as a buffer
596  * to hold the data for the pointers it returns.
597  */
598 int imap_extract_data_items(char **argv, char *items) {
599         int num_items = 0;
600         int nest = 0;
601         int i, initial_len;
602         char *start;
603
604         /* Convert all whitespace to ordinary space characters. */
605         for (i=0; i<strlen(items); ++i) {
606                 if (isspace(items[i])) items[i]=' ';
607         }
608
609         /* Strip leading and trailing whitespace, then strip leading and
610          * trailing parentheses if it's a list
611          */
612         striplt(items);
613         if ( (items[0]=='(') && (items[strlen(items)-1]==')') ) {
614                 items[strlen(items)-1] = 0;
615                 strcpy(items, &items[1]);
616                 striplt(items);
617         }
618
619         /* Parse any macro data items */
620         imap_handle_macros(items);
621
622         /*
623          * Now break out the data items.  We throw in one trailing space in
624          * order to avoid having to break out the last one manually.
625          */
626         strcat(items, " ");
627         start = items;
628         initial_len = strlen(items);
629         for (i=0; i<initial_len; ++i) {
630                 if (items[i]=='(') ++nest;
631                 if (items[i]=='[') ++nest;
632                 if (items[i]=='<') ++nest;
633                 if (items[i]=='{') ++nest;
634                 if (items[i]==')') --nest;
635                 if (items[i]==']') --nest;
636                 if (items[i]=='>') --nest;
637                 if (items[i]=='}') --nest;
638
639                 if (nest <= 0) if (items[i]==' ') {
640                         items[i] = 0;
641                         argv[num_items++] = start;
642                         start = &items[i+1];
643                 }
644         }
645
646         return(num_items);
647
648 }
649
650
651 /*
652  * One particularly hideous aspect of IMAP is that we have to allow the client
653  * to specify arbitrary ranges and/or sets of messages to fetch.  Citadel IMAP
654  * handles this by setting the IMAP_FETCHED flag for each message specified in
655  * the ranges/sets, then looping through the message array, outputting messages
656  * with the flag set.  We don't bother returning an error if an out-of-range
657  * number is specified (we just return quietly) because any client braindead
658  * enough to request a bogus message number isn't going to notice the
659  * difference anyway.
660  *
661  * This function clears out the IMAP_FETCHED bits, then sets that bit for each
662  * message included in the specified range.
663  *
664  * Set is_uid to 1 to fetch by UID instead of sequence number.
665  */
666 void imap_pick_range(char *range, int is_uid) {
667         int i;
668         int num_sets;
669         int s;
670         char setstr[1024], lostr[1024], histr[1024];
671         int lo, hi;
672
673         /*
674          * Clear out the IMAP_FETCHED flags for all messages.
675          */
676         for (i = 1; i <= IMAP->num_msgs; ++i) {
677                 IMAP->flags[i-1] = IMAP->flags[i-1] & ~IMAP_FETCHED;
678         }
679
680         /*
681          * Now set it for all specified messages.
682          */
683         num_sets = num_tokens(range, ',');
684         for (s=0; s<num_sets; ++s) {
685                 extract_token(setstr, range, s, ',');
686
687                 extract_token(lostr, setstr, 0, ':');
688                 if (num_tokens(setstr, ':') >= 2) {
689                         extract_token(histr, setstr, 1, ':');
690                         if (!strcmp(histr, "*")) sprintf(histr, "%d", INT_MAX);
691                 } 
692                 else {
693                         strcpy(histr, lostr);
694                 }
695                 lo = atoi(lostr);
696                 hi = atoi(histr);
697
698                 /* Loop through the array, flipping bits where appropriate */
699                 for (i = 1; i <= IMAP->num_msgs; ++i) {
700                         if (is_uid) {   /* fetch by sequence number */
701                                 if ( (IMAP->msgids[i-1]>=lo)
702                                    && (IMAP->msgids[i-1]<=hi)) {
703                                         IMAP->flags[i-1] =
704                                                 IMAP->flags[i-1] | IMAP_FETCHED;
705                                 }
706                         }
707                         else {          /* fetch by uid */
708                                 if ( (i>=lo) && (i<=hi)) {
709                                         IMAP->flags[i-1] =
710                                                 IMAP->flags[i-1] | IMAP_FETCHED;
711                                 }
712                         }
713                 }
714         }
715 }
716
717
718
719 /*
720  * This function is called by the main command loop.
721  */
722 void imap_fetch(int num_parms, char *parms[]) {
723         char items[1024];
724         char *itemlist[256];
725         int num_items;
726         int i;
727
728         if (num_parms < 4) {
729                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
730                 return;
731         }
732
733         imap_pick_range(parms[2], 0);
734
735         strcpy(items, "");
736         for (i=3; i<num_parms; ++i) {
737                 strcat(items, parms[i]);
738                 if (i < (num_parms-1)) strcat(items, " ");
739         }
740
741         num_items = imap_extract_data_items(itemlist, items);
742         if (num_items < 1) {
743                 cprintf("%s BAD invalid data item list\r\n", parms[0]);
744                 return;
745         }
746
747         imap_do_fetch(num_items, itemlist);
748         cprintf("%s OK FETCH completed\r\n", parms[0]);
749 }
750
751 /*
752  * This function is called by the main command loop.
753  */
754 void imap_uidfetch(int num_parms, char *parms[]) {
755         char items[1024];
756         char *itemlist[256];
757         int num_items;
758         int i;
759         int have_uid_item = 0;
760
761         if (num_parms < 5) {
762                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
763                 return;
764         }
765
766         imap_pick_range(parms[3], 1);
767
768         strcpy(items, "");
769         for (i=4; i<num_parms; ++i) {
770                 strcat(items, parms[i]);
771                 if (i < (num_parms-1)) strcat(items, " ");
772         }
773
774         num_items = imap_extract_data_items(itemlist, items);
775         if (num_items < 1) {
776                 cprintf("%s BAD invalid data item list\r\n", parms[0]);
777                 return;
778         }
779
780         /* If the "UID" item was not included, we include it implicitly
781          * because this is a UID FETCH command
782          */
783         for (i=0; i<num_items; ++i) {
784                 if (!strcasecmp(itemlist[i], "UID")) ++have_uid_item;
785         }
786         if (have_uid_item == 0) itemlist[num_items++] = "UID";
787
788         imap_do_fetch(num_items, itemlist);
789         cprintf("%s OK UID FETCH completed\r\n", parms[0]);
790 }
791
792