]> code.citadel.org Git - citadel.git/blob - citadel/imap_fetch.c
* Added RFC822, RFC822.HEADER, RFC822.SIZE, RFC822.TEXT fetch keys to IMAP
[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 lo, int hi, int num_items, char **itemlist) {
217         int i;
218         struct CtdlMessage *msg;
219
220         for (i=0; i<num_items; ++i) {
221                 lprintf(9, "* item[%d] = <%s>\r\n", i, itemlist[i]);
222         }
223
224         for (i = lo; i <= hi; ++i) {
225                 msg = CtdlFetchMessage(IMAP->msgids[i-1]);
226                 if (msg != NULL) {
227                         imap_do_fetch_msg(i, msg, num_items, itemlist);
228                         CtdlFreeMessage(msg);
229                 }
230                 else {
231                         cprintf("* %d FETCH <internal error>\r\n", i);
232                 }
233         }
234 }
235
236
237
238 /*
239  * Back end for imap_handle_macros()
240  * Note that this function *only* looks at the beginning of the string.  It
241  * is not a generic search-and-replace function.
242  */
243 void imap_macro_replace(char *str, char *find, char *replace) {
244         char holdbuf[1024];
245
246         if (!strncasecmp(str, find, strlen(find))) {
247                 if (str[strlen(find)]==' ') {
248                         strcpy(holdbuf, &str[strlen(find)+1]);
249                         strcpy(str, replace);
250                         strcat(str, " ");
251                         strcat(str, holdbuf);
252                 }
253                 if (str[strlen(find)]==0) {
254                         strcpy(holdbuf, &str[strlen(find)+1]);
255                         strcpy(str, replace);
256                 }
257         }
258 }
259
260
261
262 /*
263  * Handle macros embedded in FETCH data items.
264  * (What the heck are macros doing in a wire protocol?  Are we trying to save
265  * the computer at the other end the trouble of typing a lot of characters?)
266  */
267 void imap_handle_macros(char *str) {
268         int i;
269         int nest = 0;
270
271         for (i=0; i<strlen(str); ++i) {
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                 if (str[i]=='>') --nest;
279                 if (str[i]=='}') --nest;
280
281                 if (nest <= 0) {
282                         imap_macro_replace(&str[i],
283                                 "ALL",
284                                 "FLAGS INTERNALDATE RFC822.SIZE ENVELOPE"
285                         );
286                         imap_macro_replace(&str[i],
287                                 "BODY",
288                                 "BODYSTRUCTURE"
289                         );
290                         imap_macro_replace(&str[i],
291                                 "FAST",
292                                 "FLAGS INTERNALDATE RFC822.SIZE"
293                         );
294                         imap_macro_replace(&str[i],
295                                 "FULL",
296                                 "FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODY"
297                         );
298                 }
299         }
300 }
301
302
303 /*
304  * Break out the data items requested, possibly a parenthesized list.
305  * Returns the number of data items, or -1 if the list is invalid.
306  * NOTE: this function alters the string it is fed, and uses it as a buffer
307  * to hold the data for the pointers it returns.
308  */
309 int imap_extract_data_items(char **argv, char *items) {
310         int num_items = 0;
311         int nest = 0;
312         int i, initial_len;
313         char *start;
314
315         /* Convert all whitespace to ordinary space characters. */
316         for (i=0; i<strlen(items); ++i) {
317                 if (isspace(items[i])) items[i]=' ';
318         }
319
320         /* Strip leading and trailing whitespace, then strip leading and
321          * trailing parentheses if it's a list
322          */
323         striplt(items);
324         if ( (items[0]=='(') && (items[strlen(items)-1]==')') ) {
325                 items[strlen(items)-1] = 0;
326                 strcpy(items, &items[1]);
327                 striplt(items);
328         }
329
330         /* Parse any macro data items */
331         imap_handle_macros(items);
332
333         /*
334          * Now break out the data items.  We throw in one trailing space in
335          * order to avoid having to break out the last one manually.
336          */
337         strcat(items, " ");
338         start = items;
339         initial_len = strlen(items);
340         for (i=0; i<initial_len; ++i) {
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                 if (items[i]=='>') --nest;
348                 if (items[i]=='}') --nest;
349
350                 if (nest <= 0) if (items[i]==' ') {
351                         items[i] = 0;
352                         argv[num_items++] = start;
353                         start = &items[i+1];
354                 }
355         }
356
357         return(num_items);
358
359 }
360
361
362
363 /*
364  * This function is called by the main command loop.
365  */
366 void imap_fetch(int num_parms, char *parms[]) {
367         int lo = 0;
368         int hi = 0;
369         char lostr[1024], histr[1024], items[1024];
370         char *itemlist[256];
371         int num_items;
372         int i;
373
374         if (num_parms < 4) {
375                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
376                 return;
377         }
378
379         extract_token(lostr, parms[2], 0, ':');
380         lo = atoi(lostr);
381         extract_token(histr, parms[2], 1, ':');
382         hi = atoi(histr);
383
384         if ( (lo < 1) || (hi < 1) || (lo > hi) || (hi > IMAP->num_msgs) ) {
385                 cprintf("%s BAD invalid sequence numbers %d:%d\r\n",
386                         parms[0], lo, hi);
387                 return;
388         }
389
390         strcpy(items, "");
391         for (i=3; i<num_parms; ++i) {
392                 strcat(items, parms[i]);
393                 if (i < (num_parms-1)) strcat(items, " ");
394         }
395
396         num_items = imap_extract_data_items(itemlist, items);
397         if (num_items < 1) {
398                 cprintf("%s BAD invalid data item list\r\n", parms[0]);
399                 return;
400         }
401
402         imap_do_fetch(lo, hi, num_items, itemlist);
403         cprintf("%s OK FETCH completed\r\n", parms[0]);
404 }
405
406
407