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