]> code.citadel.org Git - citadel.git/blob - citadel/imap_fetch.c
* We're getting there. Managed to parse IMAP data item lists. A little.
[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  * Back end function.
47  */
48 void imap_do_fetch(int lo, int hi, int num_items, char **itemlist) {
49         int i;
50
51         cprintf("* imap_do_fetch() lo=%d hi=%d num_items=%d\r\n",
52                 lo, hi, num_items);
53
54         for (i=0; i<num_items; ++i) {
55                 cprintf("* item[%d] = <%s>\r\n", i, itemlist[i]);
56         }
57 }
58
59
60
61
62
63 /*
64  * Break out the data items requested, possibly a parenthesized list.
65  * Returns the number of data items, or -1 if the list is invalid.
66  * NOTE: this function alters the string it is fed, and uses it as a buffer
67  * to hold the data for the pointers it returns.
68  */
69 int imap_extract_data_items(char **argv, char *items) {
70         int num_items = 0;
71         int nest = 0;
72         int i, initial_len;
73         char *start;
74
75         /* Convert all whitespace to ordinary space characters. */
76         for (i=0; i<strlen(items); ++i) {
77                 if (isspace(items[i])) items[i]=' ';
78         }
79
80         /* Strip leading and trailing whitespace, then strip leading and
81          * trailing parentheses if it's a list
82          */
83         striplt(items);
84         if ( (items[0]=='(') && (items[strlen(items)-1]==')') ) {
85                 items[strlen(items)-1] = 0;
86                 strcpy(items, &items[1]);
87                 striplt(items);
88         }
89
90         /*
91          * Now break out the data items.  We throw in one trailing space in
92          * order to avoid having to break out the last one manually.
93          */
94         strcat(items, " ");
95         start = items;
96         initial_len = strlen(items);
97         for (i=0; i<initial_len; ++i) {
98                 if (items[i]=='(') ++nest;
99                 if (items[i]=='[') ++nest;
100                 if (items[i]=='<') ++nest;
101                 if (items[i]=='{') ++nest;
102                 if (items[i]==')') --nest;
103                 if (items[i]==']') --nest;
104                 if (items[i]=='>') --nest;
105                 if (items[i]=='}') --nest;
106
107                 if (nest <= 0) if (items[i]==' ') {
108                         items[i] = 0;
109                         argv[num_items++] = start;
110                         start = &items[i+1];
111                 }
112         }
113
114         return(num_items);
115
116 }
117
118
119
120
121
122 /*
123  * This function is called by the main command loop.
124  */
125 void imap_fetch(int num_parms, char *parms[]) {
126         int lo = 0;
127         int hi = 0;
128         char lostr[1024], histr[1024], items[1024];
129         char *itemlist[256];
130         int num_items;
131         int i;
132
133         if (num_parms < 4) {
134                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
135                 return;
136         }
137
138         extract_token(lostr, parms[2], 0, ':');
139         lo = atoi(lostr);
140         extract_token(histr, parms[2], 1, ':');
141         hi = atoi(histr);
142
143         if ( (lo < 1) || (hi < 1) || (lo > hi) || (hi > IMAP->num_msgs) ) {
144                 cprintf("%s BAD invalid sequence numbers %d:%d\r\n",
145                         parms[0], lo, hi);
146                 return;
147         }
148
149         strcpy(items, "");
150         for (i=3; i<num_parms; ++i) {
151                 strcat(items, parms[i]);
152                 if (i < (num_parms-1)) strcat(items, " ");
153         }
154
155         num_items = imap_extract_data_items(itemlist, items);
156         if (num_items < 1) {
157                 cprintf("%s BAD invalid data item list\r\n", parms[0]);
158                 return;
159         }
160
161         imap_do_fetch(lo, hi, num_items, itemlist);
162         cprintf("%s OK FETCH completed\r\n", parms[0]);
163 }
164
165
166