]> code.citadel.org Git - citadel.git/blob - citadel/imap_fetch.c
* IMAP date strings. More stupidity.
[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_internaldate(struct CtdlMessage *msg) {
57         char buf[256];
58         time_t msgdate;
59
60         if (msg->cm_fields['T'] != NULL) {
61                 msgdate = atol(msg->cm_fields['T']);
62         }
63         else {
64                 msgdate = time(NULL);
65         }
66
67         datestring(buf, msgdate, DATESTRING_IMAP);
68         cprintf("INTERNALDATE \"%s\"", buf);
69 }
70
71
72 /*
73  * imap_do_fetch() calls imap_do_fetch_msg() to output the deta of an
74  * individual message, once it has been successfully loaded from disk.
75  */
76 void imap_do_fetch_msg(int seq, struct CtdlMessage *msg,
77                         int num_items, char **itemlist) {
78         int i;
79
80         cprintf("* %d FETCH (", seq);
81
82         for (i=0; i<num_items; ++i) {
83
84                 if (!strncasecmp(itemlist[i], "BODY[", 5)) {
85                         /* FIXME do something here */
86                 }
87                 else if (!strncasecmp(itemlist[i], "BODY.PEEK[", 10)) {
88                         /* FIXME do something here */
89                 }
90                 else if (!strcasecmp(itemlist[i], "BODYSTRUCTURE")) {
91                         /* FIXME do something here */
92                 }
93                 else if (!strcasecmp(itemlist[i], "ENVELOPE")) {
94                         /* FIXME do something here */
95                 }
96                 else if (!strcasecmp(itemlist[i], "FLAGS")) {
97                         /* FIXME do something here */
98                 }
99                 else if (!strcasecmp(itemlist[i], "INTERNALDATE")) {
100                         imap_fetch_internaldate(msg);
101                 }
102                 else if (!strcasecmp(itemlist[i], "RFC822")) {
103                         /* FIXME do something here */
104                 }
105                 else if (!strcasecmp(itemlist[i], "RFC822.HEADER")) {
106                         /* FIXME do something here */
107                 }
108                 else if (!strcasecmp(itemlist[i], "RFC822.SIZE")) {
109                         /* FIXME do something here */
110                 }
111                 else if (!strcasecmp(itemlist[i], "RFC822.TEXT")) {
112                         /* FIXME do something here */
113                 }
114                 else if (!strcasecmp(itemlist[i], "UID")) {
115                         imap_fetch_uid(seq);
116                 }
117
118                 if (i != num_items-1) cprintf(" ");
119         }
120
121         cprintf(")\r\n");
122 }
123
124
125
126 /*
127  * imap_fetch() calls imap_do_fetch() to do its actual work, once it's
128  * validated and boiled down the request a bit.
129  */
130 void imap_do_fetch(int lo, int hi, int num_items, char **itemlist) {
131         int i;
132         struct CtdlMessage *msg;
133
134         for (i=0; i<num_items; ++i) {
135                 lprintf(9, "* item[%d] = <%s>\r\n", i, itemlist[i]);
136         }
137
138         for (i = lo; i <= hi; ++i) {
139                 msg = CtdlFetchMessage(IMAP->msgids[i-1]);
140                 if (msg != NULL) {
141                         imap_do_fetch_msg(i, msg, num_items, itemlist);
142                         CtdlFreeMessage(msg);
143                 }
144                 else {
145                         cprintf("* %d FETCH <internal error>\r\n", i);
146                 }
147         }
148 }
149
150
151
152 /*
153  * Back end for imap_handle_macros()
154  * Note that this function *only* looks at the beginning of the string.  It
155  * is not a generic search-and-replace function.
156  */
157 void imap_macro_replace(char *str, char *find, char *replace) {
158         char holdbuf[1024];
159
160         if (!strncasecmp(str, find, strlen(find))) {
161                 if (str[strlen(find)]==' ') {
162                         strcpy(holdbuf, &str[strlen(find)+1]);
163                         strcpy(str, replace);
164                         strcat(str, " ");
165                         strcat(str, holdbuf);
166                 }
167                 if (str[strlen(find)]==0) {
168                         strcpy(holdbuf, &str[strlen(find)+1]);
169                         strcpy(str, replace);
170                 }
171         }
172 }
173
174
175
176 /*
177  * Handle macros embedded in FETCH data items.
178  * (What the heck are macros doing in a wire protocol?  Are we trying to save
179  * the computer at the other end the trouble of typing a lot of characters?)
180  */
181 void imap_handle_macros(char *str) {
182         int i;
183         int nest = 0;
184
185         for (i=0; i<strlen(str); ++i) {
186                 if (str[i]=='(') ++nest;
187                 if (str[i]=='[') ++nest;
188                 if (str[i]=='<') ++nest;
189                 if (str[i]=='{') ++nest;
190                 if (str[i]==')') --nest;
191                 if (str[i]==']') --nest;
192                 if (str[i]=='>') --nest;
193                 if (str[i]=='}') --nest;
194
195                 if (nest <= 0) {
196                         imap_macro_replace(&str[i],
197                                 "ALL",
198                                 "FLAGS INTERNALDATE RFC822.SIZE ENVELOPE"
199                         );
200                         imap_macro_replace(&str[i],
201                                 "BODY",
202                                 "BODYSTRUCTURE"
203                         );
204                         imap_macro_replace(&str[i],
205                                 "FAST",
206                                 "FLAGS INTERNALDATE RFC822.SIZE"
207                         );
208                         imap_macro_replace(&str[i],
209                                 "FULL",
210                                 "FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODY"
211                         );
212                 }
213         }
214 }
215
216
217 /*
218  * Break out the data items requested, possibly a parenthesized list.
219  * Returns the number of data items, or -1 if the list is invalid.
220  * NOTE: this function alters the string it is fed, and uses it as a buffer
221  * to hold the data for the pointers it returns.
222  */
223 int imap_extract_data_items(char **argv, char *items) {
224         int num_items = 0;
225         int nest = 0;
226         int i, initial_len;
227         char *start;
228
229         /* Convert all whitespace to ordinary space characters. */
230         for (i=0; i<strlen(items); ++i) {
231                 if (isspace(items[i])) items[i]=' ';
232         }
233
234         /* Strip leading and trailing whitespace, then strip leading and
235          * trailing parentheses if it's a list
236          */
237         striplt(items);
238         if ( (items[0]=='(') && (items[strlen(items)-1]==')') ) {
239                 items[strlen(items)-1] = 0;
240                 strcpy(items, &items[1]);
241                 striplt(items);
242         }
243
244         /* Parse any macro data items */
245         imap_handle_macros(items);
246
247         /*
248          * Now break out the data items.  We throw in one trailing space in
249          * order to avoid having to break out the last one manually.
250          */
251         strcat(items, " ");
252         start = items;
253         initial_len = strlen(items);
254         for (i=0; i<initial_len; ++i) {
255                 if (items[i]=='(') ++nest;
256                 if (items[i]=='[') ++nest;
257                 if (items[i]=='<') ++nest;
258                 if (items[i]=='{') ++nest;
259                 if (items[i]==')') --nest;
260                 if (items[i]==']') --nest;
261                 if (items[i]=='>') --nest;
262                 if (items[i]=='}') --nest;
263
264                 if (nest <= 0) if (items[i]==' ') {
265                         items[i] = 0;
266                         argv[num_items++] = start;
267                         start = &items[i+1];
268                 }
269         }
270
271         return(num_items);
272
273 }
274
275
276
277 /*
278  * This function is called by the main command loop.
279  */
280 void imap_fetch(int num_parms, char *parms[]) {
281         int lo = 0;
282         int hi = 0;
283         char lostr[1024], histr[1024], items[1024];
284         char *itemlist[256];
285         int num_items;
286         int i;
287
288         if (num_parms < 4) {
289                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
290                 return;
291         }
292
293         extract_token(lostr, parms[2], 0, ':');
294         lo = atoi(lostr);
295         extract_token(histr, parms[2], 1, ':');
296         hi = atoi(histr);
297
298         if ( (lo < 1) || (hi < 1) || (lo > hi) || (hi > IMAP->num_msgs) ) {
299                 cprintf("%s BAD invalid sequence numbers %d:%d\r\n",
300                         parms[0], lo, hi);
301                 return;
302         }
303
304         strcpy(items, "");
305         for (i=3; i<num_parms; ++i) {
306                 strcat(items, parms[i]);
307                 if (i < (num_parms-1)) strcat(items, " ");
308         }
309
310         num_items = imap_extract_data_items(itemlist, items);
311         if (num_items < 1) {
312                 cprintf("%s BAD invalid data item list\r\n", parms[0]);
313                 return;
314         }
315
316         imap_do_fetch(lo, hi, num_items, itemlist);
317         cprintf("%s OK FETCH completed\r\n", parms[0]);
318 }
319
320
321