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