about summary refs log tree commit homepage
path: root/lib
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2019-12-14 01:02:56 +0000
committerEric Wong <e@80x24.org>2019-12-15 19:43:33 +0000
commit69ae87044ca499aa401c73a9a1d764013539fb2d (patch)
tree64abf40f05e72ec10a30d1e55af29ae31b5638da /lib
parent01d25810ec9baf643e2a278a0fefdf857bfd3883 (diff)
downloadpublic-inbox-69ae87044ca499aa401c73a9a1d764013539fb2d.tar.gz
Email::Address::XS is a dependency of modern versions of Email::MIME,
so it's likely loaded and installed on newer systems, already;
and capable of handling more corner-cases than our pure-Perl
fallback.

We still fallback to the imperfect-but-good-enough-in-practice
pure-Perl code while avoiding the non-XS Email::Address (which
was susceptible to DoS attacks (CVE-2015-7686)).  We just need
to keep "git fast-import" happy.
Diffstat (limited to 'lib')
-rw-r--r--lib/PublicInbox/Address.pm44
-rw-r--r--lib/PublicInbox/AddressPP.pm38
2 files changed, 56 insertions, 26 deletions
diff --git a/lib/PublicInbox/Address.pm b/lib/PublicInbox/Address.pm
index a58d1eff..433b36eb 100644
--- a/lib/PublicInbox/Address.pm
+++ b/lib/PublicInbox/Address.pm
@@ -4,35 +4,27 @@ package PublicInbox::Address;
 use strict;
 use warnings;
 
-# very loose regexes, here.  We don't need RFC-compliance,
-# just enough to make thing sanely displayable and pass to git
+sub xs_emails { map { $_->address() } parse_email_addresses($_[0]) }
 
-sub emails {
-        ($_[0] =~ /([\w\.\+=\?"\(\)\-!#\$%&'\*\/\^\`\|\{\}~]+\@[\w\.\-\(\)]+)
-                (?:\s[^>]*)?>?\s*(?:\(.*?\))?(?:,\s*|\z)/gx)
+sub xs_names {
+        map {
+                my $n = $_->name;
+                $n = $_->user if $n eq $_->address;
+                $n;
+        } parse_email_addresses($_[0]);
 }
 
-sub names {
-        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;
+eval {
+        require Email::Address::XS;
+        Email::Address::XS->import(qw(parse_email_addresses));
+        *emails = \&xs_emails;
+        *names = \&xs_names;
+};
+
+if ($@) {
+        require PublicInbox::AddressPP;
+        *emails = \&PublicInbox::AddressPP::emails;
+        *names = \&PublicInbox::AddressPP::names;
 }
 
 1;
diff --git a/lib/PublicInbox/AddressPP.pm b/lib/PublicInbox/AddressPP.pm
new file mode 100644
index 00000000..cd7aedb9
--- /dev/null
+++ b/lib/PublicInbox/AddressPP.pm
@@ -0,0 +1,38 @@
+# Copyright (C) 2016-2019 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+package PublicInbox::AddressPP;
+use strict;
+
+# very loose regexes, here.  We don't need RFC-compliance,
+# just enough to make thing sanely displayable and pass to git
+# We favor Email::Address::XS for conformance if available
+
+sub emails {
+        ($_[0] =~ /([\w\.\+=\?"\(\)\-!#\$%&'\*\/\^\`\|\{\}~]+\@[\w\.\-\(\)]+)
+                (?:\s[^>]*)?>?\s*(?:\(.*?\))?(?:,\s*|\z)/gx)
+}
+
+sub names {
+        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;