4 * Implements the LIST and LSUB commands.
6 * Copyright (C) 2000-2007 by Art Cancro and others.
7 * This code is released under the terms of the GNU General Public License.
19 #include <sys/types.h>
21 #if TIME_WITH_SYS_TIME
22 # include <sys/time.h>
26 # include <sys/time.h>
36 #include <libcitadel.h>
39 #include "sysdep_decls.h"
40 #include "citserver.h"
48 #include "internet_addressing.h"
49 #include "serv_imap.h"
50 #include "imap_tools.h"
51 #include "imap_fetch.h"
52 #include "imap_search.h"
53 #include "imap_store.h"
55 #include "imap_misc.h"
56 #include "imap_list.h"
57 #include "ctdl_module.h"
61 * Used by LIST and LSUB to show the floors in the listing
63 void imap_list_floors(char *verb, int num_patterns, char **patterns)
70 for (i = 0; i < MAXFLOORS; ++i) {
72 if (fl->f_flags & F_INUSE) {
74 for (j=0; j<num_patterns; ++j) {
75 if (imap_mailbox_matches_pattern (patterns[j], fl->f_name)) {
80 cprintf("* %s (\\NoSelect \\HasChildren) \"/\" ", verb);
81 imap_strout(fl->f_name);
90 * Back end for imap_list()
92 * Implementation note: IMAP "subscribed folder" is equivalent to Citadel "known room"
94 * The "user data" field is actually an array of pointers; see below for the breakdown
97 void imap_listroom(struct ctdlroom *qrbuf, void *data)
100 char return_options[256];
102 int yes_output_this_room;
104 char **data_for_callback;
106 int subscribed_rooms_only;
109 int return_subscribed = 0;
110 int return_children = 0;
111 int return_metadata = 0; /* FIXME obsolete remove this */
115 /* Here's how we break down the array of pointers passed to us */
116 data_for_callback = data;
117 verb = data_for_callback[0];
118 subscribed_rooms_only = (int) data_for_callback[1];
119 num_patterns = (int) data_for_callback[2];
120 patterns = (char **) data_for_callback[3];
121 return_subscribed = (int) data_for_callback[4];
122 return_children = (int) data_for_callback[5];
123 return_metadata = (int) data_for_callback[6]; /* FIXME obsolete remove this */
125 /* Only list rooms to which the user has access!! */
126 yes_output_this_room = 0;
127 strcpy(return_options, "");
128 CtdlRoomAccess(qrbuf, &CC->user, &ra, NULL);
130 if (return_subscribed) {
132 strcat(return_options, "\\Subscribed");
136 /* Warning: ugly hack.
137 * We don't have any way to determine the presence of child mailboxes
138 * without refactoring this entire module. So we're just going to return
139 * the \HasChildren attribute for every room.
140 * We'll fix this later when we have time.
142 if (return_children) {
143 if (!IsEmptyStr(return_options)) {
144 strcat(return_options, " ");
146 strcat(return_options, "\\HasChildren");
149 if (subscribed_rooms_only) {
151 yes_output_this_room = 1;
155 if ((ra & UA_KNOWN) || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
156 yes_output_this_room = 1;
160 if (yes_output_this_room) {
161 imap_mailboxname(buf, sizeof buf, qrbuf);
163 for (i=0; i<num_patterns; ++i) {
164 if (imap_mailbox_matches_pattern(patterns[i], buf)) {
169 cprintf("* %s (%s) \"/\" ", verb, return_options);
172 if (return_metadata) {
173 cprintf(" (METADATA ())"); /* FIXME obsolete remove this */
183 * Implements the LIST and LSUB commands
185 void imap_list(int num_parms, char *parms[])
187 int subscribed_rooms_only = 0;
189 int i, j, paren_nest;
190 char *data_for_callback[7];
191 int num_patterns = 1;
192 char *patterns[MAX_PATTERNS];
193 int selection_left = (-1);
194 int selection_right = (-1);
195 int return_left = (-1);
196 int return_right = (-1);
198 int patterns_left = 3;
199 int patterns_right = 3;
200 int extended_list_in_use = 0;
201 int return_subscribed = 0;
202 int return_children = 0;
203 int return_metadata = 0; /* FIXME obsolete remove this */
204 int select_metadata_left = (-1); /* FIXME obsolete remove this */
205 int select_metadata_right = (-1); /* FIXME obsolete remove this */
206 int select_metadata_nest = 0; /* FIXME obsolete remove this */
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 * In order to implement draft-ietf-imapext-list-extensions-18
228 * ("LIST Command Extensions") we need to:
230 * 1. Extract "selection options"
232 * SUBSCRIBED option: done
233 * RECURSIVEMATCH option: not done yet
234 * REMOTE: safe to silently ignore)
236 * 2. Extract "return options"
238 * SUBSCRIBED option: done
239 * CHILDREN option: done, but needs a non-ugly rewrite)
241 * 3. Determine whether there is more than one match pattern (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 (parms[2][0] == '(') {
250 extended_list_in_use = 1;
253 for (i=2; i<num_parms; ++i) {
254 for (j=0; parms[i][j]; ++j) {
255 if (parms[i][j] == '(') ++paren_nest;
256 if (parms[i][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 (parms[selection_left][0] == '(') {
272 strcpy(parms[selection_left], &parms[selection_left][1]);
274 if (parms[selection_right][strlen(parms[selection_right])-1] == ')') {
275 parms[selection_right][strlen(parms[selection_right])-1] = 0;
278 for (i=selection_left; i<=selection_right; ++i) {
280 /* are we in the middle of a metadata select block? */
281 if ((select_metadata_left >= 0) && (select_metadata_right < 0)) {
282 select_metadata_nest += haschar(parms[i], '(') - haschar(parms[i], ')') ;
283 if (select_metadata_nest == 0) {
284 select_metadata_right = i;
288 else if (!strcasecmp(parms[i], "METADATA")) {
289 select_metadata_left = i+1;
290 select_metadata_nest = 0;
293 else if (!strcasecmp(parms[i], "SUBSCRIBED")) {
294 subscribed_rooms_only = 1;
297 else if (!strcasecmp(parms[i], "RECURSIVEMATCH")) {
298 /* FIXME - do this! */
305 CtdlLogPrintf(CTDL_DEBUG, "select metadata: %d to %d\n", select_metadata_left, select_metadata_right);
306 /* FIXME blah, we have to do something with this */
308 /* The folder root appears immediately after the selection options,
309 * or in position 2 if no selection options were specified.
311 patterns_left = root_pos + 1;
312 patterns_right = root_pos + 1;
314 if (parms[patterns_left][0] == '(') {
315 extended_list_in_use = 1;
317 for (i=patterns_left; i<num_parms; ++i) {
318 for (j=0; &parms[i][j]; ++j) {
319 if (parms[i][j] == '(') ++paren_nest;
320 if (parms[i][j] == ')') --paren_nest;
322 if (paren_nest == 0) {
323 patterns_right = i; /* found end of patterns */
324 i = num_parms + 1; /* break out of the loop */
327 num_patterns = patterns_right - patterns_left + 1;
328 for (i=0; i<num_patterns; ++i) {
329 if (i < MAX_PATTERNS) {
330 patterns[i] = malloc(512);
331 snprintf(patterns[i], 512, "%s%s", parms[root_pos], parms[patterns_left+i]);
333 strcpy(patterns[i], &patterns[i][1]);
335 if (i == num_patterns-1) {
336 patterns[i][strlen(patterns[i])-1] = 0;
343 patterns[0] = malloc(512);
344 snprintf(patterns[0], 512, "%s%s", parms[root_pos], parms[patterns_left]);
347 /* If the word "RETURN" appears after the folder pattern list, then the client
348 * is specifying return options.
350 if (num_parms - patterns_right > 2) if (!strcasecmp(parms[patterns_right+1], "RETURN")) {
351 return_left = patterns_right + 2;
352 extended_list_in_use = 1;
354 for (i=return_left; i<num_parms; ++i) {
355 for (j=0; parms[i][j]; ++j) {
356 if (parms[i][j] == '(') ++paren_nest;
357 if (parms[i][j] == ')') --paren_nest;
360 /* Might as well look for these while we're in here... */
361 if (parms[i][0] == '(') strcpy(parms[i], &parms[i][1]);
362 if (parms[i][strlen(parms[i])-1] == ')') parms[i][strlen(parms[i])-1] = 0;
363 CtdlLogPrintf(9, "evaluating <%s>\n", parms[i]);
365 if (!strcasecmp(parms[i], "SUBSCRIBED")) {
366 return_subscribed = 1;
369 else if (!strcasecmp(parms[i], "CHILDREN")) {
373 else if (!strcasecmp(parms[i], "METADATA")) {
377 if (paren_nest == 0) {
378 return_right = i; /* found end of patterns */
379 i = num_parms + 1; /* break out of the loop */
384 /* Now start setting up the data we're going to send to the ForEachRoom() callback.
386 data_for_callback[0] = (char *) verb;
387 data_for_callback[1] = (char *) subscribed_rooms_only;
388 data_for_callback[2] = (char *) num_patterns;
389 data_for_callback[3] = (char *) patterns;
390 data_for_callback[4] = (char *) return_subscribed;
391 data_for_callback[5] = (char *) return_children;
392 data_for_callback[6] = (char *) return_metadata; /* FIXME obsolete remove this */
394 /* The non-extended LIST command is required to treat an empty
395 * ("" string) mailbox name argument as a special request to return the
396 * hierarchy delimiter and the root name of the name given in the
397 * reference parameter.
399 if ( (IsEmptyStr(patterns[0])) && (extended_list_in_use == 0) ) {
400 cprintf("* %s (\\Noselect) \"/\" \"\"\r\n", verb);
403 /* Non-empty mailbox names, and any form of the extended LIST command,
404 * is handled by this loop.
407 imap_list_floors(verb, num_patterns, patterns);
408 ForEachRoom(imap_listroom, data_for_callback);
412 * Free the pattern buffers we allocated above.
414 for (i=0; i<num_patterns; ++i) {
418 cprintf("%s OK %s completed\r\n", parms[0], verb);