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)
103 int yes_output_this_room;
105 char **data_for_callback;
107 int subscribed_rooms_only;
113 /* Here's how we break down the array of pointers passed to us */
114 data_for_callback = data;
115 verb = data_for_callback[0];
116 subscribed_rooms_only = (int) data_for_callback[1];
117 num_patterns = (int) data_for_callback[2];
118 patterns = (char **) data_for_callback[3];
120 /* Only list rooms to which the user has access!! */
121 yes_output_this_room = 0;
122 CtdlRoomAccess(qrbuf, &CC->user, &ra, NULL);
124 if (subscribed_rooms_only) {
126 yes_output_this_room = 1;
130 if ((ra & UA_KNOWN) || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
131 yes_output_this_room = 1;
135 if (yes_output_this_room) {
136 imap_mailboxname(buf, sizeof buf, qrbuf);
138 for (i=0; i<num_patterns; ++i) {
139 if (imap_mailbox_matches_pattern(patterns[i], buf)) {
144 cprintf("* %s () \"/\" ", verb);
151 #define MAX_PATTERNS 20
154 * Implements the LIST and LSUB commands
156 void imap_list(int num_parms, char *parms[])
158 int subscribed_rooms_only = 0;
160 int i, j, paren_nest;
161 char *data_for_callback[3];
162 int num_patterns = 1;
163 char *patterns[MAX_PATTERNS];
164 int selection_left = (-1);
165 int selection_right = (-1);
167 int patterns_left = 3;
168 int patterns_right = 3;
169 int extended_list_in_use = 0;
172 cprintf("%s BAD arguments invalid\r\n", parms[0]);
176 /* parms[1] is the IMAP verb being used (e.g. LIST or LSUB)
177 * This tells us how to behave, and what verb to return back to the caller
179 safestrncpy(verb, parms[1], sizeof verb);
181 for (i=0; i<j; ++i) {
182 verb[i] = toupper(verb[i]);
185 if (!strcasecmp(verb, "LSUB")) {
186 subscribed_rooms_only = 1;
190 * In order to implement draft-ietf-imapext-list-extensions-18
191 * ("LIST Command Extensions") we need to:
192 * 1. Extract "selection options" (DONE, but we don't do anything with it yet)
193 * 2. Extract "return options"
194 * 3. Determine whether there is more than one match pattern (DONE)
198 * If parameter 2 begins with a '(' character, the client is specifying
199 * selection options. Extract their exact position, and then modify our
200 * expectation of where the root folder will be specified.
202 if (parms[2][0] == '(') {
203 extended_list_in_use = 1;
206 for (i=2; i<num_parms; ++i) {
207 for (j=0; j<strlen(parms[i]); ++j) {
208 if (parms[i][j] == '(') ++paren_nest;
209 if (parms[i][j] == ')') --paren_nest;
211 if (paren_nest == 0) {
212 selection_right = i; /* found end of selection options */
213 root_pos = i+1; /* folder root appears after selection options */
214 i = num_parms + 1; /* break out of the loop */
219 /* The folder root appears immediately after the selection options,
220 * or in position 2 if no selection options were specified.
222 patterns_left = root_pos + 1;
223 patterns_right = root_pos + 1;
225 if (parms[patterns_left][0] == '(') {
226 extended_list_in_use = 1;
228 for (i=patterns_left; i<num_parms; ++i) {
229 for (j=0; j<strlen(parms[i]); ++j) {
230 if (parms[i][j] == '(') ++paren_nest;
231 if (parms[i][j] == ')') --paren_nest;
233 if (paren_nest == 0) {
234 patterns_right = i; /* found end of patterns */
235 i = num_parms + 1; /* break out of the loop */
238 num_patterns = patterns_right - patterns_left + 1;
239 for (i=0; i<num_patterns; ++i) {
240 if (i < MAX_PATTERNS) {
241 patterns[i] = malloc(512);
242 snprintf(patterns[i], 512, "%s%s", parms[root_pos], parms[patterns_left+i]);
244 strcpy(patterns[i], &patterns[i][1]);
246 if (i == num_patterns-1) {
247 patterns[i][strlen(patterns[i])-1] = 0;
254 patterns[0] = malloc(512);
255 snprintf(patterns[0], 512, "%s%s", parms[root_pos], parms[patterns_left]);
258 data_for_callback[0] = verb;
259 data_for_callback[1] = (char *) subscribed_rooms_only;
260 data_for_callback[2] = (char *) num_patterns;
261 data_for_callback[3] = (char *) patterns;
263 /* The non-extended LIST command is required to treat an empty
264 * ("" string) mailbox name argument as a special request to return the
265 * hierarchy delimiter and the root name of the name given in the
266 * reference parameter.
268 if ( (strlen(patterns[0]) == 0) && (extended_list_in_use == 0) ) {
269 cprintf("* %s (\\Noselect) \"/\" \"\"\r\n", verb);
272 /* Non-empty mailbox names, and any form of the extended LIST command,
273 * is handled by this loop.
276 imap_list_floors(verb, num_patterns, patterns);
277 ForEachRoom(imap_listroom, data_for_callback);
281 * Free the pattern buffers we allocated above.
283 for (i=0; i<num_patterns; ++i) {
287 cprintf("%s OK %s completed\r\n", parms[0], verb);