From 897ef322c7492e2d264ef4b751bb6c2421c8debd Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@osticket.com>
Date: Fri, 31 Jan 2014 16:27:20 +0000
Subject: [PATCH] Decide the thread body type at email parsing time.

This would in theory allow us to decide at the email level the content type
to accept.
---
 include/class.mailfetch.php | 44 ++++++++++++++++---------------------
 include/class.mailparse.php | 38 ++++++++++++++------------------
 2 files changed, 36 insertions(+), 46 deletions(-)

diff --git a/include/class.mailfetch.php b/include/class.mailfetch.php
index f1ce7fdc7..9c4afe9b4 100644
--- a/include/class.mailfetch.php
+++ b/include/class.mailfetch.php
@@ -472,10 +472,8 @@ class MailFetcher {
             foreach ($struct->parameters as $p) {
                 if (strtolower($p->attribute) == 'report-type'
                         && $p->value == 'delivery-status') {
-                    return sprintf('<pre>%s</pre>',
-                        Format::htmlchars(
-                            $this->getPart($mid, 'text/plain', $this->charset, $struct, false, 1)
-                        ));
+                    return new TextThreadBody( $this->getPart(
+                                $mid, 'text/plain', $this->charset, $struct, false, 1));
                 }
             }
         }
@@ -501,27 +499,23 @@ class MailFetcher {
         global $cfg;
 
         if ($cfg->isHtmlThreadEnabled()) {
-            if ($body=$this->getPart($mid, 'text/html', $this->charset)) {
-                //Cleanup the html.
-                $body = (trim($body, " <>br/\t\n\r"))
-                    ? Format::safe_html($body)
-                    : '--';
-            }
-            elseif ($body=$this->getPart($mid, 'text/plain', $this->charset)) {
-                $body = trim($body)
-                    ? sprintf('<pre>%s</pre>',
-                        Format::htmlchars($body))
-                    : '--';
-            }
-        }
-        else {
-            if (!($body=$this->getPart($mid, 'text/plain', $this->charset))) {
-                if ($body=$this->getPart($mid, 'text/html', $this->charset)) {
-                    $body = Format::html2text(Format::safe_html($body), 100, false);
-                }
-            }
-            $body = trim($body) ? $body : '--';
+            if ($html=$this->getPart($mid, 'text/html', $this->charset))
+                $body = new HtmlThreadBody($html);
+            elseif ($text=$this->getPart($mid, 'text/plain', $this->charset))
+                $body = new TextThreadBody($text);
         }
+        elseif ($text=$this->getPart($mid, 'text/plain', $this->charset))
+            $body = new TextThreadBody($text);
+        elseif ($html=$this->getPart($mid, 'text/html', $this->charset))
+            $body = new TextThreadBody(
+                    Format::html2text(Format::safe_html($html),
+                        100, false));
+        else
+            $body = new TextThreadBody('');
+
+        if ($cfg->stripQuotedReply())
+            $body->stripQuotedReply($cfg->getReplySeparator());
+
         return $body;
     }
 
@@ -572,7 +566,7 @@ class MailFetcher {
             $vars['thread-type'] = 'N';
         }
         else {
-            $vars['message'] = Format::stripEmptyLines($this->getBody($mid));
+            $vars['message'] = $this->getBody($mid);
         }
 
 
diff --git a/include/class.mailparse.php b/include/class.mailparse.php
index eacd62d2a..eca76a0b7 100644
--- a/include/class.mailparse.php
+++ b/include/class.mailparse.php
@@ -229,27 +229,23 @@ class Mail_Parse {
         global $cfg;
 
         if ($cfg->isHtmlThreadEnabled()) {
-            if ($body=$this->getPart($this->struct,'text/html')) {
-                // Cleanup the html -- Balance html tags & neutralize unsafe tags.
-                $body = (trim($body, " <>br/\t\n\r"))
-                    ? Format::safe_html($body)
-                    : '--';
-            }
-            elseif ($body=$this->getPart($this->struct,'text/plain')) {
-                $body = trim($body)
-                    ? sprintf('<pre>%s</pre>',
-                        Format::htmlchars($body))
-                    : '--';
-            }
-        }
-        else {
-            if (!($body=$this->getPart($this->struct,'text/plain'))) {
-                if ($body=$this->getPart($this->struct,'text/html')) {
-                    $body = Format::html2text(Format::safe_html($body), 100, false);
-                }
-            }
-            $body = trim($body) ? $body : '--';
+            if ($html=$this->getPart($this->struct,'text/html'))
+                $body = new HtmlThreadBody($html);
+            elseif ($text=$this->getPart($this->struct,'text/plain'))
+                $body = new TextThreadBody($text);
         }
+        elseif ($text=$this->getPart($this->struct,'text/plain'))
+            $body = new TextThreadBody($text);
+        elseif ($html=$this->getPart($this->struct,'text/html'))
+            $body = new TextThreadBody(
+                    Format::html2text(Format::safe_html($html),
+                        100, false));
+        else
+            $body = new TextThreadBody('');
+
+        if ($cfg->stripQuotedReply())
+            $body->stripQuotedReply($cfg->getReplySeparator());
+
         return $body;
     }
 
@@ -481,7 +477,7 @@ class EmailDataParser {
         }
         else {
             // Typical email
-            $data['message'] = Format::stripEmptyLines($parser->getBody());
+            $data['message'] = $parser->getBody();
             $data['in-reply-to'] = $parser->struct->headers['in-reply-to'];
             $data['references'] = $parser->struct->headers['references'];
         }
-- 
GitLab