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