14cd0476ecee1f613b20fe537bf9a7e08e4c258e
[citadel.git] / citadel / modules / imap / imap_list.c
1 /*
2  * Implements the LIST and LSUB commands.
3  *
4  * Copyright (c) 2000-2009 by Art Cancro and others.
5  *
6  *  This program is open source software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "sysdep.h"
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <pwd.h>
28 #include <errno.h>
29 #include <sys/types.h>
30
31 #if TIME_WITH_SYS_TIME
32 # include <sys/time.h>
33 # include <time.h>
34 #else
35 # if HAVE_SYS_TIME_H
36 #  include <sys/time.h>
37 # else
38 #  include <time.h>
39 # endif
40 #endif
41
42 #include <sys/wait.h>
43 #include <ctype.h>
44 #include <string.h>
45 #include <limits.h>
46 #include <libcitadel.h>
47 #include "citadel.h"
48 #include "server.h"
49 #include "sysdep_decls.h"
50 #include "citserver.h"
51 #include "support.h"
52 #include "config.h"
53 #include "user_ops.h"
54 #include "database.h"
55 #include "msgbase.h"
56 #include "internet_addressing.h"
57 #include "serv_imap.h"
58 #include "imap_tools.h"
59 #include "imap_fetch.h"
60 #include "imap_search.h"
61 #include "imap_store.h"
62 #include "imap_acl.h"
63 #include "imap_misc.h"
64 #include "imap_list.h"
65 #include "ctdl_module.h"
66
67
68 typedef struct __ImapRoomListFilter {
69         char verb[16];
70         int subscribed_rooms_only;
71         int return_subscribed;
72         int return_children;
73
74         int num_patterns;
75         int num_patterns_avail;
76         StrBuf **patterns;
77 }ImapRoomListFilter;
78
79 /*
80  * Used by LIST and LSUB to show the floors in the listing
81  */
82 void imap_list_floors(char *verb, int num_patterns, StrBuf **patterns)
83 {
84         int i;
85         struct floor *fl;
86         int j = 0;
87         int match = 0;
88
89         for (i = 0; i < MAXFLOORS; ++i) {
90                 fl = CtdlGetCachedFloor(i);
91                 if (fl->f_flags & F_INUSE) {
92                         match = 0;
93                         for (j=0; j<num_patterns; ++j) {
94                                 if (imap_mailbox_matches_pattern (ChrPtr(patterns[j]), fl->f_name)) {
95                                         match = 1;
96                                 }
97                         }
98                         if (match) {
99                                 IAPrintf("* %s (\\NoSelect \\HasChildren) \"/\" ", verb);
100                                 IPutStr(fl->f_name, (fl->f_name)?strlen(fl->f_name):0);
101                                 IAPuts("\r\n");
102                         }
103                 }
104         }
105 }
106
107
108 /*
109  * Back end for imap_list()
110  *
111  * Implementation note: IMAP "subscribed folder" is equivalent to Citadel "known room"
112  *
113  * The "user data" field is actually an array of pointers; see below for the breakdown
114  *
115  */
116 void imap_listroom(struct ctdlroom *qrbuf, void *data)
117 {
118 #define SUBSCRIBED_STR "\\Subscribed"
119 #define HASCHILD_STR "\\HasChildren"
120         char MailboxName[SIZ];
121         char return_options[256];
122         int ra;
123         int yes_output_this_room;
124         ImapRoomListFilter *ImapFilter;
125         int i = 0;
126         int match = 0;
127         int ROLen;
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         *return_options = '\0';
135         ROLen = 0;
136         CtdlRoomAccess(qrbuf, &CC->user, &ra, NULL);
137
138         if (ImapFilter->return_subscribed) {
139                 if (ra & UA_KNOWN) {
140                         memcpy(return_options, HKEY(SUBSCRIBED_STR) + 1);
141                         ROLen += sizeof(SUBSCRIBED_STR) - 1;
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 (ImapFilter->return_children) {
152                 if (!IsEmptyStr(return_options)) {
153                         memcpy(return_options + ROLen, HKEY(" "));
154                         ROLen ++;
155                 }
156                 memcpy(return_options + ROLen, HKEY(SUBSCRIBED_STR) + 1);
157         }
158
159         if (ImapFilter->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                 long len;
172                 len = imap_mailboxname(MailboxName, sizeof MailboxName, qrbuf);
173                 match = 0;
174                 for (i=0; i<ImapFilter->num_patterns; ++i) {
175                         if (imap_mailbox_matches_pattern(ChrPtr(ImapFilter->patterns[i]), MailboxName)) {
176                                 match = 1;
177                         }
178                 }
179                 if (match) {
180                         IAPrintf("* %s (%s) \"/\" ", ImapFilter->verb, return_options);
181                         IPutStr(MailboxName, len);
182                         IAPuts("\r\n");
183                 }
184         }
185 }
186
187
188 /*
189  * Implements the LIST and LSUB commands
190  */
191 void imap_list(int num_parms, ConstStr *Params)
192 {
193         struct CitContext *CCC = CC;
194         citimap *Imap = CCCIMAP;
195         int i, j, paren_nest;
196         ImapRoomListFilter ImapFilter;
197         int selection_left = (-1);
198         int selection_right = (-1);
199         int return_left = (-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                 IReply("BAD arguments invalid");
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                         IMAP_syslog(LOG_DEBUG, "evaluating <%s>", 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                                 i = num_parms + 1;      /* break out of the loop */
393                         }
394                 }
395         }
396
397         /* Now start setting up the data we're going to send to the CtdlForEachRoom() callback.
398          */
399         
400         /* The non-extended LIST command is required to treat an empty
401          * ("" string) mailbox name argument as a special request to return the
402          * hierarchy delimiter and the root name of the name given in the
403          * reference parameter.
404          */
405         if ( (StrLength(ImapFilter.patterns[0]) == 0) && (extended_list_in_use == 0) ) {
406                 IAPrintf("* %s (\\Noselect) \"/\" \"\"\r\n", ImapFilter.verb);
407         }
408
409         /* Non-empty mailbox names, and any form of the extended LIST command,
410          * is handled by this loop.
411          */
412         else {
413                 imap_list_floors(ImapFilter.verb, 
414                                  ImapFilter.num_patterns, 
415                                  ImapFilter.patterns);
416                 CtdlForEachRoom(imap_listroom, (char**)&ImapFilter);
417         }
418
419         /* 
420          * Free the pattern buffers we allocated above.
421          */
422         for (i=0; i<ImapFilter.num_patterns; ++i) {
423                 FreeStrBuf(&ImapFilter.patterns[i]);
424         }
425         free(ImapFilter.patterns);
426
427         IReplyPrintf("OK %s completed", ImapFilter.verb);
428 }