Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions lib/Email/Address.pm
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ $angle_addr = qr/$cfws*<$addr_spec>$cfws*/;
$name_addr = qr/$display_name?$angle_addr/;
$mailbox = qr/(?:$name_addr|$addr_spec)$comment*/;

my $capturing_mailbox = qr/
(?:($display_name)?($cfws*)<($addr_spec)>($cfws*)
|($addr_spec)
)($comment*)
/x;

sub _PHRASE () { 0 }
sub _ADDRESS () { 1 }
sub _COMMENT () { 2 }
Expand Down Expand Up @@ -213,36 +219,31 @@ sub parse {
return @cached;
}

my (@mailboxes) = ($line =~ /$mailbox/go);
my (@mailboxes) = ($line =~ /($capturing_mailbox)/go);
my @addrs;
foreach (@mailboxes) {
my $original = $_;

my @comments = /($comment)/go;
s/$comment//go if @comments;
while (my @list = splice @mailboxes, 0, 7) {
my ($original, $phrase, $address, @comments)
= ($list[0], $list[1], $list[3]||$list[5], @list[2,4,6]);

my ($user, $host, $com);
($user, $host) = ($1, $2) if s/<($local_part)\@($domain)>//o;
if (! defined($user) || ! defined($host)) {
s/($local_part)\@($domain)//o;
($user, $host) = ($1, $2);
}
return if $address =~ /\P{ASCII}/;

return if $user =~ /\P{ASCII}/;
return if $host =~ /\P{ASCII}/;
if ( defined $phrase ) {
# for backwards compatibility
unshift @comments, $phrase =~ /($comment)/go;
$phrase =~ s/$comment//go;

my ($phrase) = /($display_name)/o;
$phrase =~ s/\\(.)/$1/g if $phrase =~ s/\A\s*"(.*)"\s*\z/$1/;
}

for ( $phrase, $host, $user, @comments ) {
for ( $phrase, $address, @comments ) {
next unless defined $_;
s/^\s+//;
s/\s+$//;
$_ = undef unless length $_;
}

my $new_comment = join q{ }, @comments;
push @addrs,
$class->new($phrase, "$user\@$host", $new_comment, $original);
my $new_comment = join q{ }, grep defined, @comments;
push @addrs, $class->new($phrase, $address, $new_comment, $original);
$addrs[-1]->[_IN_CACHE] = [ \$line, $#addrs ]
}

Expand Down Expand Up @@ -441,7 +442,7 @@ sub _enquoted_phrase {
return $phrase if $phrase =~ /\A=\?.+\?=\z/;

$phrase =~ s/\A"(.+)"\z/$1/;
$phrase =~ s/\"/\\"/g;
$phrase =~ s/([\\"])/\\$1/g;

return qq{"$phrase"};
}
Expand Down
9 changes: 8 additions & 1 deletion t/quoting.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use strict;

use Email::Address;
use Test::More tests => 6;
use Test::More tests => 8;

my $phrase = q{jack!work};
my $email = 'jack@work.com';
Expand Down Expand Up @@ -36,3 +36,10 @@ is(
);

is($ea3->phrase, $phrase, "the phrase method returns the right thing");

{
my $mailbox = q{"jack \\"\\\\\\" robinson" <jack@work.com>};
my ($ea) = Email::Address->parse($mailbox);
is $ea->phrase, q{jack "\\" robinson};
is $ea->format, q{"jack \\"\\\\\\" robinson" <jack@work.com>};
}
32 changes: 31 additions & 1 deletion t/tests.t
Original file line number Diff line number Diff line change
Expand Up @@ -1618,7 +1618,37 @@ my @list = (
undef,
],
],
]
],
[
'"Newsletter from <superm-- ATAT --rket>" <newsletter-- ATAT --example.com>',
[
[
"Newsletter from <superm-- ATAT --rket>",
'newsletter-- ATAT --example.com',
undef,
],
],
],
[
'"Lawrence \\"Yogi\\" Berra" <yogi-- ATAT --example.com>',
[
[
'Lawrence "Yogi" Berra',
'yogi-- ATAT --example.com',
undef,
],
],
],
[
'"Peter \Sales Department" <peter-- ATAT --example.com>',
[
[
"Peter Sales Department",
'peter-- ATAT --example.com',
undef,
],
],
],
);

my $tests = 1;
Expand Down