[geary/wip/282-composer-quoted-email: 3/3] Fix RFC822 mailbox names with commas breaking composer address entry



commit e2aa44ea2c829a69b669abe823e20327b67c657d
Author: Michael Gratton <mike vee net>
Date:   Sat Mar 2 15:01:57 2019 +1100

    Fix RFC822 mailbox names with commas breaking composer address entry
    
    When pasting an email address copied from Geary, replying to a message
    or entering an address via autocmoplete in the composer, and the
    recipient's mailbox name contains a comma, it needs to be quoted so
    that it is not parsed by the entry as two seperate addresses.
    
    Fixes #282

 src/engine/rfc822/rfc822-mailbox-address.vala | 23 ++++++++++++++++++-----
 test/engine/rfc822-mailbox-address-test.vala  |  3 +++
 2 files changed, 21 insertions(+), 5 deletions(-)
---
diff --git a/src/engine/rfc822/rfc822-mailbox-address.vala b/src/engine/rfc822/rfc822-mailbox-address.vala
index 869ca76a..a1864baf 100644
--- a/src/engine/rfc822/rfc822-mailbox-address.vala
+++ b/src/engine/rfc822/rfc822-mailbox-address.vala
@@ -49,6 +49,15 @@ public class Geary.RFC822.MailboxAddress :
         return GMime.utils_header_decode_text(prepare_header_text_part(mailbox));
     }
 
+    private static bool display_name_needs_quoting(string name) {
+        // Currently we only care if the name contains a comma, since
+        // that will screw up the composer's address entry fields. See
+        // issue #282. This might be able to be removed when the
+        // composer doesn't parse recipients as a text list of
+        // addresses.
+        return (name.index_of(",") != -1);
+    }
+
     private static bool local_part_needs_quoting(string local_part) {
         bool needs_quote = false;
         bool is_dot = false;
@@ -74,13 +83,13 @@ public class Geary.RFC822.MailboxAddress :
         return needs_quote || is_dot; // no trailing dots
     }
 
-    private static string quote_local_part(string local_part) {
+    private static string quote_string(string needs_quoting) {
         StringBuilder builder = new StringBuilder();
-        if (!String.is_empty(local_part)) {
+        if (!String.is_empty(needs_quoting)) {
             builder.append_c('"');
             int index = 0;
             for (;;) {
-                char ch = local_part[index++];
+                char ch = needs_quoting[index++];
                 if (ch == String.EOS)
                     break;
 
@@ -251,7 +260,8 @@ public class Geary.RFC822.MailboxAddress :
      * address) and {@link address} parts, suitable for display to
      * people. The string will have white space reduced and
      * non-printable characters removed, and the address will be
-     * surrounded by angle brackets if a name is present.
+     * surrounded by angle brackets if a name is present, and if the
+     * name contains a reserved character, it will be quoted.
      *
      * If you need a form suitable for sending a message, see {@link
      * to_rfc822_string} instead.
@@ -269,6 +279,9 @@ public class Geary.RFC822.MailboxAddress :
      */
     public string to_full_display(string open = "<", string close = ">") {
         string clean_name = Geary.String.reduce_whitespace(this.name);
+        if (display_name_needs_quoting(clean_name)) {
+            clean_name = quote_string(clean_name);
+        }
         string clean_address = Geary.String.reduce_whitespace(this.address);
         return (!has_distinct_name() || is_spoofed())
             ? clean_address
@@ -430,7 +443,7 @@ public class Geary.RFC822.MailboxAddress :
         // manually.
         string local_part = GMime.utils_header_encode_text(this.mailbox);
         if (local_part_needs_quoting(local_part)) {
-            local_part = quote_local_part(local_part);
+            local_part = quote_string(local_part);
         }
         return "%s@%s".printf(
             local_part,
diff --git a/test/engine/rfc822-mailbox-address-test.vala b/test/engine/rfc822-mailbox-address-test.vala
index 1381d366..222b1907 100644
--- a/test/engine/rfc822-mailbox-address-test.vala
+++ b/test/engine/rfc822-mailbox-address-test.vala
@@ -195,6 +195,9 @@ class Geary.RFC822.MailboxAddressTest : TestCase {
                "example example com");
         assert(new MailboxAddress("Test", "example@example example com").to_full_display() ==
                "example@example example com");
+
+        assert(new MailboxAddress("Testerson test", "example@example example com").to_full_display() ==
+               "\"Testerson, Test\" <example@example example com>");
     }
 
     public void to_short_display() throws Error {


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