From 60ab5540038dd8827e80998f4a11c76435d0c24c Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Tue, 18 Aug 2015 13:51:21 -0400
Subject: [PATCH] Remove common subject prefixes Re:, Re[x]:, Re-x: on reply (#1490497)

---
 CHANGELOG                      |    1 +
 program/steps/mail/compose.inc |   30 ++++++++++++++++++++++--------
 2 files changed, 23 insertions(+), 8 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 89554ed..47eef9b 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,7 @@
 CHANGELOG Roundcube Webmail
 ===========================
 
+- Remove common subject prefixes Re:, Re[x]:, Re-x: on reply (#1490497)
 - Added GSSAPI/Kerberos authentication plugin - krb_authentication
 - Password: Allow temporarily disabling the plugin functionality with a notice
 - Support more secure hashing algorithms for auth cookie - configurable by PHP's session.hash_function (#1490403)
diff --git a/program/steps/mail/compose.inc b/program/steps/mail/compose.inc
index 505f5d6..2e1c240 100644
--- a/program/steps/mail/compose.inc
+++ b/program/steps/mail/compose.inc
@@ -1499,6 +1499,27 @@
     }
 }
 
+/**
+ * Creates reply subject by removing common subject
+ * prefixes/suffixes from the original message subject
+ */
+function rcmail_reply_subject($subject)
+{
+    $subject = trim($subject);
+
+    // replace Re:, Re[x]:, Re-x (#1490497)
+    $prefix = '/^(re:|re\[\d\]:|re-\d:)\s*/i';
+    do {
+        $subject = preg_replace($prefix, '', $subject, -1, $count);
+    }
+    while ($count);
+
+    // replace (was: ...) (#1489375)
+    $subject = preg_replace('/\s*\([wW]as:[^\)]+\)\s*$/', '', $subject);
+
+    return 'Re: ' . $subject;
+}
+
 function rcmail_compose_subject($attrib)
 {
     global $MESSAGE, $COMPOSE;
@@ -1521,13 +1542,7 @@
     }
     // create a reply-subject
     else if ($COMPOSE['mode'] == RCUBE_COMPOSE_REPLY) {
-        if (preg_match('/^re:/i', $MESSAGE->subject))
-            $subject = $MESSAGE->subject;
-        else
-            $subject = 'Re: '.$MESSAGE->subject;
-
-        // replace (was: ...) (#1489375)
-        $subject = preg_replace('/\s*\([wW]as:[^\)]+\)\s*$/', '', $subject);
+        $subject = rcmail_reply_subject($MESSAGE->subject);
     }
     // create a forward-subject
     else if ($COMPOSE['mode'] == RCUBE_COMPOSE_FORWARD) {
@@ -1547,7 +1562,6 @@
 
     return $out;
 }
-
 
 function rcmail_compose_attachment_list($attrib)
 {

--
Gitblit v1.9.1