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