* rework imap tokenizer, we no longer copy the stuff around, we keep a reference...
[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 "imap_tools.h"
61 #include "serv_imap.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                                 plain_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                         plain_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, ConstStr *Params)
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", Params[0].Key);
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, Params[1].Key, sizeof verb);
217         j = Params[1].len;
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 (Params[2].Key[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; Params[i].Key[j]; ++j) {
253                                 if (Params[i].Key[j] == '(') ++paren_nest;
254                                 if (Params[i].Key[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 (Params[selection_left].Key[0] == '(') {
270                         TokenCutLeft(&IMAP->Cmd, 
271                                      &Params[selection_left], 
272                                      1);
273                 }
274                 if (Params[selection_right].Key[Params[selection_right].len-1] == ')') {
275                         TokenCutRight(&IMAP->Cmd, 
276                                       &Params[selection_right], 
277                                       1);
278                 }
279
280                 for (i=selection_left; i<=selection_right; ++i) {
281
282                         if (!strcasecmp(Params[i].Key, "SUBSCRIBED")) {
283                                 subscribed_rooms_only = 1;
284                         }
285
286                         else if (!strcasecmp(Params[i].Key, "RECURSIVEMATCH")) {
287                                 /* FIXME - do this! */
288                         }
289
290                 }
291
292         }
293
294         /* The folder root appears immediately after the selection options,
295          * or in position 2 if no selection options were specified.
296          */
297         patterns_left = root_pos + 1;
298         patterns_right = root_pos + 1;
299
300         if (Params[patterns_left].Key[0] == '(') {
301                 extended_list_in_use = 1;
302                 paren_nest = 0;
303                 for (i=patterns_left; i<num_parms; ++i) {
304                         for (j=0; &Params[i].Key[j]; ++j) {
305                                 if (Params[i].Key[j] == '(') ++paren_nest;
306                                 if (Params[i].Key[j] == ')') --paren_nest;
307                         }
308                         if (paren_nest == 0) {
309                                 patterns_right = i;     /* found end of patterns */
310                                 i = num_parms + 1;      /* break out of the loop */
311                         }
312                 }
313                 num_patterns = patterns_right - patterns_left + 1;
314                 for (i=0; i<num_patterns; ++i) {
315                         if (i < MAX_PATTERNS) {
316                                 patterns[i] = malloc(512);
317                                 snprintf(patterns[i], 512, "%s%s", Params[root_pos].Key, Params[patterns_left+i].Key);
318                                 if (i == 0) {
319                                         strcpy(patterns[i], &patterns[i][1]);
320                                 }
321                                 if (i == num_patterns-1) {
322                                         patterns[i][strlen(patterns[i])-1] = 0;
323                                 }
324                         }
325                 }
326         }
327         else {
328                 num_patterns = 1;
329                 patterns[0] = malloc(512);
330                 snprintf(patterns[0], 512, "%s%s", 
331                          Params[root_pos].Key, 
332                          Params[patterns_left].Key);
333         }
334
335         /* If the word "RETURN" appears after the folder pattern list, then the client
336          * is specifying return options.
337          */
338         if (num_parms - patterns_right > 2) if (!strcasecmp(Params[patterns_right+1].Key, "RETURN")) {
339                 return_left = patterns_right + 2;
340                 extended_list_in_use = 1;
341                 paren_nest = 0;
342                 for (i=return_left; i<num_parms; ++i) {
343                         for (j=0;   Params[i].Key[j]; ++j) {
344                                 if (Params[i].Key[j] == '(') ++paren_nest;
345                                 if (Params[i].Key[j] == ')') --paren_nest;
346                         }
347
348                         /* Might as well look for these while we're in here... */
349                         if (Params[i].Key[0] == '(') 
350                                 TokenCutLeft(&IMAP->Cmd, 
351                                              &Params[i], 
352                                              1);
353                         if (Params[i].Key[Params[i].len-1] == ')')
354                             TokenCutRight(&IMAP->Cmd, 
355                                           &Params[i], 
356                                           1);
357
358                         CtdlLogPrintf(9, "evaluating <%s>\n", Params[i].Key);
359
360                         if (!strcasecmp(Params[i].Key, "SUBSCRIBED")) {
361                                 return_subscribed = 1;
362                         }
363
364                         else if (!strcasecmp(Params[i].Key, "CHILDREN")) {
365                                 return_children = 1;
366                         }
367
368                         if (paren_nest == 0) {
369                                 return_right = i;       /* found end of patterns */
370                                 i = num_parms + 1;      /* break out of the loop */
371                         }
372                 }
373         }
374
375         /* Now start setting up the data we're going to send to the CtdlForEachRoom() callback.
376          */
377         data_for_callback[0] = (char *) verb;
378         data_for_callback[1] = (char *) subscribed_rooms_only;
379         data_for_callback[2] = (char *) num_patterns;
380         data_for_callback[3] = (char *) patterns;
381         data_for_callback[4] = (char *) return_subscribed;
382         data_for_callback[5] = (char *) return_children;
383
384         /* The non-extended LIST command is required to treat an empty
385          * ("" string) mailbox name argument as a special request to return the
386          * hierarchy delimiter and the root name of the name given in the
387          * reference parameter.
388          */
389         if ( (IsEmptyStr(patterns[0])) && (extended_list_in_use == 0) ) {
390                 cprintf("* %s (\\Noselect) \"/\" \"\"\r\n", verb);
391         }
392
393         /* Non-empty mailbox names, and any form of the extended LIST command,
394          * is handled by this loop.
395          */
396         else {
397                 imap_list_floors(verb, num_patterns, patterns);
398                 CtdlForEachRoom(imap_listroom, data_for_callback);
399         }
400
401         /* 
402          * Free the pattern buffers we allocated above.
403          */
404         for (i=0; i<num_patterns; ++i) {
405                 free(patterns[i]);
406         }
407
408         cprintf("%s OK %s completed\r\n", Params[0].Key, verb);
409 }