2 * Implements the LIST and LSUB commands.
4 * Copyright (c) 2000-2017 by Art Cancro and others.
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.
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.
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
21 #include "../../sysdep.h"
29 #include <sys/types.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"
52 #include "imap_misc.h"
53 #include "imap_list.h"
54 #include "../../ctdl_module.h"
57 typedef struct __ImapRoomListFilter {
59 int subscribed_rooms_only;
60 int return_subscribed;
64 int num_patterns_avail;
69 * Used by LIST and LSUB to show the floors in the listing
71 void imap_list_floors(char *verb, int num_patterns, StrBuf **patterns)
78 for (i = 0; i < MAXFLOORS; ++i) {
79 fl = CtdlGetCachedFloor(i);
80 if (fl->f_flags & F_INUSE) {
82 for (j=0; j<num_patterns; ++j) {
83 if (imap_mailbox_matches_pattern (ChrPtr(patterns[j]), fl->f_name)) {
88 IAPrintf("* %s (\\NoSelect \\HasChildren) \"/\" ", verb);
89 IPutStr(fl->f_name, (fl->f_name)?strlen(fl->f_name):0);
98 * Back end for imap_list()
100 * Implementation note: IMAP "subscribed folder" is equivalent to Citadel "known room"
102 * The "user data" field is actually an array of pointers; see below for the breakdown
105 void imap_listroom(struct ctdlroom *qrbuf, void *data)
107 #define SUBSCRIBED_STR "\\Subscribed"
108 #define HASCHILD_STR "\\HasChildren"
109 char MailboxName[SIZ];
110 char return_options[256];
112 int yes_output_this_room;
113 ImapRoomListFilter *ImapFilter;
118 /* Here's how we break down the array of pointers passed to us */
119 ImapFilter = (ImapRoomListFilter*)data;
121 /* Only list rooms to which the user has access!! */
122 yes_output_this_room = 0;
123 *return_options = '\0';
125 CtdlRoomAccess(qrbuf, &CC->user, &ra, NULL);
127 if (ImapFilter->return_subscribed) {
129 memcpy(return_options, HKEY(SUBSCRIBED_STR) + 1);
130 ROLen += sizeof(SUBSCRIBED_STR) - 1;
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.
140 if (ImapFilter->return_children) {
141 if (!IsEmptyStr(return_options)) {
142 memcpy(return_options + ROLen, HKEY(" "));
145 memcpy(return_options + ROLen, HKEY(SUBSCRIBED_STR) + 1);
148 if (ImapFilter->subscribed_rooms_only) {
150 yes_output_this_room = 1;
154 if ((ra & UA_KNOWN) || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
155 yes_output_this_room = 1;
159 if (yes_output_this_room) {
161 len = imap_mailboxname(MailboxName, sizeof MailboxName, qrbuf);
163 for (i=0; i<ImapFilter->num_patterns; ++i) {
164 if (imap_mailbox_matches_pattern(ChrPtr(ImapFilter->patterns[i]), MailboxName)) {
169 IAPrintf("* %s (%s) \"/\" ", ImapFilter->verb, return_options);
170 IPutStr(MailboxName, len);
178 * Implements the LIST and LSUB commands
180 void imap_list(int num_parms, ConstStr *Params)
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);
190 int patterns_left = 3;
191 int patterns_right = 3;
192 int extended_list_in_use = 0;
195 IReply("BAD arguments invalid");
199 ImapFilter.num_patterns = 1;
200 ImapFilter.return_subscribed = 0;
201 ImapFilter.return_children = 0;
202 ImapFilter.subscribed_rooms_only = 0;
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
208 safestrncpy(ImapFilter.verb, Params[1].Key, sizeof ImapFilter.verb);
210 for (i=0; i<j; ++i) {
211 ImapFilter.verb[i] = toupper(ImapFilter.verb[i]);
214 if (!strcasecmp(ImapFilter.verb, "LSUB")) {
215 ImapFilter.subscribed_rooms_only = 1;
219 * Partial implementation of LIST-EXTENDED (which will not get used because
220 * we don't advertise it in our capabilities string). Several requirements:
222 * Extraction of selection options:
223 * SUBSCRIBED option: done
224 * RECURSIVEMATCH option: not done yet
225 * REMOTE: safe to silently ignore
227 * Extraction of return options:
228 * SUBSCRIBED option: done
229 * CHILDREN option: done, but needs a non-ugly rewrite
231 * Multiple match patterns: done
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.
239 if (Params[2].Key[0] == '(') {
240 extended_list_in_use = 1;
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;
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 */
256 /* If selection options were found, do something with them.
258 if ((selection_left > 0) && (selection_right >= selection_left)) {
260 /* Strip off the outer parentheses */
261 if (Params[selection_left].Key[0] == '(') {
262 TokenCutLeft(&Imap->Cmd,
263 &Params[selection_left],
266 if (Params[selection_right].Key[Params[selection_right].len-1] == ')') {
267 TokenCutRight(&Imap->Cmd,
268 &Params[selection_right],
272 for (i=selection_left; i<=selection_right; ++i) {
274 if (!strcasecmp(Params[i].Key, "SUBSCRIBED")) {
275 ImapFilter.subscribed_rooms_only = 1;
278 else if (!strcasecmp(Params[i].Key, "RECURSIVEMATCH")) {
279 /* FIXME - do this! */
286 /* The folder root appears immediately after the selection options,
287 * or in position 2 if no selection options were specified.
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*));
293 patterns_left = root_pos + 1;
294 patterns_right = root_pos + 1;
296 if (Params[patterns_left].Key[0] == '(') {
297 extended_list_in_use = 1;
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;
304 if (paren_nest == 0) {
305 patterns_right = i; /* found end of patterns */
306 i = num_parms + 1; /* break out of the loop */
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);
316 if (Params[root_pos].len > 1)
317 StrBufAppendBufPlain(ImapFilter.patterns[i],
318 1 + CKEY(Params[root_pos]) - 1, 0);
321 StrBufAppendBufPlain(ImapFilter.patterns[i],
322 CKEY(Params[root_pos]), 0);
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);
329 else StrBufAppendBufPlain(ImapFilter.patterns[i],
330 CKEY(Params[patterns_left+i]), 0);
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);
347 /* If the word "RETURN" appears after the folder pattern list, then the client
348 * is specifying return options.
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;
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;
360 /* Might as well look for these while we're in here... */
361 if (Params[i].Key[0] == '(')
362 TokenCutLeft(&Imap->Cmd,
365 if (Params[i].Key[Params[i].len-1] == ')')
366 TokenCutRight(&Imap->Cmd,
370 syslog(LOG_DEBUG, "evaluating <%s>", Params[i].Key);
372 if (!strcasecmp(Params[i].Key, "SUBSCRIBED")) {
373 ImapFilter.return_subscribed = 1;
376 else if (!strcasecmp(Params[i].Key, "CHILDREN")) {
377 ImapFilter.return_children = 1;
380 if (paren_nest == 0) {
381 i = num_parms + 1; /* break out of the loop */
386 /* Now start setting up the data we're going to send to the CtdlForEachRoom() callback.
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.
394 if ( (StrLength(ImapFilter.patterns[0]) == 0) && (extended_list_in_use == 0) ) {
395 IAPrintf("* %s (\\Noselect) \"/\" \"\"\r\n", ImapFilter.verb);
398 /* Non-empty mailbox names, and any form of the extended LIST command,
399 * is handled by this loop.
402 imap_list_floors(ImapFilter.verb,
403 ImapFilter.num_patterns,
404 ImapFilter.patterns);
405 CtdlForEachRoom(imap_listroom, (char**)&ImapFilter);
409 * Free the pattern buffers we allocated above.
411 for (i=0; i<ImapFilter.num_patterns; ++i) {
412 FreeStrBuf(&ImapFilter.patterns[i]);
414 free(ImapFilter.patterns);
416 IReplyPrintf("OK %s completed", ImapFilter.verb);