]> Sergey Matveev's repositories - public-inbox.git/commitdiff
address: use comment as name if no phrase available
authorEric Wong <e@80x24.org>
Sat, 14 Dec 2019 01:02:55 +0000 (01:02 +0000)
committerEric Wong <e@80x24.org>
Sun, 15 Dec 2019 19:43:33 +0000 (19:43 +0000)
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.

lib/PublicInbox/Address.pm
t/address.t

index 46aa0da7f9db085a1adb1f13b83b0b74455cd14d..a58d1efff9e38ea9071c9d96e4a4bec0d664e9d7 100644 (file)
@@ -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;
index bea45daafb6f75d4187bd1f2c3a25a4b94df20ed..2a2871029ba6299a6634f247b22b0f261571da7e 100644 (file)
@@ -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');
 }