From 5f47eda94b64b21b48ce95e329993c3ed50ef6db Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Tue, 27 Sep 2011 03:29:49 -0400
Subject: [PATCH] - Applied some fixes from trunk up to r5280

---
 CHANGELOG                      |    1 +
 program/js/common.js           |    4 ++--
 program/include/rcube_imap.php |    2 +-
 program/steps/mail/compose.inc |   29 +++++++++++++++++++----------
 program/js/app.js              |   11 +++++++----
 5 files changed, 30 insertions(+), 17 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 78537ec..f4daec4 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,7 @@
 CHANGELOG Roundcube Webmail
 ===========================
 
+- Fix bug where the last identity is used on reply (#1488101)
 - Fix locked folder rename option on servers supporting RFC2086 only (#1488089)
 - Fix encoding of LDAP contacts identifiers (#1488079)
 - Fix session race conditions when composing new messages
diff --git a/program/include/rcube_imap.php b/program/include/rcube_imap.php
index 5f476ab..379c8c6 100644
--- a/program/include/rcube_imap.php
+++ b/program/include/rcube_imap.php
@@ -3806,7 +3806,7 @@
             // @TODO: Honor MAXSIZE and DEPTH options
             foreach ($queries as $attrib => $entry)
                 if ($result = $this->conn->getAnnotation($mailbox, $entry, $attrib))
-                    $res = array_merge($res, $result);
+                    $res = array_merge_recursive($res, $result);
 
             return $res;
         }
diff --git a/program/js/app.js b/program/js/app.js
index be42cc8..4f05090 100644
--- a/program/js/app.js
+++ b/program/js/app.js
@@ -3604,7 +3604,8 @@
     var cpos = this.get_caret_pos(this.ksearch_input),
       p = inp_value.lastIndexOf(',', cpos-1),
       q = inp_value.substring(p+1, cpos),
-      min = this.env.autocomplete_min_length;
+      min = this.env.autocomplete_min_length,
+      ac = this.ksearch_data;
 
     // trim query string
     q = $.trim(q);
@@ -3633,8 +3634,8 @@
     if (!q.length)
       return;
 
-    // ...new search value contains old one and previous search result was empty
-    if (old_value && old_value.length && this.env.contacts && !this.env.contacts.length && q.indexOf(old_value) == 0)
+    // ...new search value contains old one and previous search was not finished or its result was empty
+    if (old_value && old_value.length && q.indexOf(old_value) == 0 && (!ac || !ac.num) && this.env.contacts && !this.env.contacts.length)
       return;
 
     var i, lock, source, xhr, reqid = new Date().getTime(),
@@ -3642,7 +3643,8 @@
       sources = props && props.sources ? props.sources : [],
       action = props && props.action ? props.action : 'mail/autocomplete';
 
-    this.ksearch_data = {id: reqid, sources: sources.slice(), action: action, locks: [], requests: []};
+    this.ksearch_data = {id: reqid, sources: sources.slice(), action: action,
+      locks: [], requests: [], num: sources.length};
 
     for (i=0; i<threads; i++) {
       source = this.ksearch_data.sources.shift();
@@ -3727,6 +3729,7 @@
     if (maxlen > 0 && this.ksearch_data.id == reqid && this.ksearch_data.sources.length) {
       var lock, xhr, props = this.ksearch_data, source = props.sources.shift();
       if (source) {
+      data.num--;
         lock = this.display_message(this.get_label('searching'), 'loading');
         xhr = this.http_post(props.action, '_search='+urlencode(s_val)+'&_id='+reqid
           +'&_source='+urlencode(source), lock);
diff --git a/program/js/common.js b/program/js/common.js
index c5f466a..c0c6dda 100644
--- a/program/js/common.js
+++ b/program/js/common.js
@@ -619,10 +619,10 @@
       return null;
   }
   else {
-    begin += 2;  
+    begin += 2;
   }
 
-  var end = document.cookie.indexOf(";", begin);
+  var end = dc.indexOf(";", begin);
   if (end == -1)
     end = dc.length;
 
diff --git a/program/steps/mail/compose.inc b/program/steps/mail/compose.inc
index 00d4b64..5a1f564 100644
--- a/program/steps/mail/compose.inc
+++ b/program/steps/mail/compose.inc
@@ -46,7 +46,7 @@
     'param' => request2param(RCUBE_INPUT_GET),
     'mailbox' => $IMAP->get_mailbox_name(),
   );
-  
+
   // process values like "mailto:foo@bar.com?subject=new+message&cc=another"
   if ($_SESSION['compose']['param']['to']) {
     // #1486037: remove "mailto:" prefix
@@ -59,10 +59,10 @@
         $_SESSION['compose']['param'][$f] = $val;
     }
   }
-  
+
   // select folder where to save the sent message
   $_SESSION['compose']['param']['sent_mbox'] = $RCMAIL->config->get('sent_mbox');
-  
+
   // pipe compose parameters thru plugins
   $plugin = $RCMAIL->plugins->exec_hook('message_compose', $_SESSION['compose']);
   $_SESSION['compose']['param'] = array_merge($_SESSION['compose']['param'], $plugin['param']);
@@ -84,12 +84,12 @@
           'path' => $attach,
         );
       }
-      
+
       // save attachment if valid
       if (($attachment['data'] && $attachment['name']) || ($attachment['path'] && file_exists($attachment['path']))) {
         $attachment = rcmail::get_instance()->plugins->exec_hook('attachment_save', $attachment);
       }
-      
+
       if ($attachment['status'] && !$attachment['abort']) {
         unset($attachment['data'], $attachment['status'], $attachment['abort']);
         $_SESSION['compose']['attachments'][$attachment['id']] = $attachment;
@@ -242,21 +242,27 @@
   $MESSAGE->compose['from'] = $_SESSION['compose']['param']['from'];
 }
 else if (count($MESSAGE->identities)) {
-  // extract all recipients of the reply-message
   $a_recipients = array();
+  $a_names      = array();
+
+  // extract all recipients of the reply-message
   if (is_object($MESSAGE->headers) && in_array($compose_mode, array(RCUBE_COMPOSE_REPLY, RCUBE_COMPOSE_FORWARD)))
   {
     $a_to = $IMAP->decode_address_list($MESSAGE->headers->to);
     foreach ($a_to as $addr) {
-      if (!empty($addr['mailto']))
+      if (!empty($addr['mailto'])) {
         $a_recipients[] = strtolower($addr['mailto']);
+        $a_names[]      = $addr['name'];
+      }
     }
 
     if (!empty($MESSAGE->headers->cc)) {
       $a_cc = $IMAP->decode_address_list($MESSAGE->headers->cc);
       foreach ($a_cc as $addr) {
-        if (!empty($addr['mailto']))
+        if (!empty($addr['mailto'])) {
           $a_recipients[] = strtolower($addr['mailto']);
+          $a_names[]      = $addr['name'];
+        }
       }
     }
   }
@@ -285,8 +291,11 @@
       break;
     }
     // use replied message recipients
-    else if (in_array($ident['email_ascii'], $a_recipients)) {
-      $from_idx = $idx;
+    else if (($found = array_search($ident['email_ascii'], $a_recipients)) !== false) {
+      // match identity name, prefer default identity
+      if ($from_idx === null || ($a_names[$found] && $ident['name'] && $a_names[$found] == $ident['name'])) {
+        $from_idx = $idx;
+      }
     }
   }
 

--
Gitblit v1.9.1