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