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].len && (Params[2].Key[0] == '(')) {
240 extended_list_in_use = 1;
243 for (i=2; i<num_parms; ++i) {
245 for (j=0; Params[i].Key[j]; ++j) {
246 if (Params[i].Key[j] == '(') ++paren_nest;
247 if (Params[i].Key[j] == ')') --paren_nest;
250 if (paren_nest == 0) {
251 selection_right = i; /* found end of selection options */
252 root_pos = i+1; /* folder root appears after selection options */
253 i = num_parms + 1; /* break out of the loop */
258 /* If selection options were found, do something with them.
260 if ((selection_left > 0) && (selection_right >= selection_left)) {
262 /* Strip off the outer parentheses */
263 if (Params[selection_left].len && (Params[selection_left].Key[0] == '(')) {
264 TokenCutLeft(&Imap->Cmd,
265 &Params[selection_left],
268 if (Params[selection_right].len && (Params[selection_right].Key[Params[selection_right].len-1] == ')')) {
269 TokenCutRight(&Imap->Cmd,
270 &Params[selection_right],
274 for (i=selection_left; i<=selection_right; ++i) {
276 if (!strcasecmp(Params[i].Key, "SUBSCRIBED")) {
277 ImapFilter.subscribed_rooms_only = 1;
280 else if (!strcasecmp(Params[i].Key, "RECURSIVEMATCH")) {
281 /* FIXME - do this! */
288 /* The folder root appears immediately after the selection options,
289 * or in position 2 if no selection options were specified.
291 ImapFilter.num_patterns_avail = num_parms + 1;
292 ImapFilter.patterns = malloc(ImapFilter.num_patterns_avail * sizeof(StrBuf*));
293 memset(ImapFilter.patterns, 0, ImapFilter.num_patterns_avail * sizeof(StrBuf*));
295 patterns_left = root_pos + 1;
296 patterns_right = root_pos + 1;
298 if (Params[patterns_left].len && (Params[patterns_left].Key[0] == '(')) {
299 extended_list_in_use = 1;
301 for (i=patterns_left; i<num_parms; ++i) {
303 for (j=0; Params[i].Key[j]; ++j) {
304 if (Params[i].Key[j] == '(') ++paren_nest;
305 if (Params[i].Key[j] == ')') --paren_nest;
308 if (paren_nest == 0) {
309 patterns_right = i; /* found end of patterns */
310 i = num_parms + 1; /* break out of the loop */
313 ImapFilter.num_patterns = patterns_right - patterns_left + 1;
314 for (i=0; i<ImapFilter.num_patterns; ++i) {
315 if (i < MAX_PATTERNS) {
316 ImapFilter.patterns[i] = NewStrBufPlain(NULL,
317 Params[root_pos].len +
318 Params[patterns_left+i].len);
320 if (Params[root_pos].len > 1)
321 StrBufAppendBufPlain(ImapFilter.patterns[i],
322 1 + CKEY(Params[root_pos]) - 1, 0);
325 StrBufAppendBufPlain(ImapFilter.patterns[i],
326 CKEY(Params[root_pos]), 0);
328 if (i == ImapFilter.num_patterns-1) {
329 if (Params[patterns_left+i].len > 1)
330 StrBufAppendBufPlain(ImapFilter.patterns[i],
331 CKEY(Params[patterns_left+i]) - 1, 0);
333 else StrBufAppendBufPlain(ImapFilter.patterns[i],
334 CKEY(Params[patterns_left+i]), 0);
341 ImapFilter.num_patterns = 1;
342 ImapFilter.patterns[0] = NewStrBufPlain(NULL,
343 Params[root_pos].len +
344 Params[patterns_left].len);
345 StrBufAppendBufPlain(ImapFilter.patterns[0],
346 CKEY(Params[root_pos]), 0);
347 StrBufAppendBufPlain(ImapFilter.patterns[0],
348 CKEY(Params[patterns_left]), 0);
351 /* If the word "RETURN" appears after the folder pattern list, then the client
352 * is specifying return options.
354 if (num_parms - patterns_right > 2) if (!strcasecmp(Params[patterns_right+1].Key, "RETURN")) {
355 return_left = patterns_right + 2;
356 extended_list_in_use = 1;
358 for (i=return_left; i<num_parms; ++i) {
360 for (j=0; Params[i].Key[j]; ++j) {
361 if (Params[i].Key[j] == '(') ++paren_nest;
362 if (Params[i].Key[j] == ')') --paren_nest;
365 /* Might as well look for these while we're in here... */
366 if (Params[i].Key[0] == '(')
367 TokenCutLeft(&Imap->Cmd,
370 if (Params[i].len && (Params[i].Key[Params[i].len-1] == ')'))
371 TokenCutRight(&Imap->Cmd,
375 syslog(LOG_DEBUG, "evaluating <%s>", Params[i].Key);
377 if (!strcasecmp(Params[i].Key, "SUBSCRIBED")) {
378 ImapFilter.return_subscribed = 1;
381 else if (!strcasecmp(Params[i].Key, "CHILDREN")) {
382 ImapFilter.return_children = 1;
386 if (paren_nest == 0) {
387 i = num_parms + 1; /* break out of the loop */
392 /* Now start setting up the data we're going to send to the CtdlForEachRoom() callback.
395 /* The non-extended LIST command is required to treat an empty
396 * ("" string) mailbox name argument as a special request to return the
397 * hierarchy delimiter and the root name of the name given in the
398 * reference parameter.
400 if ( (StrLength(ImapFilter.patterns[0]) == 0) && (extended_list_in_use == 0) ) {
401 IAPrintf("* %s (\\Noselect) \"/\" \"\"\r\n", ImapFilter.verb);
404 /* Non-empty mailbox names, and any form of the extended LIST command,
405 * is handled by this loop.
408 imap_list_floors(ImapFilter.verb,
409 ImapFilter.num_patterns,
410 ImapFilter.patterns);
411 CtdlForEachRoom(imap_listroom, (char**)&ImapFilter);
415 * Free the pattern buffers we allocated above.
417 for (i=0; i<ImapFilter.num_patterns; ++i) {
418 FreeStrBuf(&ImapFilter.patterns[i]);
420 free(ImapFilter.patterns);
422 IReplyPrintf("OK %s completed", ImapFilter.verb);