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