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>
36 #include <libcitadel.h>
39 #include "sysdep_decls.h"
40 #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"
57 #include "ctdl_module.h"
61 * Used by LIST and LSUB to show the floors in the listing
63 void imap_list_floors(char *verb
, int num_patterns
, char **patterns
)
70 for (i
= 0; i
< MAXFLOORS
; ++i
) {
72 if (fl
->f_flags
& F_INUSE
) {
74 for (j
=0; j
<num_patterns
; ++j
) {
75 if (imap_mailbox_matches_pattern (patterns
[j
], fl
->f_name
)) {
80 cprintf("* %s (\\NoSelect \\HasChildren) \"/\" ", verb
);
81 imap_strout(fl
->f_name
);
90 * Back end for imap_list()
92 * Implementation note: IMAP "subscribed folder" is equivalent to Citadel "known room"
94 * The "user data" field is actually an array of pointers; see below for the breakdown
97 void imap_listroom(struct ctdlroom
*qrbuf
, void *data
)
100 char return_options
[256];
102 int yes_output_this_room
;
104 char **data_for_callback
;
106 int subscribed_rooms_only
;
109 int return_subscribed
= 0;
110 int return_children
= 0;
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];
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 /* Warning: ugly hack.
135 * We don't have any way to determine the presence of child mailboxes
136 * without refactoring this entire module. So we're just going to return
137 * the \HasChildren attribute for every room.
138 * We'll fix this later when we have time.
140 if (return_children
) {
141 if (!IsEmptyStr(return_options
)) {
142 strcat(return_options
, " ");
144 strcat(return_options
, "\\HasChildren");
147 if (subscribed_rooms_only
) {
149 yes_output_this_room
= 1;
153 if ((ra
& UA_KNOWN
) || ((ra
& UA_GOTOALLOWED
) && (ra
& UA_ZAPPED
))) {
154 yes_output_this_room
= 1;
158 if (yes_output_this_room
) {
159 imap_mailboxname(buf
, sizeof buf
, qrbuf
);
161 for (i
=0; i
<num_patterns
; ++i
) {
162 if (imap_mailbox_matches_pattern(patterns
[i
], buf
)) {
167 cprintf("* %s (%s) \"/\" ", verb
, return_options
);
176 * Implements the LIST and LSUB commands
178 void imap_list(int num_parms
, char *parms
[])
180 int subscribed_rooms_only
= 0;
182 int i
, j
, paren_nest
;
183 char *data_for_callback
[6];
184 int num_patterns
= 1;
185 char *patterns
[MAX_PATTERNS
];
186 int selection_left
= (-1);
187 int selection_right
= (-1);
188 int return_left
= (-1);
189 int return_right
= (-1);
191 int patterns_left
= 3;
192 int patterns_right
= 3;
193 int extended_list_in_use
= 0;
194 int return_subscribed
= 0;
195 int return_children
= 0;
198 cprintf("%s BAD arguments invalid\r\n", parms
[0]);
202 /* parms[1] is the IMAP verb being used (e.g. LIST or LSUB)
203 * This tells us how to behave, and what verb to return back to the caller
205 safestrncpy(verb
, parms
[1], sizeof verb
);
207 for (i
=0; i
<j
; ++i
) {
208 verb
[i
] = toupper(verb
[i
]);
211 if (!strcasecmp(verb
, "LSUB")) {
212 subscribed_rooms_only
= 1;
216 * Partial implementation of LIST-EXTENDED (which will not get used because
217 * we don't advertise it in our capabilities string). Several requirements:
219 * Extraction of selection options:
220 * SUBSCRIBED option: done
221 * RECURSIVEMATCH option: not done yet
222 * REMOTE: safe to silently ignore
224 * Extraction of return options:
225 * SUBSCRIBED option: done
226 * CHILDREN option: done, but needs a non-ugly rewrite
228 * Multiple match patterns: done
232 * If parameter 2 begins with a '(' character, the client is specifying
233 * selection options. Extract their exact position, and then modify our
234 * expectation of where the root folder will be specified.
236 if (parms
[2][0] == '(') {
237 extended_list_in_use
= 1;
240 for (i
=2; i
<num_parms
; ++i
) {
241 for (j
=0; parms
[i
][j
]; ++j
) {
242 if (parms
[i
][j
] == '(') ++paren_nest
;
243 if (parms
[i
][j
] == ')') --paren_nest
;
245 if (paren_nest
== 0) {
246 selection_right
= i
; /* found end of selection options */
247 root_pos
= i
+1; /* folder root appears after selection options */
248 i
= num_parms
+ 1; /* break out of the loop */
253 /* If selection options were found, do something with them.
255 if ((selection_left
> 0) && (selection_right
>= selection_left
)) {
257 /* Strip off the outer parentheses */
258 if (parms
[selection_left
][0] == '(') {
259 strcpy(parms
[selection_left
], &parms
[selection_left
][1]);
261 if (parms
[selection_right
][strlen(parms
[selection_right
])-1] == ')') {
262 parms
[selection_right
][strlen(parms
[selection_right
])-1] = 0;
265 for (i
=selection_left
; i
<=selection_right
; ++i
) {
267 if (!strcasecmp(parms
[i
], "SUBSCRIBED")) {
268 subscribed_rooms_only
= 1;
271 else if (!strcasecmp(parms
[i
], "RECURSIVEMATCH")) {
272 /* FIXME - do this! */
279 /* The folder root appears immediately after the selection options,
280 * or in position 2 if no selection options were specified.
282 patterns_left
= root_pos
+ 1;
283 patterns_right
= root_pos
+ 1;
285 if (parms
[patterns_left
][0] == '(') {
286 extended_list_in_use
= 1;
288 for (i
=patterns_left
; i
<num_parms
; ++i
) {
289 for (j
=0; &parms
[i
][j
]; ++j
) {
290 if (parms
[i
][j
] == '(') ++paren_nest
;
291 if (parms
[i
][j
] == ')') --paren_nest
;
293 if (paren_nest
== 0) {
294 patterns_right
= i
; /* found end of patterns */
295 i
= num_parms
+ 1; /* break out of the loop */
298 num_patterns
= patterns_right
- patterns_left
+ 1;
299 for (i
=0; i
<num_patterns
; ++i
) {
300 if (i
< MAX_PATTERNS
) {
301 patterns
[i
] = malloc(512);
302 snprintf(patterns
[i
], 512, "%s%s", parms
[root_pos
], parms
[patterns_left
+i
]);
304 strcpy(patterns
[i
], &patterns
[i
][1]);
306 if (i
== num_patterns
-1) {
307 patterns
[i
][strlen(patterns
[i
])-1] = 0;
314 patterns
[0] = malloc(512);
315 snprintf(patterns
[0], 512, "%s%s", parms
[root_pos
], parms
[patterns_left
]);
318 /* If the word "RETURN" appears after the folder pattern list, then the client
319 * is specifying return options.
321 if (num_parms
- patterns_right
> 2) if (!strcasecmp(parms
[patterns_right
+1], "RETURN")) {
322 return_left
= patterns_right
+ 2;
323 extended_list_in_use
= 1;
325 for (i
=return_left
; i
<num_parms
; ++i
) {
326 for (j
=0; parms
[i
][j
]; ++j
) {
327 if (parms
[i
][j
] == '(') ++paren_nest
;
328 if (parms
[i
][j
] == ')') --paren_nest
;
331 /* Might as well look for these while we're in here... */
332 if (parms
[i
][0] == '(') strcpy(parms
[i
], &parms
[i
][1]);
333 if (parms
[i
][strlen(parms
[i
])-1] == ')') parms
[i
][strlen(parms
[i
])-1] = 0;
334 CtdlLogPrintf(9, "evaluating <%s>\n", parms
[i
]);
336 if (!strcasecmp(parms
[i
], "SUBSCRIBED")) {
337 return_subscribed
= 1;
340 else if (!strcasecmp(parms
[i
], "CHILDREN")) {
344 if (paren_nest
== 0) {
345 return_right
= i
; /* found end of patterns */
346 i
= num_parms
+ 1; /* break out of the loop */
351 /* Now start setting up the data we're going to send to the ForEachRoom() callback.
353 data_for_callback
[0] = (char *) verb
;
354 data_for_callback
[1] = (char *) subscribed_rooms_only
;
355 data_for_callback
[2] = (char *) num_patterns
;
356 data_for_callback
[3] = (char *) patterns
;
357 data_for_callback
[4] = (char *) return_subscribed
;
358 data_for_callback
[5] = (char *) return_children
;
360 /* The non-extended LIST command is required to treat an empty
361 * ("" string) mailbox name argument as a special request to return the
362 * hierarchy delimiter and the root name of the name given in the
363 * reference parameter.
365 if ( (IsEmptyStr(patterns
[0])) && (extended_list_in_use
== 0) ) {
366 cprintf("* %s (\\Noselect) \"/\" \"\"\r\n", verb
);
369 /* Non-empty mailbox names, and any form of the extended LIST command,
370 * is handled by this loop.
373 imap_list_floors(verb
, num_patterns
, patterns
);
374 ForEachRoom(imap_listroom
, data_for_callback
);
378 * Free the pattern buffers we allocated above.
380 for (i
=0; i
<num_patterns
; ++i
) {
384 cprintf("%s OK %s completed\r\n", parms
[0], verb
);