4 * Implements the LIST and LSUB commands.
6 * Copyright (c) 2000-2009 by Art Cancro and others.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <sys/types.h>
33 #if TIME_WITH_SYS_TIME
34 # include <sys/time.h>
38 # include <sys/time.h>
48 #include <libcitadel.h>
51 #include "sysdep_decls.h"
52 #include "citserver.h"
59 #include "internet_addressing.h"
60 #include "imap_tools.h"
61 #include "serv_imap.h"
62 #include "imap_fetch.h"
63 #include "imap_search.h"
64 #include "imap_store.h"
66 #include "imap_misc.h"
67 #include "imap_list.h"
68 #include "ctdl_module.h"
72 * Used by LIST and LSUB to show the floors in the listing
74 void imap_list_floors(char *verb, int num_patterns, char **patterns)
81 for (i = 0; i < MAXFLOORS; ++i) {
82 fl = CtdlGetCachedFloor(i);
83 if (fl->f_flags & F_INUSE) {
85 for (j=0; j<num_patterns; ++j) {
86 if (imap_mailbox_matches_pattern (patterns[j], fl->f_name)) {
91 cprintf("* %s (\\NoSelect \\HasChildren) \"/\" ", verb);
92 plain_imap_strout(fl->f_name);
101 * Back end for imap_list()
103 * Implementation note: IMAP "subscribed folder" is equivalent to Citadel "known room"
105 * The "user data" field is actually an array of pointers; see below for the breakdown
108 void imap_listroom(struct ctdlroom *qrbuf, void *data)
111 char return_options[256];
113 int yes_output_this_room;
115 char **data_for_callback;
117 int subscribed_rooms_only;
120 int return_subscribed = 0;
121 int return_children = 0;
125 /* Here's how we break down the array of pointers passed to us */
126 data_for_callback = data;
127 verb = data_for_callback[0];
128 subscribed_rooms_only = (int) data_for_callback[1];
129 num_patterns = (int) data_for_callback[2];
130 patterns = (char **) data_for_callback[3];
131 return_subscribed = (int) data_for_callback[4];
132 return_children = (int) data_for_callback[5];
134 /* Only list rooms to which the user has access!! */
135 yes_output_this_room = 0;
136 strcpy(return_options, "");
137 CtdlRoomAccess(qrbuf, &CC->user, &ra, NULL);
139 if (return_subscribed) {
141 strcat(return_options, "\\Subscribed");
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 (return_children) {
152 if (!IsEmptyStr(return_options)) {
153 strcat(return_options, " ");
155 strcat(return_options, "\\HasChildren");
158 if (subscribed_rooms_only) {
160 yes_output_this_room = 1;
164 if ((ra & UA_KNOWN) || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
165 yes_output_this_room = 1;
169 if (yes_output_this_room) {
170 imap_mailboxname(buf, sizeof buf, qrbuf);
172 for (i=0; i<num_patterns; ++i) {
173 if (imap_mailbox_matches_pattern(patterns[i], buf)) {
178 cprintf("* %s (%s) \"/\" ", verb, return_options);
179 plain_imap_strout(buf);
187 * Implements the LIST and LSUB commands
189 void imap_list(int num_parms, ConstStr *Params)
191 int subscribed_rooms_only = 0;
193 int i, j, paren_nest;
194 char *data_for_callback[6];
195 int num_patterns = 1;
196 char *patterns[MAX_PATTERNS];
197 int selection_left = (-1);
198 int selection_right = (-1);
199 int return_left = (-1);
200 int return_right = (-1);
202 int patterns_left = 3;
203 int patterns_right = 3;
204 int extended_list_in_use = 0;
205 int return_subscribed = 0;
206 int return_children = 0;
209 cprintf("%s BAD arguments invalid\r\n", Params[0].Key);
213 /* parms[1] is the IMAP verb being used (e.g. LIST or LSUB)
214 * This tells us how to behave, and what verb to return back to the caller
216 safestrncpy(verb, Params[1].Key, sizeof verb);
218 for (i=0; i<j; ++i) {
219 verb[i] = toupper(verb[i]);
222 if (!strcasecmp(verb, "LSUB")) {
223 subscribed_rooms_only = 1;
227 * Partial implementation of LIST-EXTENDED (which will not get used because
228 * we don't advertise it in our capabilities string). Several requirements:
230 * Extraction of selection options:
231 * SUBSCRIBED option: done
232 * RECURSIVEMATCH option: not done yet
233 * REMOTE: safe to silently ignore
235 * Extraction of return options:
236 * SUBSCRIBED option: done
237 * CHILDREN option: done, but needs a non-ugly rewrite
239 * Multiple match patterns: done
243 * If parameter 2 begins with a '(' character, the client is specifying
244 * selection options. Extract their exact position, and then modify our
245 * expectation of where the root folder will be specified.
247 if (Params[2].Key[0] == '(') {
248 extended_list_in_use = 1;
251 for (i=2; i<num_parms; ++i) {
252 for (j=0; Params[i].Key[j]; ++j) {
253 if (Params[i].Key[j] == '(') ++paren_nest;
254 if (Params[i].Key[j] == ')') --paren_nest;
256 if (paren_nest == 0) {
257 selection_right = i; /* found end of selection options */
258 root_pos = i+1; /* folder root appears after selection options */
259 i = num_parms + 1; /* break out of the loop */
264 /* If selection options were found, do something with them.
266 if ((selection_left > 0) && (selection_right >= selection_left)) {
268 /* Strip off the outer parentheses */
269 if (Params[selection_left].Key[0] == '(') {
270 TokenCutLeft(&IMAP->Cmd,
271 &Params[selection_left],
274 if (Params[selection_right].Key[Params[selection_right].len-1] == ')') {
275 TokenCutRight(&IMAP->Cmd,
276 &Params[selection_right],
280 for (i=selection_left; i<=selection_right; ++i) {
282 if (!strcasecmp(Params[i].Key, "SUBSCRIBED")) {
283 subscribed_rooms_only = 1;
286 else if (!strcasecmp(Params[i].Key, "RECURSIVEMATCH")) {
287 /* FIXME - do this! */
294 /* The folder root appears immediately after the selection options,
295 * or in position 2 if no selection options were specified.
297 patterns_left = root_pos + 1;
298 patterns_right = root_pos + 1;
300 if (Params[patterns_left].Key[0] == '(') {
301 extended_list_in_use = 1;
303 for (i=patterns_left; i<num_parms; ++i) {
304 for (j=0; &Params[i].Key[j]; ++j) {
305 if (Params[i].Key[j] == '(') ++paren_nest;
306 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 num_patterns = patterns_right - patterns_left + 1;
314 for (i=0; i<num_patterns; ++i) {
315 if (i < MAX_PATTERNS) {
316 patterns[i] = malloc(512);
317 snprintf(patterns[i], 512, "%s%s", Params[root_pos].Key, Params[patterns_left+i].Key);
319 strcpy(patterns[i], &patterns[i][1]);
321 if (i == num_patterns-1) {
322 patterns[i][strlen(patterns[i])-1] = 0;
329 patterns[0] = malloc(512);
330 snprintf(patterns[0], 512, "%s%s",
331 Params[root_pos].Key,
332 Params[patterns_left].Key);
335 /* If the word "RETURN" appears after the folder pattern list, then the client
336 * is specifying return options.
338 if (num_parms - patterns_right > 2) if (!strcasecmp(Params[patterns_right+1].Key, "RETURN")) {
339 return_left = patterns_right + 2;
340 extended_list_in_use = 1;
342 for (i=return_left; i<num_parms; ++i) {
343 for (j=0; Params[i].Key[j]; ++j) {
344 if (Params[i].Key[j] == '(') ++paren_nest;
345 if (Params[i].Key[j] == ')') --paren_nest;
348 /* Might as well look for these while we're in here... */
349 if (Params[i].Key[0] == '(')
350 TokenCutLeft(&IMAP->Cmd,
353 if (Params[i].Key[Params[i].len-1] == ')')
354 TokenCutRight(&IMAP->Cmd,
358 CtdlLogPrintf(9, "evaluating <%s>\n", Params[i].Key);
360 if (!strcasecmp(Params[i].Key, "SUBSCRIBED")) {
361 return_subscribed = 1;
364 else if (!strcasecmp(Params[i].Key, "CHILDREN")) {
368 if (paren_nest == 0) {
369 return_right = i; /* found end of patterns */
370 i = num_parms + 1; /* break out of the loop */
375 /* Now start setting up the data we're going to send to the CtdlForEachRoom() callback.
377 data_for_callback[0] = (char *) verb;
378 data_for_callback[1] = (char *) subscribed_rooms_only;
379 data_for_callback[2] = (char *) num_patterns;
380 data_for_callback[3] = (char *) patterns;
381 data_for_callback[4] = (char *) return_subscribed;
382 data_for_callback[5] = (char *) return_children;
384 /* The non-extended LIST command is required to treat an empty
385 * ("" string) mailbox name argument as a special request to return the
386 * hierarchy delimiter and the root name of the name given in the
387 * reference parameter.
389 if ( (IsEmptyStr(patterns[0])) && (extended_list_in_use == 0) ) {
390 cprintf("* %s (\\Noselect) \"/\" \"\"\r\n", verb);
393 /* Non-empty mailbox names, and any form of the extended LIST command,
394 * is handled by this loop.
397 imap_list_floors(verb, num_patterns, patterns);
398 CtdlForEachRoom(imap_listroom, data_for_callback);
402 * Free the pattern buffers we allocated above.
404 for (i=0; i<num_patterns; ++i) {
408 cprintf("%s OK %s completed\r\n", Params[0].Key, verb);