]> Sergey Matveev's repositories - public-inbox.git/commitdiff
address: beef up the module with name list extaction
authorEric Wong <e@80x24.org>
Sat, 25 Jun 2016 07:19:10 +0000 (07:19 +0000)
committerEric Wong <e@80x24.org>
Sat, 25 Jun 2016 09:38:31 +0000 (09:38 +0000)
We may remove from_name in the future.

...And disallow quotes in email addresses.
Technically I believe they're allowed, but they're definitely
uncommon and unlikely to show up in legitimate mail.

MANIFEST
lib/PublicInbox/Address.pm
t/address.t [new file with mode: 0644]

index 834cb5ddd33e7761e099a5bb452315cbcd8cf3d4..2156caf7021f0f04e593f174f2ed9a90ec9c4244 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -99,6 +99,7 @@ scripts/import_slrnspool
 scripts/report-spam
 scripts/slrnspool2maildir
 scripts/ssoma-replay
+t/address.t
 t/cgi.t
 t/check-www-inbox.perl
 t/common.perl
index 772adedb29b3ed1546a647181a31609e5f3d7766..abba43d1f03ef25d032ee31abf07a78767d40e97 100644 (file)
@@ -7,7 +7,18 @@ use warnings;
 # very loose regexes, here.  We don't need RFC-compliance,
 # just enough to make thing sanely displayable and pass to git
 
-sub emails { ($_[0] =~ /([^<\s,]+\@[^>\s,]+)/g) }
+sub emails { ($_[0] =~ /([\w\.\+=\-]+\@[\w\.\-]+)>?\s*(?:,\s*|\z)/g) }
+
+sub names {
+       map {
+               tr/\r\n\t/ /;
+               s/\s*<([^<]+)\z//;
+               my $e = $1;
+               s/\A['"\s]*//;
+               s/['"\s]*\z//;
+               $_ =~ /\S/ ? $_ : $e;
+       } split(/\@+[\w\.\-]+>?\s*(?:,\s*|\z)/, $_[0]);
+}
 
 sub from_name {
        my ($val) = @_;
diff --git a/t/address.t b/t/address.t
new file mode 100644 (file)
index 0000000..c488a8e
--- /dev/null
@@ -0,0 +1,21 @@
+# Copyright (C) 2016 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+use strict;
+use warnings;
+use Test::More;
+use_ok 'PublicInbox::Address';
+
+is_deeply([qw(e@example.com e@example.org)],
+       [PublicInbox::Address::emails('User <e@example.com>, e@example.org')],
+       'address extraction works as expected');
+
+is_deeply([PublicInbox::Address::emails('"ex@example.com" <ex@example.com>')],
+       [qw(ex@example.com)]);
+
+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,
+       'name extraction works as expected');
+
+
+done_testing;