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"
60 * Used by LIST and LSUB to show the floors in the listing
62 void imap_list_floors(char *verb, int num_patterns, char **patterns)
69 for (i = 0; i < MAXFLOORS; ++i) {
71 if (fl->f_flags & F_INUSE) {
73 for (j=0; j<num_patterns; ++j) {
74 if (imap_mailbox_matches_pattern (patterns[j], fl->f_name)) {
79 cprintf("* %s (\\NoSelect \\HasChildren) \"/\" ", verb);
80 imap_strout(fl->f_name);
89 * Back end for imap_list()
91 * Implementation note: IMAP "subscribed folder" is equivalent to Citadel "known room"
93 * The "user data" field is actually an array of pointers; see below for the breakdown
96 void imap_listroom(struct ctdlroom *qrbuf, void *data)
99 char return_options[256];
101 int yes_output_this_room;
103 char **data_for_callback;
105 int subscribed_rooms_only;
108 int return_subscribed;
114 /* Here's how we break down the array of pointers passed to us */
115 data_for_callback = data;
116 verb = data_for_callback[0];
117 subscribed_rooms_only = (int) data_for_callback[1];
118 num_patterns = (int) data_for_callback[2];
119 patterns = (char **) data_for_callback[3];
120 return_subscribed = (int) data_for_callback[4];
121 return_children = (int) data_for_callback[5];
122 return_metadata = (int) data_for_callback[6];
124 /* Only list rooms to which the user has access!! */
125 yes_output_this_room = 0;
126 strcpy(return_options, "");
127 CtdlRoomAccess(qrbuf, &CC->user, &ra, NULL);
129 if (return_subscribed) {
131 strcat(return_options, "\\Subscribed");
135 /* Warning: ugly hack.
136 * We don't have any way to determine the presence of child mailboxes
137 * without refactoring this entire module. So we're just going to return
138 * the \HasChildren attribute for every room.
139 * We'll fix this later when we have time.
141 if (return_children) {
142 if (!IsEmptyStr(return_options)) {
143 strcat(return_options, " ");
145 strcat(return_options, "\\HasChildren");
148 if (subscribed_rooms_only) {
150 yes_output_this_room = 1;
154 if ((ra & UA_KNOWN) || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
155 yes_output_this_room = 1;
159 if (yes_output_this_room) {
160 imap_mailboxname(buf, sizeof buf, qrbuf);
162 for (i=0; i<num_patterns; ++i) {
163 if (imap_mailbox_matches_pattern(patterns[i], buf)) {
168 cprintf("* %s (%s) \"/\" ", verb, return_options);
171 if (return_metadata) {
172 cprintf(" (METADATA ())"); /* FIXME */
182 * Implements the LIST and LSUB commands
184 void imap_list(int num_parms, char *parms[])
186 int subscribed_rooms_only = 0;
188 int i, j, paren_nest;
189 char *data_for_callback[7];
190 int num_patterns = 1;
191 char *patterns[MAX_PATTERNS];
192 int selection_left = (-1);
193 int selection_right = (-1);
194 int return_left = (-1);
195 int return_right = (-1);
197 int patterns_left = 3;
198 int patterns_right = 3;
199 int extended_list_in_use = 0;
200 int return_subscribed = 0;
201 int return_children = 0;
202 int return_metadata = 0;
203 int select_metadata_left = (-1);
204 int select_metadata_right = (-1);
205 int select_metadata_nest = 0;
208 cprintf("%s BAD arguments invalid\r\n", parms[0]);
212 /* parms[1] is the IMAP verb being used (e.g. LIST or LSUB)
213 * This tells us how to behave, and what verb to return back to the caller
215 safestrncpy(verb, parms[1], sizeof verb);
217 for (i=0; i<j; ++i) {
218 verb[i] = toupper(verb[i]);
221 if (!strcasecmp(verb, "LSUB")) {
222 subscribed_rooms_only = 1;
226 * In order to implement draft-ietf-imapext-list-extensions-18
227 * ("LIST Command Extensions") we need to:
229 * 1. Extract "selection options"
231 * SUBSCRIBED option: done
232 * RECURSIVEMATCH option: not done yet
233 * REMOTE: safe to silently ignore)
235 * 2. Extract "return options"
237 * SUBSCRIBED option: done
238 * CHILDREN option: done, but needs a non-ugly rewrite)
240 * 3. Determine whether there is more than one match pattern (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 /* are we in the middle of a metadata select block? */
280 if ((select_metadata_left >= 0) && (select_metadata_right < 0)) {
281 select_metadata_nest += haschar(parms[i], '(') - haschar(parms[i], ')') ;
282 if (select_metadata_nest == 0) {
283 select_metadata_right = i;
287 else if (!strcasecmp(parms[i], "METADATA")) {
288 select_metadata_left = i+1;
289 select_metadata_nest = 0;
292 else if (!strcasecmp(parms[i], "SUBSCRIBED")) {
293 subscribed_rooms_only = 1;
296 else if (!strcasecmp(parms[i], "RECURSIVEMATCH")) {
297 /* FIXME - do this! */
304 lprintf(CTDL_DEBUG, "select metadata: %d to %d\n", select_metadata_left, select_metadata_right);
305 /* FIXME blah, we have to do something with this */
307 /* The folder root appears immediately after the selection options,
308 * or in position 2 if no selection options were specified.
310 patterns_left = root_pos + 1;
311 patterns_right = root_pos + 1;
313 if (parms[patterns_left][0] == '(') {
314 extended_list_in_use = 1;
316 for (i=patterns_left; i<num_parms; ++i) {
317 for (j=0; &parms[i][j]; ++j) {
318 if (parms[i][j] == '(') ++paren_nest;
319 if (parms[i][j] == ')') --paren_nest;
321 if (paren_nest == 0) {
322 patterns_right = i; /* found end of patterns */
323 i = num_parms + 1; /* break out of the loop */
326 num_patterns = patterns_right - patterns_left + 1;
327 for (i=0; i<num_patterns; ++i) {
328 if (i < MAX_PATTERNS) {
329 patterns[i] = malloc(512);
330 snprintf(patterns[i], 512, "%s%s", parms[root_pos], parms[patterns_left+i]);
332 strcpy(patterns[i], &patterns[i][1]);
334 if (i == num_patterns-1) {
335 patterns[i][strlen(patterns[i])-1] = 0;
342 patterns[0] = malloc(512);
343 snprintf(patterns[0], 512, "%s%s", parms[root_pos], parms[patterns_left]);
346 /* If the word "RETURN" appears after the folder pattern list, then the client
347 * is specifying return options.
349 if (num_parms - patterns_right > 2) if (!strcasecmp(parms[patterns_right+1], "RETURN")) {
350 return_left = patterns_right + 2;
351 extended_list_in_use = 1;
353 for (i=return_left; i<num_parms; ++i) {
354 for (j=0; parms[i][j]; ++j) {
355 if (parms[i][j] == '(') ++paren_nest;
356 if (parms[i][j] == ')') --paren_nest;
359 /* Might as well look for these while we're in here... */
360 if (parms[i][0] == '(') strcpy(parms[i], &parms[i][1]);
361 if (parms[i][strlen(parms[i])-1] == ')') parms[i][strlen(parms[i])-1] = 0;
362 lprintf(9, "evaluating <%s>\n", parms[i]);
364 if (!strcasecmp(parms[i], "SUBSCRIBED")) {
365 return_subscribed = 1;
368 else if (!strcasecmp(parms[i], "CHILDREN")) {
372 else if (!strcasecmp(parms[i], "METADATA")) {
376 if (paren_nest == 0) {
377 return_right = i; /* found end of patterns */
378 i = num_parms + 1; /* break out of the loop */
383 /* Now start setting up the data we're going to send to the ForEachRoom() callback.
385 data_for_callback[0] = (char *) verb;
386 data_for_callback[1] = (char *) subscribed_rooms_only;
387 data_for_callback[2] = (char *) num_patterns;
388 data_for_callback[3] = (char *) patterns;
389 data_for_callback[4] = (char *) return_subscribed;
390 data_for_callback[5] = (char *) return_children;
391 data_for_callback[6] = (char *) return_metadata;
393 /* The non-extended LIST command is required to treat an empty
394 * ("" string) mailbox name argument as a special request to return the
395 * hierarchy delimiter and the root name of the name given in the
396 * reference parameter.
398 if ( (IsEmptyStr(patterns[0])) && (extended_list_in_use == 0) ) {
399 cprintf("* %s (\\Noselect) \"/\" \"\"\r\n", verb);
402 /* Non-empty mailbox names, and any form of the extended LIST command,
403 * is handled by this loop.
406 imap_list_floors(verb, num_patterns, patterns);
407 ForEachRoom(imap_listroom, data_for_callback);
411 * Free the pattern buffers we allocated above.
413 for (i=0; i<num_patterns; ++i) {
417 cprintf("%s OK %s completed\r\n", parms[0], verb);