5640ec5db854d8e09af2a545a0882f45388711bd
[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                                 plain_imap_strout(fl->f_name);
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                 imap_mailboxname(MailboxName, sizeof MailboxName, qrbuf);
172                 match = 0;
173                 for (i=0; i<ImapFilter->num_patterns; ++i) {
174                         if (imap_mailbox_matches_pattern(ChrPtr(ImapFilter->patterns[i]), MailboxName)) {
175                                 match = 1;
176                         }
177                 }
178                 if (match) {
179                         IAPrintf("* %s (%s) \"/\" ", ImapFilter->verb, return_options);
180                         plain_imap_strout(MailboxName);
181                         IAPuts("\r\n");
182                 }
183         }
184 }
185
186
187 /*
188  * Implements the LIST and LSUB commands
189  */
190 void imap_list(int num_parms, ConstStr *Params)
191 {
192         struct CitContext *CCC = CC;
193         citimap *Imap = CCCIMAP;
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 root_pos = 2;
200         int patterns_left = 3;
201         int patterns_right = 3;
202         int extended_list_in_use = 0;
203
204         if (num_parms < 4) {
205                 IReply("BAD arguments invalid");
206                 return;
207         }
208
209         ImapFilter.num_patterns = 1;
210         ImapFilter.return_subscribed = 0;
211         ImapFilter.return_children = 0;
212         ImapFilter.subscribed_rooms_only = 0;
213         
214
215         /* parms[1] is the IMAP verb being used (e.g. LIST or LSUB)
216          * This tells us how to behave, and what verb to return back to the caller
217          */
218         safestrncpy(ImapFilter.verb, Params[1].Key, sizeof ImapFilter.verb);
219         j = Params[1].len;
220         for (i=0; i<j; ++i) {
221                 ImapFilter.verb[i] = toupper(ImapFilter.verb[i]);
222         }
223
224         if (!strcasecmp(ImapFilter.verb, "LSUB")) {
225                 ImapFilter.subscribed_rooms_only = 1;
226         }
227
228         /*
229          * Partial implementation of LIST-EXTENDED (which will not get used because
230          * we don't advertise it in our capabilities string).  Several requirements:
231          *
232          * Extraction of selection options:
233          *      SUBSCRIBED option: done
234          *      RECURSIVEMATCH option: not done yet
235          *      REMOTE: safe to silently ignore
236          *
237          * Extraction of return options:
238          *      SUBSCRIBED option: done
239          *      CHILDREN option: done, but needs a non-ugly rewrite
240          *
241          * Multiple match patterns: done
242          */
243
244         /*
245          * If parameter 2 begins with a '(' character, the client is specifying
246          * selection options.  Extract their exact position, and then modify our
247          * expectation of where the root folder will be specified.
248          */
249         if (Params[2].Key[0] == '(') {
250                 extended_list_in_use = 1;
251                 selection_left = 2;
252                 paren_nest = 0;
253                 for (i=2; i<num_parms; ++i) {
254                         for (j=0; Params[i].Key[j]; ++j) {
255                                 if (Params[i].Key[j] == '(') ++paren_nest;
256                                 if (Params[i].Key[j] == ')') --paren_nest;
257                         }
258                         if (paren_nest == 0) {
259                                 selection_right = i;    /* found end of selection options */
260                                 root_pos = i+1;         /* folder root appears after selection options */
261                                 i = num_parms + 1;      /* break out of the loop */
262                         }
263                 }
264         }
265
266         /* If selection options were found, do something with them.
267          */
268         if ((selection_left > 0) && (selection_right >= selection_left)) {
269
270                 /* Strip off the outer parentheses */
271                 if (Params[selection_left].Key[0] == '(') {
272                         TokenCutLeft(&Imap->Cmd, 
273                                      &Params[selection_left], 
274                                      1);
275                 }
276                 if (Params[selection_right].Key[Params[selection_right].len-1] == ')') {
277                         TokenCutRight(&Imap->Cmd, 
278                                       &Params[selection_right], 
279                                       1);
280                 }
281
282                 for (i=selection_left; i<=selection_right; ++i) {
283
284                         if (!strcasecmp(Params[i].Key, "SUBSCRIBED")) {
285                                 ImapFilter.subscribed_rooms_only = 1;
286                         }
287
288                         else if (!strcasecmp(Params[i].Key, "RECURSIVEMATCH")) {
289                                 /* FIXME - do this! */
290                         }
291
292                 }
293
294         }
295
296         /* The folder root appears immediately after the selection options,
297          * or in position 2 if no selection options were specified.
298          */
299         ImapFilter.num_patterns_avail = num_parms + 1;
300         ImapFilter.patterns = malloc(ImapFilter.num_patterns_avail * sizeof(StrBuf*));
301         memset(ImapFilter.patterns, 0, ImapFilter.num_patterns_avail * sizeof(StrBuf*));
302
303         patterns_left = root_pos + 1;
304         patterns_right = root_pos + 1;
305
306         if (Params[patterns_left].Key[0] == '(') {
307                 extended_list_in_use = 1;
308                 paren_nest = 0;
309                 for (i=patterns_left; i<num_parms; ++i) {
310                         for (j=0; &Params[i].Key[j]; ++j) {
311                                 if (Params[i].Key[j] == '(') ++paren_nest;
312                                 if (Params[i].Key[j] == ')') --paren_nest;
313                         }
314                         if (paren_nest == 0) {
315                                 patterns_right = i;     /* found end of patterns */
316                                 i = num_parms + 1;      /* break out of the loop */
317                         }
318                 }
319                 ImapFilter.num_patterns = patterns_right - patterns_left + 1;
320                 for (i=0; i<ImapFilter.num_patterns; ++i) {
321                         if (i < MAX_PATTERNS) {
322                                 ImapFilter.patterns[i] = NewStrBufPlain(NULL, 
323                                                                         Params[root_pos].len + 
324                                                                         Params[patterns_left+i].len);
325                                 if (i == 0) {
326                                         if (Params[root_pos].len > 1)
327                                                 StrBufAppendBufPlain(ImapFilter.patterns[i], 
328                                                                      1 + CKEY(Params[root_pos]) - 1, 0);
329                                 }
330                                 else
331                                         StrBufAppendBufPlain(ImapFilter.patterns[i], 
332                                                              CKEY(Params[root_pos]), 0);
333
334                                 if (i == ImapFilter.num_patterns-1) {
335                                         if (Params[patterns_left+i].len > 1)
336                                                 StrBufAppendBufPlain(ImapFilter.patterns[i], 
337                                                                      CKEY(Params[patterns_left+i]) - 1, 0);
338                                 }
339                                 else StrBufAppendBufPlain(ImapFilter.patterns[i], 
340                                                           CKEY(Params[patterns_left+i]), 0);
341
342                         }
343
344                 }
345         }
346         else {
347                 ImapFilter.num_patterns = 1;
348                 ImapFilter.patterns[0] = NewStrBufPlain(NULL, 
349                                                         Params[root_pos].len + 
350                                                         Params[patterns_left].len);
351                 StrBufAppendBufPlain(ImapFilter.patterns[0], 
352                                      CKEY(Params[root_pos]), 0);
353                 StrBufAppendBufPlain(ImapFilter.patterns[0], 
354                                      CKEY(Params[patterns_left]), 0);
355         }
356
357         /* If the word "RETURN" appears after the folder pattern list, then the client
358          * is specifying return options.
359          */
360         if (num_parms - patterns_right > 2) if (!strcasecmp(Params[patterns_right+1].Key, "RETURN")) {
361                 return_left = patterns_right + 2;
362                 extended_list_in_use = 1;
363                 paren_nest = 0;
364                 for (i=return_left; i<num_parms; ++i) {
365                         for (j=0;   Params[i].Key[j]; ++j) {
366                                 if (Params[i].Key[j] == '(') ++paren_nest;
367                                 if (Params[i].Key[j] == ')') --paren_nest;
368                         }
369
370                         /* Might as well look for these while we're in here... */
371                         if (Params[i].Key[0] == '(') 
372                                 TokenCutLeft(&Imap->Cmd, 
373                                              &Params[i], 
374                                              1);
375                         if (Params[i].Key[Params[i].len-1] == ')')
376                             TokenCutRight(&Imap->Cmd, 
377                                           &Params[i], 
378                                           1);
379
380                         IMAP_syslog(LOG_DEBUG, "evaluating <%s>", Params[i].Key);
381
382                         if (!strcasecmp(Params[i].Key, "SUBSCRIBED")) {
383                                 ImapFilter.return_subscribed = 1;
384                         }
385
386                         else if (!strcasecmp(Params[i].Key, "CHILDREN")) {
387                                 ImapFilter.return_children = 1;
388                         }
389
390                         if (paren_nest == 0) {
391                                 i = num_parms + 1;      /* break out of the loop */
392                         }
393                 }
394         }
395
396         /* Now start setting up the data we're going to send to the CtdlForEachRoom() callback.
397          */
398         
399         /* The non-extended LIST command is required to treat an empty
400          * ("" string) mailbox name argument as a special request to return the
401          * hierarchy delimiter and the root name of the name given in the
402          * reference parameter.
403          */
404         if ( (StrLength(ImapFilter.patterns[0]) == 0) && (extended_list_in_use == 0) ) {
405                 IAPrintf("* %s (\\Noselect) \"/\" \"\"\r\n", ImapFilter.verb);
406         }
407
408         /* Non-empty mailbox names, and any form of the extended LIST command,
409          * is handled by this loop.
410          */
411         else {
412                 imap_list_floors(ImapFilter.verb, 
413                                  ImapFilter.num_patterns, 
414                                  ImapFilter.patterns);
415                 CtdlForEachRoom(imap_listroom, (char**)&ImapFilter);
416         }
417
418         /* 
419          * Free the pattern buffers we allocated above.
420          */
421         for (i=0; i<ImapFilter.num_patterns; ++i) {
422                 FreeStrBuf(&ImapFilter.patterns[i]);
423         }
424         free(ImapFilter.patterns);
425
426         IReplyPrintf("OK %s completed", ImapFilter.verb);
427 }