]> code.citadel.org Git - citadel.git/blob - citadel/imap_fetch.c
* plowing forward on imap
[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                 cprintf("* 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  * Break out the data items requested, possibly a parenthesized list.
86  * Returns the number of data items, or -1 if the list is invalid.
87  * NOTE: this function alters the string it is fed, and uses it as a buffer
88  * to hold the data for the pointers it returns.
89  */
90 int imap_extract_data_items(char **argv, char *items) {
91         int num_items = 0;
92         int nest = 0;
93         int i, initial_len;
94         char *start;
95
96         /* Convert all whitespace to ordinary space characters. */
97         for (i=0; i<strlen(items); ++i) {
98                 if (isspace(items[i])) items[i]=' ';
99         }
100
101         /* Strip leading and trailing whitespace, then strip leading and
102          * trailing parentheses if it's a list
103          */
104         striplt(items);
105         if ( (items[0]=='(') && (items[strlen(items)-1]==')') ) {
106                 items[strlen(items)-1] = 0;
107                 strcpy(items, &items[1]);
108                 striplt(items);
109         }
110
111         /*
112          * Now break out the data items.  We throw in one trailing space in
113          * order to avoid having to break out the last one manually.
114          */
115         strcat(items, " ");
116         start = items;
117         initial_len = strlen(items);
118         for (i=0; i<initial_len; ++i) {
119                 if (items[i]=='(') ++nest;
120                 if (items[i]=='[') ++nest;
121                 if (items[i]=='<') ++nest;
122                 if (items[i]=='{') ++nest;
123                 if (items[i]==')') --nest;
124                 if (items[i]==']') --nest;
125                 if (items[i]=='>') --nest;
126                 if (items[i]=='}') --nest;
127
128                 if (nest <= 0) if (items[i]==' ') {
129                         items[i] = 0;
130                         argv[num_items++] = start;
131                         start = &items[i+1];
132                 }
133         }
134
135         return(num_items);
136
137 }
138
139
140
141
142
143 /*
144  * This function is called by the main command loop.
145  */
146 void imap_fetch(int num_parms, char *parms[]) {
147         int lo = 0;
148         int hi = 0;
149         char lostr[1024], histr[1024], items[1024];
150         char *itemlist[256];
151         int num_items;
152         int i;
153
154         if (num_parms < 4) {
155                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
156                 return;
157         }
158
159         extract_token(lostr, parms[2], 0, ':');
160         lo = atoi(lostr);
161         extract_token(histr, parms[2], 1, ':');
162         hi = atoi(histr);
163
164         if ( (lo < 1) || (hi < 1) || (lo > hi) || (hi > IMAP->num_msgs) ) {
165                 cprintf("%s BAD invalid sequence numbers %d:%d\r\n",
166                         parms[0], lo, hi);
167                 return;
168         }
169
170         strcpy(items, "");
171         for (i=3; i<num_parms; ++i) {
172                 strcat(items, parms[i]);
173                 if (i < (num_parms-1)) strcat(items, " ");
174         }
175
176         num_items = imap_extract_data_items(itemlist, items);
177         if (num_items < 1) {
178                 cprintf("%s BAD invalid data item list\r\n", parms[0]);
179                 return;
180         }
181
182         imap_do_fetch(lo, hi, num_items, itemlist);
183         cprintf("%s OK FETCH completed\r\n", parms[0]);
184 }
185
186
187