Mail/Address.pm

filter: Statementstmt 92.3% 132 / 143 Branchbran 75.5% 68 / 90 Conditioncond 75.0% 48 / 64 MC/DCmcdc 51.3% 19 / 37 Subroutinesub 82.3% 14 / 17 Podpod 81.8% 9 / 11 totaltotal 80.1% 290 / 362 CC 69 SCAR 24.4
CC
69
Cov
84%
format
18.3
parse
17.1
_tokenise
12.2
CRAP 11.5
« Mail/Cap.pm ↑ Summary Mail/Mailer/testfile.pm »
1 # Copyrights 1995-2024 by [Mark Overmeer <markov@cpan.org>].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.03.
5 # This code is part of the bundle MailTools. Meta-POD processed with
6 # OODoc into POD and HTML manual-pages. See README.md for Copyright.
7 # Licensed under the same terms as Perl itself.
8  
9 package Mail::Address;{
10 3 our $VERSION = '2.22';
11 }
12  
13 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # use locale; removed in version 1.78, because it causes taint problems
18  
19 0 ▶sub Version { our $VERSION }
Subroutine Version: not called CC=1 SCAR=6.9
Pod undocumented
20  
21  
22  
23 # given a comment, attempt to extract a person's name
24 sub _extract_name
25 49 ▶{ # This function can be called as method as well
Subroutine _extract_name: called CC=8 SCAR=20.8
26 49 ▶ my $self = @_ && ref $_[0] ? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] ? : 44 5 49
Condition66%2/3
@_ and ref $_[0]
!ll&&!rl&&r
0544
MC/DC50%1/2
@_ and ref $_[0]
@_ ref $_[0]
Truth table66%2/3
A and B
A= @_B= ref $_[0]
ABresult
0X0
100
111
27  
28 49 ▶ local $_ = shift
Branch: 1 branch
BranchTrueFalseTotal
local $_ = shift() or return "" 38 11 49
29 or return '';
30  
31 # Using encodings, too hard. See Mail::Message::Field::Full.
32 38 ▶ return '' if m/\=\?.*?\?\=/;
Branch: 1 branch
BranchTrueFalseTotal
if /\=\?.*?\?\=/ 0 38 38
33  
34 # trim whitespace
35 38 s/^\s+//;
36 38 s/\s+$//;
37 38 s/\s+/ /;
38  
39 # Disregard numeric names (e.g. 123456.1234@compuserve.com)
40 38 ▶ return "" if /^[\d ]+$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^[\d ]+$/ 0 38 38
41  
42 38 s/^\((.*)\)$/$1/; # remove outermost parenthesis
43 38 s/^"(.*)"$/$1/; # remove outer quotation marks
44 38 s/\(.*?\)//g; # remove minimal embedded comments
45 38 s/\\//g; # remove all escapes
46 38 s/^"(.*)"$/$1/; # remove internal quotation marks
47 38 s/^([^\s]+) ?, ?(.*)$/$2 $1/; # reverse "Last, First M." if applicable
48 38 s/,.*//;
49  
50 # Change casing only when the name contains only upper or only
51 # lower cased characters.
52 38 ▶ unless( m/[A-Z]/ && m/[a-z]/ )
Branch: 1 branch
BranchTrueFalseTotal
unless (/[A-Z]/ and /[a-z]/) 9 29 38
Condition100%3/3
/[A-Z]/ and /[a-z]/
!ll&&!rl&&r
7229
MC/DC100%2/2
/[A-Z]/ and /[a-z]/
/[A-Z]/ /[a-z]/
Truth table100%3/3
A and B
A= /[A-Z]/B= /[a-z]/
ABresult
0X0
100
111
53 { # Set the case of the name to first char upper rest lower
54 9 s/\b(\w+)/\L\u$1/igo; # Upcase first letter on name
55 9 s/\bMc(\w)/Mc\u$1/igo; # Scottish names such as 'McLeod'
56 9 s/\bo'(\w)/O'\u$1/igo; # Irish names such as 'O'Malley, O'Reilly'
57 9 s/\b(x*(ix)?v*(iv)?i*)\b/\U$1/igo; # Roman numerals, eg 'Level III Support'
58 }
59  
60 # some cleanup
61 38 s/\[[^\]]*\]//g;
62 38 s/(^[\s'"]+|[\s'"]+$)//g;
63 38 s/\s{2,}/ /g;
64  
65 38 $_;
66 }
67  
68 sub _tokenise
69 44 ▶{ local $_ = join ',', @_;
Subroutine _tokenise: called CC=12 SCAR=25.1
70 44 my (@words,$snippet,$field);
71  
72 44 s/\A\s+//;
73 44 s/[\r\n]+/ /g;
74  
75 44 ▶ while ($_ ne '')
Branch: 1 branch
BranchTrueFalseTotal
if ($_ ne "") 437 44 481
76 437 { $field = '';
77 437 ▶ if(s/^\s*\(/(/ ) # (...)
Branch: 1 branch
BranchTrueFalseTotal
if (s/^\s*\(/(/) 18 419 437
78 18 { my $depth = 0;
79  
80 18 ▶ PAREN: while(s/^(\(([^\(\)\\]|\\.)*)//)
Branch: 1 branch
BranchTrueFalseTotal
if (s/^(\(([^\(\)\\]|\\.)*)//) 23 0 23
81 23 { $field .= $1;
82 23 $depth++;
83 23 ▶ while(s/^(([^\(\)\\]|\\.)*\)\s*)//)
Branch: 1 branch
BranchTrueFalseTotal
if (s/^(([^\(\)\\]|\\.)*\)\s*)//) 23 5 28
84 23 { $field .= $1;
85 23 ▶ last PAREN unless --$depth;
Branch: 1 branch
BranchTrueFalseTotal
unless --$depth 18 5 23
86 5 ▶ $field .= $1 if s/^(([^\(\)\\]|\\.)+)//;
Branch: 1 branch
BranchTrueFalseTotal
if s/^(([^\(\)\\]|\\.)+)// 1 4 5
87 }
88 }
89  
90 18 ▶ carp "Unmatched () '$field' '$_'"
Branch: 1 branch
BranchTrueFalseTotal
if $depth 0 18 18
91 if $depth;
92  
93 18 $field =~ s/\s+\Z//;
94 18 push @words, $field;
95  
96 18 next;
97 }
98  
99 419 ▶ if( s/^("(?:[^"\\]+|\\.)*")\s*// # "..."
Branch: 1 branch
BranchTrueFalseTotal
if (s/^("(?:[^"\\]+|\\.)*")\s*// or s/^(\[(?:[^\]\\]+|\\.)*\])\s*// or s/^([^\s()<>\@,;:\\".[\]]+)\s*// or s/^([()<>\@,;:\\".[\]])\s*//) 419 0 419
Condition77%7/9
s/^("(?:[^"\\]+|\\.)*")\s*// or s/^(\[(?:[^\]\\]+|\\.)*\])\s*// or s/^([^\s()<>\@,;:\\".[\]]+)\s*// or s/^([()<>\@,;:\\".[\]])\s*//
l!l&&r!l&&!r
2281910
s/^("(?:[^"\\]+|\\.)*")\s*// or s/^(\[(?:[^\]\\]+|\\.)*\])\s*// or s/^([^\s()<>\@,;:\\".[\]]+)\s*//
l!l&&r!l&&!r
10218191
s/^("(?:[^"\\]+|\\.)*")\s*// or s/^(\[(?:[^\]\\]+|\\.)*\])\s*//
l!l&&r!l&&!r
100409
MC/DC0%0/4
s/^("(?:[^"\\]+|\\.)*")\s*// or s/^(\[(?:[^\]\\]+|\\.)*\])\s*// or s/^([^\s()<>\@,;:\\".[\]]+)\s*// or s/^([()<>\@,;:\\".[\]])\s*//
s/^("(?:[^"\\]+|\\.)*")\s*// s/^(\[(?:[^\]\\]+|\\.)*\])\s*// s/^([^\s()<>\@,;:\\".[\]]+)\s*// s/^([()<>\@,;:\\".[\]])\s*//
Truth table60%3/5
A or B or C or D
A= s/^("(?:[^"\\]+|\\.)*")\s*//B= s/^(\[(?:[^\]\\]+|\\.)*\])\s*//C= s/^([^\s()<>\@,;:\\".[\]]+)\s*//D= s/^([()<>\@,;:\\".[\]])\s*//
ABCDresult
1XXX1
01XX1
001X1
00011
00000
100 || s/^(\[(?:[^\]\\]+|\\.)*\])\s*// # [...]
101 || s/^([^\s()<>\@,;:\\".[\]]+)\s*//
102 || s/^([()<>\@,;:\\".[\]])\s*//
103 )
104 419 { push @words, $1;
105 419 next;
106 }
107  
108 0 croak "Unrecognised line: $_";
109 }
110  
111 44 push @words, ",";
112 44 \@words;
113 }
114  
115 sub _find_next
116 92 ▶{ my ($idx, $tokens, $len) = @_;
Subroutine _find_next: called CC=5 SCAR=16.1
117  
118 92 ▶ while($idx < $len)
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < $len) 304 44 348
119 304 { my $c = $tokens->[$idx];
120 304 ▶ return $c if $c eq ',' || $c eq ';' || $c eq '<';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "," or $c eq ";" or $c eq "<" 48 256 304
Condition100%6/6
$c eq "," or $c eq ";" or $c eq "<"
l!l&&r!l&&!r
2820256
$c eq "," or $c eq ";"
l!l&&r!l&&!r
271276
MC/DC100%3/3
$c eq "," or $c eq ";" or $c eq "<"
$c eq "," $c eq ";" $c eq "<"
Truth table100%4/4
A or B or C
A= $c eq ","B= $c eq ";"C= $c eq "<"
ABCresult
1XX1
01X1
0011
0000
121 256 $idx++;
122 }
123  
124 44 "";
125 }
126  
127 sub _complete
128 51 ▶{ my ($class, $phrase, $address, $comment) = @_;
Subroutine _complete: called CC=4 SCAR=14.0
129  
130 51 ▶ @$phrase || @$comment || @$address
Branch: 1 branch
BranchTrueFalseTotal
@$phrase or @$comment or @$address or return undef 51 0 51
Condition83%5/6
@$phrase or @$comment or @$address
l!l&&r!l&&!r
35160
@$phrase or @$comment
l!l&&r!l&&!r
191616
MC/DC0%0/3
@$phrase or @$comment or @$address
@$phrase @$comment @$address
Truth table75%3/4
A or B or C
A= @$phraseB= @$commentC= @$address
ABCresult
1XX1
01X1
0011
0000
131 or return undef;
132  
133 51 my $o = $class->new(join(" ",@$phrase), join("",@$address), join(" ",@$comment));
134 51 @$phrase = @$address = @$comment = ();
135 51 $o;
136 }
137  
138 #------------
139  
140 sub new(@)
141 51 ▶{ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
142 51 bless [@_], $class;
143 }
144  
145  
146 sub parse(@)
147 44 ▶{ my $class = shift;
Subroutine parse: called CC=17 SCAR=28.4
Pod documented
148 44 my @line = grep {defined} @_;
149 44 my $line = join '', @line;
150  
151 44 my (@phrase, @comment, @address, @objs);
152 44 my ($depth, $idx) = (0, 0);
153  
154 44 my $tokens = _tokenise @line;
155 44 my $len = @$tokens;
156 44 my $next = _find_next $idx, $tokens, $len;
157  
158 44 local $_;
159 44 ▶ for(my $idx = 0; $idx < $len; $idx++)
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < $len) 481 44 525
160 481 { $_ = $tokens->[$idx];
161  
162 481 ▶ if(substr($_,0,1) eq '(') { push @comment, $_ }
Branch: 7 branches
BranchTrueFalseTotal
if (substr($_, 0, 1) eq '(') { } 18 463 481
elsif ($_ eq '<') { } 20 443 463
elsif ($_ eq '>') { } 20 423 443
elsif ($_ eq ',' or $_ eq ';') { } 48 375 423
elsif ($depth) { } 137 238 375
elsif ($next eq '<') { } 32 206 238
elsif (/^[.\@:;]$/ or not @address or $address[-1] =~ /^[.\@:;]$/) { } 203 3 206
163 20 elsif($_ eq '<') { $depth++ }
164 20 ▶ elsif($_ eq '>') { $depth-- if $depth }
Branch: 1 branch
BranchTrueFalseTotal
if $depth 20 0 20
165 ▶ elsif($_ eq ',' || $_ eq ';')
Condition100%3/3
$_ eq "," or $_ eq ";"
l!l&&r!l&&!r
471375
MC/DC100%2/2
$_ eq "," or $_ eq ";"
$_ eq "," $_ eq ";"
Truth table100%3/3
A or B
A= $_ eq ","B= $_ eq ";"
ABresult
1X1
011
000
166 48 ▶ { warn "Unmatched '<>' in $line" if $depth;
Branch: 1 branch
BranchTrueFalseTotal
if $depth 0 48 48
167 48 my $o = $class->_complete(\@phrase, \@address, \@comment);
168 48 ▶ push @objs, $o if defined $o;
Branch: 1 branch
BranchTrueFalseTotal
if defined $o 48 0 48
169 48 $depth = 0;
170 48 $next = _find_next $idx+1, $tokens, $len;
171 }
172 137 elsif($depth) { push @address, $_ }
173 32 elsif($next eq '<') { push @phrase, $_ }
174 ▶ elsif( /^[.\@:;]$/ || !@address || $address[-1] =~ /^[.\@:;]$/ )
Condition100%6/6
/^[.\@:;]$/ or not @address or $address[-1] =~ /^[.\@:;]$/
l!l&&r!l&&!r
115883
/^[.\@:;]$/ or not @address
l!l&&r!l&&!r
882791
MC/DC100%3/3
/^[.\@:;]$/ or not @address or $address[-1] =~ /^[.\@:;]$/
/^[.\@:;]$/ not @address $address[-1] =~ /^[.\@:;]$/
Truth table100%4/4
A or not(B) or C
A= /^[.\@:;]$/B= not @addressC= $address[-1] =~ /^[.\@:;]$/
ABCresult
1XX1
01X1
0011
0000
175 203 { push @address, $_ }
176 else
177 3 ▶ { warn "Unmatched '<>' in $line" if $depth;
Branch: 1 branch
BranchTrueFalseTotal
if $depth 0 3 3
178 3 my $o = $class->_complete(\@phrase, \@address, \@comment);
179 3 ▶ push @objs, $o if defined $o;
Branch: 1 branch
BranchTrueFalseTotal
if defined $o 3 0 3
180 3 $depth = 0;
181 3 push @address, $_;
182 }
183 }
184 44 @objs;
185 }
186  
187 #------------
188  
189 44 ▶sub phrase { shift->set_or_get(0, @_) }
Subroutine phrase: called CC=1 SCAR=0.0
Pod documented
190 44 ▶sub address { shift->set_or_get(1, @_) }
Subroutine address: called CC=1 SCAR=0.0
Pod documented
191 26 ▶sub comment { shift->set_or_get(2, @_) }
Subroutine comment: called CC=1 SCAR=0.0
Pod documented
192  
193 sub set_or_get($)
194 114 ▶{ my ($self, $i) = (shift, shift);
Subroutine set_or_get: called CC=3 SCAR=17.3
Pod undocumented
195 114 ▶ @_ or return $self->[$i];
Branch: 1 branch
BranchTrueFalseTotal
@_ or return $self->[$i] 0 114 114
196  
197 0 my $val = $self->[$i];
198 0 ▶ $self->[$i] = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
199 0 $val;
200 }
201  
202  
203 3 my $atext = '[\-\w !#$%&\'*+/=?^`{|}~]';
204 sub format
205 44 ▶{ my @addrs;
Subroutine format: called CC=15 SCAR=29.1
Pod documented
206  
207 44 foreach (@_)
208 44 { my ($phrase, $email, $comment) = @$_;
209 44 my @addr;
210  
211 44 ▶ if(defined $phrase && length $phrase)
Branch: 2 branches
BranchTrueFalseTotal
if (defined $phrase and length $phrase) { } 18 26 44
elsif (defined $email and length $email) { } 26 0 26
Condition66%2/3
defined $phrase and length $phrase
!ll&&!rl&&r
02618
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
212 18 ▶ { push @addr
Branch: 2 branches
BranchTrueFalseTotal
$phrase =~ /^(?:\s*$atext\s*)+$/o ? : 9 9 18
$phrase =~ /(?<!\\)"/ ? : 9 0 9
213 , $phrase =~ /^(?:\s*$atext\s*)+$/o ? $phrase
214 : $phrase =~ /(?<!\\)"/ ? $phrase
215 : qq("$phrase");
216  
217 18 ▶ push @addr, "<$email>"
Branch: 1 branch
BranchTrueFalseTotal
if defined $email and length $email 18 0 18
Condition33%1/3
defined $email and length $email
!ll&&!rl&&r
0018
MC/DC0%0/2
defined $email and length $email
defined $email length $email
Truth table33%1/3
A and B
A= defined $emailB= length $email
ABresult
0X0
100
111
218 if defined $email && length $email;
219 }
220 ▶ elsif(defined $email && length $email)
Condition33%1/3
defined $email and length $email
!ll&&!rl&&r
0026
MC/DC0%0/2
defined $email and length $email
defined $email length $email
Truth table33%1/3
A and B
A= defined $emailB= length $email
ABresult
0X0
100
111
221 26 { push @addr, $email;
222 }
223  
224 44 ▶ if(defined $comment && $comment =~ /\S/)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $comment and $comment =~ /\S/) 16 28 44
Condition66%2/3
defined $comment and $comment =~ /\S/
!ll&&!rl&&r
02816
MC/DC50%1/2
defined $comment and $comment =~ /\S/
defined $comment $comment =~ /\S/
Truth table66%2/3
A and B
A= defined $commentB= $comment =~ /\S/
ABresult
0X0
100
111
225 16 { $comment =~ s/^\s*\(?/(/;
226 16 $comment =~ s/\)?\s*$/)/;
227 }
228  
229 44 ▶ push @addr, $comment
Branch: 1 branch
BranchTrueFalseTotal
if defined $comment and length $comment 16 28 44
Condition66%2/3
defined $comment and length $comment
!ll&&!rl&&r
02816
MC/DC50%1/2
defined $comment and length $comment
defined $comment length $comment
Truth table66%2/3
A and B
A= defined $commentB= length $comment
ABresult
0X0
100
111
230 if defined $comment && length $comment;
231  
232 44 ▶ push @addrs, join(" ", @addr)
Branch: 1 branch
BranchTrueFalseTotal
if @addr 44 0 44
233 if @addr;
234 }
235  
236 44 join ", ", @addrs;
237 }
238  
239 #------------
240  
241 sub name
242 44 ▶{ my $self = shift;
Subroutine name: called CC=8 SCAR=20.8
Pod documented
243 44 my $phrase = $self->phrase;
244 44 my $addr = $self->address;
245  
246 44 ▶ $phrase = $self->comment
Branch: 1 branch
BranchTrueFalseTotal
unless defined $phrase and length $phrase 26 18 44
Condition66%2/3
defined $phrase and length $phrase
!ll&&!rl&&r
02618
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
247 unless defined $phrase && length $phrase;
248  
249 44 my $name = $self->_extract_name($phrase);
250  
251 # first.last@domain address
252 44 ▶ if($name eq '' && $addr =~ /([^\%\.\@_]+([\._][^\%\.\@_]+)+)[\@\%]/)
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "" and $addr =~ /([^\%\.\@_]+([\._][^\%\.\@_]+)+)[\@\%]/) 4 40 44
Condition100%3/3
$name eq "" and $addr =~ /([^\%\.\@_]+([\._][^\%\.\@_]+)+)[\@\%]/
!ll&&!rl&&r
3374
MC/DC100%2/2
$name eq "" and $addr =~ /([^\%\.\@_]+([\._][^\%\.\@_]+)+)[\@\%]/
$name eq "" $addr =~ /([^\%\.\@_]+([\._][^\%\.\@_]+)+)[\@\%]/
Truth table100%3/3
A and B
A= $name eq ""B= $addr =~ /([^\%\.\@_]+([\._][^\%\.\@_]+)+)[\@\%]/
ABresult
0X0
100
111
253 4 { ($name = $1) =~ s/[\._]+/ /g;
254 4 $name = _extract_name $name;
255 }
256  
257 44 ▶ if($name eq '' && $addr =~ m#/g=#i) # X400 style address
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "" and $addr =~ m[/g=]i) 1 43 44
Condition100%3/3
$name eq "" and $addr =~ m[/g=]i
!ll&&!rl&&r
3761
MC/DC100%2/2
$name eq "" and $addr =~ m[/g=]i
$name eq "" $addr =~ m[/g=]i
Truth table100%3/3
A and B
A= $name eq ""B= $addr =~ m[/g=]i
ABresult
0X0
100
111
258 1 { my ($f) = $addr =~ m#g=([^/]*)#i;
259 1 my ($l) = $addr =~ m#s=([^/]*)#i;
260 1 $name = _extract_name "$f $l";
261 }
262  
263 44 ▶ length $name ? $name : undef;
Branch: 1 branch
BranchTrueFalseTotal
length $name ? : 38 6 44
264 }
265  
266  
267 sub host
268 0 ▶{ my $addr = shift->address || '';
Subroutine host: not called CC=3 SCAR=24.8
Pod documented
Condition0%0/2
(shift())->address || ''
l!l
00
MC/DC0%0/1
(shift())->address || ''
(shift())->address
Truth table0%0/2
A
A= (shift())->address
Aresult
11
00
269 0 my $i = rindex $addr, '@';
270 0 ▶ $i >= 0 ? substr($addr, $i+1) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$i >= 0 ? : 0 0 0
271 }
272  
273  
274 sub user
275 0 ▶{ my $addr = shift->address || '';
Subroutine user: not called CC=3 SCAR=24.8
Pod documented
Condition0%0/2
(shift())->address || ''
l!l
00
MC/DC0%0/1
(shift())->address || ''
(shift())->address
Truth table0%0/2
A
A= (shift())->address
Aresult
11
00
276 0 my $i = rindex $addr, '@';
277 0 ▶ $i >= 0 ? substr($addr,0,$i) : $addr;
Branch: 1 branch
BranchTrueFalseTotal
$i >= 0 ? : 0 0 0
278 }
279  
280 3 1;
« Mail/Cap.pm ↑ Summary Mail/Mailer/testfile.pm »