4 * Implements the FETCH command in IMAP.
5 * This command is way too convoluted. Marc Crispin is a fscking idiot.
18 #include <sys/types.h>
27 #include "sysdep_decls.h"
28 #include "citserver.h"
31 #include "dynloader.h"
38 #include "internet_addressing.h"
39 #include "serv_imap.h"
40 #include "imap_tools.h"
41 #include "imap_fetch.h"
47 * Individual field functions for imap_do_fetch_msg() ...
52 void imap_fetch_uid(int seq) {
53 cprintf("UID %ld", IMAP->msgids[seq-1]);
56 void imap_fetch_flags(struct CtdlMessage *msg) {
57 cprintf("FLAGS ()"); /* FIXME do something here */
60 void imap_fetch_internaldate(struct CtdlMessage *msg) {
64 if (msg->cm_fields['T'] != NULL) {
65 msgdate = atol(msg->cm_fields['T']);
71 datestring(buf, msgdate, DATESTRING_IMAP);
72 cprintf("INTERNALDATE \"%s\"", buf);
77 * Fetch RFC822-formatted messages.
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)
85 void imap_fetch_rfc822(int msgnum, char *whichfmt) {
89 size_t headers_size, text_size, total_size;
90 size_t bytes_remaining = 0;
95 lprintf(1, "Cannot open temp file: %s\n", strerror(errno));
100 * Load the message into a temp file for translation and measurement
102 CtdlRedirectOutput(tmp, -1);
103 CtdlOutputMsg(msgnum, MT_RFC822, 0, 0, 1);
104 CtdlRedirectOutput(NULL, -1);
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.
113 ptr = fgets(buf, sizeof buf, tmp);
116 if (strlen(buf) == 0) headers_size = ftell(tmp);
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;
123 if (!strcasecmp(whichfmt, "RFC822.SIZE")) {
124 cprintf("RFC822.SIZE %ld", total_size);
129 else if (!strcasecmp(whichfmt, "RFC822")) {
130 bytes_remaining = total_size;
134 else if (!strcasecmp(whichfmt, "RFC822.HEADER")) {
135 bytes_remaining = headers_size;
139 else if (!strcasecmp(whichfmt, "RFC822.TEXT")) {
140 bytes_remaining = text_size;
141 fseek(tmp, headers_size, SEEK_SET);
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;
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.
162 void imap_do_fetch_msg(int seq, struct CtdlMessage *msg,
163 int num_items, char **itemlist) {
166 cprintf("* %d FETCH (", seq);
168 for (i=0; i<num_items; ++i) {
170 if (!strncasecmp(itemlist[i], "BODY[", 5)) {
171 /* FIXME do something here */
173 else if (!strncasecmp(itemlist[i], "BODY.PEEK[", 10)) {
174 /* FIXME do something here */
176 else if (!strcasecmp(itemlist[i], "BODYSTRUCTURE")) {
177 /* FIXME do something here */
179 else if (!strcasecmp(itemlist[i], "ENVELOPE")) {
180 /* FIXME do something here */
182 else if (!strcasecmp(itemlist[i], "FLAGS")) {
183 imap_fetch_flags(msg);
185 else if (!strcasecmp(itemlist[i], "INTERNALDATE")) {
186 imap_fetch_internaldate(msg);
188 else if (!strcasecmp(itemlist[i], "RFC822")) {
189 imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i]);
191 else if (!strcasecmp(itemlist[i], "RFC822.HEADER")) {
192 imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i]);
194 else if (!strcasecmp(itemlist[i], "RFC822.SIZE")) {
195 imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i]);
197 else if (!strcasecmp(itemlist[i], "RFC822.TEXT")) {
198 imap_fetch_rfc822(IMAP->msgids[seq-1], itemlist[i]);
200 else if (!strcasecmp(itemlist[i], "UID")) {
204 if (i != num_items-1) cprintf(" ");
213 * imap_fetch() calls imap_do_fetch() to do its actual work, once it's
214 * validated and boiled down the request a bit.
216 void imap_do_fetch(int num_items, char **itemlist) {
218 struct CtdlMessage *msg;
220 if (IMAP->num_msgs > 0)
221 for (i = 0; i < IMAP->num_msgs; ++i)
222 if (IMAP->flags[i] && IMAP_FETCHED) {
223 msg = CtdlFetchMessage(IMAP->msgids[i]);
225 imap_do_fetch_msg(i+1, msg, num_items, itemlist);
226 CtdlFreeMessage(msg);
229 cprintf("* %d FETCH <internal error>\r\n", i+1);
237 * Back end for imap_handle_macros()
238 * Note that this function *only* looks at the beginning of the string. It
239 * is not a generic search-and-replace function.
241 void imap_macro_replace(char *str, char *find, char *replace) {
244 if (!strncasecmp(str, find, strlen(find))) {
245 if (str[strlen(find)]==' ') {
246 strcpy(holdbuf, &str[strlen(find)+1]);
247 strcpy(str, replace);
249 strcat(str, holdbuf);
251 if (str[strlen(find)]==0) {
252 strcpy(holdbuf, &str[strlen(find)+1]);
253 strcpy(str, replace);
261 * Handle macros embedded in FETCH data items.
262 * (What the heck are macros doing in a wire protocol? Are we trying to save
263 * the computer at the other end the trouble of typing a lot of characters?)
265 void imap_handle_macros(char *str) {
269 for (i=0; i<strlen(str); ++i) {
270 if (str[i]=='(') ++nest;
271 if (str[i]=='[') ++nest;
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;
280 imap_macro_replace(&str[i],
282 "FLAGS INTERNALDATE RFC822.SIZE ENVELOPE"
284 imap_macro_replace(&str[i],
288 imap_macro_replace(&str[i],
290 "FLAGS INTERNALDATE RFC822.SIZE"
292 imap_macro_replace(&str[i],
294 "FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODY"
302 * Break out the data items requested, possibly a parenthesized list.
303 * Returns the number of data items, or -1 if the list is invalid.
304 * NOTE: this function alters the string it is fed, and uses it as a buffer
305 * to hold the data for the pointers it returns.
307 int imap_extract_data_items(char **argv, char *items) {
313 /* Convert all whitespace to ordinary space characters. */
314 for (i=0; i<strlen(items); ++i) {
315 if (isspace(items[i])) items[i]=' ';
318 /* Strip leading and trailing whitespace, then strip leading and
319 * trailing parentheses if it's a list
322 if ( (items[0]=='(') && (items[strlen(items)-1]==')') ) {
323 items[strlen(items)-1] = 0;
324 strcpy(items, &items[1]);
328 /* Parse any macro data items */
329 imap_handle_macros(items);
332 * Now break out the data items. We throw in one trailing space in
333 * order to avoid having to break out the last one manually.
337 initial_len = strlen(items);
338 for (i=0; i<initial_len; ++i) {
339 if (items[i]=='(') ++nest;
340 if (items[i]=='[') ++nest;
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;
348 if (nest <= 0) if (items[i]==' ') {
350 argv[num_items++] = start;
362 * This function is called by the main command loop.
364 void imap_fetch(int num_parms, char *parms[]) {
367 char lostr[1024], histr[1024], items[1024];
373 cprintf("%s BAD invalid parameters\r\n", parms[0]);
377 extract_token(lostr, parms[2], 0, ':');
379 extract_token(histr, parms[2], 1, ':');
380 if (!strcmp(histr, "*")) {
387 /* Clear out the IMAP_FETCHED flags and then set them for the messages
388 * we're interested in.
390 for (i = 1; i <= IMAP->num_msgs; ++i) {
391 IMAP->flags[i-1] = IMAP->flags[i-1] & ~IMAP_FETCHED;
394 for (i = 1; i <= IMAP->num_msgs; ++i) {
395 if ( (i>=lo) && (i<=hi) ) {
396 IMAP->flags[i-1] = IMAP->flags[i-1] | IMAP_FETCHED;
401 for (i=3; i<num_parms; ++i) {
402 strcat(items, parms[i]);
403 if (i < (num_parms-1)) strcat(items, " ");
406 num_items = imap_extract_data_items(itemlist, items);
408 cprintf("%s BAD invalid data item list\r\n", parms[0]);
412 imap_do_fetch(num_items, itemlist);
413 cprintf("%s OK FETCH completed\r\n", parms[0]);