2 * Implements the LIST and LSUB commands.
4 * Copyright (c) 2000-2009 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
29 #include <sys/types.h>
31 #if TIME_WITH_SYS_TIME
32 # include <sys/time.h>
36 # include <sys/time.h>
46 #include <libcitadel.h>
49 #include "sysdep_decls.h"
50 #include "citserver.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"
63 #include "imap_misc.h"
64 #include "imap_list.h"
65 #include "ctdl_module.h"
68 typedef struct __ImapRoomListFilter {
70 int subscribed_rooms_only;
71 int return_subscribed;
75 int num_patterns_avail;
80 * Used by LIST and LSUB to show the floors in the listing
82 void imap_list_floors(char *verb, int num_patterns, StrBuf **patterns)
89 for (i = 0; i < MAXFLOORS; ++i) {
90 fl = CtdlGetCachedFloor(i);
91 if (fl->f_flags & F_INUSE) {
93 for (j=0; j<num_patterns; ++j) {
94 if (imap_mailbox_matches_pattern (ChrPtr(patterns[j]), fl->f_name)) {
99 IAPrintf("* %s (\\NoSelect \\HasChildren) \"/\" ", verb);
100 plain_imap_strout(fl->f_name);
109 * Back end for imap_list()
111 * Implementation note: IMAP "subscribed folder" is equivalent to Citadel "known room"
113 * The "user data" field is actually an array of pointers; see below for the breakdown
116 void imap_listroom(struct ctdlroom *qrbuf, void *data)
118 #define SUBSCRIBED_STR "\\Subscribed"
119 #define HASCHILD_STR "\\HasChildren"
120 char MailboxName[SIZ];
121 char return_options[256];
123 int yes_output_this_room;
124 ImapRoomListFilter *ImapFilter;
129 /* Here's how we break down the array of pointers passed to us */
130 ImapFilter = (ImapRoomListFilter*)data;
132 /* Only list rooms to which the user has access!! */
133 yes_output_this_room = 0;
134 *return_options = '\0';
136 CtdlRoomAccess(qrbuf, &CC->user, &ra, NULL);
138 if (ImapFilter->return_subscribed) {
140 memcpy(return_options, HKEY(SUBSCRIBED_STR) + 1);
141 ROLen += sizeof(SUBSCRIBED_STR) - 1;
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.
151 if (ImapFilter->return_children) {
152 if (!IsEmptyStr(return_options)) {
153 memcpy(return_options + ROLen, HKEY(" "));
156 memcpy(return_options + ROLen, HKEY(SUBSCRIBED_STR) + 1);
159 if (ImapFilter->subscribed_rooms_only) {
161 yes_output_this_room = 1;
165 if ((ra & UA_KNOWN) || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
166 yes_output_this_room = 1;
170 if (yes_output_this_room) {
171 imap_mailboxname(MailboxName, sizeof MailboxName, qrbuf);
173 for (i=0; i<ImapFilter->num_patterns; ++i) {
174 if (imap_mailbox_matches_pattern(ChrPtr(ImapFilter->patterns[i]), MailboxName)) {
179 IAPrintf("* %s (%s) \"/\" ", ImapFilter->verb, return_options);
180 plain_imap_strout(MailboxName);
188 * Implements the LIST and LSUB commands
190 void imap_list(int num_parms, ConstStr *Params)
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);
199 int patterns_left = 3;
200 int patterns_right = 3;
201 int extended_list_in_use = 0;
204 IReply("BAD arguments invalid");
208 ImapFilter.num_patterns = 1;
209 ImapFilter.return_subscribed = 0;
210 ImapFilter.return_children = 0;
211 ImapFilter.subscribed_rooms_only = 0;
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
217 safestrncpy(ImapFilter.verb, Params[1].Key, sizeof ImapFilter.verb);
219 for (i=0; i<j; ++i) {
220 ImapFilter.verb[i] = toupper(ImapFilter.verb[i]);
223 if (!strcasecmp(ImapFilter.verb, "LSUB")) {
224 ImapFilter.subscribed_rooms_only = 1;
228 * Partial implementation of LIST-EXTENDED (which will not get used because
229 * we don't advertise it in our capabilities string). Several requirements:
231 * Extraction of selection options:
232 * SUBSCRIBED option: done
233 * RECURSIVEMATCH option: not done yet
234 * REMOTE: safe to silently ignore
236 * Extraction of return options:
237 * SUBSCRIBED option: done
238 * CHILDREN option: done, but needs a non-ugly rewrite
240 * Multiple match patterns: done
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.
248 if (Params[2].Key[0] == '(') {
249 extended_list_in_use = 1;
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;
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 */
265 /* If selection options were found, do something with them.
267 if ((selection_left > 0) && (selection_right >= selection_left)) {
269 /* Strip off the outer parentheses */
270 if (Params[selection_left].Key[0] == '(') {
271 TokenCutLeft(&Imap->Cmd,
272 &Params[selection_left],
275 if (Params[selection_right].Key[Params[selection_right].len-1] == ')') {
276 TokenCutRight(&Imap->Cmd,
277 &Params[selection_right],
281 for (i=selection_left; i<=selection_right; ++i) {
283 if (!strcasecmp(Params[i].Key, "SUBSCRIBED")) {
284 ImapFilter.subscribed_rooms_only = 1;
287 else if (!strcasecmp(Params[i].Key, "RECURSIVEMATCH")) {
288 /* FIXME - do this! */
295 /* The folder root appears immediately after the selection options,
296 * or in position 2 if no selection options were specified.
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*));
302 patterns_left = root_pos + 1;
303 patterns_right = root_pos + 1;
305 if (Params[patterns_left].Key[0] == '(') {
306 extended_list_in_use = 1;
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;
313 if (paren_nest == 0) {
314 patterns_right = i; /* found end of patterns */
315 i = num_parms + 1; /* break out of the loop */
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);
325 if (Params[root_pos].len > 1)
326 StrBufAppendBufPlain(ImapFilter.patterns[i],
327 1 + CKEY(Params[root_pos]) - 1, 0);
330 StrBufAppendBufPlain(ImapFilter.patterns[i],
331 CKEY(Params[root_pos]), 0);
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);
338 else StrBufAppendBufPlain(ImapFilter.patterns[i],
339 CKEY(Params[patterns_left+i]), 0);
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);
356 /* If the word "RETURN" appears after the folder pattern list, then the client
357 * is specifying return options.
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;
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;
369 /* Might as well look for these while we're in here... */
370 if (Params[i].Key[0] == '(')
371 TokenCutLeft(&Imap->Cmd,
374 if (Params[i].Key[Params[i].len-1] == ')')
375 TokenCutRight(&Imap->Cmd,
379 syslog(LOG_DEBUG, "evaluating <%s>", Params[i].Key);
381 if (!strcasecmp(Params[i].Key, "SUBSCRIBED")) {
382 ImapFilter.return_subscribed = 1;
385 else if (!strcasecmp(Params[i].Key, "CHILDREN")) {
386 ImapFilter.return_children = 1;
389 if (paren_nest == 0) {
390 i = num_parms + 1; /* break out of the loop */
395 /* Now start setting up the data we're going to send to the CtdlForEachRoom() callback.
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.
403 if ( (StrLength(ImapFilter.patterns[0]) == 0) && (extended_list_in_use == 0) ) {
404 IAPrintf("* %s (\\Noselect) \"/\" \"\"\r\n", ImapFilter.verb);
407 /* Non-empty mailbox names, and any form of the extended LIST command,
408 * is handled by this loop.
411 imap_list_floors(ImapFilter.verb,
412 ImapFilter.num_patterns,
413 ImapFilter.patterns);
414 CtdlForEachRoom(imap_listroom, (char**)&ImapFilter);
418 * Free the pattern buffers we allocated above.
420 for (i=0; i<ImapFilter.num_patterns; ++i) {
421 FreeStrBuf(&ImapFilter.patterns[i]);
422 free(ImapFilter.patterns);
426 IReplyPrintf("OK %s completed", ImapFilter.verb);