]> code.citadel.org Git - citadel.git/blob - citadel/imap_list.c
Completed the portion of draft-ietf-imapext-list-extensions-18
[citadel.git] / citadel / imap_list.c
1 /*
2  * $Id$ 
3  *
4  * Implements the LIST and LSUB commands.
5  *
6  * Copyright (C) 2000-2007 by Art Cancro and others.
7  * This code is released under the terms of the GNU General Public License.
8  *
9  */
10
11 #include "sysdep.h"
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <stdio.h>
15 #include <fcntl.h>
16 #include <signal.h>
17 #include <pwd.h>
18 #include <errno.h>
19 #include <sys/types.h>
20
21 #if TIME_WITH_SYS_TIME
22 # include <sys/time.h>
23 # include <time.h>
24 #else
25 # if HAVE_SYS_TIME_H
26 #  include <sys/time.h>
27 # else
28 #  include <time.h>
29 # endif
30 #endif
31
32 #include <sys/wait.h>
33 #include <ctype.h>
34 #include <string.h>
35 #include <limits.h>
36 #include "citadel.h"
37 #include "server.h"
38 #include "sysdep_decls.h"
39 #include "citserver.h"
40 #include "support.h"
41 #include "config.h"
42 #include "serv_extensions.h"
43 #include "room_ops.h"
44 #include "user_ops.h"
45 #include "policy.h"
46 #include "database.h"
47 #include "msgbase.h"
48 #include "tools.h"
49 #include "internet_addressing.h"
50 #include "serv_imap.h"
51 #include "imap_tools.h"
52 #include "imap_fetch.h"
53 #include "imap_search.h"
54 #include "imap_store.h"
55 #include "imap_acl.h"
56 #include "imap_misc.h"
57
58 #ifdef HAVE_OPENSSL
59 #include "serv_crypto.h"
60 #endif
61
62 /*
63  * Used by LIST and LSUB to show the floors in the listing
64  */
65 void imap_list_floors(char *verb, int num_patterns, char **patterns)
66 {
67         int i;
68         struct floor *fl;
69         int j = 0;
70         int match = 0;
71
72         for (i = 0; i < MAXFLOORS; ++i) {
73                 fl = cgetfloor(i);
74                 if (fl->f_flags & F_INUSE) {
75                         match = 0;
76                         for (j=0; j<num_patterns; ++j) {
77                                 if (imap_mailbox_matches_pattern (patterns[j], fl->f_name)) {
78                                         match = 1;
79                                 }
80                         }
81                         if (match) {
82                                 cprintf("* %s (\\NoSelect) \"/\" ", verb);
83                                 imap_strout(fl->f_name);
84                                 cprintf("\r\n");
85                         }
86                 }
87         }
88 }
89
90
91 /*
92  * Back end for imap_list()
93  *
94  * Implementation note: IMAP "subscribed folder" is equivalent to Citadel "known room"
95  *
96  * The "user data" field is actually an array of pointers; see below for the breakdown
97  *
98  */
99 void imap_listroom(struct ctdlroom *qrbuf, void *data)
100 {
101         char buf[SIZ];
102         int ra;
103         int yes_output_this_room;
104
105         char **data_for_callback;
106         char *verb;
107         int subscribed_rooms_only;
108         int num_patterns;
109         char **patterns;
110         int i = 0;
111         int match = 0;
112
113         /* Here's how we break down the array of pointers passed to us */
114         data_for_callback = data;
115         verb = data_for_callback[0];
116         subscribed_rooms_only = (int) data_for_callback[1];
117         num_patterns = (int) data_for_callback[2];
118         patterns = (char **) data_for_callback[3];
119
120         /* Only list rooms to which the user has access!! */
121         yes_output_this_room = 0;
122         CtdlRoomAccess(qrbuf, &CC->user, &ra, NULL);
123
124         if (subscribed_rooms_only) {
125                 if (ra & UA_KNOWN) {
126                         yes_output_this_room = 1;
127                 }
128         }
129         else {
130                 if ((ra & UA_KNOWN) || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
131                         yes_output_this_room = 1;
132                 }
133         }
134
135         if (yes_output_this_room) {
136                 imap_mailboxname(buf, sizeof buf, qrbuf);
137                 match = 0;
138                 for (i=0; i<num_patterns; ++i) {
139                         if (imap_mailbox_matches_pattern(patterns[i], buf)) {
140                                 match = 1;
141                         }
142                 }
143                 if (match) {
144                         cprintf("* %s () \"/\" ", verb);
145                         imap_strout(buf);
146                         cprintf("\r\n");
147                 }
148         }
149 }
150
151 #define MAX_PATTERNS 20
152
153 /*
154  * Implements the LIST and LSUB commands
155  */
156 void imap_list(int num_parms, char *parms[])
157 {
158         int subscribed_rooms_only = 0;
159         char verb[16];
160         int i, j, paren_nest;
161         char *data_for_callback[3];
162         int num_patterns = 1;
163         char *patterns[MAX_PATTERNS];
164         int selection_left = (-1);
165         int selection_right = (-1);
166         int root_pos = 2;
167         int patterns_left = 3;
168         int patterns_right = 3;
169         int extended_list_in_use = 0;
170
171         if (num_parms < 4) {
172                 cprintf("%s BAD arguments invalid\r\n", parms[0]);
173                 return;
174         }
175
176         /* parms[1] is the IMAP verb being used (e.g. LIST or LSUB)
177          * This tells us how to behave, and what verb to return back to the caller
178          */
179         safestrncpy(verb, parms[1], sizeof verb);
180         j = strlen(verb);
181         for (i=0; i<j; ++i) {
182                 verb[i] = toupper(verb[i]);
183         }
184
185         if (!strcasecmp(verb, "LSUB")) {
186                 subscribed_rooms_only = 1;
187         }
188
189         /*
190          * In order to implement draft-ietf-imapext-list-extensions-18
191          * ("LIST Command Extensions") we need to:
192          * 1. Extract "selection options" (DONE, but we don't do anything with it yet)
193          * 2. Extract "return options"
194          * 3. Determine whether there is more than one match pattern (DONE)
195          */
196
197         /*
198          * If parameter 2 begins with a '(' character, the client is specifying
199          * selection options.  Extract their exact position, and then modify our
200          * expectation of where the root folder will be specified.
201          */
202         if (parms[2][0] == '(') {
203                 extended_list_in_use = 1;
204                 selection_left = 2;
205                 paren_nest = 0;
206                 for (i=2; i<num_parms; ++i) {
207                         for (j=0; j<strlen(parms[i]); ++j) {
208                                 if (parms[i][j] == '(') ++paren_nest;
209                                 if (parms[i][j] == ')') --paren_nest;
210                         }
211                         if (paren_nest == 0) {
212                                 selection_right = i;    /* found end of selection options */
213                                 root_pos = i+1;         /* folder root appears after selection options */
214                                 i = num_parms + 1;      /* break out of the loop */
215                         }
216                 }
217         }
218
219         /* The folder root appears immediately after the selection options,
220          * or in position 2 if no selection options were specified.
221          */
222         patterns_left = root_pos + 1;
223         patterns_right = root_pos + 1;
224
225         if (parms[patterns_left][0] == '(') {
226                 extended_list_in_use = 1;
227                 paren_nest = 0;
228                 for (i=patterns_left; i<num_parms; ++i) {
229                         for (j=0; j<strlen(parms[i]); ++j) {
230                                 if (parms[i][j] == '(') ++paren_nest;
231                                 if (parms[i][j] == ')') --paren_nest;
232                         }
233                         if (paren_nest == 0) {
234                                 patterns_right = i;     /* found end of patterns */
235                                 i = num_parms + 1;      /* break out of the loop */
236                         }
237                 }
238                 num_patterns = patterns_right - patterns_left + 1;
239                 for (i=0; i<num_patterns; ++i) {
240                         if (i < MAX_PATTERNS) {
241                                 patterns[i] = malloc(512);
242                                 snprintf(patterns[i], 512, "%s%s", parms[root_pos], parms[patterns_left+i]);
243                                 if (i == 0) {
244                                         strcpy(patterns[i], &patterns[i][1]);
245                                 }
246                                 if (i == num_patterns-1) {
247                                         patterns[i][strlen(patterns[i])-1] = 0;
248                                 }
249                         }
250                 }
251         }
252         else {
253                 num_patterns = 1;
254                 patterns[0] = malloc(512);
255                 snprintf(patterns[0], 512, "%s%s", parms[root_pos], parms[patterns_left]);
256         }
257
258         data_for_callback[0] = verb;
259         data_for_callback[1] = (char *) subscribed_rooms_only;
260         data_for_callback[2] = (char *) num_patterns;
261         data_for_callback[3] = (char *) patterns;
262
263         /* The non-extended LIST command is required to treat an empty
264          * ("" string) mailbox name argument as a special request to return the
265          * hierarchy delimiter and the root name of the name given in the
266          * reference parameter.
267          */
268         if ( (strlen(patterns[0]) == 0) && (extended_list_in_use == 0) ) {
269                 cprintf("* %s (\\Noselect) \"/\" \"\"\r\n", verb);
270         }
271
272         /* Non-empty mailbox names, and any form of the extended LIST command,
273          * is handled by this loop.
274          */
275         else {
276                 imap_list_floors(verb, num_patterns, patterns);
277                 ForEachRoom(imap_listroom, data_for_callback);
278         }
279
280         /* 
281          * Free the pattern buffers we allocated above.
282          */
283         for (i=0; i<num_patterns; ++i) {
284                 free(patterns[i]);
285         }
286
287         cprintf("%s OK %s completed\r\n", parms[0], verb);
288 }