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