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>
38 #include "sysdep_decls.h"
39 #include "citserver.h"
42 #include "serv_extensions.h"
49 #include "internet_addressing.h"
50 #include "serv_imap.h"
51 #include "imap_tools.h"
52 #include "imap_fetch.h"
53 #include "imap_search.h"
54 #include "imap_store.h"
56 #include "imap_misc.h"
57 #include "imap_list.h"
60 #include "serv_crypto.h"
64 * Used by LIST and LSUB to show the floors in the listing
66 void imap_list_floors(char *verb, int num_patterns, char **patterns)
73 for (i = 0; i < MAXFLOORS; ++i) {
75 if (fl->f_flags & F_INUSE) {
77 for (j=0; j<num_patterns; ++j) {
78 if (imap_mailbox_matches_pattern (patterns[j], fl->f_name)) {
83 cprintf("* %s (\\NoSelect \\HasChildren) \"/\" ", verb);
84 imap_strout(fl->f_name);
93 * Back end for imap_list()
95 * Implementation note: IMAP "subscribed folder" is equivalent to Citadel "known room"
97 * The "user data" field is actually an array of pointers; see below for the breakdown
100 void imap_listroom(struct ctdlroom *qrbuf, void *data)
103 char return_options[256];
105 int yes_output_this_room;
107 char **data_for_callback;
109 int subscribed_rooms_only;
112 int return_subscribed;
118 /* Here's how we break down the array of pointers passed to us */
119 data_for_callback = data;
120 verb = data_for_callback[0];
121 subscribed_rooms_only = (int) data_for_callback[1];
122 num_patterns = (int) data_for_callback[2];
123 patterns = (char **) data_for_callback[3];
124 return_subscribed = (int) data_for_callback[4];
125 return_children = (int) data_for_callback[5];
126 return_metadata = (int) data_for_callback[6];
128 /* Only list rooms to which the user has access!! */
129 yes_output_this_room = 0;
130 strcpy(return_options, "");
131 CtdlRoomAccess(qrbuf, &CC->user, &ra, NULL);
133 if (return_subscribed) {
135 strcat(return_options, "\\Subscribed");
139 /* Warning: ugly hack.
140 * We don't have any way to determine the presence of child mailboxes
141 * without refactoring this entire module. So we're just going to return
142 * the \HasChildren attribute for every room.
143 * We'll fix this later when we have time.
145 if (return_children) {
146 if (strlen(return_options) > 0) {
147 strcat(return_options, " ");
149 strcat(return_options, "\\HasChildren");
152 if (subscribed_rooms_only) {
154 yes_output_this_room = 1;
158 if ((ra & UA_KNOWN) || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
159 yes_output_this_room = 1;
163 if (yes_output_this_room) {
164 imap_mailboxname(buf, sizeof buf, qrbuf);
166 for (i=0; i<num_patterns; ++i) {
167 if (imap_mailbox_matches_pattern(patterns[i], buf)) {
172 cprintf("* %s (%s) \"/\" ", verb, return_options);
175 if (return_metadata) {
176 cprintf(" (METADATA ())"); /* FIXME */
186 * Implements the LIST and LSUB commands
188 void imap_list(int num_parms, char *parms[])
190 int subscribed_rooms_only = 0;
192 int i, j, paren_nest;
193 char *data_for_callback[7];
194 int num_patterns = 1;
195 char *patterns[MAX_PATTERNS];
196 int selection_left = (-1);
197 int selection_right = (-1);
198 int return_left = (-1);
199 int return_right = (-1);
201 int patterns_left = 3;
202 int patterns_right = 3;
203 int extended_list_in_use = 0;
204 int return_subscribed = 0;
205 int return_children = 0;
206 int return_metadata = 0;
207 int select_metadata_left = (-1);
208 int select_metadata_right = (-1);
209 int select_metadata_nest = 0;
212 cprintf("%s BAD arguments invalid\r\n", parms[0]);
216 /* parms[1] is the IMAP verb being used (e.g. LIST or LSUB)
217 * This tells us how to behave, and what verb to return back to the caller
219 safestrncpy(verb, parms[1], sizeof verb);
221 for (i=0; i<j; ++i) {
222 verb[i] = toupper(verb[i]);
225 if (!strcasecmp(verb, "LSUB")) {
226 subscribed_rooms_only = 1;
230 * In order to implement draft-ietf-imapext-list-extensions-18
231 * ("LIST Command Extensions") we need to:
233 * 1. Extract "selection options"
235 * SUBSCRIBED option: done
236 * RECURSIVEMATCH option: not done yet
237 * REMOTE: safe to silently ignore)
239 * 2. Extract "return options"
241 * SUBSCRIBED option: done
242 * CHILDREN option: done, but needs a non-ugly rewrite)
244 * 3. Determine whether there is more than one match pattern (done)
248 * If parameter 2 begins with a '(' character, the client is specifying
249 * selection options. Extract their exact position, and then modify our
250 * expectation of where the root folder will be specified.
252 if (parms[2][0] == '(') {
253 extended_list_in_use = 1;
256 for (i=2; i<num_parms; ++i) {
257 for (j=0; j<strlen(parms[i]); ++j) {
258 if (parms[i][j] == '(') ++paren_nest;
259 if (parms[i][j] == ')') --paren_nest;
261 if (paren_nest == 0) {
262 selection_right = i; /* found end of selection options */
263 root_pos = i+1; /* folder root appears after selection options */
264 i = num_parms + 1; /* break out of the loop */
269 /* If selection options were found, do something with them.
271 if ((selection_left > 0) && (selection_right >= selection_left)) {
273 /* Strip off the outer parentheses */
274 if (parms[selection_left][0] == '(') {
275 strcpy(parms[selection_left], &parms[selection_left][1]);
277 if (parms[selection_right][strlen(parms[selection_right])-1] == ')') {
278 parms[selection_right][strlen(parms[selection_right])-1] = 0;
281 for (i=selection_left; i<=selection_right; ++i) {
283 /* are we in the middle of a metadata select block? */
284 if ((select_metadata_left >= 0) && (select_metadata_right < 0)) {
285 select_metadata_nest += haschar(parms[i], '(') - haschar(parms[i], ')') ;
286 if (select_metadata_nest == 0) {
287 select_metadata_right = i;
291 else if (!strcasecmp(parms[i], "METADATA")) {
292 select_metadata_left = i+1;
293 select_metadata_nest = 0;
296 else if (!strcasecmp(parms[i], "SUBSCRIBED")) {
297 subscribed_rooms_only = 1;
300 else if (!strcasecmp(parms[i], "RECURSIVEMATCH")) {
301 /* FIXME - do this! */
308 lprintf(CTDL_DEBUG, "select metadata: %d to %d\n", select_metadata_left, select_metadata_right);
309 /* FIXME blah, we have to do something with this */
311 /* The folder root appears immediately after the selection options,
312 * or in position 2 if no selection options were specified.
314 patterns_left = root_pos + 1;
315 patterns_right = root_pos + 1;
317 if (parms[patterns_left][0] == '(') {
318 extended_list_in_use = 1;
320 for (i=patterns_left; i<num_parms; ++i) {
321 for (j=0; j<strlen(parms[i]); ++j) {
322 if (parms[i][j] == '(') ++paren_nest;
323 if (parms[i][j] == ')') --paren_nest;
325 if (paren_nest == 0) {
326 patterns_right = i; /* found end of patterns */
327 i = num_parms + 1; /* break out of the loop */
330 num_patterns = patterns_right - patterns_left + 1;
331 for (i=0; i<num_patterns; ++i) {
332 if (i < MAX_PATTERNS) {
333 patterns[i] = malloc(512);
334 snprintf(patterns[i], 512, "%s%s", parms[root_pos], parms[patterns_left+i]);
336 strcpy(patterns[i], &patterns[i][1]);
338 if (i == num_patterns-1) {
339 patterns[i][strlen(patterns[i])-1] = 0;
346 patterns[0] = malloc(512);
347 snprintf(patterns[0], 512, "%s%s", parms[root_pos], parms[patterns_left]);
350 /* If the word "RETURN" appears after the folder pattern list, then the client
351 * is specifying return options.
353 if (num_parms - patterns_right > 2) if (!strcasecmp(parms[patterns_right+1], "RETURN")) {
354 return_left = patterns_right + 2;
355 extended_list_in_use = 1;
357 for (i=return_left; i<num_parms; ++i) {
358 for (j=0; j<strlen(parms[i]); ++j) {
359 if (parms[i][j] == '(') ++paren_nest;
360 if (parms[i][j] == ')') --paren_nest;
363 /* Might as well look for these while we're in here... */
364 if (parms[i][0] == '(') strcpy(parms[i], &parms[i][1]);
365 if (parms[i][strlen(parms[i])-1] == ')') parms[i][strlen(parms[i])-1] = 0;
366 lprintf(9, "evaluating <%s>\n", parms[i]);
368 if (!strcasecmp(parms[i], "SUBSCRIBED")) {
369 return_subscribed = 1;
372 else if (!strcasecmp(parms[i], "CHILDREN")) {
376 else if (!strcasecmp(parms[i], "METADATA")) {
380 if (paren_nest == 0) {
381 return_right = i; /* found end of patterns */
382 i = num_parms + 1; /* break out of the loop */
387 /* Now start setting up the data we're going to send to the ForEachRoom() callback.
389 data_for_callback[0] = (char *) verb;
390 data_for_callback[1] = (char *) subscribed_rooms_only;
391 data_for_callback[2] = (char *) num_patterns;
392 data_for_callback[3] = (char *) patterns;
393 data_for_callback[4] = (char *) return_subscribed;
394 data_for_callback[5] = (char *) return_children;
395 data_for_callback[6] = (char *) return_metadata;
397 /* The non-extended LIST command is required to treat an empty
398 * ("" string) mailbox name argument as a special request to return the
399 * hierarchy delimiter and the root name of the name given in the
400 * reference parameter.
402 if ( (strlen(patterns[0]) == 0) && (extended_list_in_use == 0) ) {
403 cprintf("* %s (\\Noselect) \"/\" \"\"\r\n", verb);
406 /* Non-empty mailbox names, and any form of the extended LIST command,
407 * is handled by this loop.
410 imap_list_floors(verb, num_patterns, patterns);
411 ForEachRoom(imap_listroom, data_for_callback);
415 * Free the pattern buffers we allocated above.
417 for (i=0; i<num_patterns; ++i) {
421 cprintf("%s OK %s completed\r\n", parms[0], verb);