blib/lib/Email/Address.pm

filter: Statementstmt 90.6% 155 / 171 Branchbran 85.4% 53 / 62 Conditioncond 81.8% 18 / 22 MC/DCmcdc 50.0% 6 / 12 Subroutinesub 73.9% 17 / 23 Podpod 90.0% 9 / 10 totaltotal 86.0% 258 / 300 SCAR 37.5
CC
40
Cov
89%
parse
16.0
_format
10.0
name
4.3
CRAP 42.4
↑ Summary
1 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Email::Address;
4 # ABSTRACT: RFC 2822 Address Parsing and Creation
5 10 $Email::Address::VERSION = '1.912';
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 unless defined $COLLAPSE_SPACES; # I miss //=
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Email::Address::COLLAPSE_SPACES 10 0 10
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.
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 sub __dump {
149 return {
150 0 ▶ phrase => $_[0][_PHRASE],
Subroutine __dump: not called CC=1 SCAR=6.9
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 sub __get_cached_parse {
196 118 ▶ return if $NOCACHE;
Subroutine __get_cached_parse: called CC=3 SCAR=11.0
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 sub __cache_parse {
205 116 ▶ return if $NOCACHE;
Subroutine __cache_parse: called CC=2 SCAR=7.1
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 sub parse {
213 119 ▶ my ($class, $line) = @_;
Subroutine parse: called CC=16 SCAR=27.7
Pod documented
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/ 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//o 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}/ 4 240 244
244 240 ▶ next if $host =~ /\P{ASCII}/;
Branch: 1 branch
BranchTrueFalseTotal
if $host =~ /\P{ASCII}/ 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 sub new {
281 693 ▶ my ($class, $phrase, $email, $comment, $orig) = @_;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
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 sub purge_cache {
301 0 ▶ %NAME_CACHE = ();
Subroutine purge_cache: not called CC=1 SCAR=6.9
Pod documented
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 sub disable_cache {
319 0 ▶ my ($class) = @_;
Subroutine disable_cache: not called CC=1 SCAR=6.9
Pod documented
320 0 $class->purge_cache;
321 0 $NOCACHE = 1;
322 }
323  
324 sub enable_cache {
325 0 ▶ $NOCACHE = undef;
Subroutine enable_cache: not called CC=1 SCAR=6.9
Pod documented
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 BEGIN {
377 10 ▶ my %_INDEX = (
Subroutine BEGIN: called CC=2 SCAR=6.9
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 *$method = sub {
388 229 ▶ if ($_[1]) {
Subroutine __ANON__: called CC=3 SCAR=11.0
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 sub format {
418 10 ▶ my $cache_str = do { no warnings 'uninitialized'; "@{$_[0]}" };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine format: called CC=2 SCAR=6.9
Pod documented
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 sub _format {
424 334 ▶ my ($self) = @_;
Subroutine _format: called CC=10 SCAR=23.0
425  
426 334 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->[0] and length $self->[0] or defined $self->[2] and length $self->[2]) 86 248 334
Condition100%9/9
defined $self->[0] and length $self->[0] or defined $self->[2] and length $self->[2]
l!l&&r!l&&!r
247186
defined $self->[0] and length $self->[0]
!ll&&!rl&&r
861247
defined $self->[2] and length $self->[2]
!ll&&!rl&&r
15711
MC/DC50%2/4
defined $self->[0] and length $self->[0] or defined $self->[2] and length $self->[2]
defined $self->[0] length $self->[0] defined $self->[2] length $self->[2]
Truth table42%3/7
A and B or C and D
A= defined $self->[0]B= length $self->[0]C= defined $self->[2]D= length $self->[2]
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
427 defined $self->[_PHRASE] && length $self->[_PHRASE]
428 ||
429 defined $self->[_COMMENT] && length $self->[_COMMENT]
430 ) {
431 86 ▶ return defined $self->[_ADDRESS] ? $self->[_ADDRESS] : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->[1] ? : 85 1 86
432 }
433  
434 248 ▶ my $comment = defined $self->[_COMMENT] ? $self->[_COMMENT] : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->[2] ? : 163 85 248
435 248 ▶ $comment = "($comment)" if length $comment and $comment !~ /\A\(.*\)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if length $comment and not $comment =~ /\A\(.*\)\z/ 3 245 248
Condition100%3/3
length $comment and not $comment =~ /\A\(.*\)\z/
!ll&&!rl&&r
24323
MC/DC100%2/2
length $comment and not $comment =~ /\A\(.*\)\z/
length $comment not $comment =~ /\A\(.*\)\z/
Truth table100%3/3
A and not(B)
A= length $commentB= not $comment =~ /\A\(.*\)\z/
ABresult
0X0
100
111
436  
437 248 ▶ my $format = sprintf q{%s <%s> %s},
Branch: 1 branch
BranchTrueFalseTotal
defined $self->[1] ? : 248 0 248
438 $self->_enquoted_phrase,
439 (defined $self->[_ADDRESS] ? $self->[_ADDRESS] : ''),
440 $comment;
441  
442 248 $format =~ s/^\s+//;
443 248 $format =~ s/\s+$//;
444  
445 248 return $format;
446 }
447  
448 sub _enquoted_phrase {
449 248 ▶ my ($self) = @_;
Subroutine _enquoted_phrase: called CC=4 SCAR=13.9
450  
451 248 my $phrase = $self->[_PHRASE];
452  
453 248 ▶ return '' unless defined $phrase and length $phrase;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $phrase and length $phrase 1 247 248
Condition66%2/3
defined $phrase and length $phrase
!ll&&!rl&&r
10247
MC/DC50%1/2
defined $phrase and length $phrase
defined $phrase length $phrase
Truth table66%2/3
A and B
A= defined $phraseB= length $phrase
ABresult
0X0
100
111
454  
455 # if it's encoded -- rjbs, 2007-02-28
456 247 ▶ return $phrase if $phrase =~ /\A=\?.+\?=\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $phrase =~ /\A=\?.+\?=\z/ 1 246 247
457  
458 246 $phrase =~ s/\A"(.+)"\z/$1/;
459 246 $phrase =~ s/([\\"])/\\$1/g;
460  
461 246 return qq{"$phrase"};
462 }
463  
464 #pod =item name
465 #pod
466 #pod my $name = $address->name;
467 #pod
468 #pod This method tries very hard to determine the name belonging to the address.
469 #pod First the C<phrase> is checked. If that doesn't work out the C<comment>
470 #pod is looked into. If that still doesn't work out, the C<user> portion of
471 #pod the C<address> is returned.
472 #pod
473 #pod This method does B<not> try to massage any name it identifies and instead
474 #pod leaves that up to someone else. Who is it to decide if someone wants their
475 #pod name capitalized, or if they're Irish?
476 #pod
477 #pod =cut
478  
479 sub name {
480 10 ▶ my $cache_str = do { no warnings 'uninitialized'; "@{$_[0]}" };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine name: called CC=4 SCAR=14.5
Pod documented
481 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
482  
483 314 my ($self) = @_;
484 314 my $name = q{};
485 314 ▶ if ( $name = $self->[_PHRASE] ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($name = $self->[0]) { } 233 81 314
elsif ($name = $self->[2]) { } 0 81 81
486 233 $name =~ s/^"//;
487 233 $name =~ s/"$//;
488 233 $name =~ s/($quoted_pair)/substr $1, -1/goe;
489 } elsif ( $name = $self->[_COMMENT] ) {
490 0 $name =~ s/^\(//;
491 0 $name =~ s/\)$//;
492 0 $name =~ s/($quoted_pair)/substr $1, -1/goe;
493 0 $name =~ s/$comment/ /go;
494 } else {
495 81 ($name) = $self->[_ADDRESS] =~ /($local_part)\@/o;
496 }
497 314 $NAME_CACHE{$cache_str} = $name;
498 }
499  
500 #pod =back
501 #pod
502 #pod =head2 Overloaded Operators
503 #pod
504 #pod =over 4
505 #pod
506 #pod =item stringify
507 #pod
508 #pod print "I have your email address, $address.";
509 #pod
510 #pod Objects stringify to C<format> by default. It's possible that you don't
511 #pod like that idea. Okay, then, you can change it by modifying
512 #pod C<$Email:Address::STRINGIFY>. Please consider modifying this package
513 #pod variable using C<local>. You might step on someone else's toes if you
514 #pod don't.
515 #pod
516 #pod {
517 #pod local $Email::Address::STRINGIFY = 'host';
518 #pod print "I have your address, $address.";
519 #pod # geeknest.com
520 #pod }
521 #pod print "I have your address, $address.";
522 #pod # "Casey West" <casey@geeknest.com>
523 #pod
524 #pod Modifying this package variable is now deprecated. Subclassing is now the
525 #pod recommended approach.
526 #pod
527 #pod =cut
528  
529 sub as_string {
530 669 ▶ warn 'altering $Email::Address::STRINGIFY is deprecated; subclass instead'
Subroutine as_string: called CC=2 SCAR=7.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if $Email::Address::STRINGIFY ne "format" 0 669 669
531 if $STRINGIFY ne 'format';
532  
533 669 $_[0]->can($STRINGIFY)->($_[0]);
534 }
535  
536 10 ▶use overload '""' => 'as_string', fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
537  
538 #pod =pod
539 #pod
540 #pod =back
541 #pod
542 #pod =cut
543  
544 10 1;
545  
546 =pod
547  
548 =encoding UTF-8
549  
550 =head1 NAME
551  
552 Email::Address - RFC 2822 Address Parsing and Creation
553  
554 =head1 VERSION
555  
556 version 1.912
557  
558 =head1 SYNOPSIS
559  
560 use Email::Address;
561  
562 my @addresses = Email::Address->parse($line);
563 my $address = Email::Address->new(Casey => 'casey@localhost');
564  
565 print $address->format;
566  
567 =head1 DESCRIPTION
568  
569 This class implements a regex-based RFC 2822 parser that locates email
570 addresses in strings and returns a list of C<Email::Address> objects found.
571 Alternatively you may construct objects manually. The goal of this software is
572 to be correct, and very very fast.
573  
574 Version 1.909 and earlier of this module had vulnerabilies
575 (L<CVE-2015-7686|https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2015-7686>)
576 and (L<CVE-2015-12558|https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2018-12558>)
577 which allowed specially constructed email to cause a denial of service. The
578 reported vulnerabilities and some other pathalogical cases (meaning they really
579 shouldn't occur in normal email) have been addressed in version 1.910 and newer.
580 If you're running version 1.909 or older, you should update!
581  
582 Alternatively, you could switch to L<B<Email::Address::XS>|Email::Address::XS>
583 which has a backward compatible API.
584  
585 =head2 Package Variables
586  
587 B<ACHTUNG!> Email isn't easy (if even possible) to parse with a regex, I<at
588 least> if you're on a C<perl> prior to 5.10.0. Providing regular expressions
589 for use by other programs isn't a great idea, because it makes it hard to
590 improve the parser without breaking the "it's a regex" feature. Using these
591 regular expressions is not encouraged, and methods like C<<
592 Email::Address->is_addr_spec >> should be provided in the future.
593  
594 Several regular expressions used in this package are useful to others.
595 For convenience, these variables are declared as package variables that
596 you may access from your program.
597  
598 These regular expressions conform to the rules specified in RFC 2822.
599  
600 You can access these variables using the full namespace. If you want
601 short names, define them yourself.
602  
603 my $addr_spec = $Email::Address::addr_spec;
604  
605 =over 4
606  
607 =item $Email::Address::addr_spec
608  
609 This regular expression defined what an email address is allowed to
610 look like.
611  
612 =item $Email::Address::angle_addr
613  
614 This regular expression defines an C<$addr_spec> wrapped in angle
615 brackets.
616  
617 =item $Email::Address::name_addr
618  
619 This regular expression defines what an email address can look like
620 with an optional preceding display name, also known as the C<phrase>.
621  
622 =item $Email::Address::mailbox
623  
624 This is the complete regular expression defining an RFC 2822 email
625 address with an optional preceding display name and optional
626 following comment.
627  
628 =back
629  
630 =head2 Class Methods
631  
632 =over
633  
634 =item parse
635  
636 my @addrs = Email::Address->parse(
637 q[me@local, Casey <me@local>, "Casey" <me@local> (West)]
638 );
639  
640 This method returns a list of C<Email::Address> objects it finds in the input
641 string. B<Please note> that it returns a list, and expects that it may find
642 multiple addresses. The behavior in scalar context is undefined.
643  
644 The specification for an email address allows for infinitely nestable comments.
645 That's nice in theory, but a little over done. By default this module allows
646 for one (C<1>) level of nested comments. If you think you need more, modify the
647 C<$Email::Address::COMMENT_NEST_LEVEL> package variable to allow more.
648  
649 $Email::Address::COMMENT_NEST_LEVEL = 10; # I'm deep
650  
651 The reason for this hardly-limiting limitation is simple: efficiency.
652  
653 Long strings of whitespace can be problematic for this module to parse, a bug
654 which has not yet been adequately addressed. The default behavior is now to
655 collapse multiple spaces into a single space, which avoids this problem. To
656 prevent this behavior, set C<$Email::Address::COLLAPSE_SPACES> to zero. This
657 variable will go away when the bug is resolved properly.
658  
659 In accordance with RFC 822 and its descendants, this module demands that email
660 addresses be ASCII only. Any non-ASCII content in the parsed addresses will
661 cause the parser to return no results.
662  
663 =item new
664  
665 my $address = Email::Address->new(undef, 'casey@local');
666 my $address = Email::Address->new('Casey West', 'casey@local');
667 my $address = Email::Address->new(undef, 'casey@local', '(Casey)');
668  
669 Constructs and returns a new C<Email::Address> object. Takes four
670 positional arguments: phrase, email, and comment, and original string.
671  
672 The original string should only really be set using C<parse>.
673  
674 =item purge_cache
675  
676 Email::Address->purge_cache;
677  
678 One way this module stays fast is with internal caches. Caches live
679 in memory and there is the remote possibility that you will have a
680 memory problem. On the off chance that you think you're one of those
681 people, this class method will empty those caches.
682  
683 I've loaded over 12000 objects and not encountered a memory problem.
684  
685 =item disable_cache
686  
687 =item enable_cache
688  
689 Email::Address->disable_cache if memory_low();
690  
691 If you'd rather not cache address parses at all, you can disable (and
692 re-enable) the Email::Address cache with these methods. The cache is enabled
693 by default.
694  
695 =back
696  
697 =head2 Instance Methods
698  
699 =over 4
700  
701 =item phrase
702  
703 my $phrase = $address->phrase;
704 $address->phrase( "Me oh my" );
705  
706 Accessor and mutator for the phrase portion of an address.
707  
708 =item address
709  
710 my $addr = $address->address;
711 $addr->address( "me@PROTECTED.com" );
712  
713 Accessor and mutator for the address portion of an address.
714  
715 =item comment
716  
717 my $comment = $address->comment;
718 $address->comment( "(Work address)" );
719  
720 Accessor and mutator for the comment portion of an address.
721  
722 =item original
723  
724 my $orig = $address->original;
725  
726 Accessor for the original address found when parsing, or passed
727 to C<new>.
728  
729 =item host
730  
731 my $host = $address->host;
732  
733 Accessor for the host portion of an address's address.
734  
735 =item user
736  
737 my $user = $address->user;
738  
739 Accessor for the user portion of an address's address.
740  
741 =item format
742  
743 my $printable = $address->format;
744  
745 Returns a properly formatted RFC 2822 address representing the
746 object.
747  
748 =item name
749  
750 my $name = $address->name;
751  
752 This method tries very hard to determine the name belonging to the address.
753 First the C<phrase> is checked. If that doesn't work out the C<comment>
754 is looked into. If that still doesn't work out, the C<user> portion of
755 the C<address> is returned.
756  
757 This method does B<not> try to massage any name it identifies and instead
758 leaves that up to someone else. Who is it to decide if someone wants their
759 name capitalized, or if they're Irish?
760  
761 =back
762  
763 =head2 Overloaded Operators
764  
765 =over 4
766  
767 =item stringify
768  
769 print "I have your email address, $address.";
770  
771 Objects stringify to C<format> by default. It's possible that you don't
772 like that idea. Okay, then, you can change it by modifying
773 C<$Email:Address::STRINGIFY>. Please consider modifying this package
774 variable using C<local>. You might step on someone else's toes if you
775 don't.
776  
777 {
778 local $Email::Address::STRINGIFY = 'host';
779 print "I have your address, $address.";
780 # geeknest.com
781 }
782 print "I have your address, $address.";
783 # "Casey West" <casey@geeknest.com>
784  
785 Modifying this package variable is now deprecated. Subclassing is now the
786 recommended approach.
787  
788 =back
789  
790 =head2 Did I Mention Fast?
791  
792 On his 1.8GHz Apple MacBook, rjbs gets these results:
793  
794 $ perl -Ilib bench/ea-vs-ma.pl bench/corpus.txt 5
795 Rate Mail::Address Email::Address
796 Mail::Address 2.59/s -- -44%
797 Email::Address 4.59/s 77% --
798  
799 $ perl -Ilib bench/ea-vs-ma.pl bench/corpus.txt 25
800 Rate Mail::Address Email::Address
801 Mail::Address 2.58/s -- -67%
802 Email::Address 7.84/s 204% --
803  
804 $ perl -Ilib bench/ea-vs-ma.pl bench/corpus.txt 50
805 Rate Mail::Address Email::Address
806 Mail::Address 2.57/s -- -70%
807 Email::Address 8.53/s 232% --
808  
809 ...unfortunately, a known bug causes a loss of speed the string to parse has
810 certain known characteristics, and disabling cache will also degrade
811 performance.
812  
813 =head1 ACKNOWLEDGEMENTS
814  
815 Thanks to Kevin Riggle and Tatsuhiko Miyagawa for tests for annoying
816 phrase-quoting bugs!
817  
818 =head1 AUTHORS
819  
820 =over 4
821  
822 =item *
823  
824 Casey West
825  
826 =item *
827  
828 Ricardo SIGNES <rjbs@cpan.org>
829  
830 =back
831  
832 =head1 CONTRIBUTORS
833  
834 =for stopwords Alex Vandiver David Golden Steinbrunner Glenn Fowler Jim Brandt Kevin Falcone Pali Ruslan Zakirov sunnavy William Yardley
835  
836 =over 4
837  
838 =item *
839  
840 Alex Vandiver <alex@chmrr.net>
841  
842 =item *
843  
844 David Golden <dagolden@cpan.org>
845  
846 =item *
847  
848 David Steinbrunner <dsteinbrunner@pobox.com>
849  
850 =item *
851  
852 Glenn Fowler <cebjyre@cpan.org>
853  
854 =item *
855  
856 Jim Brandt <jbrandt@bestpractical.com>
857  
858 =item *
859  
860 Kevin Falcone <kevin@jibsheet.com>
861  
862 =item *
863  
864 Pali <pali@cpan.org>
865  
866 =item *
867  
868 Ruslan Zakirov <ruz@bestpractical.com>
869  
870 =item *
871  
872 sunnavy <sunnavy@bestpractical.com>
873  
874 =item *
875  
876 William Yardley <pep@veggiechinese.net>
877  
878 =back
879  
880 =head1 COPYRIGHT AND LICENSE
881  
882 This software is copyright (c) 2004 by Casey West.
883  
884 This is free software; you can redistribute it and/or modify it under
885 the same terms as the Perl 5 programming language system itself.
886  
887 =cut
888  
889 __END__
↑ Summary