Did away with lprintf all together now its called CtdlLogPrintf()
[citadel.git] / citadel / modules / imap / imap_list.c
1 /*
2  * $Id$
3  *
4  * Implements the LIST and LSUB commands.
5  *
6  * Copyright (C) 2000-2007 by Art Cancro and others.
7  * This code is released under the terms of the GNU General Public License.
8  *
9  */
10
11 #include "sysdep.h"
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <stdio.h>
15 #include <fcntl.h>
16 #include <signal.h>
17 #include <pwd.h>
18 #include <errno.h>
19 #include <sys/types.h>
20
21 #if TIME_WITH_SYS_TIME
22 # include <sys/time.h>
23 # include <time.h>
24 #else
25 # if HAVE_SYS_TIME_H
26 #  include <sys/time.h>
27 # else
28 #  include <time.h>
29 # endif
30 #endif
31
32 #include <sys/wait.h>
33 #include <ctype.h>
34 #include <string.h>
35 #include <limits.h>
36 #include <libcitadel.h>
37 #include "citadel.h"
38 #include "server.h"
39 #include "sysdep_decls.h"
40 #include "citserver.h"
41 #include "support.h"
42 #include "config.h"
43 #include "room_ops.h"
44 #include "user_ops.h"
45 #include "policy.h"
46 #include "database.h"
47 #include "msgbase.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"
54 #include "imap_acl.h"
55 #include "imap_misc.h"
56 #include "imap_list.h"
57 #include "ctdl_module.h"
58
59
60 /*
61  * Used by LIST and LSUB to show the floors in the listing
62  */
63 void imap_list_floors(char *verb, int num_patterns, char **patterns)
64 {
65         int i;
66         struct floor *fl;
67         int j = 0;
68         int match = 0;
69
70         for (i = 0; i < MAXFLOORS; ++i) {
71                 fl = cgetfloor(i);
72                 if (fl->f_flags & F_INUSE) {
73                         match = 0;
74                         for (j=0; j<num_patterns; ++j) {
75                                 if (imap_mailbox_matches_pattern (patterns[j], fl->f_name)) {
76                                         match = 1;
77                                 }
78                         }
79                         if (match) {
80                                 cprintf("* %s (\\NoSelect \\HasChildren) \"/\" ", verb);
81                                 imap_strout(fl->f_name);
82                                 cprintf("\r\n");
83                         }
84                 }
85         }
86 }
87
88
89 /*
90  * Back end for imap_list()
91  *
92  * Implementation note: IMAP "subscribed folder" is equivalent to Citadel "known room"
93  *
94  * The "user data" field is actually an array of pointers; see below for the breakdown
95  *
96  */
97 void imap_listroom(struct ctdlroom *qrbuf, void *data)
98 {
99         char buf[SIZ];
100         char return_options[256];
101         int ra;
102         int yes_output_this_room;
103
104         char **data_for_callback;
105         char *verb;
106         int subscribed_rooms_only;
107         int num_patterns;
108         char **patterns;
109         int return_subscribed = 0;
110         int return_children = 0;
111         int return_metadata = 0;
112         int i = 0;
113         int match = 0;
114
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];
122         return_children = (int) data_for_callback[5];
123         return_metadata = (int) data_for_callback[6];
124
125         /* Only list rooms to which the user has access!! */
126         yes_output_this_room = 0;
127         strcpy(return_options, "");
128         CtdlRoomAccess(qrbuf, &CC->user, &ra, NULL);
129
130         if (return_subscribed) {
131                 if (ra & UA_KNOWN) {
132                         strcat(return_options, "\\Subscribed");
133                 }
134         }
135
136         /* Warning: ugly hack.
137          * We don't have any way to determine the presence of child mailboxes
138          * without refactoring this entire module.  So we're just going to return
139          * the \HasChildren attribute for every room.
140          * We'll fix this later when we have time.
141          */
142         if (return_children) {
143                 if (!IsEmptyStr(return_options)) {
144                         strcat(return_options, " ");
145                 }
146                 strcat(return_options, "\\HasChildren");
147         }
148
149         if (subscribed_rooms_only) {
150                 if (ra & UA_KNOWN) {
151                         yes_output_this_room = 1;
152                 }
153         }
154         else {
155                 if ((ra & UA_KNOWN) || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
156                         yes_output_this_room = 1;
157                 }
158         }
159
160         if (yes_output_this_room) {
161                 imap_mailboxname(buf, sizeof buf, qrbuf);
162                 match = 0;
163                 for (i=0; i<num_patterns; ++i) {
164                         if (imap_mailbox_matches_pattern(patterns[i], buf)) {
165                                 match = 1;
166                         }
167                 }
168                 if (match) {
169                         cprintf("* %s (%s) \"/\" ", verb, return_options);
170                         imap_strout(buf);
171
172                         if (return_metadata) {
173                                 cprintf(" (METADATA ())");      /* FIXME */
174                         }
175
176                         cprintf("\r\n");
177                 }
178         }
179 }
180
181
182 /*
183  * Implements the LIST and LSUB commands
184  */
185 void imap_list(int num_parms, char *parms[])
186 {
187         int subscribed_rooms_only = 0;
188         char verb[16];
189         int i, j, paren_nest;
190         char *data_for_callback[7];
191         int num_patterns = 1;
192         char *patterns[MAX_PATTERNS];
193         int selection_left = (-1);
194         int selection_right = (-1);
195         int return_left = (-1);
196         int return_right = (-1);
197         int root_pos = 2;
198         int patterns_left = 3;
199         int patterns_right = 3;
200         int extended_list_in_use = 0;
201         int return_subscribed = 0;
202         int return_children = 0;
203         int return_metadata = 0;
204         int select_metadata_left = (-1);
205         int select_metadata_right = (-1);
206         int select_metadata_nest = 0;
207
208         if (num_parms < 4) {
209                 cprintf("%s BAD arguments invalid\r\n", parms[0]);
210                 return;
211         }
212
213         /* parms[1] is the IMAP verb being used (e.g. LIST or LSUB)
214          * This tells us how to behave, and what verb to return back to the caller
215          */
216         safestrncpy(verb, parms[1], sizeof verb);
217         j = strlen(verb);
218         for (i=0; i<j; ++i) {
219                 verb[i] = toupper(verb[i]);
220         }
221
222         if (!strcasecmp(verb, "LSUB")) {
223                 subscribed_rooms_only = 1;
224         }
225
226         /*
227          * In order to implement draft-ietf-imapext-list-extensions-18
228          * ("LIST Command Extensions") we need to:
229          *
230          * 1. Extract "selection options"
231          *                              (Extraction: done
232          *                              SUBSCRIBED option: done
233          *                              RECURSIVEMATCH option: not done yet
234          *                              REMOTE: safe to silently ignore)
235          *
236          * 2. Extract "return options"
237          *                              (Extraction: done
238          *                              SUBSCRIBED option: done
239          *                              CHILDREN option: done, but needs a non-ugly rewrite)
240          *
241          * 3. Determine whether there is more than one match pattern (done)
242          */
243
244         /*
245          * If parameter 2 begins with a '(' character, the client is specifying
246          * selection options.  Extract their exact position, and then modify our
247          * expectation of where the root folder will be specified.
248          */
249         if (parms[2][0] == '(') {
250                 extended_list_in_use = 1;
251                 selection_left = 2;
252                 paren_nest = 0;
253                 for (i=2; i<num_parms; ++i) {
254                         for (j=0; parms[i][j]; ++j) {
255                                 if (parms[i][j] == '(') ++paren_nest;
256                                 if (parms[i][j] == ')') --paren_nest;
257                         }
258                         if (paren_nest == 0) {
259                                 selection_right = i;    /* found end of selection options */
260                                 root_pos = i+1;         /* folder root appears after selection options */
261                                 i = num_parms + 1;      /* break out of the loop */
262                         }
263                 }
264         }
265
266         /* If selection options were found, do something with them.
267          */
268         if ((selection_left > 0) && (selection_right >= selection_left)) {
269
270                 /* Strip off the outer parentheses */
271                 if (parms[selection_left][0] == '(') {
272                         strcpy(parms[selection_left], &parms[selection_left][1]);
273                 }
274                 if (parms[selection_right][strlen(parms[selection_right])-1] == ')') {
275                         parms[selection_right][strlen(parms[selection_right])-1] = 0;
276                 }
277
278                 for (i=selection_left; i<=selection_right; ++i) {
279
280                         /* are we in the middle of a metadata select block? */
281                         if ((select_metadata_left >= 0) && (select_metadata_right < 0)) {
282                                 select_metadata_nest += haschar(parms[i], '(') - haschar(parms[i], ')') ;
283                                 if (select_metadata_nest == 0) {
284                                         select_metadata_right = i;
285                                 }
286                         }
287
288                         else if (!strcasecmp(parms[i], "METADATA")) {
289                                 select_metadata_left = i+1;
290                                 select_metadata_nest = 0;
291                         }
292
293                         else if (!strcasecmp(parms[i], "SUBSCRIBED")) {
294                                 subscribed_rooms_only = 1;
295                         }
296
297                         else if (!strcasecmp(parms[i], "RECURSIVEMATCH")) {
298                                 /* FIXME - do this! */
299                         }
300
301                 }
302
303         }
304
305         CtdlLogPrintf(CTDL_DEBUG, "select metadata: %d to %d\n", select_metadata_left, select_metadata_right);
306         /* FIXME blah, we have to do something with this */
307
308         /* The folder root appears immediately after the selection options,
309          * or in position 2 if no selection options were specified.
310          */
311         patterns_left = root_pos + 1;
312         patterns_right = root_pos + 1;
313
314         if (parms[patterns_left][0] == '(') {
315                 extended_list_in_use = 1;
316                 paren_nest = 0;
317                 for (i=patterns_left; i<num_parms; ++i) {
318                         for (j=0; &parms[i][j]; ++j) {
319                                 if (parms[i][j] == '(') ++paren_nest;
320                                 if (parms[i][j] == ')') --paren_nest;
321                         }
322                         if (paren_nest == 0) {
323                                 patterns_right = i;     /* found end of patterns */
324                                 i = num_parms + 1;      /* break out of the loop */
325                         }
326                 }
327                 num_patterns = patterns_right - patterns_left + 1;
328                 for (i=0; i<num_patterns; ++i) {
329                         if (i < MAX_PATTERNS) {
330                                 patterns[i] = malloc(512);
331                                 snprintf(patterns[i], 512, "%s%s", parms[root_pos], parms[patterns_left+i]);
332                                 if (i == 0) {
333                                         strcpy(patterns[i], &patterns[i][1]);
334                                 }
335                                 if (i == num_patterns-1) {
336                                         patterns[i][strlen(patterns[i])-1] = 0;
337                                 }
338                         }
339                 }
340         }
341         else {
342                 num_patterns = 1;
343                 patterns[0] = malloc(512);
344                 snprintf(patterns[0], 512, "%s%s", parms[root_pos], parms[patterns_left]);
345         }
346
347         /* If the word "RETURN" appears after the folder pattern list, then the client
348          * is specifying return options.
349          */
350         if (num_parms - patterns_right > 2) if (!strcasecmp(parms[patterns_right+1], "RETURN")) {
351                 return_left = patterns_right + 2;
352                 extended_list_in_use = 1;
353                 paren_nest = 0;
354                 for (i=return_left; i<num_parms; ++i) {
355                         for (j=0; parms[i][j]; ++j) {
356                                 if (parms[i][j] == '(') ++paren_nest;
357                                 if (parms[i][j] == ')') --paren_nest;
358                         }
359
360                         /* Might as well look for these while we're in here... */
361                         if (parms[i][0] == '(') strcpy(parms[i], &parms[i][1]);
362                         if (parms[i][strlen(parms[i])-1] == ')') parms[i][strlen(parms[i])-1] = 0;
363                         CtdlLogPrintf(9, "evaluating <%s>\n", parms[i]);
364
365                         if (!strcasecmp(parms[i], "SUBSCRIBED")) {
366                                 return_subscribed = 1;
367                         }
368
369                         else if (!strcasecmp(parms[i], "CHILDREN")) {
370                                 return_children = 1;
371                         }
372
373                         else if (!strcasecmp(parms[i], "METADATA")) {
374                                 return_metadata = 1;
375                         }
376
377                         if (paren_nest == 0) {
378                                 return_right = i;       /* found end of patterns */
379                                 i = num_parms + 1;      /* break out of the loop */
380                         }
381                 }
382         }
383
384         /* Now start setting up the data we're going to send to the ForEachRoom() callback.
385          */
386         data_for_callback[0] = (char *) verb;
387         data_for_callback[1] = (char *) subscribed_rooms_only;
388         data_for_callback[2] = (char *) num_patterns;
389         data_for_callback[3] = (char *) patterns;
390         data_for_callback[4] = (char *) return_subscribed;
391         data_for_callback[5] = (char *) return_children;
392         data_for_callback[6] = (char *) return_metadata;
393
394         /* The non-extended LIST command is required to treat an empty
395          * ("" string) mailbox name argument as a special request to return the
396          * hierarchy delimiter and the root name of the name given in the
397          * reference parameter.
398          */
399         if ( (IsEmptyStr(patterns[0])) && (extended_list_in_use == 0) ) {
400                 cprintf("* %s (\\Noselect) \"/\" \"\"\r\n", verb);
401         }
402
403         /* Non-empty mailbox names, and any form of the extended LIST command,
404          * is handled by this loop.
405          */
406         else {
407                 imap_list_floors(verb, num_patterns, patterns);
408                 ForEachRoom(imap_listroom, data_for_callback);
409         }
410
411         /* 
412          * Free the pattern buffers we allocated above.
413          */
414         for (i=0; i<num_patterns; ++i) {
415                 free(patterns[i]);
416         }
417
418         cprintf("%s OK %s completed\r\n", parms[0], verb);
419 }