]> code.citadel.org Git - citadel.git/blob - citadel/imap_fetch.c
* die crispin die
[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 "serv_imap.h"
40 #include "imap_tools.h"
41 #include "imap_fetch.h"
42 #include "genstamp.h"
43
44
45
46 /*
47  * Individual field functions for imap_do_fetch_msg() ...
48  */
49
50
51
52 void imap_fetch_uid(int seq) {
53         cprintf("UID %ld", IMAP->msgids[seq-1]);
54 }
55
56 void imap_fetch_flags(struct CtdlMessage *msg) {
57         cprintf("FLAGS ()");    /* FIXME do something here */
58 }
59
60 void imap_fetch_internaldate(struct CtdlMessage *msg) {
61         char buf[256];
62         time_t msgdate;
63
64         if (msg->cm_fields['T'] != NULL) {
65                 msgdate = atol(msg->cm_fields['T']);
66         }
67         else {
68                 msgdate = time(NULL);
69         }
70
71         datestring(buf, msgdate, DATESTRING_IMAP);
72         cprintf("INTERNALDATE \"%s\"", buf);
73 }
74
75
76 /*
77  * Fetch RFC822-formatted messages.
78  *
79  * 'whichfmt' should be set to one of:
80  *      "RFC822"        entire message
81  *      "RFC822.HEADER" headers only (with trailing blank line)
82  *      "RFC822.SIZE"   size of translated message
83  *      "RFC822.TEXT"   body only (without leading blank line)
84  */
85 void imap_fetch_rfc822(int msgnum, char *whichfmt) {
86         FILE *tmp;
87         char buf[1024];
88         char *ptr;
89         size_t headers_size, text_size, total_size;
90         size_t bytes_remaining = 0;
91         size_t blocksize;
92
93         tmp = tmpfile();
94         if (tmp == NULL) {
95                 lprintf(1, "Cannot open temp file: %s\n", strerror(errno));
96                 return;
97         }
98
99         /*
100          * Load the message into a temp file for translation and measurement
101          */ 
102         CtdlRedirectOutput(tmp, -1);
103         CtdlOutputMsg(msgnum, MT_RFC822, 0, 0, 1);
104         CtdlRedirectOutput(NULL, -1);
105
106         /*
107          * Now figure out where the headers/text break is.  IMAP considers the
108          * intervening blank line to be part of the headers, not the text.
109          */
110         rewind(tmp);
111         headers_size = 0L;
112         do {
113                 ptr = fgets(buf, sizeof buf, tmp);
114                 if (ptr != NULL) {
115                         striplt(buf);
116                         if (strlen(buf) == 0) headers_size = ftell(tmp);
117                 }
118         } while ( (headers_size == 0L) && (ptr != NULL) );
119         fseek(tmp, 0L, SEEK_END);
120         total_size = ftell(tmp);
121         text_size = total_size - headers_size;
122
123         if (!strcasecmp(whichfmt, "RFC822.SIZE")) {
124                 cprintf("RFC822.SIZE %ld", total_size);
125                 fclose(tmp);
126                 return;
127         }
128
129         else if (!strcasecmp(whichfmt, "RFC822")) {
130                 bytes_remaining = total_size;
131                 rewind(tmp);
132         }
133
134         else if (!strcasecmp(whichfmt, "RFC822.HEADER")) {
135                 bytes_remaining = headers_size;
136                 rewind(tmp);
137         }
138
139         else if (!strcasecmp(whichfmt, "RFC822.TEXT")) {
140                 bytes_remaining = text_size;
141                 fseek(tmp, headers_size, SEEK_SET);
142         }
143
144         cprintf("%s {%ld}\r\n", whichfmt, bytes_remaining);
145         blocksize = sizeof(buf);
146         while (bytes_remaining > 0L) {
147                 if (blocksize > bytes_remaining) blocksize = bytes_remaining;
148                 fread(buf, blocksize, 1, tmp);
149                 client_write(buf, blocksize);
150                 bytes_remaining = bytes_remaining - blocksize;
151         }
152
153         fclose(tmp);
154 }
155
156
157
158 /*
159  * imap_do_fetch() calls imap_do_fetch_msg() to output the deta of an
160  * individual message, once it has been successfully loaded from disk.
161  */
162 void imap_do_fetch_msg(int seq, struct CtdlMessage *msg,
163                         int num_items, char **itemlist) {
164         int i;
165
166         cprintf("* %d FETCH (", seq);
167
168         for (i=0; i<num_items; ++i) {
169
170                 if (!strncasecmp(itemlist[i], "BODY[", 5)) {
171                         /* FIXME do something here */
172                 }
173                 else if (!strncasecmp(itemlist[i], "BODY.PEEK[", 10)) {
174                         /* FIXME do something here */
175                 }
176                 else if (!strcasecmp(itemlist[i], "BODYSTRUCTURE")) {
177                         /* FIXME do something here */
178                 }
179                 else if (!strcasecmp(itemlist[i], "ENVELOPE")) {
180                         /* FIXME do something here */
181                 }
182                 else if (!strcasecmp(itemlist[i], "FLAGS")) {
183                         imap_fetch_flags(msg);
184                 }
185                 else if (!strcasecmp(itemlist[i], "INTERNALDATE")) {
186                         imap_fetch_internaldate(msg);
187                 }
188                 else if (!strcasecmp(itemlist[i], "RFC822")) {
189                         imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i]);
190                 }
191                 else if (!strcasecmp(itemlist[i], "RFC822.HEADER")) {
192                         imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i]);
193                 }
194                 else if (!strcasecmp(itemlist[i], "RFC822.SIZE")) {
195                         imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i]);
196                 }
197                 else if (!strcasecmp(itemlist[i], "RFC822.TEXT")) {
198                         imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i]);
199                 }
200                 else if (!strcasecmp(itemlist[i], "UID")) {
201                         imap_fetch_uid(seq);
202                 }
203
204                 if (i != num_items-1) cprintf(" ");
205         }
206
207         cprintf(")\r\n");
208 }
209
210
211
212 /*
213  * imap_fetch() calls imap_do_fetch() to do its actual work, once it's
214  * validated and boiled down the request a bit.
215  */
216 void imap_do_fetch(int num_items, char **itemlist) {
217         int i;
218         struct CtdlMessage *msg;
219
220         if (IMAP->num_msgs > 0)
221          for (i = 0; i < IMAP->num_msgs; ++i)
222           if (IMAP->flags[i] && IMAP_FETCHED) {
223                 msg = CtdlFetchMessage(IMAP->msgids[i]);
224                 if (msg != NULL) {
225                         imap_do_fetch_msg(i+1, msg, num_items, itemlist);
226                         CtdlFreeMessage(msg);
227                 }
228                 else {
229                         cprintf("* %d FETCH <internal error>\r\n", i+1);
230                 }
231         }
232 }
233
234
235
236 /*
237  * Back end for imap_handle_macros()
238  * Note that this function *only* looks at the beginning of the string.  It
239  * is not a generic search-and-replace function.
240  */
241 void imap_macro_replace(char *str, char *find, char *replace) {
242         char holdbuf[1024];
243
244         if (!strncasecmp(str, find, strlen(find))) {
245                 if (str[strlen(find)]==' ') {
246                         strcpy(holdbuf, &str[strlen(find)+1]);
247                         strcpy(str, replace);
248                         strcat(str, " ");
249                         strcat(str, holdbuf);
250                 }
251                 if (str[strlen(find)]==0) {
252                         strcpy(holdbuf, &str[strlen(find)+1]);
253                         strcpy(str, replace);
254                 }
255         }
256 }
257
258
259
260 /*
261  * Handle macros embedded in FETCH data items.
262  * (What the heck are macros doing in a wire protocol?  Are we trying to save
263  * the computer at the other end the trouble of typing a lot of characters?)
264  */
265 void imap_handle_macros(char *str) {
266         int i;
267         int nest = 0;
268
269         for (i=0; i<strlen(str); ++i) {
270                 if (str[i]=='(') ++nest;
271                 if (str[i]=='[') ++nest;
272                 if (str[i]=='<') ++nest;
273                 if (str[i]=='{') ++nest;
274                 if (str[i]==')') --nest;
275                 if (str[i]==']') --nest;
276                 if (str[i]=='>') --nest;
277                 if (str[i]=='}') --nest;
278
279                 if (nest <= 0) {
280                         imap_macro_replace(&str[i],
281                                 "ALL",
282                                 "FLAGS INTERNALDATE RFC822.SIZE ENVELOPE"
283                         );
284                         imap_macro_replace(&str[i],
285                                 "BODY",
286                                 "BODYSTRUCTURE"
287                         );
288                         imap_macro_replace(&str[i],
289                                 "FAST",
290                                 "FLAGS INTERNALDATE RFC822.SIZE"
291                         );
292                         imap_macro_replace(&str[i],
293                                 "FULL",
294                                 "FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODY"
295                         );
296                 }
297         }
298 }
299
300
301 /*
302  * Break out the data items requested, possibly a parenthesized list.
303  * Returns the number of data items, or -1 if the list is invalid.
304  * NOTE: this function alters the string it is fed, and uses it as a buffer
305  * to hold the data for the pointers it returns.
306  */
307 int imap_extract_data_items(char **argv, char *items) {
308         int num_items = 0;
309         int nest = 0;
310         int i, initial_len;
311         char *start;
312
313         /* Convert all whitespace to ordinary space characters. */
314         for (i=0; i<strlen(items); ++i) {
315                 if (isspace(items[i])) items[i]=' ';
316         }
317
318         /* Strip leading and trailing whitespace, then strip leading and
319          * trailing parentheses if it's a list
320          */
321         striplt(items);
322         if ( (items[0]=='(') && (items[strlen(items)-1]==')') ) {
323                 items[strlen(items)-1] = 0;
324                 strcpy(items, &items[1]);
325                 striplt(items);
326         }
327
328         /* Parse any macro data items */
329         imap_handle_macros(items);
330
331         /*
332          * Now break out the data items.  We throw in one trailing space in
333          * order to avoid having to break out the last one manually.
334          */
335         strcat(items, " ");
336         start = items;
337         initial_len = strlen(items);
338         for (i=0; i<initial_len; ++i) {
339                 if (items[i]=='(') ++nest;
340                 if (items[i]=='[') ++nest;
341                 if (items[i]=='<') ++nest;
342                 if (items[i]=='{') ++nest;
343                 if (items[i]==')') --nest;
344                 if (items[i]==']') --nest;
345                 if (items[i]=='>') --nest;
346                 if (items[i]=='}') --nest;
347
348                 if (nest <= 0) if (items[i]==' ') {
349                         items[i] = 0;
350                         argv[num_items++] = start;
351                         start = &items[i+1];
352                 }
353         }
354
355         return(num_items);
356
357 }
358
359
360
361 /*
362  * This function is called by the main command loop.
363  */
364 void imap_fetch(int num_parms, char *parms[]) {
365         int lo = 0;
366         int hi = 0;
367         char lostr[1024], histr[1024], items[1024];
368         char *itemlist[256];
369         int num_items;
370         int i;
371
372         if (num_parms < 4) {
373                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
374                 return;
375         }
376
377         extract_token(lostr, parms[2], 0, ':');
378         lo = atoi(lostr);
379         extract_token(histr, parms[2], 1, ':');
380         if (!strcmp(histr, "*")) {
381                 hi = IMAP->num_msgs;
382         }
383         else {
384                 hi = atoi(histr);
385         }
386
387         /* Clear out the IMAP_FETCHED flags and then set them for the messages
388          * we're interested in.
389          */
390         for (i = 1; i <= IMAP->num_msgs; ++i) {
391                 IMAP->flags[i-1] = IMAP->flags[i-1] & ~IMAP_FETCHED;
392         }
393
394         for (i = 1; i <= IMAP->num_msgs; ++i) {
395                 if ( (i>=lo) && (i<=hi) ) {
396                         IMAP->flags[i-1] = IMAP->flags[i-1] | IMAP_FETCHED;
397                 }
398         }
399
400         strcpy(items, "");
401         for (i=3; i<num_parms; ++i) {
402                 strcat(items, parms[i]);
403                 if (i < (num_parms-1)) strcat(items, " ");
404         }
405
406         num_items = imap_extract_data_items(itemlist, items);
407         if (num_items < 1) {
408                 cprintf("%s BAD invalid data item list\r\n", parms[0]);
409                 return;
410         }
411
412         imap_do_fetch(num_items, itemlist);
413         cprintf("%s OK FETCH completed\r\n", parms[0]);
414 }
415
416
417
418
419
420