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"
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"
59 #include "serv_crypto.h"
63 * Used by LIST and LSUB to show the floors in the listing
65 void imap_list_floors(char *verb, int num_patterns, char **patterns)
72 for (i = 0; i < MAXFLOORS; ++i) {
74 if (fl->f_flags & F_INUSE) {
76 for (j=0; j<num_patterns; ++j) {
77 if (imap_mailbox_matches_pattern (patterns[j], fl->f_name)) {
82 cprintf("* %s (\\NoSelect \\HasChildren) \"/\" ", verb);
83 imap_strout(fl->f_name);
92 * Back end for imap_list()
94 * Implementation note: IMAP "subscribed folder" is equivalent to Citadel "known room"
96 * The "user data" field is actually an array of pointers; see below for the breakdown
99 void imap_listroom(struct ctdlroom *qrbuf, void *data)
102 char return_options[256];
104 int yes_output_this_room;
106 char **data_for_callback;
108 int subscribed_rooms_only;
111 int return_subscribed;
117 /* Here's how we break down the array of pointers passed to us */
118 data_for_callback = data;
119 verb = data_for_callback[0];
120 subscribed_rooms_only = (int) data_for_callback[1];
121 num_patterns = (int) data_for_callback[2];
122 patterns = (char **) data_for_callback[3];
123 return_subscribed = (int) data_for_callback[4];
124 return_children = (int) data_for_callback[5];
125 return_metadata = (int) data_for_callback[6];
127 /* Only list rooms to which the user has access!! */
128 yes_output_this_room = 0;
129 strcpy(return_options, "");
130 CtdlRoomAccess(qrbuf, &CC->user, &ra, NULL);
132 if (return_subscribed) {
134 strcat(return_options, "\\Subscribed");
138 /* Warning: ugly hack.
139 * We don't have any way to determine the presence of child mailboxes
140 * without refactoring this entire module. So we're just going to return
141 * the \HasChildren attribute for every room.
142 * We'll fix this later when we have time.
144 if (return_children) {
145 if (strlen(return_options) > 0) {
146 strcat(return_options, " ");
148 strcat(return_options, "\\HasChildren");
151 if (subscribed_rooms_only) {
153 yes_output_this_room = 1;
157 if ((ra & UA_KNOWN) || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
158 yes_output_this_room = 1;
162 if (yes_output_this_room) {
163 imap_mailboxname(buf, sizeof buf, qrbuf);
165 for (i=0; i<num_patterns; ++i) {
166 if (imap_mailbox_matches_pattern(patterns[i], buf)) {
171 cprintf("* %s (%s) \"/\" ", verb, return_options);
174 if (return_metadata) {
175 cprintf(" (METADATA ())"); /* FIXME */
185 * Implements the LIST and LSUB commands
187 void imap_list(int num_parms, char *parms[])
189 int subscribed_rooms_only = 0;
191 int i, j, paren_nest;
192 char *data_for_callback[7];
193 int num_patterns = 1;
194 char *patterns[MAX_PATTERNS];
195 int selection_left = (-1);
196 int selection_right = (-1);
197 int return_left = (-1);
198 int return_right = (-1);
200 int patterns_left = 3;
201 int patterns_right = 3;
202 int extended_list_in_use = 0;
203 int return_subscribed = 0;
204 int return_children = 0;
205 int return_metadata = 0;
206 int select_metadata_left = (-1);
207 int select_metadata_right = (-1);
208 int select_metadata_nest = 0;
211 cprintf("%s BAD arguments invalid\r\n", parms[0]);
215 /* parms[1] is the IMAP verb being used (e.g. LIST or LSUB)
216 * This tells us how to behave, and what verb to return back to the caller
218 safestrncpy(verb, parms[1], sizeof verb);
220 for (i=0; i<j; ++i) {
221 verb[i] = toupper(verb[i]);
224 if (!strcasecmp(verb, "LSUB")) {
225 subscribed_rooms_only = 1;
229 * In order to implement draft-ietf-imapext-list-extensions-18
230 * ("LIST Command Extensions") we need to:
232 * 1. Extract "selection options"
234 * SUBSCRIBED option: done
235 * RECURSIVEMATCH option: not done yet
236 * REMOTE: safe to silently ignore)
238 * 2. Extract "return options"
240 * SUBSCRIBED option: done
241 * CHILDREN option: done, but needs a non-ugly rewrite)
243 * 3. Determine whether there is more than one match pattern (done)
247 * If parameter 2 begins with a '(' character, the client is specifying
248 * selection options. Extract their exact position, and then modify our
249 * expectation of where the root folder will be specified.
251 if (parms[2][0] == '(') {
252 extended_list_in_use = 1;
255 for (i=2; i<num_parms; ++i) {
256 for (j=0; j<strlen(parms[i]); ++j) {
257 if (parms[i][j] == '(') ++paren_nest;
258 if (parms[i][j] == ')') --paren_nest;
260 if (paren_nest == 0) {
261 selection_right = i; /* found end of selection options */
262 root_pos = i+1; /* folder root appears after selection options */
263 i = num_parms + 1; /* break out of the loop */
268 /* If selection options were found, do something with them.
270 if ((selection_left > 0) && (selection_right >= selection_left)) {
272 /* Strip off the outer parentheses */
273 if (parms[selection_left][0] == '(') {
274 strcpy(parms[selection_left], &parms[selection_left][1]);
276 if (parms[selection_right][strlen(parms[selection_right])-1] == ')') {
277 parms[selection_right][strlen(parms[selection_right])-1] = 0;
280 for (i=selection_left; i<=selection_right; ++i) {
282 /* are we in the middle of a metadata select block? */
283 if ((select_metadata_left >= 0) && (select_metadata_right < 0)) {
284 select_metadata_nest += haschar(parms[i], '(') - haschar(parms[i], ')') ;
285 if (select_metadata_nest == 0) {
286 select_metadata_right = i;
290 else if (!strcasecmp(parms[i], "METADATA")) {
291 select_metadata_left = i+1;
292 select_metadata_nest = 0;
295 else if (!strcasecmp(parms[i], "SUBSCRIBED")) {
296 subscribed_rooms_only = 1;
299 else if (!strcasecmp(parms[i], "RECURSIVEMATCH")) {
300 /* FIXME - do this! */
307 lprintf(CTDL_DEBUG, "select metadata: %d to %d\n", select_metadata_left, select_metadata_right);
308 /* FIXME blah, we have to do something with this */
310 /* The folder root appears immediately after the selection options,
311 * or in position 2 if no selection options were specified.
313 patterns_left = root_pos + 1;
314 patterns_right = root_pos + 1;
316 if (parms[patterns_left][0] == '(') {
317 extended_list_in_use = 1;
319 for (i=patterns_left; i<num_parms; ++i) {
320 for (j=0; j<strlen(parms[i]); ++j) {
321 if (parms[i][j] == '(') ++paren_nest;
322 if (parms[i][j] == ')') --paren_nest;
324 if (paren_nest == 0) {
325 patterns_right = i; /* found end of patterns */
326 i = num_parms + 1; /* break out of the loop */
329 num_patterns = patterns_right - patterns_left + 1;
330 for (i=0; i<num_patterns; ++i) {
331 if (i < MAX_PATTERNS) {
332 patterns[i] = malloc(512);
333 snprintf(patterns[i], 512, "%s%s", parms[root_pos], parms[patterns_left+i]);
335 strcpy(patterns[i], &patterns[i][1]);
337 if (i == num_patterns-1) {
338 patterns[i][strlen(patterns[i])-1] = 0;
345 patterns[0] = malloc(512);
346 snprintf(patterns[0], 512, "%s%s", parms[root_pos], parms[patterns_left]);
349 /* If the word "RETURN" appears after the folder pattern list, then the client
350 * is specifying return options.
352 if (num_parms - patterns_right > 2) if (!strcasecmp(parms[patterns_right+1], "RETURN")) {
353 return_left = patterns_right + 2;
354 extended_list_in_use = 1;
356 for (i=return_left; i<num_parms; ++i) {
357 for (j=0; j<strlen(parms[i]); ++j) {
358 if (parms[i][j] == '(') ++paren_nest;
359 if (parms[i][j] == ')') --paren_nest;
362 /* Might as well look for these while we're in here... */
363 if (parms[i][0] == '(') strcpy(parms[i], &parms[i][1]);
364 if (parms[i][strlen(parms[i])-1] == ')') parms[i][strlen(parms[i])-1] = 0;
365 lprintf(9, "evaluating <%s>\n", parms[i]);
367 if (!strcasecmp(parms[i], "SUBSCRIBED")) {
368 return_subscribed = 1;
371 else if (!strcasecmp(parms[i], "CHILDREN")) {
375 else if (!strcasecmp(parms[i], "METADATA")) {
379 if (paren_nest == 0) {
380 return_right = i; /* found end of patterns */
381 i = num_parms + 1; /* break out of the loop */
386 /* Now start setting up the data we're going to send to the ForEachRoom() callback.
388 data_for_callback[0] = (char *) verb;
389 data_for_callback[1] = (char *) subscribed_rooms_only;
390 data_for_callback[2] = (char *) num_patterns;
391 data_for_callback[3] = (char *) patterns;
392 data_for_callback[4] = (char *) return_subscribed;
393 data_for_callback[5] = (char *) return_children;
394 data_for_callback[6] = (char *) return_metadata;
396 /* The non-extended LIST command is required to treat an empty
397 * ("" string) mailbox name argument as a special request to return the
398 * hierarchy delimiter and the root name of the name given in the
399 * reference parameter.
401 if ( (strlen(patterns[0]) == 0) && (extended_list_in_use == 0) ) {
402 cprintf("* %s (\\Noselect) \"/\" \"\"\r\n", verb);
405 /* Non-empty mailbox names, and any form of the extended LIST command,
406 * is handled by this loop.
409 imap_list_floors(verb, num_patterns, patterns);
410 ForEachRoom(imap_listroom, data_for_callback);
414 * Free the pattern buffers we allocated above.
416 for (i=0; i<num_patterns; ++i) {
420 cprintf("%s OK %s completed\r\n", parms[0], verb);