]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/AddressPP.pm
treewide: run update-copyrights from gnulib for 2019
[public-inbox.git] / lib / PublicInbox / AddressPP.pm
1 # Copyright (C) 2016-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 package PublicInbox::AddressPP;
4 use strict;
5
6 # very loose regexes, here.  We don't need RFC-compliance,
7 # just enough to make thing sanely displayable and pass to git
8 # We favor Email::Address::XS for conformance if available
9
10 sub emails {
11         ($_[0] =~ /([\w\.\+=\?"\(\)\-!#\$%&'\*\/\^\`\|\{\}~]+\@[\w\.\-\(\)]+)
12                 (?:\s[^>]*)?>?\s*(?:\(.*?\))?(?:,\s*|\z)/gx)
13 }
14
15 sub names {
16         my @p = split(/<?([^@<>]+)\@[\w\.\-]+>?\s*(\(.*?\))?(?:,\s*|\z)/,
17                         $_[0]);
18         my @ret;
19         for (my $i = 0; $i <= $#p;) {
20                 my $phrase = $p[$i++];
21                 $phrase =~ tr/\r\n\t / /s;
22                 $phrase =~ s/\A['"\s]*//;
23                 $phrase =~ s/['"\s]*\z//;
24                 my $user = $p[$i++] // '';
25                 my $comment = $p[$i++] // '';
26                 if ($phrase =~ /\S/) {
27                         $phrase =~ s/\@\S+\z//;
28                         push @ret, $phrase;
29                 } elsif ($comment =~ /\A\((.*?)\)\z/) {
30                         push @ret, $1;
31                 } else {
32                         push @ret, $user;
33                 }
34         }
35         @ret;
36 }
37
38 1;