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"
60 #include "internet_addressing.h"
61 #include "serv_imap.h"
62 #include "imap_tools.h"
63 #include "imap_fetch.h"
64 #include "imap_search.h"
65 #include "imap_store.h"
67 #include "imap_misc.h"
68 #include "imap_list.h"
69 #include "ctdl_module.h"
73 * Used by LIST and LSUB to show the floors in the listing
75 void imap_list_floors(char *verb, int num_patterns, char **patterns)
82 for (i = 0; i < MAXFLOORS; ++i) {
84 if (fl->f_flags & F_INUSE) {
86 for (j=0; j<num_patterns; ++j) {
87 if (imap_mailbox_matches_pattern (patterns[j], fl->f_name)) {
92 cprintf("* %s (\\NoSelect \\HasChildren) \"/\" ", verb);
93 imap_strout(fl->f_name);
102 * Back end for imap_list()
104 * Implementation note: IMAP "subscribed folder" is equivalent to Citadel "known room"
106 * The "user data" field is actually an array of pointers; see below for the breakdown
109 void imap_listroom(struct ctdlroom *qrbuf, void *data)
112 char return_options[256];
114 int yes_output_this_room;
116 char **data_for_callback;
118 int subscribed_rooms_only;
121 int return_subscribed = 0;
122 int return_children = 0;
126 /* Here's how we break down the array of pointers passed to us */
127 data_for_callback = data;
128 verb = data_for_callback[0];
129 subscribed_rooms_only = (int) data_for_callback[1];
130 num_patterns = (int) data_for_callback[2];
131 patterns = (char **) data_for_callback[3];
132 return_subscribed = (int) data_for_callback[4];
133 return_children = (int) data_for_callback[5];
135 /* Only list rooms to which the user has access!! */
136 yes_output_this_room = 0;
137 strcpy(return_options, "");
138 CtdlRoomAccess(qrbuf, &CC->user, &ra, NULL);
140 if (return_subscribed) {
142 strcat(return_options, "\\Subscribed");
146 /* Warning: ugly hack.
147 * We don't have any way to determine the presence of child mailboxes
148 * without refactoring this entire module. So we're just going to return
149 * the \HasChildren attribute for every room.
150 * We'll fix this later when we have time.
152 if (return_children) {
153 if (!IsEmptyStr(return_options)) {
154 strcat(return_options, " ");
156 strcat(return_options, "\\HasChildren");
159 if (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(buf, sizeof buf, qrbuf);
173 for (i=0; i<num_patterns; ++i) {
174 if (imap_mailbox_matches_pattern(patterns[i], buf)) {
179 cprintf("* %s (%s) \"/\" ", verb, return_options);
188 * Implements the LIST and LSUB commands
190 void imap_list(int num_parms, char *parms[])
192 int subscribed_rooms_only = 0;
194 int i, j, paren_nest;
195 char *data_for_callback[6];
196 int num_patterns = 1;
197 char *patterns[MAX_PATTERNS];
198 int selection_left = (-1);
199 int selection_right = (-1);
200 int return_left = (-1);
201 int return_right = (-1);
203 int patterns_left = 3;
204 int patterns_right = 3;
205 int extended_list_in_use = 0;
206 int return_subscribed = 0;
207 int return_children = 0;
210 cprintf("%s BAD arguments invalid\r\n", parms[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(verb, parms[1], sizeof verb);
219 for (i=0; i<j; ++i) {
220 verb[i] = toupper(verb[i]);
223 if (!strcasecmp(verb, "LSUB")) {
224 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 (parms[2][0] == '(') {
249 extended_list_in_use = 1;
252 for (i=2; i<num_parms; ++i) {
253 for (j=0; parms[i][j]; ++j) {
254 if (parms[i][j] == '(') ++paren_nest;
255 if (parms[i][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 (parms[selection_left][0] == '(') {
271 strcpy(parms[selection_left], &parms[selection_left][1]);
273 if (parms[selection_right][strlen(parms[selection_right])-1] == ')') {
274 parms[selection_right][strlen(parms[selection_right])-1] = 0;
277 for (i=selection_left; i<=selection_right; ++i) {
279 if (!strcasecmp(parms[i], "SUBSCRIBED")) {
280 subscribed_rooms_only = 1;
283 else if (!strcasecmp(parms[i], "RECURSIVEMATCH")) {
284 /* FIXME - do this! */
291 /* The folder root appears immediately after the selection options,
292 * or in position 2 if no selection options were specified.
294 patterns_left = root_pos + 1;
295 patterns_right = root_pos + 1;
297 if (parms[patterns_left][0] == '(') {
298 extended_list_in_use = 1;
300 for (i=patterns_left; i<num_parms; ++i) {
301 for (j=0; &parms[i][j]; ++j) {
302 if (parms[i][j] == '(') ++paren_nest;
303 if (parms[i][j] == ')') --paren_nest;
305 if (paren_nest == 0) {
306 patterns_right = i; /* found end of patterns */
307 i = num_parms + 1; /* break out of the loop */
310 num_patterns = patterns_right - patterns_left + 1;
311 for (i=0; i<num_patterns; ++i) {
312 if (i < MAX_PATTERNS) {
313 patterns[i] = malloc(512);
314 snprintf(patterns[i], 512, "%s%s", parms[root_pos], parms[patterns_left+i]);
316 strcpy(patterns[i], &patterns[i][1]);
318 if (i == num_patterns-1) {
319 patterns[i][strlen(patterns[i])-1] = 0;
326 patterns[0] = malloc(512);
327 snprintf(patterns[0], 512, "%s%s", parms[root_pos], parms[patterns_left]);
330 /* If the word "RETURN" appears after the folder pattern list, then the client
331 * is specifying return options.
333 if (num_parms - patterns_right > 2) if (!strcasecmp(parms[patterns_right+1], "RETURN")) {
334 return_left = patterns_right + 2;
335 extended_list_in_use = 1;
337 for (i=return_left; i<num_parms; ++i) {
338 for (j=0; parms[i][j]; ++j) {
339 if (parms[i][j] == '(') ++paren_nest;
340 if (parms[i][j] == ')') --paren_nest;
343 /* Might as well look for these while we're in here... */
344 if (parms[i][0] == '(') strcpy(parms[i], &parms[i][1]);
345 if (parms[i][strlen(parms[i])-1] == ')') parms[i][strlen(parms[i])-1] = 0;
346 CtdlLogPrintf(9, "evaluating <%s>\n", parms[i]);
348 if (!strcasecmp(parms[i], "SUBSCRIBED")) {
349 return_subscribed = 1;
352 else if (!strcasecmp(parms[i], "CHILDREN")) {
356 if (paren_nest == 0) {
357 return_right = i; /* found end of patterns */
358 i = num_parms + 1; /* break out of the loop */
363 /* Now start setting up the data we're going to send to the ForEachRoom() callback.
365 data_for_callback[0] = (char *) verb;
366 data_for_callback[1] = (char *) subscribed_rooms_only;
367 data_for_callback[2] = (char *) num_patterns;
368 data_for_callback[3] = (char *) patterns;
369 data_for_callback[4] = (char *) return_subscribed;
370 data_for_callback[5] = (char *) return_children;
372 /* The non-extended LIST command is required to treat an empty
373 * ("" string) mailbox name argument as a special request to return the
374 * hierarchy delimiter and the root name of the name given in the
375 * reference parameter.
377 if ( (IsEmptyStr(patterns[0])) && (extended_list_in_use == 0) ) {
378 cprintf("* %s (\\Noselect) \"/\" \"\"\r\n", verb);
381 /* Non-empty mailbox names, and any form of the extended LIST command,
382 * is handled by this loop.
385 imap_list_floors(verb, num_patterns, patterns);
386 ForEachRoom(imap_listroom, data_for_callback);
390 * Free the pattern buffers we allocated above.
392 for (i=0; i<num_patterns; ++i) {
396 cprintf("%s OK %s completed\r\n", parms[0], verb);