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