* More license declarations
[citadel.git] / citadel / modules / imap / imap_list.c
1 /*
2  * $Id$
3  *
4  * Implements the LIST and LSUB commands.
5  *
6  * Copyright (c) 2000-2009 by Art Cancro and others.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 3 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "sysdep.h"
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include <signal.h>
29 #include <pwd.h>
30 #include <errno.h>
31 #include <sys/types.h>
32
33 #if TIME_WITH_SYS_TIME
34 # include <sys/time.h>
35 # include <time.h>
36 #else
37 # if HAVE_SYS_TIME_H
38 #  include <sys/time.h>
39 # else
40 #  include <time.h>
41 # endif
42 #endif
43
44 #include <sys/wait.h>
45 #include <ctype.h>
46 #include <string.h>
47 #include <limits.h>
48 #include <libcitadel.h>
49 #include "citadel.h"
50 #include "server.h"
51 #include "sysdep_decls.h"
52 #include "citserver.h"
53 #include "support.h"
54 #include "config.h"
55 #include "room_ops.h"
56 #include "user_ops.h"
57 #include "policy.h"
58 #include "database.h"
59 #include "msgbase.h"
60 #include "internet_addressing.h"
61 #include "serv_imap.h"
62 #include "imap_tools.h"
63 #include "imap_fetch.h"
64 #include "imap_search.h"
65 #include "imap_store.h"
66 #include "imap_acl.h"
67 #include "imap_misc.h"
68 #include "imap_list.h"
69 #include "ctdl_module.h"
70
71
72 /*
73  * Used by LIST and LSUB to show the floors in the listing
74  */
75 void imap_list_floors(char *verb, int num_patterns, char **patterns)
76 {
77         int i;
78         struct floor *fl;
79         int j = 0;
80         int match = 0;
81
82         for (i = 0; i < MAXFLOORS; ++i) {
83                 fl = cgetfloor(i);
84                 if (fl->f_flags & F_INUSE) {
85                         match = 0;
86                         for (j=0; j<num_patterns; ++j) {
87                                 if (imap_mailbox_matches_pattern (patterns[j], fl->f_name)) {
88                                         match = 1;
89                                 }
90                         }
91                         if (match) {
92                                 cprintf("* %s (\\NoSelect \\HasChildren) \"/\" ", verb);
93                                 imap_strout(fl->f_name);
94                                 cprintf("\r\n");
95                         }
96                 }
97         }
98 }
99
100
101 /*
102  * Back end for imap_list()
103  *
104  * Implementation note: IMAP "subscribed folder" is equivalent to Citadel "known room"
105  *
106  * The "user data" field is actually an array of pointers; see below for the breakdown
107  *
108  */
109 void imap_listroom(struct ctdlroom *qrbuf, void *data)
110 {
111         char buf[SIZ];
112         char return_options[256];
113         int ra;
114         int yes_output_this_room;
115
116         char **data_for_callback;
117         char *verb;
118         int subscribed_rooms_only;
119         int num_patterns;
120         char **patterns;
121         int return_subscribed = 0;
122         int return_children = 0;
123         int i = 0;
124         int match = 0;
125
126         /* Here's how we break down the array of pointers passed to us */
127         data_for_callback = data;
128         verb = data_for_callback[0];
129         subscribed_rooms_only = (int) data_for_callback[1];
130         num_patterns = (int) data_for_callback[2];
131         patterns = (char **) data_for_callback[3];
132         return_subscribed = (int) data_for_callback[4];
133         return_children = (int) data_for_callback[5];
134
135         /* Only list rooms to which the user has access!! */
136         yes_output_this_room = 0;
137         strcpy(return_options, "");
138         CtdlRoomAccess(qrbuf, &CC->user, &ra, NULL);
139
140         if (return_subscribed) {
141                 if (ra & UA_KNOWN) {
142                         strcat(return_options, "\\Subscribed");
143                 }
144         }
145
146         /* Warning: ugly hack.
147          * We don't have any way to determine the presence of child mailboxes
148          * without refactoring this entire module.  So we're just going to return
149          * the \HasChildren attribute for every room.
150          * We'll fix this later when we have time.
151          */
152         if (return_children) {
153                 if (!IsEmptyStr(return_options)) {
154                         strcat(return_options, " ");
155                 }
156                 strcat(return_options, "\\HasChildren");
157         }
158
159         if (subscribed_rooms_only) {
160                 if (ra & UA_KNOWN) {
161                         yes_output_this_room = 1;
162                 }
163         }
164         else {
165                 if ((ra & UA_KNOWN) || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
166                         yes_output_this_room = 1;
167                 }
168         }
169
170         if (yes_output_this_room) {
171                 imap_mailboxname(buf, sizeof buf, qrbuf);
172                 match = 0;
173                 for (i=0; i<num_patterns; ++i) {
174                         if (imap_mailbox_matches_pattern(patterns[i], buf)) {
175                                 match = 1;
176                         }
177                 }
178                 if (match) {
179                         cprintf("* %s (%s) \"/\" ", verb, return_options);
180                         imap_strout(buf);
181                         cprintf("\r\n");
182                 }
183         }
184 }
185
186
187 /*
188  * Implements the LIST and LSUB commands
189  */
190 void imap_list(int num_parms, char *parms[])
191 {
192         int subscribed_rooms_only = 0;
193         char verb[16];
194         int i, j, paren_nest;
195         char *data_for_callback[6];
196         int num_patterns = 1;
197         char *patterns[MAX_PATTERNS];
198         int selection_left = (-1);
199         int selection_right = (-1);
200         int return_left = (-1);
201         int return_right = (-1);
202         int root_pos = 2;
203         int patterns_left = 3;
204         int patterns_right = 3;
205         int extended_list_in_use = 0;
206         int return_subscribed = 0;
207         int return_children = 0;
208
209         if (num_parms < 4) {
210                 cprintf("%s BAD arguments invalid\r\n", parms[0]);
211                 return;
212         }
213
214         /* parms[1] is the IMAP verb being used (e.g. LIST or LSUB)
215          * This tells us how to behave, and what verb to return back to the caller
216          */
217         safestrncpy(verb, parms[1], sizeof verb);
218         j = strlen(verb);
219         for (i=0; i<j; ++i) {
220                 verb[i] = toupper(verb[i]);
221         }
222
223         if (!strcasecmp(verb, "LSUB")) {
224                 subscribed_rooms_only = 1;
225         }
226
227         /*
228          * Partial implementation of LIST-EXTENDED (which will not get used because
229          * we don't advertise it in our capabilities string).  Several requirements:
230          *
231          * Extraction of selection options:
232          *      SUBSCRIBED option: done
233          *      RECURSIVEMATCH option: not done yet
234          *      REMOTE: safe to silently ignore
235          *
236          * Extraction of return options:
237          *      SUBSCRIBED option: done
238          *      CHILDREN option: done, but needs a non-ugly rewrite
239          *
240          * Multiple match patterns: 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                         if (!strcasecmp(parms[i], "SUBSCRIBED")) {
280                                 subscribed_rooms_only = 1;
281                         }
282
283                         else if (!strcasecmp(parms[i], "RECURSIVEMATCH")) {
284                                 /* FIXME - do this! */
285                         }
286
287                 }
288
289         }
290
291         /* The folder root appears immediately after the selection options,
292          * or in position 2 if no selection options were specified.
293          */
294         patterns_left = root_pos + 1;
295         patterns_right = root_pos + 1;
296
297         if (parms[patterns_left][0] == '(') {
298                 extended_list_in_use = 1;
299                 paren_nest = 0;
300                 for (i=patterns_left; i<num_parms; ++i) {
301                         for (j=0; &parms[i][j]; ++j) {
302                                 if (parms[i][j] == '(') ++paren_nest;
303                                 if (parms[i][j] == ')') --paren_nest;
304                         }
305                         if (paren_nest == 0) {
306                                 patterns_right = i;     /* found end of patterns */
307                                 i = num_parms + 1;      /* break out of the loop */
308                         }
309                 }
310                 num_patterns = patterns_right - patterns_left + 1;
311                 for (i=0; i<num_patterns; ++i) {
312                         if (i < MAX_PATTERNS) {
313                                 patterns[i] = malloc(512);
314                                 snprintf(patterns[i], 512, "%s%s", parms[root_pos], parms[patterns_left+i]);
315                                 if (i == 0) {
316                                         strcpy(patterns[i], &patterns[i][1]);
317                                 }
318                                 if (i == num_patterns-1) {
319                                         patterns[i][strlen(patterns[i])-1] = 0;
320                                 }
321                         }
322                 }
323         }
324         else {
325                 num_patterns = 1;
326                 patterns[0] = malloc(512);
327                 snprintf(patterns[0], 512, "%s%s", parms[root_pos], parms[patterns_left]);
328         }
329
330         /* If the word "RETURN" appears after the folder pattern list, then the client
331          * is specifying return options.
332          */
333         if (num_parms - patterns_right > 2) if (!strcasecmp(parms[patterns_right+1], "RETURN")) {
334                 return_left = patterns_right + 2;
335                 extended_list_in_use = 1;
336                 paren_nest = 0;
337                 for (i=return_left; i<num_parms; ++i) {
338                         for (j=0; parms[i][j]; ++j) {
339                                 if (parms[i][j] == '(') ++paren_nest;
340                                 if (parms[i][j] == ')') --paren_nest;
341                         }
342
343                         /* Might as well look for these while we're in here... */
344                         if (parms[i][0] == '(') strcpy(parms[i], &parms[i][1]);
345                         if (parms[i][strlen(parms[i])-1] == ')') parms[i][strlen(parms[i])-1] = 0;
346                         CtdlLogPrintf(9, "evaluating <%s>\n", parms[i]);
347
348                         if (!strcasecmp(parms[i], "SUBSCRIBED")) {
349                                 return_subscribed = 1;
350                         }
351
352                         else if (!strcasecmp(parms[i], "CHILDREN")) {
353                                 return_children = 1;
354                         }
355
356                         if (paren_nest == 0) {
357                                 return_right = i;       /* found end of patterns */
358                                 i = num_parms + 1;      /* break out of the loop */
359                         }
360                 }
361         }
362
363         /* Now start setting up the data we're going to send to the ForEachRoom() callback.
364          */
365         data_for_callback[0] = (char *) verb;
366         data_for_callback[1] = (char *) subscribed_rooms_only;
367         data_for_callback[2] = (char *) num_patterns;
368         data_for_callback[3] = (char *) patterns;
369         data_for_callback[4] = (char *) return_subscribed;
370         data_for_callback[5] = (char *) return_children;
371
372         /* The non-extended LIST command is required to treat an empty
373          * ("" string) mailbox name argument as a special request to return the
374          * hierarchy delimiter and the root name of the name given in the
375          * reference parameter.
376          */
377         if ( (IsEmptyStr(patterns[0])) && (extended_list_in_use == 0) ) {
378                 cprintf("* %s (\\Noselect) \"/\" \"\"\r\n", verb);
379         }
380
381         /* Non-empty mailbox names, and any form of the extended LIST command,
382          * is handled by this loop.
383          */
384         else {
385                 imap_list_floors(verb, num_patterns, patterns);
386                 ForEachRoom(imap_listroom, data_for_callback);
387         }
388
389         /* 
390          * Free the pattern buffers we allocated above.
391          */
392         for (i=0; i<num_patterns; ++i) {
393                 free(patterns[i]);
394         }
395
396         cprintf("%s OK %s completed\r\n", parms[0], verb);
397 }