Back to index

citadel  8.12
imap_list.c
Go to the documentation of this file.
00001 /*
00002  * Implements the LIST and LSUB commands.
00003  *
00004  * Copyright (c) 2000-2009 by Art Cancro and others.
00005  *
00006  *  This program is open source software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 3 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 #include "sysdep.h"
00022 #include <stdlib.h>
00023 #include <unistd.h>
00024 #include <stdio.h>
00025 #include <fcntl.h>
00026 #include <signal.h>
00027 #include <pwd.h>
00028 #include <errno.h>
00029 #include <sys/types.h>
00030 
00031 #if TIME_WITH_SYS_TIME
00032 # include <sys/time.h>
00033 # include <time.h>
00034 #else
00035 # if HAVE_SYS_TIME_H
00036 #  include <sys/time.h>
00037 # else
00038 #  include <time.h>
00039 # endif
00040 #endif
00041 
00042 #include <sys/wait.h>
00043 #include <ctype.h>
00044 #include <string.h>
00045 #include <limits.h>
00046 #include <libcitadel.h>
00047 #include "citadel.h"
00048 #include "server.h"
00049 #include "sysdep_decls.h"
00050 #include "citserver.h"
00051 #include "support.h"
00052 #include "config.h"
00053 #include "user_ops.h"
00054 #include "database.h"
00055 #include "msgbase.h"
00056 #include "internet_addressing.h"
00057 #include "serv_imap.h"
00058 #include "imap_tools.h"
00059 #include "imap_fetch.h"
00060 #include "imap_search.h"
00061 #include "imap_store.h"
00062 #include "imap_acl.h"
00063 #include "imap_misc.h"
00064 #include "imap_list.h"
00065 #include "ctdl_module.h"
00066 
00067 
00068 typedef struct __ImapRoomListFilter {
00069        char verb[16];
00070        int subscribed_rooms_only;
00071        int return_subscribed;
00072        int return_children;
00073 
00074        int num_patterns;
00075        int num_patterns_avail;
00076        StrBuf **patterns;
00077 }ImapRoomListFilter;
00078 
00079 /*
00080  * Used by LIST and LSUB to show the floors in the listing
00081  */
00082 void imap_list_floors(char *verb, int num_patterns, StrBuf **patterns)
00083 {
00084        int i;
00085        struct floor *fl;
00086        int j = 0;
00087        int match = 0;
00088 
00089        for (i = 0; i < MAXFLOORS; ++i) {
00090               fl = CtdlGetCachedFloor(i);
00091               if (fl->f_flags & F_INUSE) {
00092                      match = 0;
00093                      for (j=0; j<num_patterns; ++j) {
00094                             if (imap_mailbox_matches_pattern (ChrPtr(patterns[j]), fl->f_name)) {
00095                                    match = 1;
00096                             }
00097                      }
00098                      if (match) {
00099                             IAPrintf("* %s (\\NoSelect \\HasChildren) \"/\" ", verb);
00100                             plain_imap_strout(fl->f_name);
00101                             IAPuts("\r\n");
00102                      }
00103               }
00104        }
00105 }
00106 
00107 
00108 /*
00109  * Back end for imap_list()
00110  *
00111  * Implementation note: IMAP "subscribed folder" is equivalent to Citadel "known room"
00112  *
00113  * The "user data" field is actually an array of pointers; see below for the breakdown
00114  *
00115  */
00116 void imap_listroom(struct ctdlroom *qrbuf, void *data)
00117 {
00118 #define SUBSCRIBED_STR "\\Subscribed"
00119 #define HASCHILD_STR "\\HasChildren"
00120        char MailboxName[SIZ];
00121        char return_options[256];
00122        int ra;
00123        int yes_output_this_room;
00124        ImapRoomListFilter *ImapFilter;
00125        int i = 0;
00126        int match = 0;
00127        int ROLen;
00128 
00129        /* Here's how we break down the array of pointers passed to us */
00130        ImapFilter = (ImapRoomListFilter*)data;
00131 
00132        /* Only list rooms to which the user has access!! */
00133        yes_output_this_room = 0;
00134        *return_options = '\0';
00135        ROLen = 0;
00136        CtdlRoomAccess(qrbuf, &CC->user, &ra, NULL);
00137 
00138        if (ImapFilter->return_subscribed) {
00139               if (ra & UA_KNOWN) {
00140                      memcpy(return_options, HKEY(SUBSCRIBED_STR) + 1);
00141                      ROLen += sizeof(SUBSCRIBED_STR) - 1;
00142               }
00143        }
00144 
00145        /* Warning: ugly hack.
00146         * We don't have any way to determine the presence of child mailboxes
00147         * without refactoring this entire module.  So we're just going to return
00148         * the \HasChildren attribute for every room.
00149         * We'll fix this later when we have time.
00150         */
00151        if (ImapFilter->return_children) {
00152               if (!IsEmptyStr(return_options)) {
00153                      memcpy(return_options + ROLen, HKEY(" "));
00154                      ROLen ++;
00155               }
00156               memcpy(return_options + ROLen, HKEY(SUBSCRIBED_STR) + 1);
00157        }
00158 
00159        if (ImapFilter->subscribed_rooms_only) {
00160               if (ra & UA_KNOWN) {
00161                      yes_output_this_room = 1;
00162               }
00163        }
00164        else {
00165               if ((ra & UA_KNOWN) || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
00166                      yes_output_this_room = 1;
00167               }
00168        }
00169 
00170        if (yes_output_this_room) {
00171               imap_mailboxname(MailboxName, sizeof MailboxName, qrbuf);
00172               match = 0;
00173               for (i=0; i<ImapFilter->num_patterns; ++i) {
00174                      if (imap_mailbox_matches_pattern(ChrPtr(ImapFilter->patterns[i]), MailboxName)) {
00175                             match = 1;
00176                      }
00177               }
00178               if (match) {
00179                      IAPrintf("* %s (%s) \"/\" ", ImapFilter->verb, return_options);
00180                      plain_imap_strout(MailboxName);
00181                      IAPuts("\r\n");
00182               }
00183        }
00184 }
00185 
00186 
00187 /*
00188  * Implements the LIST and LSUB commands
00189  */
00190 void imap_list(int num_parms, ConstStr *Params)
00191 {
00192        struct CitContext *CCC = CC;
00193        citimap *Imap = CCCIMAP;
00194        int i, j, paren_nest;
00195        ImapRoomListFilter ImapFilter;
00196        int selection_left = (-1);
00197        int selection_right = (-1);
00198        int return_left = (-1);
00199        int root_pos = 2;
00200        int patterns_left = 3;
00201        int patterns_right = 3;
00202        int extended_list_in_use = 0;
00203 
00204        if (num_parms < 4) {
00205               IReply("BAD arguments invalid");
00206               return;
00207        }
00208 
00209        ImapFilter.num_patterns = 1;
00210        ImapFilter.return_subscribed = 0;
00211        ImapFilter.return_children = 0;
00212        ImapFilter.subscribed_rooms_only = 0;
00213        
00214 
00215        /* parms[1] is the IMAP verb being used (e.g. LIST or LSUB)
00216         * This tells us how to behave, and what verb to return back to the caller
00217         */
00218        safestrncpy(ImapFilter.verb, Params[1].Key, sizeof ImapFilter.verb);
00219        j = Params[1].len;
00220        for (i=0; i<j; ++i) {
00221               ImapFilter.verb[i] = toupper(ImapFilter.verb[i]);
00222        }
00223 
00224        if (!strcasecmp(ImapFilter.verb, "LSUB")) {
00225               ImapFilter.subscribed_rooms_only = 1;
00226        }
00227 
00228        /*
00229         * Partial implementation of LIST-EXTENDED (which will not get used because
00230         * we don't advertise it in our capabilities string).  Several requirements:
00231         *
00232         * Extraction of selection options:
00233         *     SUBSCRIBED option: done
00234         *     RECURSIVEMATCH option: not done yet
00235         *     REMOTE: safe to silently ignore
00236         *
00237         * Extraction of return options:
00238         *     SUBSCRIBED option: done
00239         *     CHILDREN option: done, but needs a non-ugly rewrite
00240         *
00241         * Multiple match patterns: done
00242         */
00243 
00244        /*
00245         * If parameter 2 begins with a '(' character, the client is specifying
00246         * selection options.  Extract their exact position, and then modify our
00247         * expectation of where the root folder will be specified.
00248         */
00249        if (Params[2].Key[0] == '(') {
00250               extended_list_in_use = 1;
00251               selection_left = 2;
00252               paren_nest = 0;
00253               for (i=2; i<num_parms; ++i) {
00254                      for (j=0; Params[i].Key[j]; ++j) {
00255                             if (Params[i].Key[j] == '(') ++paren_nest;
00256                             if (Params[i].Key[j] == ')') --paren_nest;
00257                      }
00258                      if (paren_nest == 0) {
00259                             selection_right = i; /* found end of selection options */
00260                             root_pos = i+1;             /* folder root appears after selection options */
00261                             i = num_parms + 1;   /* break out of the loop */
00262                      }
00263               }
00264        }
00265 
00266        /* If selection options were found, do something with them.
00267         */
00268        if ((selection_left > 0) && (selection_right >= selection_left)) {
00269 
00270               /* Strip off the outer parentheses */
00271               if (Params[selection_left].Key[0] == '(') {
00272                      TokenCutLeft(&Imap->Cmd, 
00273                                  &Params[selection_left], 
00274                                  1);
00275               }
00276               if (Params[selection_right].Key[Params[selection_right].len-1] == ')') {
00277                      TokenCutRight(&Imap->Cmd, 
00278                                   &Params[selection_right], 
00279                                   1);
00280               }
00281 
00282               for (i=selection_left; i<=selection_right; ++i) {
00283 
00284                      if (!strcasecmp(Params[i].Key, "SUBSCRIBED")) {
00285                             ImapFilter.subscribed_rooms_only = 1;
00286                      }
00287 
00288                      else if (!strcasecmp(Params[i].Key, "RECURSIVEMATCH")) {
00289                             /* FIXME - do this! */
00290                      }
00291 
00292               }
00293 
00294        }
00295 
00296        /* The folder root appears immediately after the selection options,
00297         * or in position 2 if no selection options were specified.
00298         */
00299        ImapFilter.num_patterns_avail = num_parms + 1;
00300        ImapFilter.patterns = malloc(ImapFilter.num_patterns_avail * sizeof(StrBuf*));
00301        memset(ImapFilter.patterns, 0, ImapFilter.num_patterns_avail * sizeof(StrBuf*));
00302 
00303        patterns_left = root_pos + 1;
00304        patterns_right = root_pos + 1;
00305 
00306        if (Params[patterns_left].Key[0] == '(') {
00307               extended_list_in_use = 1;
00308               paren_nest = 0;
00309               for (i=patterns_left; i<num_parms; ++i) {
00310                      for (j=0; &Params[i].Key[j]; ++j) {
00311                             if (Params[i].Key[j] == '(') ++paren_nest;
00312                             if (Params[i].Key[j] == ')') --paren_nest;
00313                      }
00314                      if (paren_nest == 0) {
00315                             patterns_right = i;  /* found end of patterns */
00316                             i = num_parms + 1;   /* break out of the loop */
00317                      }
00318               }
00319               ImapFilter.num_patterns = patterns_right - patterns_left + 1;
00320               for (i=0; i<ImapFilter.num_patterns; ++i) {
00321                      if (i < MAX_PATTERNS) {
00322                             ImapFilter.patterns[i] = NewStrBufPlain(NULL, 
00323                                                                Params[root_pos].len + 
00324                                                                Params[patterns_left+i].len);
00325                             if (i == 0) {
00326                                    if (Params[root_pos].len > 1)
00327                                           StrBufAppendBufPlain(ImapFilter.patterns[i], 
00328                                                              1 + CKEY(Params[root_pos]) - 1, 0);
00329                             }
00330                             else
00331                                    StrBufAppendBufPlain(ImapFilter.patterns[i], 
00332                                                       CKEY(Params[root_pos]), 0);
00333 
00334                             if (i == ImapFilter.num_patterns-1) {
00335                                    if (Params[patterns_left+i].len > 1)
00336                                           StrBufAppendBufPlain(ImapFilter.patterns[i], 
00337                                                              CKEY(Params[patterns_left+i]) - 1, 0);
00338                             }
00339                             else StrBufAppendBufPlain(ImapFilter.patterns[i], 
00340                                                    CKEY(Params[patterns_left+i]), 0);
00341 
00342                      }
00343 
00344               }
00345        }
00346        else {
00347               ImapFilter.num_patterns = 1;
00348               ImapFilter.patterns[0] = NewStrBufPlain(NULL, 
00349                                                  Params[root_pos].len + 
00350                                                  Params[patterns_left].len);
00351               StrBufAppendBufPlain(ImapFilter.patterns[0], 
00352                                  CKEY(Params[root_pos]), 0);
00353               StrBufAppendBufPlain(ImapFilter.patterns[0], 
00354                                  CKEY(Params[patterns_left]), 0);
00355        }
00356 
00357        /* If the word "RETURN" appears after the folder pattern list, then the client
00358         * is specifying return options.
00359         */
00360        if (num_parms - patterns_right > 2) if (!strcasecmp(Params[patterns_right+1].Key, "RETURN")) {
00361               return_left = patterns_right + 2;
00362               extended_list_in_use = 1;
00363               paren_nest = 0;
00364               for (i=return_left; i<num_parms; ++i) {
00365                      for (j=0;   Params[i].Key[j]; ++j) {
00366                             if (Params[i].Key[j] == '(') ++paren_nest;
00367                             if (Params[i].Key[j] == ')') --paren_nest;
00368                      }
00369 
00370                      /* Might as well look for these while we're in here... */
00371                      if (Params[i].Key[0] == '(') 
00372                             TokenCutLeft(&Imap->Cmd, 
00373                                         &Params[i], 
00374                                         1);
00375                      if (Params[i].Key[Params[i].len-1] == ')')
00376                          TokenCutRight(&Imap->Cmd, 
00377                                      &Params[i], 
00378                                      1);
00379 
00380                      IMAP_syslog(LOG_DEBUG, "evaluating <%s>", Params[i].Key);
00381 
00382                      if (!strcasecmp(Params[i].Key, "SUBSCRIBED")) {
00383                             ImapFilter.return_subscribed = 1;
00384                      }
00385 
00386                      else if (!strcasecmp(Params[i].Key, "CHILDREN")) {
00387                             ImapFilter.return_children = 1;
00388                      }
00389 
00390                      if (paren_nest == 0) {
00391                             i = num_parms + 1;   /* break out of the loop */
00392                      }
00393               }
00394        }
00395 
00396        /* Now start setting up the data we're going to send to the CtdlForEachRoom() callback.
00397         */
00398        
00399        /* The non-extended LIST command is required to treat an empty
00400         * ("" string) mailbox name argument as a special request to return the
00401         * hierarchy delimiter and the root name of the name given in the
00402         * reference parameter.
00403         */
00404        if ( (StrLength(ImapFilter.patterns[0]) == 0) && (extended_list_in_use == 0) ) {
00405               IAPrintf("* %s (\\Noselect) \"/\" \"\"\r\n", ImapFilter.verb);
00406        }
00407 
00408        /* Non-empty mailbox names, and any form of the extended LIST command,
00409         * is handled by this loop.
00410         */
00411        else {
00412               imap_list_floors(ImapFilter.verb, 
00413                              ImapFilter.num_patterns, 
00414                              ImapFilter.patterns);
00415               CtdlForEachRoom(imap_listroom, (char**)&ImapFilter);
00416        }
00417 
00418        /* 
00419         * Free the pattern buffers we allocated above.
00420         */
00421        for (i=0; i<ImapFilter.num_patterns; ++i) {
00422               FreeStrBuf(&ImapFilter.patterns[i]);
00423               free(ImapFilter.patterns);
00424 
00425        }
00426 
00427        IReplyPrintf("OK %s completed", ImapFilter.verb);
00428 }