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