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"
46 * Individual field functions for imap_do_fetch_msg() ...
51 void imap_fetch_uid(int seq) {
52 cprintf("UID %ld", IMAP->msgids[seq-1]);
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.
60 void imap_do_fetch_msg(int seq, struct CtdlMessage *msg,
61 int num_items, char **itemlist) {
64 cprintf("* %d FETCH (", seq);
66 for (i=0; i<num_items; ++i) {
68 if (!strncasecmp(itemlist[i], "BODY[", 5)) {
69 /* FIXME do something here */
71 else if (!strncasecmp(itemlist[i], "BODY.PEEK[", 10)) {
72 /* FIXME do something here */
74 else if (!strcasecmp(itemlist[i], "BODYSTRUCTURE")) {
75 /* FIXME do something here */
77 else if (!strcasecmp(itemlist[i], "ENVELOPE")) {
78 /* FIXME do something here */
80 else if (!strcasecmp(itemlist[i], "FLAGS")) {
81 /* FIXME do something here */
83 else if (!strcasecmp(itemlist[i], "INTERNALDATE")) {
84 /* FIXME do something here */
86 else if (!strcasecmp(itemlist[i], "RFC822")) {
87 /* FIXME do something here */
89 else if (!strcasecmp(itemlist[i], "RFC822.HEADER")) {
90 /* FIXME do something here */
92 else if (!strcasecmp(itemlist[i], "RFC822.SIZE")) {
93 /* FIXME do something here */
95 else if (!strcasecmp(itemlist[i], "RFC822.TEXT")) {
96 /* FIXME do something here */
98 else if (!strcasecmp(itemlist[i], "UID")) {
102 if (i != num_items-1) cprintf(" ");
111 * imap_fetch() calls imap_do_fetch() to do its actual work, once it's
112 * validated and boiled down the request a bit.
114 void imap_do_fetch(int lo, int hi, int num_items, char **itemlist) {
116 struct CtdlMessage *msg;
118 for (i=0; i<num_items; ++i) {
119 lprintf(9, "* item[%d] = <%s>\r\n", i, itemlist[i]);
122 for (i = lo; i <= hi; ++i) {
123 msg = CtdlFetchMessage(IMAP->msgids[i-1]);
125 imap_do_fetch_msg(i, msg, num_items, itemlist);
126 CtdlFreeMessage(msg);
129 cprintf("* %d FETCH <internal error>\r\n", i);
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.
141 void imap_macro_replace(char *str, char *find, char *replace) {
144 if (!strncasecmp(str, find, strlen(find))) {
145 if (str[strlen(find)]==' ') {
146 strcpy(holdbuf, &str[strlen(find)+1]);
147 strcpy(str, replace);
149 strcat(str, holdbuf);
151 if (str[strlen(find)]==0) {
152 strcpy(holdbuf, &str[strlen(find)+1]);
153 strcpy(str, replace);
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?)
165 void imap_handle_macros(char *str) {
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;
180 imap_macro_replace(&str[i],
182 "FLAGS INTERNALDATE RFC822.SIZE ENVELOPE"
184 imap_macro_replace(&str[i],
188 imap_macro_replace(&str[i],
190 "FLAGS INTERNALDATE RFC822.SIZE"
192 imap_macro_replace(&str[i],
194 "FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODY"
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.
207 int imap_extract_data_items(char **argv, char *items) {
213 /* Convert all whitespace to ordinary space characters. */
214 for (i=0; i<strlen(items); ++i) {
215 if (isspace(items[i])) items[i]=' ';
218 /* Strip leading and trailing whitespace, then strip leading and
219 * trailing parentheses if it's a list
222 if ( (items[0]=='(') && (items[strlen(items)-1]==')') ) {
223 items[strlen(items)-1] = 0;
224 strcpy(items, &items[1]);
228 /* Parse any macro data items */
229 imap_handle_macros(items);
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.
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;
248 if (nest <= 0) if (items[i]==' ') {
250 argv[num_items++] = start;
262 * This function is called by the main command loop.
264 void imap_fetch(int num_parms, char *parms[]) {
267 char lostr[1024], histr[1024], items[1024];
273 cprintf("%s BAD invalid parameters\r\n", parms[0]);
277 extract_token(lostr, parms[2], 0, ':');
279 extract_token(histr, parms[2], 1, ':');
282 if ( (lo < 1) || (hi < 1) || (lo > hi) || (hi > IMAP->num_msgs) ) {
283 cprintf("%s BAD invalid sequence numbers %d:%d\r\n",
289 for (i=3; i<num_parms; ++i) {
290 strcat(items, parms[i]);
291 if (i < (num_parms-1)) strcat(items, " ");
294 num_items = imap_extract_data_items(itemlist, items);
296 cprintf("%s BAD invalid data item list\r\n", parms[0]);
300 imap_do_fetch(lo, hi, num_items, itemlist);
301 cprintf("%s OK FETCH completed\r\n", parms[0]);