stable now but there are GIANT PIECES MISSING
[citadel.git] / citadel / modules / imap / imap_list.c
index ad01e6c0980504200097988fbe054b877704adb9..1be8bab29bed6275d1c8489cb486505cf9cf623c 100644 (file)
@@ -1,11 +1,21 @@
 /*
- * $Id$
- *
  * Implements the LIST and LSUB commands.
  *
- * Copyright (C) 2000-2007 by Art Cancro and others.
- * This code is released under the terms of the GNU General Public License.
+ * Copyright (c) 2000-2017 by Art Cancro and others.
+ *
+ *  This program is open source software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
  *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
 #include "sysdep.h"
 #include <pwd.h>
 #include <errno.h>
 #include <sys/types.h>
-
-#if TIME_WITH_SYS_TIME
-# include <sys/time.h>
-# include <time.h>
-#else
-# if HAVE_SYS_TIME_H
-#  include <sys/time.h>
-# else
-#  include <time.h>
-# endif
-#endif
-
+#include <time.h>
 #include <sys/wait.h>
 #include <ctype.h>
 #include <string.h>
 #include <limits.h>
+#include <libcitadel.h>
 #include "citadel.h"
 #include "server.h"
 #include "sysdep_decls.h"
 #include "citserver.h"
 #include "support.h"
 #include "config.h"
-#include "room_ops.h"
 #include "user_ops.h"
-#include "policy.h"
 #include "database.h"
 #include "msgbase.h"
-#include "tools.h"
 #include "internet_addressing.h"
 #include "serv_imap.h"
 #include "imap_tools.h"
 #include "imap_acl.h"
 #include "imap_misc.h"
 #include "imap_list.h"
+#include "ctdl_module.h"
+
+
+typedef struct __ImapRoomListFilter {
+       char verb[16];
+       int subscribed_rooms_only;
+       int return_subscribed;
+       int return_children;
 
+       int num_patterns;
+       int num_patterns_avail;
+       StrBuf **patterns;
+}ImapRoomListFilter;
 
 /*
  * Used by LIST and LSUB to show the floors in the listing
  */
-void imap_list_floors(char *verb, int num_patterns, char **patterns)
+void imap_list_floors(char *verb, int num_patterns, StrBuf **patterns)
 {
        int i;
        struct floor *fl;
@@ -67,18 +76,18 @@ void imap_list_floors(char *verb, int num_patterns, char **patterns)
        int match = 0;
 
        for (i = 0; i < MAXFLOORS; ++i) {
-               fl = cgetfloor(i);
+               fl = CtdlGetCachedFloor(i);
                if (fl->f_flags & F_INUSE) {
                        match = 0;
                        for (j=0; j<num_patterns; ++j) {
-                               if (imap_mailbox_matches_pattern (patterns[j], fl->f_name)) {
+                               if (imap_mailbox_matches_pattern (ChrPtr(patterns[j]), fl->f_name)) {
                                        match = 1;
                                }
                        }
                        if (match) {
-                               cprintf("* %s (\\NoSelect \\HasChildren) \"/\" ", verb);
-                               imap_strout(fl->f_name);
-                               cprintf("\r\n");
+                               IAPrintf("* %s (\\NoSelect \\HasChildren) \"/\" ", verb);
+                               IPutStr(fl->f_name, (fl->f_name)?strlen(fl->f_name):0);
+                               IAPuts("\r\n");
                        }
                }
        }
@@ -95,40 +104,30 @@ void imap_list_floors(char *verb, int num_patterns, char **patterns)
  */
 void imap_listroom(struct ctdlroom *qrbuf, void *data)
 {
-       char buf[SIZ];
+#define SUBSCRIBED_STR "\\Subscribed"
+#define HASCHILD_STR "\\HasChildren"
+       char MailboxName[SIZ];
        char return_options[256];
        int ra;
        int yes_output_this_room;
-
-       char **data_for_callback;
-       char *verb;
-       int subscribed_rooms_only;
-       int num_patterns;
-       char **patterns;
-       int return_subscribed;
-       int return_children;
-       int return_metadata;
+       ImapRoomListFilter *ImapFilter;
        int i = 0;
        int match = 0;
+       int ROLen;
 
        /* Here's how we break down the array of pointers passed to us */
-       data_for_callback = data;
-       verb = data_for_callback[0];
-       subscribed_rooms_only = (int) data_for_callback[1];
-       num_patterns = (int) data_for_callback[2];
-       patterns = (char **) data_for_callback[3];
-       return_subscribed = (int) data_for_callback[4];
-       return_children = (int) data_for_callback[5];
-       return_metadata = (int) data_for_callback[6];
+       ImapFilter = (ImapRoomListFilter*)data;
 
        /* Only list rooms to which the user has access!! */
        yes_output_this_room = 0;
-       strcpy(return_options, "");
+       *return_options = '\0';
+       ROLen = 0;
        CtdlRoomAccess(qrbuf, &CC->user, &ra, NULL);
 
-       if (return_subscribed) {
+       if (ImapFilter->return_subscribed) {
                if (ra & UA_KNOWN) {
-                       strcat(return_options, "\\Subscribed");
+                       memcpy(return_options, HKEY(SUBSCRIBED_STR) + 1);
+                       ROLen += sizeof(SUBSCRIBED_STR) - 1;
                }
        }
 
@@ -138,14 +137,15 @@ void imap_listroom(struct ctdlroom *qrbuf, void *data)
         * the \HasChildren attribute for every room.
         * We'll fix this later when we have time.
         */
-       if (return_children) {
+       if (ImapFilter->return_children) {
                if (!IsEmptyStr(return_options)) {
-                       strcat(return_options, " ");
+                       memcpy(return_options + ROLen, HKEY(" "));
+                       ROLen ++;
                }
-               strcat(return_options, "\\HasChildren");
+               memcpy(return_options + ROLen, HKEY(SUBSCRIBED_STR) + 1);
        }
 
-       if (subscribed_rooms_only) {
+       if (ImapFilter->subscribed_rooms_only) {
                if (ra & UA_KNOWN) {
                        yes_output_this_room = 1;
                }
@@ -157,22 +157,18 @@ void imap_listroom(struct ctdlroom *qrbuf, void *data)
        }
 
        if (yes_output_this_room) {
-               imap_mailboxname(buf, sizeof buf, qrbuf);
+               long len;
+               len = imap_mailboxname(MailboxName, sizeof MailboxName, qrbuf);
                match = 0;
-               for (i=0; i<num_patterns; ++i) {
-                       if (imap_mailbox_matches_pattern(patterns[i], buf)) {
+               for (i=0; i<ImapFilter->num_patterns; ++i) {
+                       if (imap_mailbox_matches_pattern(ChrPtr(ImapFilter->patterns[i]), MailboxName)) {
                                match = 1;
                        }
                }
                if (match) {
-                       cprintf("* %s (%s) \"/\" ", verb, return_options);
-                       imap_strout(buf);
-
-                       if (return_metadata) {
-                               cprintf(" (METADATA ())");      /* FIXME */
-                       }
-
-                       cprintf("\r\n");
+                       IAPrintf("* %s (%s) \"/\" ", ImapFilter->verb, return_options);
+                       IPutStr(MailboxName, len);
+                       IAPuts("\r\n");
                }
        }
 }
@@ -181,63 +177,58 @@ void imap_listroom(struct ctdlroom *qrbuf, void *data)
 /*
  * Implements the LIST and LSUB commands
  */
-void imap_list(int num_parms, char *parms[])
+void imap_list(int num_parms, ConstStr *Params)
 {
-       int subscribed_rooms_only = 0;
-       char verb[16];
+       struct CitContext *CCC = CC;
+       citimap *Imap = CCCIMAP;
        int i, j, paren_nest;
-       char *data_for_callback[7];
-       int num_patterns = 1;
-       char *patterns[MAX_PATTERNS];
+       ImapRoomListFilter ImapFilter;
        int selection_left = (-1);
        int selection_right = (-1);
        int return_left = (-1);
-       int return_right = (-1);
        int root_pos = 2;
        int patterns_left = 3;
        int patterns_right = 3;
        int extended_list_in_use = 0;
-       int return_subscribed = 0;
-       int return_children = 0;
-       int return_metadata = 0;
-       int select_metadata_left = (-1);
-       int select_metadata_right = (-1);
-       int select_metadata_nest = 0;
 
        if (num_parms < 4) {
-               cprintf("%s BAD arguments invalid\r\n", parms[0]);
+               IReply("BAD arguments invalid");
                return;
        }
 
+       ImapFilter.num_patterns = 1;
+       ImapFilter.return_subscribed = 0;
+       ImapFilter.return_children = 0;
+       ImapFilter.subscribed_rooms_only = 0;
+       
+
        /* parms[1] is the IMAP verb being used (e.g. LIST or LSUB)
         * This tells us how to behave, and what verb to return back to the caller
         */
-       safestrncpy(verb, parms[1], sizeof verb);
-       j = strlen(verb);
+       safestrncpy(ImapFilter.verb, Params[1].Key, sizeof ImapFilter.verb);
+       j = Params[1].len;
        for (i=0; i<j; ++i) {
-               verb[i] = toupper(verb[i]);
+               ImapFilter.verb[i] = toupper(ImapFilter.verb[i]);
        }
 
-       if (!strcasecmp(verb, "LSUB")) {
-               subscribed_rooms_only = 1;
+       if (!strcasecmp(ImapFilter.verb, "LSUB")) {
+               ImapFilter.subscribed_rooms_only = 1;
        }
 
        /*
-        * In order to implement draft-ietf-imapext-list-extensions-18
-        * ("LIST Command Extensions") we need to:
+        * Partial implementation of LIST-EXTENDED (which will not get used because
+        * we don't advertise it in our capabilities string).  Several requirements:
         *
-        * 1. Extract "selection options"
-        *                              (Extraction: done
-        *                              SUBSCRIBED option: done
-        *                              RECURSIVEMATCH option: not done yet
-        *                              REMOTE: safe to silently ignore)
+        * Extraction of selection options:
+        *      SUBSCRIBED option: done
+        *      RECURSIVEMATCH option: not done yet
+        *      REMOTE: safe to silently ignore
         *
-        * 2. Extract "return options"
-        *                              (Extraction: done
-        *                              SUBSCRIBED option: done
-        *                              CHILDREN option: done, but needs a non-ugly rewrite)
+        * Extraction of return options:
+        *      SUBSCRIBED option: done
+        *      CHILDREN option: done, but needs a non-ugly rewrite
         *
-        * 3. Determine whether there is more than one match pattern (done)
+        * Multiple match patterns: done
         */
 
        /*
@@ -245,14 +236,14 @@ void imap_list(int num_parms, char *parms[])
         * selection options.  Extract their exact position, and then modify our
         * expectation of where the root folder will be specified.
         */
-       if (parms[2][0] == '(') {
+       if (Params[2].Key[0] == '(') {
                extended_list_in_use = 1;
                selection_left = 2;
                paren_nest = 0;
                for (i=2; i<num_parms; ++i) {
-                       for (j=0; parms[i][j]; ++j) {
-                               if (parms[i][j] == '(') ++paren_nest;
-                               if (parms[i][j] == ')') --paren_nest;
+                       for (j=0; Params[i].Key[j]; ++j) {
+                               if (Params[i].Key[j] == '(') ++paren_nest;
+                               if (Params[i].Key[j] == ')') --paren_nest;
                        }
                        if (paren_nest == 0) {
                                selection_right = i;    /* found end of selection options */
@@ -267,33 +258,24 @@ void imap_list(int num_parms, char *parms[])
        if ((selection_left > 0) && (selection_right >= selection_left)) {
 
                /* Strip off the outer parentheses */
-               if (parms[selection_left][0] == '(') {
-                       strcpy(parms[selection_left], &parms[selection_left][1]);
+               if (Params[selection_left].Key[0] == '(') {
+                       TokenCutLeft(&Imap->Cmd, 
+                                    &Params[selection_left], 
+                                    1);
                }
-               if (parms[selection_right][strlen(parms[selection_right])-1] == ')') {
-                       parms[selection_right][strlen(parms[selection_right])-1] = 0;
+               if (Params[selection_right].Key[Params[selection_right].len-1] == ')') {
+                       TokenCutRight(&Imap->Cmd, 
+                                     &Params[selection_right], 
+                                     1);
                }
 
                for (i=selection_left; i<=selection_right; ++i) {
 
-                       /* are we in the middle of a metadata select block? */
-                       if ((select_metadata_left >= 0) && (select_metadata_right < 0)) {
-                               select_metadata_nest += haschar(parms[i], '(') - haschar(parms[i], ')') ;
-                               if (select_metadata_nest == 0) {
-                                       select_metadata_right = i;
-                               }
-                       }
-
-                       else if (!strcasecmp(parms[i], "METADATA")) {
-                               select_metadata_left = i+1;
-                               select_metadata_nest = 0;
-                       }
-
-                       else if (!strcasecmp(parms[i], "SUBSCRIBED")) {
-                               subscribed_rooms_only = 1;
+                       if (!strcasecmp(Params[i].Key, "SUBSCRIBED")) {
+                               ImapFilter.subscribed_rooms_only = 1;
                        }
 
-                       else if (!strcasecmp(parms[i], "RECURSIVEMATCH")) {
+                       else if (!strcasecmp(Params[i].Key, "RECURSIVEMATCH")) {
                                /* FIXME - do this! */
                        }
 
@@ -301,118 +283,135 @@ void imap_list(int num_parms, char *parms[])
 
        }
 
-       lprintf(CTDL_DEBUG, "select metadata: %d to %d\n", select_metadata_left, select_metadata_right);
-       /* FIXME blah, we have to do something with this */
-
        /* The folder root appears immediately after the selection options,
         * or in position 2 if no selection options were specified.
         */
+       ImapFilter.num_patterns_avail = num_parms + 1;
+       ImapFilter.patterns = malloc(ImapFilter.num_patterns_avail * sizeof(StrBuf*));
+       memset(ImapFilter.patterns, 0, ImapFilter.num_patterns_avail * sizeof(StrBuf*));
+
        patterns_left = root_pos + 1;
        patterns_right = root_pos + 1;
 
-       if (parms[patterns_left][0] == '(') {
+       if (Params[patterns_left].Key[0] == '(') {
                extended_list_in_use = 1;
                paren_nest = 0;
                for (i=patterns_left; i<num_parms; ++i) {
-                       for (j=0; &parms[i][j]; ++j) {
-                               if (parms[i][j] == '(') ++paren_nest;
-                               if (parms[i][j] == ')') --paren_nest;
+                       for (j=0; &Params[i].Key[j]; ++j) {
+                               if (Params[i].Key[j] == '(') ++paren_nest;
+                               if (Params[i].Key[j] == ')') --paren_nest;
                        }
                        if (paren_nest == 0) {
                                patterns_right = i;     /* found end of patterns */
                                i = num_parms + 1;      /* break out of the loop */
                        }
                }
-               num_patterns = patterns_right - patterns_left + 1;
-               for (i=0; i<num_patterns; ++i) {
+               ImapFilter.num_patterns = patterns_right - patterns_left + 1;
+               for (i=0; i<ImapFilter.num_patterns; ++i) {
                        if (i < MAX_PATTERNS) {
-                               patterns[i] = malloc(512);
-                               snprintf(patterns[i], 512, "%s%s", parms[root_pos], parms[patterns_left+i]);
+                               ImapFilter.patterns[i] = NewStrBufPlain(NULL, 
+                                                                       Params[root_pos].len + 
+                                                                       Params[patterns_left+i].len);
                                if (i == 0) {
-                                       strcpy(patterns[i], &patterns[i][1]);
+                                       if (Params[root_pos].len > 1)
+                                               StrBufAppendBufPlain(ImapFilter.patterns[i], 
+                                                                    1 + CKEY(Params[root_pos]) - 1, 0);
                                }
-                               if (i == num_patterns-1) {
-                                       patterns[i][strlen(patterns[i])-1] = 0;
+                               else
+                                       StrBufAppendBufPlain(ImapFilter.patterns[i], 
+                                                            CKEY(Params[root_pos]), 0);
+
+                               if (i == ImapFilter.num_patterns-1) {
+                                       if (Params[patterns_left+i].len > 1)
+                                               StrBufAppendBufPlain(ImapFilter.patterns[i], 
+                                                                    CKEY(Params[patterns_left+i]) - 1, 0);
                                }
+                               else StrBufAppendBufPlain(ImapFilter.patterns[i], 
+                                                         CKEY(Params[patterns_left+i]), 0);
+
                        }
+
                }
        }
        else {
-               num_patterns = 1;
-               patterns[0] = malloc(512);
-               snprintf(patterns[0], 512, "%s%s", parms[root_pos], parms[patterns_left]);
+               ImapFilter.num_patterns = 1;
+               ImapFilter.patterns[0] = NewStrBufPlain(NULL, 
+                                                       Params[root_pos].len + 
+                                                       Params[patterns_left].len);
+               StrBufAppendBufPlain(ImapFilter.patterns[0], 
+                                    CKEY(Params[root_pos]), 0);
+               StrBufAppendBufPlain(ImapFilter.patterns[0], 
+                                    CKEY(Params[patterns_left]), 0);
        }
 
        /* If the word "RETURN" appears after the folder pattern list, then the client
         * is specifying return options.
         */
-       if (num_parms - patterns_right > 2) if (!strcasecmp(parms[patterns_right+1], "RETURN")) {
+       if (num_parms - patterns_right > 2) if (!strcasecmp(Params[patterns_right+1].Key, "RETURN")) {
                return_left = patterns_right + 2;
                extended_list_in_use = 1;
                paren_nest = 0;
                for (i=return_left; i<num_parms; ++i) {
-                       for (j=0; parms[i][j]; ++j) {
-                               if (parms[i][j] == '(') ++paren_nest;
-                               if (parms[i][j] == ')') --paren_nest;
+                       for (j=0;   Params[i].Key[j]; ++j) {
+                               if (Params[i].Key[j] == '(') ++paren_nest;
+                               if (Params[i].Key[j] == ')') --paren_nest;
                        }
 
                        /* Might as well look for these while we're in here... */
-                       if (parms[i][0] == '(') strcpy(parms[i], &parms[i][1]);
-                       if (parms[i][strlen(parms[i])-1] == ')') parms[i][strlen(parms[i])-1] = 0;
-                       lprintf(9, "evaluating <%s>\n", parms[i]);
-
-                       if (!strcasecmp(parms[i], "SUBSCRIBED")) {
-                               return_subscribed = 1;
-                       }
-
-                       else if (!strcasecmp(parms[i], "CHILDREN")) {
-                               return_children = 1;
+                       if (Params[i].Key[0] == '(') 
+                               TokenCutLeft(&Imap->Cmd, 
+                                            &Params[i], 
+                                            1);
+                       if (Params[i].Key[Params[i].len-1] == ')')
+                           TokenCutRight(&Imap->Cmd, 
+                                         &Params[i], 
+                                         1);
+
+                       syslog(LOG_DEBUG, "evaluating <%s>", Params[i].Key);
+
+                       if (!strcasecmp(Params[i].Key, "SUBSCRIBED")) {
+                               ImapFilter.return_subscribed = 1;
                        }
 
-                       else if (!strcasecmp(parms[i], "METADATA")) {
-                               return_metadata = 1;
+                       else if (!strcasecmp(Params[i].Key, "CHILDREN")) {
+                               ImapFilter.return_children = 1;
                        }
 
                        if (paren_nest == 0) {
-                               return_right = i;       /* found end of patterns */
                                i = num_parms + 1;      /* break out of the loop */
                        }
                }
        }
 
-       /* Now start setting up the data we're going to send to the ForEachRoom() callback.
+       /* Now start setting up the data we're going to send to the CtdlForEachRoom() callback.
         */
-       data_for_callback[0] = (char *) verb;
-       data_for_callback[1] = (char *) subscribed_rooms_only;
-       data_for_callback[2] = (char *) num_patterns;
-       data_for_callback[3] = (char *) patterns;
-       data_for_callback[4] = (char *) return_subscribed;
-       data_for_callback[5] = (char *) return_children;
-       data_for_callback[6] = (char *) return_metadata;
-
+       
        /* The non-extended LIST command is required to treat an empty
         * ("" string) mailbox name argument as a special request to return the
         * hierarchy delimiter and the root name of the name given in the
         * reference parameter.
         */
-       if ( (IsEmptyStr(patterns[0])) && (extended_list_in_use == 0) ) {
-               cprintf("* %s (\\Noselect) \"/\" \"\"\r\n", verb);
+       if ( (StrLength(ImapFilter.patterns[0]) == 0) && (extended_list_in_use == 0) ) {
+               IAPrintf("* %s (\\Noselect) \"/\" \"\"\r\n", ImapFilter.verb);
        }
 
        /* Non-empty mailbox names, and any form of the extended LIST command,
         * is handled by this loop.
         */
        else {
-               imap_list_floors(verb, num_patterns, patterns);
-               ForEachRoom(imap_listroom, data_for_callback);
+               imap_list_floors(ImapFilter.verb, 
+                                ImapFilter.num_patterns, 
+                                ImapFilter.patterns);
+               CtdlForEachRoom(imap_listroom, (char**)&ImapFilter);
        }
 
        /* 
         * Free the pattern buffers we allocated above.
         */
-       for (i=0; i<num_patterns; ++i) {
-               free(patterns[i]);
+       for (i=0; i<ImapFilter.num_patterns; ++i) {
+               FreeStrBuf(&ImapFilter.patterns[i]);
        }
+       free(ImapFilter.patterns);
 
-       cprintf("%s OK %s completed\r\n", parms[0], verb);
+       IReplyPrintf("OK %s completed", ImapFilter.verb);
 }