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