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