From c6a9cd679970c61e097bfc6d73ca861cdc985fe3 Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Fri, 02 Mar 2012 08:43:45 -0500
Subject: [PATCH] - Fix automatic unsubscribe of non-existent folders (with small perf fix)

---
 program/include/rcube_imap.php |  182 +++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 167 insertions(+), 15 deletions(-)

diff --git a/program/include/rcube_imap.php b/program/include/rcube_imap.php
index 6db15dd..81ad185 100644
--- a/program/include/rcube_imap.php
+++ b/program/include/rcube_imap.php
@@ -2508,7 +2508,7 @@
         $a_defaults = $a_out = array();
 
         // Give plugins a chance to provide a list of folders
-        $data = rcmail::get_instance()->plugins->exec_hook('folders_list',
+        $data = rcmail::get_instance()->plugins->exec_hook('storage_folders',
             array('root' => $root, 'name' => $name, 'filter' => $filter, 'mode' => 'LSUB'));
 
         if (isset($data['folders'])) {
@@ -2527,9 +2527,10 @@
                     NULL, array('SUBSCRIBED'));
 
                 // unsubscribe non-existent folders, remove from the list
-                if (is_array($a_folders) && $name == '*') {
+                // we can do this only when LIST response is available
+                if (is_array($a_folders) && $name == '*' && !empty($this->conn->data['LIST'])) {
                     foreach ($a_folders as $idx => $folder) {
-                        if ($this->conn->data['LIST'] && ($opts = $this->conn->data['LIST'][$folder])
+                        if (($opts = $this->conn->data['LIST'][$folder])
                             && in_array('\\NonExistent', $opts)
                         ) {
                             $this->conn->unsubscribe($folder);
@@ -2542,11 +2543,12 @@
             else {
                 $a_folders = $this->conn->listSubscribed($root, $name);
 
-                // unsubscribe non-existent folders, remove from the list
-                if (is_array($a_folders) && $name == '*') {
+                // unsubscribe non-existent folders, remove them from the list,
+                // we can do this only when LIST response is available
+                if (is_array($a_folders) && $name == '*' && !empty($this->conn->data['LIST'])) {
                     foreach ($a_folders as $idx => $folder) {
-                        if ($this->conn->data['LIST'] && ($opts = $this->conn->data['LIST'][$folder])
-                            && in_array('\\Noselect', $opts)
+                        if (!isset($this->conn->data['LIST'][$folder])
+                            || in_array('\\Noselect', $this->conn->data['LIST'][$folder])
                         ) {
                             // Some servers returns \Noselect for existing folders
                             if (!$this->folder_exists($folder)) {
@@ -2594,7 +2596,7 @@
         }
 
         // Give plugins a chance to provide a list of folders
-        $data = rcmail::get_instance()->plugins->exec_hook('folders_list',
+        $data = rcmail::get_instance()->plugins->exec_hook('storage_folders',
             array('root' => $root, 'name' => $name, 'filter' => $filter, 'mode' => 'LIST'));
 
         if (isset($data['folders'])) {
@@ -2729,7 +2731,7 @@
      */
     public function get_quota()
     {
-        if ($this->get_capability('QUOTA')) {
+        if ($this->get_capability('QUOTA') && $this->check_connection()) {
             return $this->conn->getQuota();
         }
 
@@ -2896,10 +2898,10 @@
 
         // get list of folders
         if ((strpos($folder, '%') === false) && (strpos($folder, '*') === false)) {
-            $sub_mboxes = $this->list_unsubscribed('', $folder . $delm . '*');
+            $sub_mboxes = $this->list_folders('', $folder . $delm . '*');
         }
         else {
-            $sub_mboxes = $this->list_unsubscribed();
+            $sub_mboxes = $this->list_folders();
         }
 
         // send delete command to server
@@ -3488,7 +3490,7 @@
         if (substr($entry, 0, 7) == '/shared') {
             return array(substr($entry, 7), 'value.shared');
         }
-        else if (substr($entry, 0, 8) == '/protected') {
+        else if (substr($entry, 0, 8) == '/private') {
             return array(substr($entry, 8), 'value.priv');
         }
 
@@ -3527,7 +3529,8 @@
     {
         if ($this->caching && !$this->cache) {
             $rcmail = rcmail::get_instance();
-            $this->cache = $rcmail->get_cache('IMAP', $this->caching);
+            $ttl = $rcmail->config->get('message_cache_lifetime', '10d') - mktime();
+            $this->cache = $rcmail->get_cache('IMAP', $this->caching, $ttl);
         }
 
         return $this->cache;
@@ -3553,7 +3556,7 @@
      * @param string $key  Cache key
      * @param mixed  $data Data
      */
-    protected function update_cache($key, $data)
+    public function update_cache($key, $data)
     {
         if ($cache = $this->get_cache_engine()) {
             $cache->set($key, $data);
@@ -3572,6 +3575,20 @@
         if ($cache = $this->get_cache_engine()) {
             $cache->remove($key, $prefix_mode);
         }
+    }
+
+    /**
+     * Delete outdated cache entries
+     */
+    public function expunge_cache()
+    {
+        if ($this->mcache) {
+            $ttl = rcmail::get_instance()->config->get('message_cache_lifetime', '10d');
+            $this->mcache->expunge($ttl);
+        }
+
+        if ($this->cache)
+            $this->cache->expunge();
     }
 
 
@@ -3835,4 +3852,139 @@
         write_log('imap', $message);
     }
 
-}  // end class rcube_imap
+
+    /**
+     * Deprecated methods (to be removed)
+     */
+
+    public function decode_address_list($input, $max = null, $decode = true, $fallback = null)
+    {
+        return rcube_mime::decode_address_list($input, $max, $decode, $fallback);
+    }
+
+    public function decode_header($input, $fallback = null)
+    {
+        return rcube_mime::decode_mime_string((string)$input, $fallback);
+    }
+
+    public static function decode_mime_string($input, $fallback = null)
+    {
+        return rcube_mime::decode_mime_string($input, $fallback);
+    }
+
+    public function mime_decode($input, $encoding = '7bit')
+    {
+        return rcube_mime::decode($input, $encoding);
+    }
+
+    public static function explode_header_string($separator, $str, $remove_comments = false)
+    {
+        return rcube_mime::explode_header_string($separator, $str, $remove_comments);
+    }
+
+    public function select_mailbox($mailbox)
+    {
+        // do nothing
+    }
+
+    public function set_mailbox($folder)
+    {
+        $this->set_folder($folder);
+    }
+
+    public function get_mailbox_name()
+    {
+        return $this->get_folder();
+    }
+
+    public function list_headers($folder='', $page=NULL, $sort_field=NULL, $sort_order=NULL, $slice=0)
+    {
+        return $this->list_messages($folder, $page, $sort_field, $sort_order, $slice);
+    }
+
+    public function mailbox_status($folder = null)
+    {
+        return $this->folder_status($folder);
+    }
+
+    public function message_index($folder = '', $sort_field = NULL, $sort_order = NULL)
+    {
+        return $this->index($folder, $sort_field, $sort_order);
+    }
+
+    public function message_index_direct($folder, $sort_field = null, $sort_order = null, $skip_cache = true)
+    {
+        return $this->index_direct($folder, $sort_field, $sort_order, $skip_cache);
+    }
+
+    public function list_mailboxes($root='', $name='*', $filter=null, $rights=null, $skip_sort=false)
+    {
+        return $this->list_folders_subscribed($root, $name, $filter, $rights, $skip_sort);
+    }
+
+    public function list_unsubscribed($root='', $name='*', $filter=null, $rights=null, $skip_sort=false)
+    {
+        return $this->list_folders($root, $name, $filter, $rights, $skip_sort);
+    }
+
+    public function get_mailbox_size($folder)
+    {
+        return $this->folder_size($folder);
+    }
+
+    public function create_mailbox($folder, $subscribe=false)
+    {
+        return $this->create_folder($folder, $subscribe);
+    }
+
+    public function rename_mailbox($folder, $new_name)
+    {
+        return $this->rename_folder($folder, $new_name);
+    }
+
+    function delete_mailbox($folder)
+    {
+        return $this->delete_folder($folder);
+    }
+
+    public function mailbox_exists($folder, $subscription=false)
+    {
+        return $this->folder_exists($folder, $subscription);
+    }
+
+    public function mailbox_namespace($folder)
+    {
+        return $this->folder_namespace($folder);
+    }
+
+    public function mod_mailbox($folder, $mode = 'out')
+    {
+        return $this->mod_folder($folder, $mode);
+    }
+
+    public function mailbox_attributes($folder, $force=false)
+    {
+        return $this->folder_attributes($folder, $force);
+    }
+
+    public function mailbox_data($folder)
+    {
+        return $this->folder_data($folder);
+    }
+
+    public function mailbox_info($folder)
+    {
+        return $this->folder_info($folder);
+    }
+
+    public function mailbox_sync($folder)
+    {
+        return $this->folder_sync($folder);
+    }
+
+    public function expunge($folder='', $clear_cache=true)
+    {
+        return $this->expunge_folder($folder, $clear_cache);
+    }
+
+}

--
Gitblit v1.9.1