be01c99a77ac4204644c41b09a36b4412fd9b41a
[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 "room_ops.h"
43 #include "user_ops.h"
44 #include "policy.h"
45 #include "database.h"
46 #include "msgbase.h"
47 #include "tools.h"
48 #include "internet_addressing.h"
49 #include "serv_imap.h"
50 #include "imap_tools.h"
51 #include "imap_fetch.h"
52 #include "imap_search.h"
53 #include "imap_store.h"
54 #include "imap_acl.h"
55 #include "imap_misc.h"
56 #include "imap_list.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 \\HasChildren) \"/\" ", 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         char return_options[256];
103         int ra;
104         int yes_output_this_room;
105
106         char **data_for_callback;
107         char *verb;
108         int subscribed_rooms_only;
109         int num_patterns;
110         char **patterns;
111         int return_subscribed;
112         int return_children;
113         int return_metadata;
114         int i = 0;
115         int match = 0;
116
117         /* Here's how we break down the array of pointers passed to us */
118         data_for_callback = data;
119         verb = data_for_callback[0];
120         subscribed_rooms_only = (int) data_for_callback[1];
121         num_patterns = (int) data_for_callback[2];
122         patterns = (char **) data_for_callback[3];
123         return_subscribed = (int) data_for_callback[4];
124         return_children = (int) data_for_callback[5];
125         return_metadata = (int) data_for_callback[6];
126
127         /* Only list rooms to which the user has access!! */
128         yes_output_this_room = 0;
129         strcpy(return_options, "");
130         CtdlRoomAccess(qrbuf, &CC->user, &ra, NULL);
131
132         if (return_subscribed) {
133                 if (ra & UA_KNOWN) {
134                         strcat(return_options, "\\Subscribed");
135                 }
136         }
137
138         /* Warning: ugly hack.
139          * We don't have any way to determine the presence of child mailboxes
140          * without refactoring this entire module.  So we're just going to return
141          * the \HasChildren attribute for every room.
142          * We'll fix this later when we have time.
143          */
144         if (return_children) {
145                 if (strlen(return_options) > 0) {
146                         strcat(return_options, " ");
147                 }
148                 strcat(return_options, "\\HasChildren");
149         }
150
151         if (subscribed_rooms_only) {
152                 if (ra & UA_KNOWN) {
153                         yes_output_this_room = 1;
154                 }
155         }
156         else {
157                 if ((ra & UA_KNOWN) || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
158                         yes_output_this_room = 1;
159                 }
160         }
161
162         if (yes_output_this_room) {
163                 imap_mailboxname(buf, sizeof buf, qrbuf);
164                 match = 0;
165                 for (i=0; i<num_patterns; ++i) {
166                         if (imap_mailbox_matches_pattern(patterns[i], buf)) {
167                                 match = 1;
168                         }
169                 }
170                 if (match) {
171                         cprintf("* %s (%s) \"/\" ", verb, return_options);
172                         imap_strout(buf);
173
174                         if (return_metadata) {
175                                 cprintf(" (METADATA ())");      /* FIXME */
176                         }
177
178                         cprintf("\r\n");
179                 }
180         }
181 }
182
183
184 /*
185  * Implements the LIST and LSUB commands
186  */
187 void imap_list(int num_parms, char *parms[])
188 {
189         int subscribed_rooms_only = 0;
190         char verb[16];
191         int i, j, paren_nest;
192         char *data_for_callback[7];
193         int num_patterns = 1;
194         char *patterns[MAX_PATTERNS];
195         int selection_left = (-1);
196         int selection_right = (-1);
197         int return_left = (-1);
198         int return_right = (-1);
199         int root_pos = 2;
200         int patterns_left = 3;
201         int patterns_right = 3;
202         int extended_list_in_use = 0;
203         int return_subscribed = 0;
204         int return_children = 0;
205         int return_metadata = 0;
206         int select_metadata_left = (-1);
207         int select_metadata_right = (-1);
208         int select_metadata_nest = 0;
209
210         if (num_parms < 4) {
211                 cprintf("%s BAD arguments invalid\r\n", parms[0]);
212                 return;
213         }
214
215         /* parms[1] is the IMAP verb being used (e.g. LIST or LSUB)
216          * This tells us how to behave, and what verb to return back to the caller
217          */
218         safestrncpy(verb, parms[1], sizeof verb);
219         j = strlen(verb);
220         for (i=0; i<j; ++i) {
221                 verb[i] = toupper(verb[i]);
222         }
223
224         if (!strcasecmp(verb, "LSUB")) {
225                 subscribed_rooms_only = 1;
226         }
227
228         /*
229          * In order to implement draft-ietf-imapext-list-extensions-18
230          * ("LIST Command Extensions") we need to:
231          *
232          * 1. Extract "selection options"
233          *                              (Extraction: done
234          *                              SUBSCRIBED option: done
235          *                              RECURSIVEMATCH option: not done yet
236          *                              REMOTE: safe to silently ignore)
237          *
238          * 2. Extract "return options"
239          *                              (Extraction: done
240          *                              SUBSCRIBED option: done
241          *                              CHILDREN option: done, but needs a non-ugly rewrite)
242          *
243          * 3. Determine whether there is more than one match pattern (done)
244          */
245
246         /*
247          * If parameter 2 begins with a '(' character, the client is specifying
248          * selection options.  Extract their exact position, and then modify our
249          * expectation of where the root folder will be specified.
250          */
251         if (parms[2][0] == '(') {
252                 extended_list_in_use = 1;
253                 selection_left = 2;
254                 paren_nest = 0;
255                 for (i=2; i<num_parms; ++i) {
256                         for (j=0; j<strlen(parms[i]); ++j) {
257                                 if (parms[i][j] == '(') ++paren_nest;
258                                 if (parms[i][j] == ')') --paren_nest;
259                         }
260                         if (paren_nest == 0) {
261                                 selection_right = i;    /* found end of selection options */
262                                 root_pos = i+1;         /* folder root appears after selection options */
263                                 i = num_parms + 1;      /* break out of the loop */
264                         }
265                 }
266         }
267
268         /* If selection options were found, do something with them.
269          */
270         if ((selection_left > 0) && (selection_right >= selection_left)) {
271
272                 /* Strip off the outer parentheses */
273                 if (parms[selection_left][0] == '(') {
274                         strcpy(parms[selection_left], &parms[selection_left][1]);
275                 }
276                 if (parms[selection_right][strlen(parms[selection_right])-1] == ')') {
277                         parms[selection_right][strlen(parms[selection_right])-1] = 0;
278                 }
279
280                 for (i=selection_left; i<=selection_right; ++i) {
281
282                         /* are we in the middle of a metadata select block? */
283                         if ((select_metadata_left >= 0) && (select_metadata_right < 0)) {
284                                 select_metadata_nest += haschar(parms[i], '(') - haschar(parms[i], ')') ;
285                                 if (select_metadata_nest == 0) {
286                                         select_metadata_right = i;
287                                 }
288                         }
289
290                         else if (!strcasecmp(parms[i], "METADATA")) {
291                                 select_metadata_left = i+1;
292                                 select_metadata_nest = 0;
293                         }
294
295                         else if (!strcasecmp(parms[i], "SUBSCRIBED")) {
296                                 subscribed_rooms_only = 1;
297                         }
298
299                         else if (!strcasecmp(parms[i], "RECURSIVEMATCH")) {
300                                 /* FIXME - do this! */
301                         }
302
303                 }
304
305         }
306
307         lprintf(CTDL_DEBUG, "select metadata: %d to %d\n", select_metadata_left, select_metadata_right);
308         /* FIXME blah, we have to do something with this */
309
310         /* The folder root appears immediately after the selection options,
311          * or in position 2 if no selection options were specified.
312          */
313         patterns_left = root_pos + 1;
314         patterns_right = root_pos + 1;
315
316         if (parms[patterns_left][0] == '(') {
317                 extended_list_in_use = 1;
318                 paren_nest = 0;
319                 for (i=patterns_left; i<num_parms; ++i) {
320                         for (j=0; j<strlen(parms[i]); ++j) {
321                                 if (parms[i][j] == '(') ++paren_nest;
322                                 if (parms[i][j] == ')') --paren_nest;
323                         }
324                         if (paren_nest == 0) {
325                                 patterns_right = i;     /* found end of patterns */
326                                 i = num_parms + 1;      /* break out of the loop */
327                         }
328                 }
329                 num_patterns = patterns_right - patterns_left + 1;
330                 for (i=0; i<num_patterns; ++i) {
331                         if (i < MAX_PATTERNS) {
332                                 patterns[i] = malloc(512);
333                                 snprintf(patterns[i], 512, "%s%s", parms[root_pos], parms[patterns_left+i]);
334                                 if (i == 0) {
335                                         strcpy(patterns[i], &patterns[i][1]);
336                                 }
337                                 if (i == num_patterns-1) {
338                                         patterns[i][strlen(patterns[i])-1] = 0;
339                                 }
340                         }
341                 }
342         }
343         else {
344                 num_patterns = 1;
345                 patterns[0] = malloc(512);
346                 snprintf(patterns[0], 512, "%s%s", parms[root_pos], parms[patterns_left]);
347         }
348
349         /* If the word "RETURN" appears after the folder pattern list, then the client
350          * is specifying return options.
351          */
352         if (num_parms - patterns_right > 2) if (!strcasecmp(parms[patterns_right+1], "RETURN")) {
353                 return_left = patterns_right + 2;
354                 extended_list_in_use = 1;
355                 paren_nest = 0;
356                 for (i=return_left; i<num_parms; ++i) {
357                         for (j=0; j<strlen(parms[i]); ++j) {
358                                 if (parms[i][j] == '(') ++paren_nest;
359                                 if (parms[i][j] == ')') --paren_nest;
360                         }
361
362                         /* Might as well look for these while we're in here... */
363                         if (parms[i][0] == '(') strcpy(parms[i], &parms[i][1]);
364                         if (parms[i][strlen(parms[i])-1] == ')') parms[i][strlen(parms[i])-1] = 0;
365                         lprintf(9, "evaluating <%s>\n", parms[i]);
366
367                         if (!strcasecmp(parms[i], "SUBSCRIBED")) {
368                                 return_subscribed = 1;
369                         }
370
371                         else if (!strcasecmp(parms[i], "CHILDREN")) {
372                                 return_children = 1;
373                         }
374
375                         else if (!strcasecmp(parms[i], "METADATA")) {
376                                 return_metadata = 1;
377                         }
378
379                         if (paren_nest == 0) {
380                                 return_right = i;       /* found end of patterns */
381                                 i = num_parms + 1;      /* break out of the loop */
382                         }
383                 }
384         }
385
386         /* Now start setting up the data we're going to send to the ForEachRoom() callback.
387          */
388         data_for_callback[0] = (char *) verb;
389         data_for_callback[1] = (char *) subscribed_rooms_only;
390         data_for_callback[2] = (char *) num_patterns;
391         data_for_callback[3] = (char *) patterns;
392         data_for_callback[4] = (char *) return_subscribed;
393         data_for_callback[5] = (char *) return_children;
394         data_for_callback[6] = (char *) return_metadata;
395
396         /* The non-extended LIST command is required to treat an empty
397          * ("" string) mailbox name argument as a special request to return the
398          * hierarchy delimiter and the root name of the name given in the
399          * reference parameter.
400          */
401         if ( (strlen(patterns[0]) == 0) && (extended_list_in_use == 0) ) {
402                 cprintf("* %s (\\Noselect) \"/\" \"\"\r\n", verb);
403         }
404
405         /* Non-empty mailbox names, and any form of the extended LIST command,
406          * is handled by this loop.
407          */
408         else {
409                 imap_list_floors(verb, num_patterns, patterns);
410                 ForEachRoom(imap_listroom, data_for_callback);
411         }
412
413         /* 
414          * Free the pattern buffers we allocated above.
415          */
416         for (i=0; i<num_patterns; ++i) {
417                 free(patterns[i]);
418         }
419
420         cprintf("%s OK %s completed\r\n", parms[0], verb);
421 }