Index: CHANGELOG
===================================================================
--- CHANGELOG	(revision 1c7b94b52e025bac64f610dd5e482548db2451bc)
+++ CHANGELOG	(revision 814bafcc5a94d69bd0c353ad9939f0e8bc71e8d5)
@@ -4,4 +4,5 @@
 - Show the same message only once (#1487641)
 - Fix namespaces handling (#1487649)
+- Add handling of multifolder METADATA/ANNOTATION responses
 
 RELEASE 0.5-RC
Index: program/include/rcube_imap_generic.php
===================================================================
--- program/include/rcube_imap_generic.php	(revision 7f1da4818efda0f4f0874f73c840ec46dc332223)
+++ program/include/rcube_imap_generic.php	(revision 814bafcc5a94d69bd0c353ad9939f0e8bc71e8d5)
@@ -2807,9 +2807,7 @@
             $this->escape($mailbox), $optlist));
 
-        if ($code == self::ERROR_OK && preg_match('/^\* METADATA /i', $response)) {
-            // Parse server response (remove "* METADATA ")
-            $response = substr($response, 11);
-            $ret_mbox = $this->tokenizeResponse($response, 1);
-            $data     = $this->tokenizeResponse($response);
+        if ($code == self::ERROR_OK) {
+            $result = array();
+            $data   = $this->tokenizeResponse($response);
 
             // The METADATA response can contain multiple entries in a single
@@ -2817,25 +2815,36 @@
             if (!empty($data) && ($size = count($data))) {
                 for ($i=0; $i<$size; $i++) {
-                    if (is_array($data[$i])) {
+                    if (isset($mbox) && is_array($data[$i])) {
                         $size_sub = count($data[$i]);
                         for ($x=0; $x<$size_sub; $x++) {
-                            $data[$data[$i][$x]] = $data[$i][++$x];
+                            $result[$mbox][$data[$i][$x]] = $data[$i][++$x];
                         }
                         unset($data[$i]);
                     }
-                    else if ($data[$i] == '*' && $data[$i+1] == 'METADATA') {
-                        unset($data[$i]);   // "*"
-                        unset($data[++$i]); // "METADATA"
-                        unset($data[++$i]); // Mailbox
-                    }
-                    else {
-                        $data[$data[$i]] = $data[++$i];
+                    else if ($data[$i] == '*') {
+                        if ($data[$i+1] == 'METADATA') {
+                            $mbox = $data[$i+2];
+                            unset($data[$i]);   // "*"
+                            unset($data[++$i]); // "METADATA"
+                            unset($data[++$i]); // Mailbox
+                        }
+                        // get rid of other untagged responses
+                        else {
+                            unset($mbox);
+                            unset($data[$i]);
+                        }
+                    }
+                    else if (isset($mbox)) {
+                        $result[$mbox][$data[$i]] = $data[++$i];
                         unset($data[$i]);
                         unset($data[$i-1]);
                     }
-                }
-            }
-
-            return $data;
+                    else {
+                        unset($data[$i]);
+                    }
+                }
+            }
+
+            return $result;
         }
 
@@ -2941,21 +2950,37 @@
             $this->escape($mailbox), $entries, $attribs));
 
-        if ($code == self::ERROR_OK && preg_match('/^\* ANNOTATION /i', $response)) {
-            // Parse server response (remove "* ANNOTATION ")
-            $response = substr($response, 13);
-            $ret_mbox = $this->tokenizeResponse($response, 1);
-            $data     = $this->tokenizeResponse($response);
-            $res      = array();
+        if ($code == self::ERROR_OK) {
+            $result = array();
+            $data   = $this->tokenizeResponse($response);
 
             // Here we returns only data compatible with METADATA result format
             if (!empty($data) && ($size = count($data))) {
                 for ($i=0; $i<$size; $i++) {
-                    $entry = $data[$i++];
-                    if (is_array($entry)) {
+                    $entry = $data[$i];
+                    if (isset($mbox) && is_array($entry)) {
                         $attribs = $entry;
                         $entry   = $last_entry;
                     }
-                    else
-                        $attribs = $data[$i++];
+                    else if ($entry == '*') {
+                        if ($data[$i+1] == 'ANNOTATION') {
+                            $mbox = $data[$i+2];
+                            unset($data[$i]);   // "*"
+                            unset($data[++$i]); // "ANNOTATION"
+                            unset($data[++$i]); // Mailbox
+                        }
+                        // get rid of other untagged responses
+                        else {
+                            unset($mbox);
+                            unset($data[$i]);
+                        }
+                        continue;
+                    }
+                    else if (isset($mbox)) {
+                        $attribs = $data[++$i];
+                    }
+                    else {
+                        unset($data[$i]);
+                        continue;
+                    }
 
                     if (!empty($attribs)) {
@@ -2964,18 +2989,17 @@
                             $value = $attribs[$x++];
                             if ($attr == 'value.priv') {
-                                $res['/private' . $entry] = $value;
+                                $result[$mbox]['/private' . $entry] = $value;
                             }
                             else if ($attr == 'value.shared') {
-                                $res['/shared' . $entry] = $value;
+                                $result[$mbox]['/shared' . $entry] = $value;
                             }
                         }
                     }
                     $last_entry = $entry;
-                    unset($data[$i-1]);
-                    unset($data[$i-2]);
-                }
-            }
-
-            return $res;
+                    unset($data[$i]);
+                }
+            }
+
+            return $result;
         }
 
