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 "serv_imap.h"
61 #include "imap_tools.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 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);
187 * Implements the LIST and LSUB commands
189 void imap_list(int num_parms, char *parms[])
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", parms[0]);
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, parms[1], 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 (parms[2][0] == '(') {
248 extended_list_in_use = 1;
251 for (i=2; i<num_parms; ++i) {
252 for (j=0; parms[i][j]; ++j) {
253 if (parms[i][j] == '(') ++paren_nest;
254 if (parms[i][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 (parms[selection_left][0] == '(') {
270 strcpy(parms[selection_left], &parms[selection_left][1]);
272 if (parms[selection_right][strlen(parms[selection_right])-1] == ')') {
273 parms[selection_right][strlen(parms[selection_right])-1] = 0;
276 for (i=selection_left; i<=selection_right; ++i) {
278 if (!strcasecmp(parms[i], "SUBSCRIBED")) {
279 subscribed_rooms_only = 1;
282 else if (!strcasecmp(parms[i], "RECURSIVEMATCH")) {
283 /* FIXME - do this! */
290 /* The folder root appears immediately after the selection options,
291 * or in position 2 if no selection options were specified.
293 patterns_left = root_pos + 1;
294 patterns_right = root_pos + 1;
296 if (parms[patterns_left][0] == '(') {
297 extended_list_in_use = 1;
299 for (i=patterns_left; i<num_parms; ++i) {
300 for (j=0; &parms[i][j]; ++j) {
301 if (parms[i][j] == '(') ++paren_nest;
302 if (parms[i][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 num_patterns = patterns_right - patterns_left + 1;
310 for (i=0; i<num_patterns; ++i) {
311 if (i < MAX_PATTERNS) {
312 patterns[i] = malloc(512);
313 snprintf(patterns[i], 512, "%s%s", parms[root_pos], parms[patterns_left+i]);
315 strcpy(patterns[i], &patterns[i][1]);
317 if (i == num_patterns-1) {
318 patterns[i][strlen(patterns[i])-1] = 0;
325 patterns[0] = malloc(512);
326 snprintf(patterns[0], 512, "%s%s", parms[root_pos], parms[patterns_left]);
329 /* If the word "RETURN" appears after the folder pattern list, then the client
330 * is specifying return options.
332 if (num_parms - patterns_right > 2) if (!strcasecmp(parms[patterns_right+1], "RETURN")) {
333 return_left = patterns_right + 2;
334 extended_list_in_use = 1;
336 for (i=return_left; i<num_parms; ++i) {
337 for (j=0; parms[i][j]; ++j) {
338 if (parms[i][j] == '(') ++paren_nest;
339 if (parms[i][j] == ')') --paren_nest;
342 /* Might as well look for these while we're in here... */
343 if (parms[i][0] == '(') strcpy(parms[i], &parms[i][1]);
344 if (parms[i][strlen(parms[i])-1] == ')') parms[i][strlen(parms[i])-1] = 0;
345 CtdlLogPrintf(9, "evaluating <%s>\n", parms[i]);
347 if (!strcasecmp(parms[i], "SUBSCRIBED")) {
348 return_subscribed = 1;
351 else if (!strcasecmp(parms[i], "CHILDREN")) {
355 if (paren_nest == 0) {
356 return_right = i; /* found end of patterns */
357 i = num_parms + 1; /* break out of the loop */
362 /* Now start setting up the data we're going to send to the CtdlForEachRoom() callback.
364 data_for_callback[0] = (char *) verb;
365 data_for_callback[1] = (char *) subscribed_rooms_only;
366 data_for_callback[2] = (char *) num_patterns;
367 data_for_callback[3] = (char *) patterns;
368 data_for_callback[4] = (char *) return_subscribed;
369 data_for_callback[5] = (char *) return_children;
371 /* The non-extended LIST command is required to treat an empty
372 * ("" string) mailbox name argument as a special request to return the
373 * hierarchy delimiter and the root name of the name given in the
374 * reference parameter.
376 if ( (IsEmptyStr(patterns[0])) && (extended_list_in_use == 0) ) {
377 cprintf("* %s (\\Noselect) \"/\" \"\"\r\n", verb);
380 /* Non-empty mailbox names, and any form of the extended LIST command,
381 * is handled by this loop.
384 imap_list_floors(verb, num_patterns, patterns);
385 CtdlForEachRoom(imap_listroom, data_for_callback);
389 * Free the pattern buffers we allocated above.
391 for (i=0; i<num_patterns; ++i) {
395 cprintf("%s OK %s completed\r\n", parms[0], verb);