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