Message/Field/Addresses.pm

filter: Statementstmt 91.3% 116 / 127 Branchbran 71.8% 46 / 64 Conditioncond 60.0% 12 / 20 MC/DCmcdc 30.7% 4 / 13 Subroutinesub 90.0% 18 / 20 Podpod 91.6% 11 / 12 totaltotal 80.8% 207 / 256 CC 41 SCAR 20.6
CC
41
Cov
82%
parse
15.6
init
11.4
addAddress
6.6
CRAP 7.8
« Message/Construct/Text.pm ↑ Summary Message/Construct/Rebuild.pm »
1 # Copyrights 2001-2022 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 distribution Mail-Message. Meta-POD processed with
6 # OODoc into POD and HTML manual-pages. See README.md
7 # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8  
9 package Mail::Message::Field::Addresses;
10 23 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 23 $VERSION = '3.012';
12  
13 23 ▶use base 'Mail::Message::Field::Structured';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 23 ▶use Mail::Message::Field::AddrGroup;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 23 ▶use Mail::Message::Field::Address;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 23 ▶use List::Util 'first';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22  
23 #------------------------------------------
24 # what is permitted for each field.
25  
26 23 my $address_list = {groups => 1, multi => 1};
27 23 my $mailbox_list = {multi => 1};
28 23 my $mailbox = {};
29  
30 23 my %accepted = # defaults to $address_list
31 ( from => $mailbox_list
32 , sender => $mailbox
33 );
34  
35 sub init($)
36 13 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=9 SCAR=24.4
Pod undocumented
37  
38 13 $self->{MMFF_groups} = [];
39  
40 13 ( my $def = lc $args->{name} ) =~ s/^resent\-//;
41 13 ▶ $self->{MMFF_defaults} = $accepted{$def} || $address_list;
Condition66%2/3
$accepted{$def} || $address_list
l!l&&r!l&&!r
580
MC/DC0%0/2
$accepted{$def} || $address_list
$accepted{$def} $address_list
Truth table66%2/3
A || B
A= $accepted{$def}B= $address_list
ABresult
1X1
011
000
42  
43 13 my ($body, @body);
44 13 ▶ if($body = $args->{body})
Branch: 1 branch
BranchTrueFalseTotal
if ($body = $args->{"body"}) 12 1 13
45 12 ▶ { @body = ref $body eq 'ARRAY' ? @$body : ($body);
Branch: 1 branch
BranchTrueFalseTotal
ref $body eq 'ARRAY' ? : 0 12 12
46 12 ▶ return () unless @body;
Branch: 1 branch
BranchTrueFalseTotal
unless @body 0 12 12
47 }
48  
49 13 ▶ if(@body > 1 || ref $body[0])
Branch: 1 branch
BranchTrueFalseTotal
if (@body > 1 or ref $body[0]) 0 13 13
Condition33%1/3
@body > 1 or ref $body[0]
l!l&&r!l&&!r
0013
MC/DC0%0/2
@body > 1 or ref $body[0]
@body > 1 ref $body[0]
Truth table33%1/3
A or B
A= @body > 1B= ref $body[0]
ABresult
1X1
011
000
50 0 { $self->addAddress($_) foreach @body;
51 0 delete $args->{body};
52 }
53  
54 13 ▶ $self->SUPER::init($args) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init($args) or return 0 13 13
55 13 $self;
56 }
57  
58 #------------------------------------------
59  
60  
61 sub addAddress(@)
62 22 ▶{ my $self = shift;
Subroutine addAddress: called CC=6 SCAR=18.8
Pod documented
63 22 ▶ my $email = @_ && ref $_[0] ? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] ? : 22 0 22
Condition33%1/3
@_ and ref $_[0]
!ll&&!rl&&r
0022
MC/DC0%0/2
@_ and ref $_[0]
@_ ref $_[0]
Truth table33%1/3
A and B
A= @_B= ref $_[0]
ABresult
0X0
100
111
64 22 my %args = @_;
65 22 ▶ my $group = delete $args{group} || '';
Condition100%2/2
delete $args{'group'} || ''
l!l
616
MC/DC100%1/1
delete $args{'group'} || ''
delete $args{'group'}
Truth table100%2/2
A
A= delete $args{'group'}
Aresult
11
00
66  
67 22 ▶ $email = Mail::Message::Field::Address->new(%args)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $email 0 22 22
68 unless defined $email;
69  
70 22 ▶ my $set = $self->group($group) || $self->addGroup(name => $group);
Condition66%2/3
$self->group($group) || $self->addGroup('name', $group)
l!l&&r!l&&!r
1309
MC/DC50%1/2
$self->group($group) || $self->addGroup('name', $group)
$self->group($group) $self->addGroup('name', $group)
Truth table66%2/3
A || B
A= $self->group($group)B= $self->addGroup('name', $group)
ABresult
1X1
011
000
71 22 $set->addAddress($email);
72 22 $email;
73 }
74  
75  
76 sub addGroup(@)
77 12 ▶{ my $self = shift;
Subroutine addGroup: called CC=2 SCAR=7.0
Pod documented
78 12 ▶ my $group = @_ == 1 ? shift
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 12 12
79 : Mail::Message::Field::AddrGroup->new(@_);
80  
81 12 push @{$self->{MMFF_groups}}, $group;
82 12 $group;
83 }
84  
85  
86 sub group($)
87 25 ▶{ my ($self, $name) = @_;
Subroutine group: called CC=2 SCAR=7.0
Pod documented
88 25 ▶ $name = '' unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 25 25
89 25 ▶ first { lc($_->name) eq lc($name) } $self->groups;
Subroutine __ANON__: called CC=1 SCAR=0.0
90 }
91  
92  
93 41 ▶sub groups() { @{shift->{MMFF_groups}} }
Subroutine groups: called CC=1 SCAR=0.0
Pod documented
94  
95  
96 0 ▶sub groupNames() { map {$_->name} shift->groups }
Subroutine groupNames: not called CC=1 SCAR=6.9
Pod documented
97  
98  
99 8 ▶sub addresses() { map {$_->addresses} shift->groups }
Subroutine addresses: called CC=1 SCAR=0.0
Pod documented
100  
101  
102 sub addAttribute($;@)
103 0 ▶{ my $self = shift;
Subroutine addAttribute: not called CC=1 SCAR=6.9
Pod documented
104 0 $self->log(ERROR => 'No attributes for address fields.');
105 0 $self;
106 }
107  
108 #------------------------------------------
109  
110  
111 sub parse($)
112 12 ▶{ my ($self, $string) = @_;
Subroutine parse: called CC=15 SCAR=27.5
Pod documented
113 12 my ($group, $email) = ('', undef);
114 12 $string =~ s/\s+/ /gs;
115  
116 12 while(1)
117 46 { (my $comment, $string) = $self->consumeComment($string);
118  
119 46 ▶ if($string =~ s/^\s*\;//s ) { $group = ''; next } # end group
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ s/^\s*\;//s) 3 43 46
120 43 ▶ if($string =~ s/^\s*\,//s ) { next } # end address
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ s/^\s*\,//s) 12 31 43
121  
122 31 (my $email, $string) = $self->consumeAddress($string);
123 31 ▶ if(defined $email)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $email) { } 5 26 31
124 { # Pattern starts with e-mail address
125 5 ($comment, $string) = $self->consumeComment($string);
126 5 ▶ $email->comment($comment) if defined $comment;
Branch: 1 branch
BranchTrueFalseTotal
if defined $comment 0 5 5
127 }
128 else
129 { # Pattern not plain address
130 26 my $real_phrase = $string =~ m/^\s*\"/;
131 26 (my $phrase, $string) = $self->consumePhrase($string);
132  
133 26 ▶ if(defined $phrase)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $phrase) 22 4 26
134 22 { ($comment, $string) = $self->consumeComment($string);
135  
136 22 ▶ if($string =~ s/^\s*\://s )
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ s/^\s*\://s) 3 19 22
137 3 { $group = $phrase;
138 # even empty groups must appear
139 3 ▶ $self->addGroup(name=>$group) unless $self->group($group);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->group($group) 3 0 3
140 3 next;
141 }
142 }
143  
144 23 my $angle;
145 23 ▶ if($string =~ s/^\s*\<([^>]*)\>//s) { $angle = $1 }
Branch: 3 branches
BranchTrueFalseTotal
if ($string =~ s/^\s*\<([^>]*)\>//s) { } 19 4 23
elsif ($real_phrase) { } 0 4 4
elsif (defined $phrase) { } 1 3 4
146 elsif($real_phrase)
147 0 ▶ { $self->log(ERROR => "Ignore unrelated phrase `$1'")
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ s/^\s*\"(.*?)\r?\n// 0 0 0
148 if $string =~ s/^\s*\"(.*?)\r?\n//;
149 0 next;
150 }
151 elsif(defined $phrase)
152 1 { ($angle = $phrase) =~ s/\s+/./g;
153 1 undef $phrase;
154 }
155  
156 23 ($comment, $string) = $self->consumeComment($string);
157  
158 # remove obsoleted route info.
159 23 ▶ return 1 unless defined $angle;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $angle 3 20 23
160 20 $angle =~ s/^\@.*?\://;
161  
162 20 ($email, $angle) = $self->consumeAddress($angle
163 , phrase => $phrase, comment => $comment);
164 }
165  
166 25 ▶ $self->addAddress($email, group => $group) if defined $email;
Branch: 1 branch
BranchTrueFalseTotal
if defined $email 22 3 25
167 25 ▶ return 1 if $string =~ m/^\s*$/s;
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ /^\s*$/s 9 16 25
168 }
169  
170 0 $self->log(WARNING => 'Illegal part in address field '.$self->Name.
171 ": $string\n");
172  
173 0 0;
174 }
175  
176 sub produceBody()
177 4 ▶{ my @groups = sort {$a->name cmp $b->name} shift->groups;
Subroutine produceBody: called CC=5 SCAR=16.8
Pod documented
178  
179 4 ▶ @groups or return '';
Branch: 1 branch
BranchTrueFalseTotal
@groups or return "" 0 4 4
180 4 ▶ @groups > 1 or return $groups[0]->string;
Branch: 1 branch
BranchTrueFalseTotal
@groups > 1 or return $groups[0]->string 2 2 4
181  
182 2 ▶ my $plain
Branch: 1 branch
BranchTrueFalseTotal
$groups[0]->name eq '' && $groups[0]->addresses ? : 2 0 2
Condition33%1/3
$groups[0]->name eq "" and $groups[0]->addresses
!ll&&!rl&&r
002
MC/DC0%0/2
$groups[0]->name eq "" and $groups[0]->addresses
$groups[0]->name eq "" $groups[0]->addresses
Truth table33%1/3
A and B
A= $groups[0]->name eq ""B= $groups[0]->addresses
ABresult
0X0
100
111
183 = $groups[0]->name eq '' && $groups[0]->addresses
184 ? (shift @groups)->string.','
185 : '';
186  
187 2 join ' ', $plain, map({$_->string} @groups);
188 }
189  
190  
191 sub consumeAddress($@)
192 51 ▶{ my ($self, $string, @options) = @_;
Subroutine consumeAddress: called CC=6 SCAR=17.9
Pod documented
193  
194 51 my ($local, $shorter, $loccomment);
195 51 ▶ if($string =~ s/^\s*"((?:\\.|[^"])*)"\s*\@/@/)
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ s/^\s*"((?:\\.|[^"])*)"\s*\@/\@/) { } 1 50 51
196 { # local part is quoted-string rfc2822
197 1 ($local, $shorter) = ($1, $string);
198 1 $local =~ s/\\"/"/g;
199 }
200 else
201 50 { ($local, $shorter, $loccomment) = $self->consumeDotAtom($string);
202 50 ▶ $local =~ s/\s//g if defined $local;
Branch: 1 branch
BranchTrueFalseTotal
if defined $local 38 12 50
203 }
204  
205 51 ▶ return (undef, $string)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $local and $shorter =~ s/^\s*\@// 29 22 51
Condition100%3/3
defined $local and $shorter =~ s/^\s*\@//
!ll&&!rl&&r
121722
MC/DC100%2/2
defined $local and $shorter =~ s/^\s*\@//
defined $local $shorter =~ s/^\s*\@//
Truth table100%3/3
A and B
A= defined $localB= $shorter =~ s/^\s*\@//
ABresult
0X0
100
111
206 unless defined $local && $shorter =~ s/^\s*\@//;
207
208 22 (my $domain, $shorter, my $domcomment) = $self->consumeDomain($shorter);
209 22 ▶ return (undef, $string) unless defined $domain;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $domain 0 22 22
210  
211 # loccomment and domcomment ignored
212 22 my $email = Mail::Message::Field::Address
213 ->new(username => $local, domain => $domain, @options);
214  
215 22 ($email, $shorter);
216 }
217  
218  
219 sub consumeDomain($)
220 22 ▶{ my ($self, $string) = @_;
Subroutine consumeDomain: called CC=3 SCAR=11.3
Pod documented
221  
222 22 ▶ return ($self->stripCFWS($1), $string)
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ s/\s*(\[(?:[^[]\\]*|\\.)*\])// 0 22 22
223 if $string =~ s/\s*(\[(?:[^[]\\]*|\\.)*\])//;
224  
225 22 my ($atom, $rest, $comment) = $self->consumeDotAtom($string);
226 22 ▶ $atom =~ s/\s//g if defined $atom;
Branch: 1 branch
BranchTrueFalseTotal
if defined $atom 22 0 22
227 22 ($atom, $rest, $comment);
228 }
229  
230 #------------------------------------------
231  
232  
233 23 1;
« Message/Construct/Text.pm ↑ Summary Message/Construct/Rebuild.pm »