[geary/wip/714317-hide-html-in-preview] Make to_preview_text() require UNIX (LF), not RFC833 (CRLF) strings.



commit fdb3c6cac6404a289f03d90f76418458b161158c
Author: Michael James Gratton <mike vee net>
Date:   Wed Dec 21 10:45:21 2016 +1100

    Make to_preview_text() require UNIX (LF), not RFC833 (CRLF) strings.
    
    Fixes Base64 encoded parts when fetching PREVIEW, and armour, etc
    stripping etc not being applied to previews geneated via
    Geary.RFC822.Message.
    
    * src/engine/rfc822/rfc822-utils.vala
      (Geary.RFC822.Utils::to_preview_text): Assume line endings are LF
      encoded, update doc comment and unit tests to reflect that.
    
    * src/engine/rfc822/rfc822-message-data.vala (PreviewText.with_header):
      Add CRLF filter to preveiw text to strip CR chars from lines.

 src/engine/rfc822/rfc822-message-data.vala |    1 +
 src/engine/rfc822/rfc822-utils.vala        |    6 ++++--
 test/engine/rfc822-message-data-test.vala  |   12 ++++++++++--
 test/engine/rfc822-utils-test.vala         |    3 +--
 4 files changed, 16 insertions(+), 6 deletions(-)
---
diff --git a/src/engine/rfc822/rfc822-message-data.vala b/src/engine/rfc822/rfc822-message-data.vala
index 7459c02..541ad10 100644
--- a/src/engine/rfc822/rfc822-message-data.vala
+++ b/src/engine/rfc822/rfc822-message-data.vala
@@ -390,6 +390,7 @@ public class Geary.RFC822.PreviewText : Geary.RFC822.Text {
                 filter.add(new GMime.FilterBasic(GMime.content_encoding_from_string(encoding), false));
 
             filter.add(Geary.RFC822.Utils.create_utf8_filter_charset(charset));
+            filter.add(new GMime.FilterCRLF(false, false));
 
             input_stream.write_to_stream(filter);
             uint8[] data = output.data;
diff --git a/src/engine/rfc822/rfc822-utils.vala b/src/engine/rfc822/rfc822-utils.vala
index 64aea29..0d556bd 100644
--- a/src/engine/rfc822/rfc822-utils.vala
+++ b/src/engine/rfc822/rfc822-utils.vala
@@ -345,14 +345,16 @@ public bool comp_char_arr_slice(char[] array, uint start, string comp) {
 /**
  * Obtains the best preview text from a plain or HTML string.
  *
- * The string returned will will have had its whitespace squashed.
+ * The given string `text` should have UNIX encoded line endings (LF),
+ * rather than RFC822 (CRLF). The string returned will will have had
+ * its whitespace squashed.
  */
 public string to_preview_text(string? text, TextFormat format) {
     string preview = "";
 
     if (format == TextFormat.PLAIN) {
         StringBuilder buf = new StringBuilder();
-        string[] all_lines = text.split("\r\n");
+        string[] all_lines = text.split("\n");
         bool in_inline_pgp_header = false;
         foreach (string line in all_lines) {
             if (in_inline_pgp_header) {
diff --git a/test/engine/rfc822-message-data-test.vala b/test/engine/rfc822-message-data-test.vala
index f36e72f..61814f1 100644
--- a/test/engine/rfc822-message-data-test.vala
+++ b/test/engine/rfc822-message-data-test.vala
@@ -19,6 +19,12 @@ class Geary.RFC822.MessageDataTest : Gee.TestCase {
         );
         assert(plain_preview1.buffer.to_string() == PLAIN_BODY1_EXPECTED);
 
+        PreviewText base64_preview = new PreviewText.with_header(
+            new Geary.Memory.StringBuffer(BASE64_BODY_ENCODED),
+            new Geary.Memory.StringBuffer(BASE64_BODY_HEADERS)
+        );
+        assert(base64_preview.buffer.to_string() == BASE64_BODY_EXPECTED);
+
         string html_part_headers = "Content-Type: text/html; charset=utf-8\r\nContent-Transfer-Encoding: 
quoted-printable\r\n\r\n";
 
         PreviewText html_preview1 = new PreviewText.with_header(
@@ -35,11 +41,13 @@ class Geary.RFC822.MessageDataTest : Gee.TestCase {
     }
 
     public static string PLAIN_BODY1_HEADERS = "Content-Type: text/plain; 
charset=\"us-ascii\"\r\nContent-Transfer-Encoding: 7bit\r\n";
-
     public static string PLAIN_BODY1_ENCODED = "-----BEGIN PGP SIGNED MESSAGE-----\r\nHash: 
SHA512\r\n\r\n=============================================================================\r\nFreeBSD-EN-16:11.vmbus
                                          Errata Notice\r\n                                                   
       The FreeBSD Project\r\n\r\nTopic:          Avoid using spin locks for channel message 
locks\r\n\r\nCategory:       core\r\nModule:         vmbus\r\nAnnounced:      2016-08-12\r\nCredits:        
Microsoft OSTC\r\nAffects:        FreeBSD 10.3\r\nCorrected:      2016-06-15 09:52:01 UTC (stable/10, 
10.3-STABLE)\r\n                2016-08-12 04:01:16 UTC (releng/10.3, 10.3-RELEASE-p7)\r\n\r\nFor general 
information regarding FreeBSD Errata Notices and Security\r\nAdvisories, including descriptions of the fields 
above, security\r\nbranches, and the following sections, please 
visit\r\n<URL:https://security.FreeBSD.org/>.\r\n";
-
     public static string PLAIN_BODY1_EXPECTED = "FreeBSD-EN-16:11.vmbus Errata Notice The FreeBSD Project 
Topic: Avoid using spin locks for channel message locks Category: core Module: vmbus Announced: 2016-08-12 
Credits: Microsoft OSTC Affects: FreeBSD 10.3 Corrected: 2016-06-15 09:52:01 UTC (stable/10, 10.3-STABLE) 
2016-08-12 04:01:16 UTC (releng/10.3, 10.3-RELEASE-p7) For general information regarding FreeBSD Errata 
Notices and Security Advisories, including descriptions of the fields above, security branches, and the 
following sections, please visit <URL:https://security.FreeBSD.org/>.";
 
+    public static string BASE64_BODY_HEADERS = "Content-Transfer-Encoding: base64\r\nContent-Type: 
text/plain; charset=\"utf-8\"; Format=\"flowed\"\r\n";
+    public static string BASE64_BODY_ENCODED = 
"CkhleSBSaWNhcmRvLAoKVGhhbmtzIGZvciBsb29raW5nIGludG8gdGhpcy4KCk9uIFR1ZSwgRGVj\r\nIDEzLCAyMDE2IGF0IDEwOjIzIEFNLCBSaWNhcmRvIEJ1Z2FsaG8gPHJidWdhbGhvQGdtYWlsLmNv\r\nbT4gCndyb3RlOgo+IGZyb20gbXkgdGVzdGluZywgdGhlIHByZWZldGNoX3BlcmlvZF9kYXlzIGRv\r\nZXMgbm90IHdvcmsgZm9yIElOQk9YLgo+IFRoaXMgaXMgYW5ub3lpbmcsIEkgd2FudCB0byBwcmVm\r\nZXRjaCBhbGwgbXkgZS1tYWlsLCBzbyBJIGNhbiBydW4gCj4gc2VhcmNoCj4gZXMuCj4gCj4gQXMg\r\nZmFyIGFzIEkgY291bGQsIEkndmUgdHJhY2VkIHRoZSBwcm9ibGVtIGRvd24gdG8gdGhpcyBjb25k\r\naXRpb24gaW4KPiBzZW5kX2FsbDoKPiAKPiAgICAgaWYgKGltYXBfZm9sZGVyLmdldF9vcGVuX3N0\r\nYXRlKCkgIT0gRm9sZGVyLk9wZW5TdGF0ZS5DTE9TRUQpCj4gICAgICAgICAgICAgICAgIGNvbnRp\r\nbnVlOwo+IAo+IGh0dHBzOi8vZ2l0aHViLmNvbS9HTk9NRS9nZWFyeS9ibG9iL21hc3Rlci9zcmMv\r\nZW5naW5lL2ltYXAtZW5naW5lL2ltYXAtCj4gZW5naW5lLWFjY291bnQtc3luY2hyb25pemVyLnZh\r\nbGEjTDE1MQo+IAo+IElOQk9YIGlzIGFsd2F5cyBvcGVuIGFuZCB0aHVzIGlzIG5ldmVyIHNlbnQg\r\ndG8gcHJvY2Vzc19xdWV1ZV9hc3luYy4KPiAKPi";
+    public static string BASE64_BODY_EXPECTED = "Hey Ricardo, Thanks for looking into this. On Tue, Dec 13, 
2016 at 10:23 AM, Ricardo Bugalho <rbugalho gmail com> wrote:";
+
     public static string HTML_BODY1_ENCODED = """<html><head>
 <meta http-equiv=3DContent-Type content=3D"text/html; charset=3Dutf-8">
 <style>
diff --git a/test/engine/rfc822-utils-test.vala b/test/engine/rfc822-utils-test.vala
index 0566983..2af01f7 100644
--- a/test/engine/rfc822-utils-test.vala
+++ b/test/engine/rfc822-utils-test.vala
@@ -21,8 +21,7 @@ class Geary.RFC822.Utils.Test : Gee.TestCase {
                HTML_BODY_EXPECTED);
     }
 
-    public static string PLAIN_BODY_ENCODED = "-----BEGIN PGP SIGNED MESSAGE-----\r\nHash: 
SHA512\r\n\r\n=============================================================================\r\nFreeBSD-EN-16:11.vmbus
                                          Errata Notice\r\n                                                   
       The FreeBSD Project\r\n\r\nTopic:          Avoid using spin locks for channel message 
locks\r\n\r\nCategory:       core\r\nModule:         vmbus\r\nAnnounced:      2016-08-12\r\nCredits:        
Microsoft OSTC\r\nAffects:        FreeBSD 10.3\r\nCorrected:      2016-06-15 09:52:01 UTC (stable/10, 
10.3-STABLE)\r\n                2016-08-12 04:01:16 UTC (releng/10.3, 10.3-RELEASE-p7)\r\n\r\nFor general 
information regarding FreeBSD Errata Notices and Security\r\nAdvisories, including descriptions of the fields 
above, security\r\nbranches, and the following sections, please 
visit\r\n<URL:https://security.FreeBSD.org/>.\r\n";
-
+    public static string PLAIN_BODY_ENCODED = "-----BEGIN PGP SIGNED MESSAGE-----\nHash: 
SHA512\n\n=============================================================================\nFreeBSD-EN-16:11.vmbus
                                          Errata Notice\n                                                     
     The FreeBSD Project\n\nTopic:          Avoid using spin locks for channel message locks\n\nCategory:     
  core\nModule:         vmbus\nAnnounced:      2016-08-12\nCredits:        Microsoft OSTC\nAffects:        
FreeBSD 10.3\nCorrected:      2016-06-15 09:52:01 UTC (stable/10, 10.3-STABLE)\n                2016-08-12 
04:01:16 UTC (releng/10.3, 10.3-RELEASE-p7)\n\nFor general information regarding FreeBSD Errata Notices and 
Security\nAdvisories, including descriptions of the fields above, security\nbranches, and the following 
sections, please visit\n<URL:https://security.FreeBSD.org/>.\n";
     public static string PLAIN_BODY_EXPECTED = "FreeBSD-EN-16:11.vmbus Errata Notice The FreeBSD Project 
Topic: Avoid using spin locks for channel message locks Category: core Module: vmbus Announced: 2016-08-12 
Credits: Microsoft OSTC Affects: FreeBSD 10.3 Corrected: 2016-06-15 09:52:01 UTC (stable/10, 10.3-STABLE) 
2016-08-12 04:01:16 UTC (releng/10.3, 10.3-RELEASE-p7) For general information regarding FreeBSD Errata 
Notices and Security Advisories, including descriptions of the fields above, security branches, and the 
following sections, please visit <URL:https://security.FreeBSD.org/>.";
 
     public static string HTML_BODY_ENCODED = """<html><head>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]