From 4ffa559227a2abecc12f8402b6ef99a06dfa4806 Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Sat, 03 Mar 2012 08:20:14 -0500
Subject: [PATCH] - Apply fixes from trunk

---
 CHANGELOG                              |    3 +++
 program/include/rcube_imap.php         |   14 ++++++++------
 program/steps/mail/sendmail.inc        |    6 +++++-
 program/include/rcube_imap_generic.php |   16 ++++++++++------
 program/js/app.js                      |    5 +++--
 5 files changed, 29 insertions(+), 15 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index fec50f9..c7fef7d 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,9 @@
 CHANGELOG Roundcube Webmail
 ===========================
 
+- Prevent from folder selection on virtual folder collapsing (#1488346)
+- Fix automatic unsubscribe of non-existent folders
+- Fix double-quotes handling in recipient names
 - User configurable setting how to display contact names in list
 - Make contacts list sorting configurable for the admin/user
 - Fix parse errors in DDL files for MS SQL Server
diff --git a/program/include/rcube_imap.php b/program/include/rcube_imap.php
index 514d11b..a536e92 100644
--- a/program/include/rcube_imap.php
+++ b/program/include/rcube_imap.php
@@ -3040,9 +3040,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);
@@ -3055,11 +3056,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->mailbox_exists($folder)) {
diff --git a/program/include/rcube_imap_generic.php b/program/include/rcube_imap_generic.php
index e390b24..00802eb 100644
--- a/program/include/rcube_imap_generic.php
+++ b/program/include/rcube_imap_generic.php
@@ -2279,12 +2279,16 @@
                         $folders[$mailbox] = array();
                     }
 
-                    // Add to options array
-                    if (empty($this->data['LIST'][$mailbox]))
-                        $this->data['LIST'][$mailbox] = $opts;
-                    else if (!empty($opts))
-                        $this->data['LIST'][$mailbox] = array_unique(array_merge(
-                            $this->data['LIST'][$mailbox], $opts));
+                    // store LSUB options only if not empty, this way
+                    // we can detect a situation when LIST doesn't return specified folder
+                    if (!empty($opts) || $cmd == 'LIST') {
+                        // Add to options array
+                        if (empty($this->data['LIST'][$mailbox]))
+                            $this->data['LIST'][$mailbox] = $opts;
+                        else if (!empty($opts))
+                            $this->data['LIST'][$mailbox] = array_unique(array_merge(
+                                $this->data['LIST'][$mailbox], $opts));
+                    }
                 }
                 // * STATUS <mailbox> (<result>)
                 else if ($cmd == 'STATUS') {
diff --git a/program/js/app.js b/program/js/app.js
index 58d7f1e..f83025d 100644
--- a/program/js/app.js
+++ b/program/js/app.js
@@ -1407,8 +1407,9 @@
       div.removeClass('expanded').addClass('collapsed');
       this.env.collapsed_folders = this.env.collapsed_folders+'&'+urlencode(name)+'&';
 
-      // select parent folder if one of its childs is currently selected
-      if (this.env.mailbox.indexOf(name + this.env.delimiter) == 0)
+      // select the folder if one of its childs is currently selected
+      // don't select if it's virtual (#1488346)
+      if (this.env.mailbox.indexOf(name + this.env.delimiter) == 0 && !$(li).hasClass('virtual'))
         this.command('list', name);
     }
     else
diff --git a/program/steps/mail/sendmail.inc b/program/steps/mail/sendmail.inc
index 1b9d387..25eb059 100644
--- a/program/steps/mail/sendmail.inc
+++ b/program/steps/mail/sendmail.inc
@@ -176,7 +176,11 @@
     // address with name (handle name)
     } else if (preg_match('/<*'.$email_regexp.'>*$/', $item, $matches)) {
       $address = $matches[0];
-      $name = trim(str_replace($address, '', $item), '" ');
+      $name = trim(str_replace($address, '', $item));
+      if ($name[0] == '"' && $name[count($name)-1] == '"') {
+        $name = substr($name, 1, -1);
+      }
+      $name = stripcslashes($name);
       $address = rcube_idn_to_ascii(trim($address, '<>'));
       $result[] = format_email_recipient($address, $name);
       $item = $address;

--
Gitblit v1.9.1