stable now but there are GIANT PIECES MISSING
[citadel.git] / citadel / modules / imap / imap_list.c
index 4151981be13815b0d81f2e38e9af200d8f595157..1be8bab29bed6275d1c8489cb486505cf9cf623c 100644 (file)
@@ -1,11 +1,9 @@
 /*
- * $Id$
- *
  * Implements the LIST and LSUB commands.
  *
- * Copyright (c) 2000-2009 by Art Cancro and others.
+ * Copyright (c) 2000-2017 by Art Cancro and others.
  *
- *  This program is free software; you can redistribute it and/or modify
+ *  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.
 #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 "support.h"
 #include "config.h"
 #include "user_ops.h"
-#include "policy.h"
 #include "database.h"
 #include "msgbase.h"
 #include "internet_addressing.h"
-#include "imap_tools.h"
 #include "serv_imap.h"
+#include "imap_tools.h"
 #include "imap_fetch.h"
 #include "imap_search.h"
 #include "imap_store.h"
@@ -99,9 +85,9 @@ void imap_list_floors(char *verb, int num_patterns, StrBuf **patterns)
                                }
                        }
                        if (match) {
-                               cprintf("* %s (\\NoSelect \\HasChildren) \"/\" ", verb);
-                               plain_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");
                        }
                }
        }
@@ -118,25 +104,30 @@ void imap_list_floors(char *verb, int num_patterns, StrBuf **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;
        ImapRoomListFilter *ImapFilter;
        int i = 0;
        int match = 0;
+       int ROLen;
 
        /* Here's how we break down the array of pointers passed to us */
        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 (ImapFilter->return_subscribed) {
                if (ra & UA_KNOWN) {
-                       strcat(return_options, "\\Subscribed");
+                       memcpy(return_options, HKEY(SUBSCRIBED_STR) + 1);
+                       ROLen += sizeof(SUBSCRIBED_STR) - 1;
                }
        }
 
@@ -148,9 +139,10 @@ void imap_listroom(struct ctdlroom *qrbuf, void *data)
         */
        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 (ImapFilter->subscribed_rooms_only) {
@@ -165,17 +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<ImapFilter->num_patterns; ++i) {
-                       if (imap_mailbox_matches_pattern(ChrPtr(ImapFilter->patterns[i]), buf)) {
+                       if (imap_mailbox_matches_pattern(ChrPtr(ImapFilter->patterns[i]), MailboxName)) {
                                match = 1;
                        }
                }
                if (match) {
-                       cprintf("* %s (%s) \"/\" ", ImapFilter->verb, return_options);
-                       plain_imap_strout(buf);
-                       cprintf("\r\n");
+                       IAPrintf("* %s (%s) \"/\" ", ImapFilter->verb, return_options);
+                       IPutStr(MailboxName, len);
+                       IAPuts("\r\n");
                }
        }
 }
@@ -186,26 +179,27 @@ void imap_listroom(struct ctdlroom *qrbuf, void *data)
  */
 void imap_list(int num_parms, ConstStr *Params)
 {
-       int subscribed_rooms_only = 0;
+       struct CitContext *CCC = CC;
+       citimap *Imap = CCCIMAP;
        int i, j, paren_nest;
        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;
 
        if (num_parms < 4) {
-               cprintf("%s BAD arguments invalid\r\n", Params[0].Key);
+               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)
@@ -218,7 +212,7 @@ void imap_list(int num_parms, ConstStr *Params)
        }
 
        if (!strcasecmp(ImapFilter.verb, "LSUB")) {
-               subscribed_rooms_only = 1;
+               ImapFilter.subscribed_rooms_only = 1;
        }
 
        /*
@@ -265,12 +259,12 @@ void imap_list(int num_parms, ConstStr *Params)
 
                /* Strip off the outer parentheses */
                if (Params[selection_left].Key[0] == '(') {
-                       TokenCutLeft(&IMAP->Cmd, 
+                       TokenCutLeft(&Imap->Cmd, 
                                     &Params[selection_left], 
                                     1);
                }
                if (Params[selection_right].Key[Params[selection_right].len-1] == ')') {
-                       TokenCutRight(&IMAP->Cmd, 
+                       TokenCutRight(&Imap->Cmd, 
                                      &Params[selection_right], 
                                      1);
                }
@@ -278,7 +272,7 @@ void imap_list(int num_parms, ConstStr *Params)
                for (i=selection_left; i<=selection_right; ++i) {
 
                        if (!strcasecmp(Params[i].Key, "SUBSCRIBED")) {
-                               subscribed_rooms_only = 1;
+                               ImapFilter.subscribed_rooms_only = 1;
                        }
 
                        else if (!strcasecmp(Params[i].Key, "RECURSIVEMATCH")) {
@@ -365,15 +359,15 @@ void imap_list(int num_parms, ConstStr *Params)
 
                        /* Might as well look for these while we're in here... */
                        if (Params[i].Key[0] == '(') 
-                               TokenCutLeft(&IMAP->Cmd, 
+                               TokenCutLeft(&Imap->Cmd, 
                                             &Params[i], 
                                             1);
                        if (Params[i].Key[Params[i].len-1] == ')')
-                           TokenCutRight(&IMAP->Cmd, 
+                           TokenCutRight(&Imap->Cmd, 
                                          &Params[i], 
                                          1);
 
-                       CtdlLogPrintf(9, "evaluating <%s>\n", Params[i].Key);
+                       syslog(LOG_DEBUG, "evaluating <%s>", Params[i].Key);
 
                        if (!strcasecmp(Params[i].Key, "SUBSCRIBED")) {
                                ImapFilter.return_subscribed = 1;
@@ -384,7 +378,6 @@ void imap_list(int num_parms, ConstStr *Params)
                        }
 
                        if (paren_nest == 0) {
-                               return_right = i;       /* found end of patterns */
                                i = num_parms + 1;      /* break out of the loop */
                        }
                }
@@ -399,7 +392,7 @@ void imap_list(int num_parms, ConstStr *Params)
         * reference parameter.
         */
        if ( (StrLength(ImapFilter.patterns[0]) == 0) && (extended_list_in_use == 0) ) {
-               cprintf("* %s (\\Noselect) \"/\" \"\"\r\n", ImapFilter.verb);
+               IAPrintf("* %s (\\Noselect) \"/\" \"\"\r\n", ImapFilter.verb);
        }
 
        /* Non-empty mailbox names, and any form of the extended LIST command,
@@ -417,9 +410,8 @@ void imap_list(int num_parms, ConstStr *Params)
         */
        for (i=0; i<ImapFilter.num_patterns; ++i) {
                FreeStrBuf(&ImapFilter.patterns[i]);
-               free(ImapFilter.patterns);
-
        }
+       free(ImapFilter.patterns);
 
-       cprintf("%s OK %s completed\r\n", Params[0].Key, ImapFilter.verb);
+       IReplyPrintf("OK %s completed", ImapFilter.verb);
 }