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