From ccd63c5591d56e9dcb43ddc5171b4b54dda08c42 Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Sat, 06 Sep 2008 12:41:43 -0400
Subject: [PATCH] Don't wrap worwarded text; better wrap reply message text

---
 program/steps/mail/compose.inc |    7 ++-----
 program/steps/mail/func.inc    |   48 +++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 47 insertions(+), 8 deletions(-)

diff --git a/program/steps/mail/compose.inc b/program/steps/mail/compose.inc
index 783ac98..0a5226e 100644
--- a/program/steps/mail/compose.inc
+++ b/program/steps/mail/compose.inc
@@ -32,7 +32,7 @@
   if (is_array($_SESSION['compose']['attachments'][$id]))
   {
     @unlink($_SESSION['compose']['attachments'][$id]['path']);
-    $_SESSION['compose']['attachments'][$id] = NULL;
+    unset($_SESSION['compose']['attachments'][$id]);
     $OUTPUT->command('remove_from_attachment_list', "rcmfile$id");
     $OUTPUT->send();
     exit;
@@ -478,7 +478,7 @@
   if (! $bodyIsHtml)
   {
     // soft-wrap message first
-    $body = wordwrap($body, 75);
+    $body = rcmail_wrap_quoted($body, 75);
   
     // split body into single lines
     $a_lines = preg_split('/\r?\n/', $body);
@@ -526,9 +526,6 @@
 
   if (!$bodyIsHtml)
   {
-    // soft-wrap message first
-    $body = wordwrap($body, 80);
-
     $prefix = sprintf("\n\n\n-------- Original Message --------\nSubject: %s\nDate: %s\nFrom: %s\nTo: %s\n\n",
       $MESSAGE->subject,
       $MESSAGE->headers->date,
diff --git a/program/steps/mail/func.inc b/program/steps/mail/func.inc
index e10843b..54abb24 100644
--- a/program/steps/mail/func.inc
+++ b/program/steps/mail/func.inc
@@ -642,9 +642,6 @@
   $convert_patterns[] = '/([a-z0-9][a-z0-9\-\.\+\_]*@[a-z0-9]([a-z0-9\-][.]?)*[a-z0-9]\\.[a-z]{2,5})/ie';
   $convert_replaces[] = "rcmail_str_replacement('<a href=\"mailto:\\1\" onclick=\"return ".JS_OBJECT_NAME.".command(\'compose\',\'\\1\',this)\">\\1</a>', \$replace_strings)";
   
-//    if ($part->ctype_parameters['format'] != 'flowed')
-//      $body = wordwrap(trim($body), 80);
-
   // search for patterns like links and e-mail addresses
   $body = preg_replace($convert_patterns, $convert_replaces, $body);
 
@@ -1013,6 +1010,51 @@
 }
 
 
+/**
+ * Wrap text to a given number of characters per line
+ * but respect the mail quotation of replies messages (>)
+ *
+ * @param string Text to wrap
+ * @param int The line width
+ * @return string The wrapped text
+ */
+function rcmail_wrap_quoted($text, $max = 76)
+{
+  // Rebuild the message body with a maximum of $max chars, while keeping quoted message.
+  $lines = preg_split('/\r?\n/', trim($text));
+  $out = '';
+
+  foreach ($lines as $line) {
+    if (strlen($line) > $max) {
+      if (preg_match('/^([>\s]+)/', $line, $regs)) {
+        $length = strlen($regs[0]);
+        $prefix = substr($line, 0, $length);
+
+        // Remove '> ' from the line, then wordwrap() the line
+        $line = wordwrap(substr($line, $length), $max - $length);
+
+        // Rebuild the line with '> ' at the beginning of each 'subline'
+        $newline = '';
+        foreach (explode("\n", $line) as $l) {
+          $newline .= $prefix . $l . "\n";
+        }
+
+        // Remove the righest newline char
+        $line = rtrim($newline);
+      }
+      else {
+        $line = wordwrap($line, $max);
+      }
+    }
+
+    // Append the line
+    $out .= $line . "\n";
+  }
+  
+  return $out;
+}
+
+
 function rcmail_message_part_controls()
   {
   global $MESSAGE;

--
Gitblit v1.9.1