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"
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) \"/\" ", 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;
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];
123 /* Only list rooms to which the user has access!! */
124 yes_output_this_room = 0;
125 strcpy(return_options, "");
126 CtdlRoomAccess(qrbuf, &CC->user, &ra, NULL);
128 if (return_subscribed) {
130 strcat(return_options, "\\Subscribed");
134 if (subscribed_rooms_only) {
136 yes_output_this_room = 1;
140 if ((ra & UA_KNOWN) || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
141 yes_output_this_room = 1;
145 if (yes_output_this_room) {
146 imap_mailboxname(buf, sizeof buf, qrbuf);
148 for (i=0; i<num_patterns; ++i) {
149 if (imap_mailbox_matches_pattern(patterns[i], buf)) {
154 cprintf("* %s (%s) \"/\" ", verb, return_options);
161 #define MAX_PATTERNS 20
164 * Implements the LIST and LSUB commands
166 void imap_list(int num_parms, char *parms[])
168 int subscribed_rooms_only = 0;
170 int i, j, paren_nest;
171 char *data_for_callback[5];
172 int num_patterns = 1;
173 char *patterns[MAX_PATTERNS];
174 int selection_left = (-1);
175 int selection_right = (-1);
176 int return_left = (-1);
177 int return_right = (-1);
179 int patterns_left = 3;
180 int patterns_right = 3;
181 int extended_list_in_use = 0;
182 int return_subscribed = 0;
185 cprintf("%s BAD arguments invalid\r\n", parms[0]);
189 /* parms[1] is the IMAP verb being used (e.g. LIST or LSUB)
190 * This tells us how to behave, and what verb to return back to the caller
192 safestrncpy(verb, parms[1], sizeof verb);
194 for (i=0; i<j; ++i) {
195 verb[i] = toupper(verb[i]);
198 if (!strcasecmp(verb, "LSUB")) {
199 subscribed_rooms_only = 1;
203 * In order to implement draft-ietf-imapext-list-extensions-18
204 * ("LIST Command Extensions") we need to:
205 * 1. Extract "selection options" (DONE, but we don't do anything with it yet. We
206 * need to implement SUBSCRIBED, and RECURSIVEMATCH,
207 * and silently ignore REMOTE)
208 * 2. Extract "return options" (DONE, but so far we only implement the SUBSCRIBED
209 * option; we also need to implement the CHILDREN
211 * 3. Determine whether there is more than one match pattern (DONE)
215 * If parameter 2 begins with a '(' character, the client is specifying
216 * selection options. Extract their exact position, and then modify our
217 * expectation of where the root folder will be specified.
219 if (parms[2][0] == '(') {
220 extended_list_in_use = 1;
223 for (i=2; i<num_parms; ++i) {
224 for (j=0; j<strlen(parms[i]); ++j) {
225 if (parms[i][j] == '(') ++paren_nest;
226 if (parms[i][j] == ')') --paren_nest;
228 if (paren_nest == 0) {
229 selection_right = i; /* found end of selection options */
230 root_pos = i+1; /* folder root appears after selection options */
231 i = num_parms + 1; /* break out of the loop */
236 /* The folder root appears immediately after the selection options,
237 * or in position 2 if no selection options were specified.
239 patterns_left = root_pos + 1;
240 patterns_right = root_pos + 1;
242 if (parms[patterns_left][0] == '(') {
243 extended_list_in_use = 1;
245 for (i=patterns_left; i<num_parms; ++i) {
246 for (j=0; j<strlen(parms[i]); ++j) {
247 if (parms[i][j] == '(') ++paren_nest;
248 if (parms[i][j] == ')') --paren_nest;
250 if (paren_nest == 0) {
251 patterns_right = i; /* found end of patterns */
252 i = num_parms + 1; /* break out of the loop */
255 num_patterns = patterns_right - patterns_left + 1;
256 for (i=0; i<num_patterns; ++i) {
257 if (i < MAX_PATTERNS) {
258 patterns[i] = malloc(512);
259 snprintf(patterns[i], 512, "%s%s", parms[root_pos], parms[patterns_left+i]);
261 strcpy(patterns[i], &patterns[i][1]);
263 if (i == num_patterns-1) {
264 patterns[i][strlen(patterns[i])-1] = 0;
271 patterns[0] = malloc(512);
272 snprintf(patterns[0], 512, "%s%s", parms[root_pos], parms[patterns_left]);
275 /* If the word "RETURN" appears after the folder pattern list, then the client
276 * is specifying return options.
278 if (num_parms - patterns_right > 2) if (!strcasecmp(parms[patterns_right+1], "RETURN")) {
279 return_left = patterns_right + 2;
280 extended_list_in_use = 1;
282 for (i=return_left; i<num_parms; ++i) {
283 for (j=0; j<strlen(parms[i]); ++j) {
284 if (parms[i][j] == '(') ++paren_nest;
285 if (parms[i][j] == ')') --paren_nest;
288 /* Might as well look for these while we're in here... */
289 if (parms[i][0] == '(') strcpy(parms[i], &parms[i][1]);
290 if (parms[i][strlen(parms[i])-1] == ')') parms[i][strlen(parms[i])-1] = 0;
291 lprintf(9, "evaluating <%s>\n", parms[i]);
292 if (!strcasecmp(parms[i], "SUBSCRIBED")) {
293 return_subscribed = 1;
296 if (paren_nest == 0) {
297 return_right = i; /* found end of patterns */
298 i = num_parms + 1; /* break out of the loop */
303 /* Now start setting up the data we're going to send to the ForEachRoom() callback.
305 data_for_callback[0] = (char *) verb;
306 data_for_callback[1] = (char *) subscribed_rooms_only;
307 data_for_callback[2] = (char *) num_patterns;
308 data_for_callback[3] = (char *) patterns;
309 data_for_callback[4] = (char *) return_subscribed;
311 /* The non-extended LIST command is required to treat an empty
312 * ("" string) mailbox name argument as a special request to return the
313 * hierarchy delimiter and the root name of the name given in the
314 * reference parameter.
316 if ( (strlen(patterns[0]) == 0) && (extended_list_in_use == 0) ) {
317 cprintf("* %s (\\Noselect) \"/\" \"\"\r\n", verb);
320 /* Non-empty mailbox names, and any form of the extended LIST command,
321 * is handled by this loop.
324 imap_list_floors(verb, num_patterns, patterns);
325 ForEachRoom(imap_listroom, data_for_callback);
329 * Free the pattern buffers we allocated above.
331 for (i=0; i<num_patterns; ++i) {
335 cprintf("%s OK %s completed\r\n", parms[0], verb);