blib/lib/Email/Address.pm

filter: Statementstmt 90.5% 153 / 169 Branchbran 87.0% 47 / 54 Conditioncond 76.1% 16 / 21 MC/DCmcdc 58.3% 7 / 12 Subroutinesub 73.9% 17 / 23 Podpod 90.0% 9 / 10 totaltotal 86.1% 249 / 289 CC 37 SCAR 19.2
CC
37
Cov
89%
parse
16.0
_format
8.0
name
4.3
CRAP 6.8
↑ Summary
1 10 ▶use v5.12.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Email::Address 1.913;
4 # ABSTRACT: RFC 2822 Address Parsing and Creation
5  
6 10 ▶our $COMMENT_NEST_LEVEL ||= 1;
Condition50%1/2
our $COMMENT_NEST_LEVEL ||= 1
l!l
010
MC/DC0%0/1
our $COMMENT_NEST_LEVEL ||= 1
our $COMMENT_NEST_LEVEL
Truth table50%1/2
A
A= our $COMMENT_NEST_LEVEL
Aresult
11
00
7 10 ▶our $STRINGIFY ||= 'format';
Condition50%1/2
our $STRINGIFY ||= "format"
l!l
010
MC/DC0%0/1
our $STRINGIFY ||= "format"
our $STRINGIFY
Truth table50%1/2
A
A= our $STRINGIFY
Aresult
11
00
8 10 ▶our $COLLAPSE_SPACES //= 1;
Condition50%1/2
our $COLLAPSE_SPACES //= 1
l!l
010
MC/DC0%0/1
our $COLLAPSE_SPACES //= 1
our $COLLAPSE_SPACES
Truth table50%1/2
A
A= our $COLLAPSE_SPACES
Aresult
11
00
9  
10 #pod =head1 SYNOPSIS
11 #pod
12 #pod use Email::Address;
13 #pod
14 #pod my @addresses = Email::Address->parse($line);
15 #pod my $address = Email::Address->new(Casey => 'casey@localhost');
16 #pod
17 #pod print $address->format;
18 #pod
19 #pod =head1 DESCRIPTION
20 #pod
21 #pod This class implements a regex-based RFC 2822 parser that locates email
22 #pod addresses in strings and returns a list of C<Email::Address> objects found.
23 #pod Alternatively you may construct objects manually. The goal of this software is
24 #pod to be correct, and very very fast.
25 #pod
26 #pod Version 1.909 and earlier of this module had vulnerabilies
27 #pod (L<CVE-2015-7686|https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2015-7686>)
28 #pod and (L<CVE-2015-12558|https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2018-12558>)
29 #pod which allowed specially constructed email to cause a denial of service. The
30 #pod reported vulnerabilities and some other pathalogical cases (meaning they really
31 #pod shouldn't occur in normal email) have been addressed in version 1.910 and newer.
32 #pod If you're running version 1.909 or older, you should update!
33 #pod
34 #pod Alternatively, you could switch to L<B<Email::Address::XS>|Email::Address::XS>
35 #pod which has a backward compatible API. B<Why not just use that?>
36 #pod
37 #pod =cut
38  
39 10 my $CTL = q{\x00-\x1F\x7F};
40 10 my $special = q{()<>\\[\\]:;@\\\\,."};
41  
42 10 my $text = qr/[^\x0A\x0D]/;
43  
44 10 my $quoted_pair = qr/\\$text/;
45  
46 10 my $ctext = qr/(?>[^()\\]+)/;
47 10 my ($ccontent, $comment) = (q{})x2;
48 10 for (1 .. $COMMENT_NEST_LEVEL) {
49 10 $ccontent = qr/$ctext|$quoted_pair|$comment/;
50 10 $comment = qr/(?>\s*\((?:\s*$ccontent)*\s*\)\s*)/;
51 }
52 10 my $cfws = qr/$comment|(?>\s+)/;
53  
54 10 my $atext = qq/[^$CTL$special\\s]/;
55 10 my $atom = qr/(?>$cfws*$atext+$cfws*)/;
56 10 my $dot_atom_text = qr/(?>$atext+(?:\.$atext+)*)/;
57 10 my $dot_atom = qr/(?>$cfws*$dot_atom_text$cfws*)/;
58  
59 10 my $qtext = qr/[^\\"]/;
60 10 my $qcontent = qr/$qtext|$quoted_pair/;
61 10 my $quoted_string = qr/(?>$cfws*"$qcontent*"$cfws*)/;
62  
63 10 my $word = qr/$atom|$quoted_string/;
64  
65 # XXX: This ($phrase) used to just be: my $phrase = qr/$word+/; It was changed
66 # to resolve bug 22991, creating a significant slowdown. Given current speed
67 # problems. Once 16320 is resolved, this section should be dealt with.
68 # -- rjbs, 2006-11-11
69 #my $obs_phrase = qr/$word(?:$word|\.|$cfws)*/;
70  
71 # XXX: ...and the above solution caused endless problems (never returned) when
72 # examining this address, now in a test:
73 # admin+=E6=96=B0=E5=8A=A0=E5=9D=A1_Weblog-- ATAT --test.socialtext.com
74 # So we disallow the hateful CFWS in this context for now. Of modern mail
75 # agents, only Apple Web Mail 2.0 is known to produce obs-phrase.
76 # -- rjbs, 2006-11-19
77 10 my $simple_word = qr/(?>$atom|\.|\s*"$qcontent+"\s*)/;
78 10 my $obs_phrase = qr/(?>$simple_word+)/;
79  
80 10 my $phrase = qr/$obs_phrase|(?>$word+)/;
81  
82 10 my $local_part = qr/$dot_atom|$quoted_string/;
83 10 my $dtext = qr/[^\[\]\\]/;
84 10 my $dcontent = qr/$dtext|$quoted_pair/;
85 10 my $domain_literal = qr/(?>$cfws*\[(?:\s*$dcontent)*\s*\]$cfws*)/;
86 10 my $domain = qr/$dot_atom|$domain_literal/;
87  
88 10 my $display_name = $phrase;
89  
90 #pod =head2 Package Variables
91 #pod
92 #pod B<ACHTUNG!> Email isn't easy (if even possible) to parse with a regex, I<at
93 #pod least> if you're on a C<perl> prior to 5.10.0. Providing regular expressions
94 #pod for use by other programs isn't a great idea, because it makes it hard to
95 #pod improve the parser without breaking the "it's a regex" feature. Using these
96 #pod regular expressions is not encouraged, and methods like C<<
97 #pod Email::Address->is_addr_spec >> should be provided in the future.
98 #pod
99 #pod Several regular expressions used in this package are useful to others.
100 #pod For convenience, these variables are declared as package variables that
101 #pod you may access from your program.
102 #pod
103 #pod These regular expressions conform to the rules specified in RFC 2822.
104 #pod
105 #pod You can access these variables using the full namespace. If you want
106 #pod short names, define them yourself.
107 #pod
108 #pod my $addr_spec = $Email::Address::addr_spec;
109 #pod
110 #pod =over 4
111 #pod
112 #pod =item $Email::Address::addr_spec
113 #pod
114 #pod This regular expression defined what an email address is allowed to
115 #pod look like.
116 #pod
117 #pod =item $Email::Address::angle_addr
118 #pod
119 #pod This regular expression defines an C<$addr_spec> wrapped in angle
120 #pod brackets.
121 #pod
122 #pod =item $Email::Address::name_addr
123 #pod
124 #pod This regular expression defines what an email address can look like
125 #pod with an optional preceding display name, also known as the C<phrase>.
126 #pod
127 #pod =item $Email::Address::mailbox
128 #pod
129 #pod This is the complete regular expression defining an RFC 2822 email
130 #pod address with an optional preceding display name and optional
131 #pod following comment.
132 #pod
133 #pod =back
134 #pod
135 #pod =cut
136  
137 10 our $addr_spec = qr/$local_part\@$domain/;
138 10 our $angle_addr = qr/(?>$cfws*<$addr_spec>$cfws*)/;
139 10 our $name_addr = qr/(?>$display_name?)$angle_addr/;
140 10 our $mailbox = qr/(?:$name_addr|$addr_spec)(?>$comment*)/;
141  
142 sub _PHRASE () { 0 }
143 sub _ADDRESS () { 1 }
144 sub _COMMENT () { 2 }
145 sub _ORIGINAL () { 3 }
146 sub _IN_CACHE () { 4 }
147  
148 0 ▶sub __dump {
Subroutine __dump: not called CC=1 SCAR=6.9
149 return {
150 0 phrase => $_[0][_PHRASE],
151 address => $_[0][_ADDRESS],
152 comment => $_[0][_COMMENT],
153 original => $_[0][_ORIGINAL],
154 }
155 }
156  
157 #pod =head2 Class Methods
158 #pod
159 #pod =over
160 #pod
161 #pod =item parse
162 #pod
163 #pod my @addrs = Email::Address->parse(
164 #pod q[me@local, Casey <me@local>, "Casey" <me@local> (West)]
165 #pod );
166 #pod
167 #pod This method returns a list of C<Email::Address> objects it finds in the input
168 #pod string. B<Please note> that it returns a list, and expects that it may find
169 #pod multiple addresses. The behavior in scalar context is undefined.
170 #pod
171 #pod The specification for an email address allows for infinitely nestable comments.
172 #pod That's nice in theory, but a little over done. By default this module allows
173 #pod for one (C<1>) level of nested comments. If you think you need more, modify the
174 #pod C<$Email::Address::COMMENT_NEST_LEVEL> package variable to allow more.
175 #pod
176 #pod $Email::Address::COMMENT_NEST_LEVEL = 10; # I'm deep
177 #pod
178 #pod The reason for this hardly-limiting limitation is simple: efficiency.
179 #pod
180 #pod Long strings of whitespace can be problematic for this module to parse, a bug
181 #pod which has not yet been adequately addressed. The default behavior is now to
182 #pod collapse multiple spaces into a single space, which avoids this problem. To
183 #pod prevent this behavior, set C<$Email::Address::COLLAPSE_SPACES> to zero. This
184 #pod variable will go away when the bug is resolved properly.
185 #pod
186 #pod In accordance with RFC 822 and its descendants, this module demands that email
187 #pod addresses be ASCII only. Any non-ASCII content in the parsed addresses will
188 #pod cause the parser to return no results.
189 #pod
190 #pod =cut
191  
192 10 our (%PARSE_CACHE, %FORMAT_CACHE, %NAME_CACHE);
193 10 my $NOCACHE;
194  
195 118 ▶sub __get_cached_parse {
Subroutine __get_cached_parse: called CC=3 SCAR=11.0
196 118 ▶ return if $NOCACHE;
Branch: 1 branch
BranchTrueFalseTotal
if $NOCACHE 0 118 118
197  
198 118 my ($class, $line) = @_;
199  
200 118 ▶ return @{$PARSE_CACHE{$line}} if exists $PARSE_CACHE{$line};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Email::Address::PARSE_CACHE{$line} 2 116 118
201 116 return;
202 }
203  
204 116 ▶sub __cache_parse {
Subroutine __cache_parse: called CC=2 SCAR=7.1
205 116 ▶ return if $NOCACHE;
Branch: 1 branch
BranchTrueFalseTotal
if $NOCACHE 0 116 116
206  
207 116 my ($class, $line, $addrs) = @_;
208  
209 116 $PARSE_CACHE{$line} = $addrs;
210 }
211  
212 119 ▶sub parse {
Subroutine parse: called CC=16 SCAR=27.7
Pod documented
213 119 my ($class, $line) = @_;
214 119 ▶ return unless $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 1 118 119
215  
216 118 ▶ $line =~ s/[ \t]+/ /g if $COLLAPSE_SPACES;
Branch: 1 branch
BranchTrueFalseTotal
if $Email::Address::COLLAPSE_SPACES 118 0 118
217  
218 118 ▶ if (my @cached = $class->__get_cached_parse($line)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@cached) = $class->__get_cached_parse($line)) 2 116 118
219 2 return @cached;
220 }
221  
222 116 my %mailboxes;
223 116 my $str = $line;
224 116 ▶ $str =~ s!($name_addr(?>$comment*))!$mailboxes{pos($str)} = $1; ',' x length $1!ego
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /$Email::Address::angle_addr/u 109 7 116
225 if $str =~ /$angle_addr/;
226 116 $str =~ s!($addr_spec(?>$comment*))!$mailboxes{pos($str)} = $1; ',' x length $1!ego;
227 116 my @mailboxes = map { $mailboxes{$_} } sort { $a <=> $b } keys %mailboxes;
228  
229 116 my @addrs;
230 116 foreach (@mailboxes) {
231 244 my $original = $_;
232  
233 244 my @comments = /($comment)/go;
234 244 ▶ s/$comment//go if @comments;
Branch: 1 branch
BranchTrueFalseTotal
if @comments 4 240 244
235  
236 244 my ($user, $host, $com);
237 244 ▶ ($user, $host) = ($1, $2) if s/<($local_part)\@($domain)>\s*\z//o;
Branch: 1 branch
BranchTrueFalseTotal
if s/<($local_part)\@($domain)>\s*\z//ou 198 46 244
238 244 ▶ if (! defined($user) || ! defined($host)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $user and defined $host) 46 198 244
Condition66%2/3
defined $user and defined $host
!ll&&!rl&&r
460198
MC/DC50%1/2
defined $user and defined $host
defined $user defined $host
Truth table66%2/3
A and B
A= defined $userB= defined $host
ABresult
0X0
100
111
239 46 s/($local_part)\@($domain)//o;
240 46 ($user, $host) = ($1, $2);
241 }
242  
243 244 ▶ next if $user =~ /\P{ASCII}/;
Branch: 1 branch
BranchTrueFalseTotal
if $user =~ /\P{ASCII}/u 4 240 244
244 240 ▶ next if $host =~ /\P{ASCII}/;
Branch: 1 branch
BranchTrueFalseTotal
if $host =~ /\P{ASCII}/u 1 239 240
245  
246 239 my ($phrase) = /($display_name)/o;
247  
248 239 for ( $phrase, $host, $user, @comments ) {
249 727 ▶ next unless defined $_;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 76 651 727
250 651 s/^\s+//;
251 651 s/\s+$//;
252 651 ▶ $_ = undef unless length $_;
Branch: 1 branch
BranchTrueFalseTotal
unless length $_ 0 651 651
253 }
254  
255 239 ▶ $phrase =~ s/\\(.)/$1/g if $phrase;
Branch: 1 branch
BranchTrueFalseTotal
if $phrase 163 76 239
256  
257 239 my $new_comment = join q{ }, @comments;
258 239 push @addrs,
259 $class->new($phrase, "$user\@$host", $new_comment, $original);
260 239 $addrs[-1]->[_IN_CACHE] = [ \$line, $#addrs ]
261 }
262  
263 116 $class->__cache_parse($line, \@addrs);
264 116 return @addrs;
265 }
266  
267 #pod =item new
268 #pod
269 #pod my $address = Email::Address->new(undef, 'casey@local');
270 #pod my $address = Email::Address->new('Casey West', 'casey@local');
271 #pod my $address = Email::Address->new(undef, 'casey@local', '(Casey)');
272 #pod
273 #pod Constructs and returns a new C<Email::Address> object. Takes four
274 #pod positional arguments: phrase, email, and comment, and original string.
275 #pod
276 #pod The original string should only really be set using C<parse>.
277 #pod
278 #pod =cut
279  
280 693 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
281 693 my ($class, $phrase, $email, $comment, $orig) = @_;
282 693 ▶ $phrase =~ s/\A"(.+)"\z/$1/ if $phrase;
Branch: 1 branch
BranchTrueFalseTotal
if $phrase 477 216 693
283  
284 693 bless [ $phrase, $email, $comment, $orig ] => $class;
285 }
286  
287 #pod =item purge_cache
288 #pod
289 #pod Email::Address->purge_cache;
290 #pod
291 #pod One way this module stays fast is with internal caches. Caches live
292 #pod in memory and there is the remote possibility that you will have a
293 #pod memory problem. On the off chance that you think you're one of those
294 #pod people, this class method will empty those caches.
295 #pod
296 #pod I've loaded over 12000 objects and not encountered a memory problem.
297 #pod
298 #pod =cut
299  
300 0 ▶sub purge_cache {
Subroutine purge_cache: not called CC=1 SCAR=6.9
Pod documented
301 0 %NAME_CACHE = ();
302 0 %FORMAT_CACHE = ();
303 0 %PARSE_CACHE = ();
304 }
305  
306 #pod =item disable_cache
307 #pod
308 #pod =item enable_cache
309 #pod
310 #pod Email::Address->disable_cache if memory_low();
311 #pod
312 #pod If you'd rather not cache address parses at all, you can disable (and
313 #pod re-enable) the Email::Address cache with these methods. The cache is enabled
314 #pod by default.
315 #pod
316 #pod =cut
317  
318 0 ▶sub disable_cache {
Subroutine disable_cache: not called CC=1 SCAR=6.9
Pod documented
319 0 my ($class) = @_;
320 0 $class->purge_cache;
321 0 $NOCACHE = 1;
322 }
323  
324 0 ▶sub enable_cache {
Subroutine enable_cache: not called CC=1 SCAR=6.9
Pod documented
325 0 $NOCACHE = undef;
326 }
327  
328 #pod =back
329 #pod
330 #pod =head2 Instance Methods
331 #pod
332 #pod =over 4
333 #pod
334 #pod =item phrase
335 #pod
336 #pod my $phrase = $address->phrase;
337 #pod $address->phrase( "Me oh my" );
338 #pod
339 #pod Accessor and mutator for the phrase portion of an address.
340 #pod
341 #pod =item address
342 #pod
343 #pod my $addr = $address->address;
344 #pod $addr->address( "me@PROTECTED.com" );
345 #pod
346 #pod Accessor and mutator for the address portion of an address.
347 #pod
348 #pod =item comment
349 #pod
350 #pod my $comment = $address->comment;
351 #pod $address->comment( "(Work address)" );
352 #pod
353 #pod Accessor and mutator for the comment portion of an address.
354 #pod
355 #pod =item original
356 #pod
357 #pod my $orig = $address->original;
358 #pod
359 #pod Accessor for the original address found when parsing, or passed
360 #pod to C<new>.
361 #pod
362 #pod =item host
363 #pod
364 #pod my $host = $address->host;
365 #pod
366 #pod Accessor for the host portion of an address's address.
367 #pod
368 #pod =item user
369 #pod
370 #pod my $user = $address->user;
371 #pod
372 #pod Accessor for the user portion of an address's address.
373 #pod
374 #pod =cut
375  
376 10 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
377 10 my %_INDEX = (
378 phrase => _PHRASE,
379 address => _ADDRESS,
380 comment => _COMMENT,
381 original => _ORIGINAL,
382 );
383  
384 10 for my $method (keys %_INDEX) {
385 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
386 40 my $index = $_INDEX{ $method };
387 229 ▶ *$method = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
388 229 ▶ if ($_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1]) { } 1 228 229
389 1 ▶ if ($_[0][_IN_CACHE]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0][4]) 1 0 1
390 1 my $replicant = bless [ @{$_[0]} ] => ref $_[0];
391 1 $PARSE_CACHE{ ${ $_[0][_IN_CACHE][0] } }[ $_[0][_IN_CACHE][1] ]
392 = $replicant;
393 1 $_[0][_IN_CACHE] = undef;
394 }
395 1 $_[0]->[ $index ] = $_[1];
396 } else {
397 228 $_[0]->[ $index ];
398 }
399 40 };
400 }
401 }
402  
403 0 ▶sub host { ($_[0]->[_ADDRESS] =~ /\@($domain)/o)[0] }
Subroutine host: not called CC=1 SCAR=6.9
Pod documented
404 0 ▶sub user { ($_[0]->[_ADDRESS] =~ /($local_part)\@/o)[0] }
Subroutine user: not called CC=1 SCAR=6.9
Pod documented
405  
406 #pod =pod
407 #pod
408 #pod =item format
409 #pod
410 #pod my $printable = $address->format;
411 #pod
412 #pod Returns a properly formatted RFC 2822 address representing the
413 #pod object.
414 #pod
415 #pod =cut
416  
417 2014 ▶sub format {
Subroutine format: called CC=2 SCAR=6.9
Pod documented
418 10 ▶ my $cache_str = do { no warnings 'uninitialized'; "@{$_[0]}" };
Subroutine BEGIN: called CC=1 SCAR=0.0
419 2014 ▶ return $FORMAT_CACHE{$cache_str} if exists $FORMAT_CACHE{$cache_str};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Email::Address::FORMAT_CACHE{$cache_str} 1680 334 2014
420 334 $FORMAT_CACHE{$cache_str} = $_[0]->_format;
421 }
422  
423 334 ▶sub _format {
Subroutine _format: called CC=8 SCAR=20.8
424 334 my ($self) = @_;
425  
426 334 ▶ unless (length $self->[_PHRASE] || length $self->[_COMMENT]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $self->[0] or length $self->[2]) 86 248 334
Condition100%3/3
length $self->[0] or length $self->[2]
l!l&&r!l&&!r
247186
MC/DC100%2/2
length $self->[0] or length $self->[2]
length $self->[0] length $self->[2]
Truth table100%3/3
A or B
A= length $self->[0]B= length $self->[2]
ABresult
1X1
011
000
427 86 ▶ return $self->[_ADDRESS] // '';
Condition100%2/2
$self->[1] // ""
l!l
851
MC/DC100%1/1
$self->[1] // ""
$self->[1]
Truth table100%2/2
A
A= $self->[1]
Aresult
11
00
428 }
429  
430 248 ▶ my $comment = $self->[_COMMENT] // '';
Condition100%2/2
$self->[2] // ""
l!l
16385
MC/DC100%1/1
$self->[2] // ""
$self->[2]
Truth table100%2/2
A
A= $self->[2]
Aresult
11
00
431 248 ▶ $comment = "($comment)" if length $comment and $comment !~ /\A\(.*\)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if length $comment and not $comment =~ /\A\(.*\)\z/u 3 245 248
Condition100%3/3
length $comment and not $comment =~ /\A\(.*\)\z/u
!ll&&!rl&&r
24323
MC/DC100%2/2
length $comment and not $comment =~ /\A\(.*\)\z/u
length $comment not $comment =~ /\A\(.*\)\z/u
Truth table100%3/3
A and not(B)
A= length $commentB= not $comment =~ /\A\(.*\)\z/u
ABresult
0X0
100
111
432  
433 248 ▶ my $format = sprintf q{%s <%s> %s},
Condition50%1/2
$self->[1] // ""
l!l
2480
MC/DC0%0/1
$self->[1] // ""
$self->[1]
Truth table50%1/2
A
A= $self->[1]
Aresult
11
00
434 $self->_enquoted_phrase,
435 ($self->[_ADDRESS] // ''),
436 $comment;
437  
438 248 $format =~ s/^\s+//;
439 248 $format =~ s/\s+$//;
440  
441 248 return $format;
442 }
443  
444 248 ▶sub _enquoted_phrase {
Subroutine _enquoted_phrase: called CC=3 SCAR=11.0
445 248 my ($self) = @_;
446  
447 248 my $phrase = $self->[_PHRASE];
448  
449 248 ▶ return '' unless length $phrase;
Branch: 1 branch
BranchTrueFalseTotal
unless length $phrase 1 247 248
450  
451 # if it's encoded -- rjbs, 2007-02-28
452 247 ▶ return $phrase if $phrase =~ /\A=\?.+\?=\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $phrase =~ /\A=\?.+\?=\z/u 1 246 247
453  
454 246 $phrase =~ s/\A"(.+)"\z/$1/;
455 246 $phrase =~ s/([\\"])/\\$1/g;
456  
457 246 return qq{"$phrase"};
458 }
459  
460 #pod =item name
461 #pod
462 #pod my $name = $address->name;
463 #pod
464 #pod This method tries very hard to determine the name belonging to the address.
465 #pod First the C<phrase> is checked. If that doesn't work out the C<comment>
466 #pod is looked into. If that still doesn't work out, the C<user> portion of
467 #pod the C<address> is returned.
468 #pod
469 #pod This method does B<not> try to massage any name it identifies and instead
470 #pod leaves that up to someone else. Who is it to decide if someone wants their
471 #pod name capitalized, or if they're Irish?
472 #pod
473 #pod =cut
474  
475 663 ▶sub name {
Subroutine name: called CC=4 SCAR=14.5
Pod documented
476 10 ▶ my $cache_str = do { no warnings 'uninitialized'; "@{$_[0]}" };
Subroutine BEGIN: called CC=1 SCAR=0.0
477 663 ▶ return $NAME_CACHE{$cache_str} if exists $NAME_CACHE{$cache_str};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Email::Address::NAME_CACHE{$cache_str} 349 314 663
478  
479 314 my ($self) = @_;
480 314 my $name = q{};
481 314 ▶ if ( $name = $self->[_PHRASE] ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($name = $self->[0]) { } 233 81 314
elsif ($name = $self->[2]) { } 0 81 81
482 233 $name =~ s/^"//;
483 233 $name =~ s/"$//;
484 0 $name =~ s/($quoted_pair)/substr $1, -1/goe;
485 } elsif ( $name = $self->[_COMMENT] ) {
486 0 $name =~ s/^\(//;
487 0 $name =~ s/\)$//;
488 0 $name =~ s/($quoted_pair)/substr $1, -1/goe;
489 0 $name =~ s/$comment/ /go;
490 } else {
491 81 ($name) = $self->[_ADDRESS] =~ /($local_part)\@/o;
492 }
493 314 $NAME_CACHE{$cache_str} = $name;
494 }
495  
496 #pod =back
497 #pod
498 #pod =head2 Overloaded Operators
499 #pod
500 #pod =over 4
501 #pod
502 #pod =item stringify
503 #pod
504 #pod print "I have your email address, $address.";
505 #pod
506 #pod Objects stringify to C<format> by default. It's possible that you don't
507 #pod like that idea. Okay, then, you can change it by modifying
508 #pod C<$Email:Address::STRINGIFY>. Please consider modifying this package
509 #pod variable using C<local>. You might step on someone else's toes if you
510 #pod don't.
511 #pod
512 #pod {
513 #pod local $Email::Address::STRINGIFY = 'host';
514 #pod print "I have your address, $address.";
515 #pod # geeknest.com
516 #pod }
517 #pod print "I have your address, $address.";
518 #pod # "Casey West" <casey@geeknest.com>
519 #pod
520 #pod Modifying this package variable is now deprecated. Subclassing is now the
521 #pod recommended approach.
522 #pod
523 #pod =cut
524  
525 669 ▶sub as_string {
Subroutine as_string: called CC=2 SCAR=7.2
Pod undocumented
526 669 ▶ warn 'altering $Email::Address::STRINGIFY is deprecated; subclass instead'
Branch: 1 branch
BranchTrueFalseTotal
if $Email::Address::STRINGIFY ne "format" 0 669 669
527 if $STRINGIFY ne 'format';
528  
529 669 $_[0]->can($STRINGIFY)->($_[0]);
530 }
531  
532 10 ▶use overload '""' => 'as_string', fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
533  
534 #pod =pod
535 #pod
536 #pod =back
537 #pod
538 #pod =cut
539  
540 10 1;
541  
542 =pod
543  
544 =encoding UTF-8
545  
546 =head1 NAME
547  
548 Email::Address - RFC 2822 Address Parsing and Creation
549  
550 =head1 VERSION
551  
552 version 1.913
553  
554 =head1 SYNOPSIS
555  
556 use Email::Address;
557  
558 my @addresses = Email::Address->parse($line);
559 my $address = Email::Address->new(Casey => 'casey@localhost');
560  
561 print $address->format;
562  
563 =head1 DESCRIPTION
564  
565 This class implements a regex-based RFC 2822 parser that locates email
566 addresses in strings and returns a list of C<Email::Address> objects found.
567 Alternatively you may construct objects manually. The goal of this software is
568 to be correct, and very very fast.
569  
570 Version 1.909 and earlier of this module had vulnerabilies
571 (L<CVE-2015-7686|https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2015-7686>)
572 and (L<CVE-2015-12558|https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2018-12558>)
573 which allowed specially constructed email to cause a denial of service. The
574 reported vulnerabilities and some other pathalogical cases (meaning they really
575 shouldn't occur in normal email) have been addressed in version 1.910 and newer.
576 If you're running version 1.909 or older, you should update!
577  
578 Alternatively, you could switch to L<B<Email::Address::XS>|Email::Address::XS>
579 which has a backward compatible API. B<Why not just use that?>
580  
581 =head2 Package Variables
582  
583 B<ACHTUNG!> Email isn't easy (if even possible) to parse with a regex, I<at
584 least> if you're on a C<perl> prior to 5.10.0. Providing regular expressions
585 for use by other programs isn't a great idea, because it makes it hard to
586 improve the parser without breaking the "it's a regex" feature. Using these
587 regular expressions is not encouraged, and methods like C<<
588 Email::Address->is_addr_spec >> should be provided in the future.
589  
590 Several regular expressions used in this package are useful to others.
591 For convenience, these variables are declared as package variables that
592 you may access from your program.
593  
594 These regular expressions conform to the rules specified in RFC 2822.
595  
596 You can access these variables using the full namespace. If you want
597 short names, define them yourself.
598  
599 my $addr_spec = $Email::Address::addr_spec;
600  
601 =over 4
602  
603 =item $Email::Address::addr_spec
604  
605 This regular expression defined what an email address is allowed to
606 look like.
607  
608 =item $Email::Address::angle_addr
609  
610 This regular expression defines an C<$addr_spec> wrapped in angle
611 brackets.
612  
613 =item $Email::Address::name_addr
614  
615 This regular expression defines what an email address can look like
616 with an optional preceding display name, also known as the C<phrase>.
617  
618 =item $Email::Address::mailbox
619  
620 This is the complete regular expression defining an RFC 2822 email
621 address with an optional preceding display name and optional
622 following comment.
623  
624 =back
625  
626 =head2 Class Methods
627  
628 =over
629  
630 =item parse
631  
632 my @addrs = Email::Address->parse(
633 q[me@local, Casey <me@local>, "Casey" <me@local> (West)]
634 );
635  
636 This method returns a list of C<Email::Address> objects it finds in the input
637 string. B<Please note> that it returns a list, and expects that it may find
638 multiple addresses. The behavior in scalar context is undefined.
639  
640 The specification for an email address allows for infinitely nestable comments.
641 That's nice in theory, but a little over done. By default this module allows
642 for one (C<1>) level of nested comments. If you think you need more, modify the
643 C<$Email::Address::COMMENT_NEST_LEVEL> package variable to allow more.
644  
645 $Email::Address::COMMENT_NEST_LEVEL = 10; # I'm deep
646  
647 The reason for this hardly-limiting limitation is simple: efficiency.
648  
649 Long strings of whitespace can be problematic for this module to parse, a bug
650 which has not yet been adequately addressed. The default behavior is now to
651 collapse multiple spaces into a single space, which avoids this problem. To
652 prevent this behavior, set C<$Email::Address::COLLAPSE_SPACES> to zero. This
653 variable will go away when the bug is resolved properly.
654  
655 In accordance with RFC 822 and its descendants, this module demands that email
656 addresses be ASCII only. Any non-ASCII content in the parsed addresses will
657 cause the parser to return no results.
658  
659 =item new
660  
661 my $address = Email::Address->new(undef, 'casey@local');
662 my $address = Email::Address->new('Casey West', 'casey@local');
663 my $address = Email::Address->new(undef, 'casey@local', '(Casey)');
664  
665 Constructs and returns a new C<Email::Address> object. Takes four
666 positional arguments: phrase, email, and comment, and original string.
667  
668 The original string should only really be set using C<parse>.
669  
670 =item purge_cache
671  
672 Email::Address->purge_cache;
673  
674 One way this module stays fast is with internal caches. Caches live
675 in memory and there is the remote possibility that you will have a
676 memory problem. On the off chance that you think you're one of those
677 people, this class method will empty those caches.
678  
679 I've loaded over 12000 objects and not encountered a memory problem.
680  
681 =item disable_cache
682  
683 =item enable_cache
684  
685 Email::Address->disable_cache if memory_low();
686  
687 If you'd rather not cache address parses at all, you can disable (and
688 re-enable) the Email::Address cache with these methods. The cache is enabled
689 by default.
690  
691 =back
692  
693 =head2 Instance Methods
694  
695 =over 4
696  
697 =item phrase
698  
699 my $phrase = $address->phrase;
700 $address->phrase( "Me oh my" );
701  
702 Accessor and mutator for the phrase portion of an address.
703  
704 =item address
705  
706 my $addr = $address->address;
707 $addr->address( "me@PROTECTED.com" );
708  
709 Accessor and mutator for the address portion of an address.
710  
711 =item comment
712  
713 my $comment = $address->comment;
714 $address->comment( "(Work address)" );
715  
716 Accessor and mutator for the comment portion of an address.
717  
718 =item original
719  
720 my $orig = $address->original;
721  
722 Accessor for the original address found when parsing, or passed
723 to C<new>.
724  
725 =item host
726  
727 my $host = $address->host;
728  
729 Accessor for the host portion of an address's address.
730  
731 =item user
732  
733 my $user = $address->user;
734  
735 Accessor for the user portion of an address's address.
736  
737 =item format
738  
739 my $printable = $address->format;
740  
741 Returns a properly formatted RFC 2822 address representing the
742 object.
743  
744 =item name
745  
746 my $name = $address->name;
747  
748 This method tries very hard to determine the name belonging to the address.
749 First the C<phrase> is checked. If that doesn't work out the C<comment>
750 is looked into. If that still doesn't work out, the C<user> portion of
751 the C<address> is returned.
752  
753 This method does B<not> try to massage any name it identifies and instead
754 leaves that up to someone else. Who is it to decide if someone wants their
755 name capitalized, or if they're Irish?
756  
757 =back
758  
759 =head2 Overloaded Operators
760  
761 =over 4
762  
763 =item stringify
764  
765 print "I have your email address, $address.";
766  
767 Objects stringify to C<format> by default. It's possible that you don't
768 like that idea. Okay, then, you can change it by modifying
769 C<$Email:Address::STRINGIFY>. Please consider modifying this package
770 variable using C<local>. You might step on someone else's toes if you
771 don't.
772  
773 {
774 local $Email::Address::STRINGIFY = 'host';
775 print "I have your address, $address.";
776 # geeknest.com
777 }
778 print "I have your address, $address.";
779 # "Casey West" <casey@geeknest.com>
780  
781 Modifying this package variable is now deprecated. Subclassing is now the
782 recommended approach.
783  
784 =back
785  
786 =head2 Did I Mention Fast?
787  
788 On his 1.8GHz Apple MacBook, rjbs gets these results:
789  
790 $ perl -Ilib bench/ea-vs-ma.pl bench/corpus.txt 5
791 Rate Mail::Address Email::Address
792 Mail::Address 2.59/s -- -44%
793 Email::Address 4.59/s 77% --
794  
795 $ perl -Ilib bench/ea-vs-ma.pl bench/corpus.txt 25
796 Rate Mail::Address Email::Address
797 Mail::Address 2.58/s -- -67%
798 Email::Address 7.84/s 204% --
799  
800 $ perl -Ilib bench/ea-vs-ma.pl bench/corpus.txt 50
801 Rate Mail::Address Email::Address
802 Mail::Address 2.57/s -- -70%
803 Email::Address 8.53/s 232% --
804  
805 ...unfortunately, a known bug causes a loss of speed the string to parse has
806 certain known characteristics, and disabling cache will also degrade
807 performance.
808  
809 =head1 PERL VERSION
810  
811 This library should run on perls released even a long time ago. It should work
812 on any version of perl released in the last five years.
813  
814 Although it may work on older versions of perl, no guarantee is made that the
815 minimum required version will not be increased. The version may be increased
816 for any reason, and there is no promise that patches will be accepted to lower
817 the minimum required perl.
818  
819 =head1 ACKNOWLEDGEMENTS
820  
821 Thanks to Kevin Riggle and Tatsuhiko Miyagawa for tests for annoying
822 phrase-quoting bugs!
823  
824 =head1 AUTHORS
825  
826 =over 4
827  
828 =item *
829  
830 Casey West
831  
832 =item *
833  
834 Ricardo SIGNES <cpan@semiotic.systems>
835  
836 =back
837  
838 =head1 CONTRIBUTORS
839  
840 =for stopwords Alex Vandiver David Golden Steinbrunner Glenn Fowler Jim Brandt Kevin Falcone Pali Ricardo Signes Ruslan Zakirov sunnavy William Yardley
841  
842 =over 4
843  
844 =item *
845  
846 Alex Vandiver <alex@chmrr.net>
847  
848 =item *
849  
850 David Golden <dagolden@cpan.org>
851  
852 =item *
853  
854 David Steinbrunner <dsteinbrunner@pobox.com>
855  
856 =item *
857  
858 Glenn Fowler <cebjyre@cpan.org>
859  
860 =item *
861  
862 Jim Brandt <jbrandt@bestpractical.com>
863  
864 =item *
865  
866 Kevin Falcone <kevin@jibsheet.com>
867  
868 =item *
869  
870 Pali <pali@cpan.org>
871  
872 =item *
873  
874 Ricardo Signes <rjbs@semiotic.systems>
875  
876 =item *
877  
878 Ruslan Zakirov <ruz@bestpractical.com>
879  
880 =item *
881  
882 sunnavy <sunnavy@bestpractical.com>
883  
884 =item *
885  
886 William Yardley <pep@veggiechinese.net>
887  
888 =back
889  
890 =head1 COPYRIGHT AND LICENSE
891  
892 This software is copyright (c) 2004 by Casey West.
893  
894 This is free software; you can redistribute it and/or modify it under
895 the same terms as the Perl 5 programming language system itself.
896  
897 =cut
898  
899 __END__
↑ Summary