2 * Implements the LIST and LSUB commands.
4 * Copyright (c) 2000-2009 by Art Cancro and others.
6 * This program is free 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);
198 int return_right = (-1);
200 int patterns_left = 3;
201 int patterns_right = 3;
202 int extended_list_in_use = 0;
205 IReply("BAD arguments invalid");
209 ImapFilter.num_patterns = 1;
210 ImapFilter.return_subscribed = 0;
211 ImapFilter.return_children = 0;
212 ImapFilter.subscribed_rooms_only = 0;
215 /* parms[1] is the IMAP verb being used (e.g. LIST or LSUB)
216 * This tells us how to behave, and what verb to return back to the caller
218 safestrncpy(ImapFilter.verb, Params[1].Key, sizeof ImapFilter.verb);
220 for (i=0; i<j; ++i) {
221 ImapFilter.verb[i] = toupper(ImapFilter.verb[i]);
224 if (!strcasecmp(ImapFilter.verb, "LSUB")) {
225 ImapFilter.subscribed_rooms_only = 1;
229 * Partial implementation of LIST-EXTENDED (which will not get used because
230 * we don't advertise it in our capabilities string). Several requirements:
232 * Extraction of selection options:
233 * SUBSCRIBED option: done
234 * RECURSIVEMATCH option: not done yet
235 * REMOTE: safe to silently ignore
237 * Extraction of return options:
238 * SUBSCRIBED option: done
239 * CHILDREN option: done, but needs a non-ugly rewrite
241 * Multiple match patterns: done
245 * If parameter 2 begins with a '(' character, the client is specifying
246 * selection options. Extract their exact position, and then modify our
247 * expectation of where the root folder will be specified.
249 if (Params[2].Key[0] == '(') {
250 extended_list_in_use = 1;
253 for (i=2; i<num_parms; ++i) {
254 for (j=0; Params[i].Key[j]; ++j) {
255 if (Params[i].Key[j] == '(') ++paren_nest;
256 if (Params[i].Key[j] == ')') --paren_nest;
258 if (paren_nest == 0) {
259 selection_right = i; /* found end of selection options */
260 root_pos = i+1; /* folder root appears after selection options */
261 i = num_parms + 1; /* break out of the loop */
266 /* If selection options were found, do something with them.
268 if ((selection_left > 0) && (selection_right >= selection_left)) {
270 /* Strip off the outer parentheses */
271 if (Params[selection_left].Key[0] == '(') {
272 TokenCutLeft(&Imap->Cmd,
273 &Params[selection_left],
276 if (Params[selection_right].Key[Params[selection_right].len-1] == ')') {
277 TokenCutRight(&Imap->Cmd,
278 &Params[selection_right],
282 for (i=selection_left; i<=selection_right; ++i) {
284 if (!strcasecmp(Params[i].Key, "SUBSCRIBED")) {
285 ImapFilter.subscribed_rooms_only = 1;
288 else if (!strcasecmp(Params[i].Key, "RECURSIVEMATCH")) {
289 /* FIXME - do this! */
296 /* The folder root appears immediately after the selection options,
297 * or in position 2 if no selection options were specified.
299 ImapFilter.num_patterns_avail = num_parms + 1;
300 ImapFilter.patterns = malloc(ImapFilter.num_patterns_avail * sizeof(StrBuf*));
301 memset(ImapFilter.patterns, 0, ImapFilter.num_patterns_avail * sizeof(StrBuf*));
303 patterns_left = root_pos + 1;
304 patterns_right = root_pos + 1;
306 if (Params[patterns_left].Key[0] == '(') {
307 extended_list_in_use = 1;
309 for (i=patterns_left; i<num_parms; ++i) {
310 for (j=0; &Params[i].Key[j]; ++j) {
311 if (Params[i].Key[j] == '(') ++paren_nest;
312 if (Params[i].Key[j] == ')') --paren_nest;
314 if (paren_nest == 0) {
315 patterns_right = i; /* found end of patterns */
316 i = num_parms + 1; /* break out of the loop */
319 ImapFilter.num_patterns = patterns_right - patterns_left + 1;
320 for (i=0; i<ImapFilter.num_patterns; ++i) {
321 if (i < MAX_PATTERNS) {
322 ImapFilter.patterns[i] = NewStrBufPlain(NULL,
323 Params[root_pos].len +
324 Params[patterns_left+i].len);
326 if (Params[root_pos].len > 1)
327 StrBufAppendBufPlain(ImapFilter.patterns[i],
328 1 + CKEY(Params[root_pos]) - 1, 0);
331 StrBufAppendBufPlain(ImapFilter.patterns[i],
332 CKEY(Params[root_pos]), 0);
334 if (i == ImapFilter.num_patterns-1) {
335 if (Params[patterns_left+i].len > 1)
336 StrBufAppendBufPlain(ImapFilter.patterns[i],
337 CKEY(Params[patterns_left+i]) - 1, 0);
339 else StrBufAppendBufPlain(ImapFilter.patterns[i],
340 CKEY(Params[patterns_left+i]), 0);
347 ImapFilter.num_patterns = 1;
348 ImapFilter.patterns[0] = NewStrBufPlain(NULL,
349 Params[root_pos].len +
350 Params[patterns_left].len);
351 StrBufAppendBufPlain(ImapFilter.patterns[0],
352 CKEY(Params[root_pos]), 0);
353 StrBufAppendBufPlain(ImapFilter.patterns[0],
354 CKEY(Params[patterns_left]), 0);
357 /* If the word "RETURN" appears after the folder pattern list, then the client
358 * is specifying return options.
360 if (num_parms - patterns_right > 2) if (!strcasecmp(Params[patterns_right+1].Key, "RETURN")) {
361 return_left = patterns_right + 2;
362 extended_list_in_use = 1;
364 for (i=return_left; i<num_parms; ++i) {
365 for (j=0; Params[i].Key[j]; ++j) {
366 if (Params[i].Key[j] == '(') ++paren_nest;
367 if (Params[i].Key[j] == ')') --paren_nest;
370 /* Might as well look for these while we're in here... */
371 if (Params[i].Key[0] == '(')
372 TokenCutLeft(&Imap->Cmd,
375 if (Params[i].Key[Params[i].len-1] == ')')
376 TokenCutRight(&Imap->Cmd,
380 syslog(LOG_DEBUG, "evaluating <%s>\n", Params[i].Key);
382 if (!strcasecmp(Params[i].Key, "SUBSCRIBED")) {
383 ImapFilter.return_subscribed = 1;
386 else if (!strcasecmp(Params[i].Key, "CHILDREN")) {
387 ImapFilter.return_children = 1;
390 if (paren_nest == 0) {
391 return_right = i; /* found end of patterns */
392 i = num_parms + 1; /* break out of the loop */
397 /* Now start setting up the data we're going to send to the CtdlForEachRoom() callback.
400 /* The non-extended LIST command is required to treat an empty
401 * ("" string) mailbox name argument as a special request to return the
402 * hierarchy delimiter and the root name of the name given in the
403 * reference parameter.
405 if ( (StrLength(ImapFilter.patterns[0]) == 0) && (extended_list_in_use == 0) ) {
406 IAPrintf("* %s (\\Noselect) \"/\" \"\"\r\n", ImapFilter.verb);
409 /* Non-empty mailbox names, and any form of the extended LIST command,
410 * is handled by this loop.
413 imap_list_floors(ImapFilter.verb,
414 ImapFilter.num_patterns,
415 ImapFilter.patterns);
416 CtdlForEachRoom(imap_listroom, (char**)&ImapFilter);
420 * Free the pattern buffers we allocated above.
422 for (i=0; i<ImapFilter.num_patterns; ++i) {
423 FreeStrBuf(&ImapFilter.patterns[i]);
424 free(ImapFilter.patterns);
428 IReplyPrintf("OK %s completed", ImapFilter.verb);