From 69ae87044ca499aa401c73a9a1d764013539fb2d Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 14 Dec 2019 01:02:56 +0000 Subject: address: use Email::Address::XS if available 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. --- lib/PublicInbox/AddressPP.pm | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 lib/PublicInbox/AddressPP.pm (limited to 'lib/PublicInbox/AddressPP.pm') 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 +# License: AGPL-3.0+ +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; -- cgit v1.2.3-24-ge0c7