about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2019-12-14 01:02:55 +0000
committerEric Wong <e@80x24.org>2019-12-15 19:43:33 +0000
commit01d25810ec9baf643e2a278a0fefdf857bfd3883 (patch)
tree1bfcc07f7d52a7d12c70cc8f6ee81de2fcc697b5
parent2ad19256bc285e40188a9058941883979ff45c86 (diff)
downloadpublic-inbox-01d25810ec9baf643e2a278a0fefdf857bfd3883.tar.gz
Some users will set their From: headers in the form of:
"<user@example.com> (A U Thor)", where their name is in
the parenthesized comment.  Use that instead of the
email address, if available.
-rw-r--r--lib/PublicInbox/Address.pm30
-rw-r--r--t/address.t7
2 files changed, 24 insertions, 13 deletions
diff --git a/lib/PublicInbox/Address.pm b/lib/PublicInbox/Address.pm
index 46aa0da7..a58d1eff 100644
--- a/lib/PublicInbox/Address.pm
+++ b/lib/PublicInbox/Address.pm
@@ -13,16 +13,26 @@ sub emails {
 }
 
 sub names {
-        map {
-                tr/\r\n\t/ /;
-                s/\s*<([^<]+)\z//;
-                my $e = $1;
-                s/\A['"\s]*//;
-                s/['"\s]*\z//;
-                $e = $_ =~ /\S/ ? $_ : $e;
-                $e =~ s/\@\S+\z//;
-                $e;
-        } split(/\@+[\w\.\-]+>?\s*(?:\(.*?\))?(?:,\s*|\z)/, $_[0]);
+        my @p = split(/<?([^@<>]+)\@[\w\.\-]+>?\s*(\(.*?\))?(?:,\s*|\z)/,
+                        $_[0]);
+        my @ret;
+        for (my $i = 0; $i <= $#p;) {
+                my $phrase = $p[$i++];
+                $phrase =~ tr/\r\n\t / /s;
+                $phrase =~ s/\A['"\s]*//;
+                $phrase =~ s/['"\s]*\z//;
+                my $user = $p[$i++] // '';
+                my $comment = $p[$i++] // '';
+                if ($phrase =~ /\S/) {
+                        $phrase =~ s/\@\S+\z//;
+                        push @ret, $phrase;
+                } elsif ($comment =~ /\A\((.*?)\)\z/) {
+                        push @ret, $1;
+                } else {
+                        push @ret, $user;
+                }
+        }
+        @ret;
 }
 
 1;
diff --git a/t/address.t b/t/address.t
index bea45daa..2a287102 100644
--- a/t/address.t
+++ b/t/address.t
@@ -14,8 +14,9 @@ is_deeply(['user@example.com'],
         'comment after domain accepted before >');
 
 my @names = PublicInbox::Address::names(
-        'User <e@e>, e@e, "John A. Doe" <j@d>, <x@x>');
-is_deeply(['User', 'e', 'John A. Doe', 'x'], \@names,
+        'User <e@e>, e@e, "John A. Doe" <j@d>, <x@x>, <y@x> (xyz), '.
+        'U Ser <u@x> (do not use)');
+is_deeply(\@names, ['User', 'e', 'John A. Doe', 'x', 'xyz', 'U Ser'],
         'name extraction works as expected');
 
 @names = PublicInbox::Address::names('"user@example.com" <user@example.com>');
@@ -25,7 +26,7 @@ is_deeply(['user'], \@names, 'address-as-name extraction works as expected');
 {
         my $backwards = 'u@example.com (John Q. Public)';
         @names = PublicInbox::Address::names($backwards);
-        is_deeply(\@names, ['u'], 'backwards name OK');
+        is_deeply(\@names, ['John Q. Public'], 'backwards name OK');
         my @emails = PublicInbox::Address::emails($backwards);
         is_deeply(\@emails, ['u@example.com'], 'backwards emails OK');
 }