Message/Field/Addresses.pm

filter: Statementstmt 91.6% 121 / 132 Branchbran 71.4% 50 / 70 Conditioncond 60.0% 12 / 20 MC/DCmcdc 30.7% 4 / 13 Subroutinesub 90.0% 18 / 20 Podpod 91.6% 11 / 12 totaltotal 80.9% 216 / 267 SCAR 40.0
CC
44
Cov
82%
parse
18.9
init
11.4
addAddress
6.6
CRAP 54.5
« Message/Field/Date.pm ↑ Summary Message/Field/AuthResults.pm »
1 # Copyrights 2001-2023 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 24 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 24 $VERSION = '3.013';
12  
13 24 ▶use base 'Mail::Message::Field::Structured';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 24 ▶use Mail::Message::Field::AddrGroup;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 24 ▶use Mail::Message::Field::Address;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 24 ▶use List::Util 'first';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22  
23 #------------------------------------------
24 # what is permitted for each field.
25  
26 24 my $address_list = {groups => 1, multi => 1};
27 24 my $mailbox_list = {multi => 1};
28 24 my $mailbox = {};
29  
30 24 my %accepted = # defaults to $address_list
31 ( from => $mailbox_list
32 , sender => $mailbox
33 );
34  
35 sub init($)
36 14 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=9 SCAR=24.4
Pod undocumented
37  
38 14 $self->{MMFF_groups} = [];
39  
40 14 ( my $def = lc $args->{name} ) =~ s/^resent\-//;
41 14 ▶ $self->{MMFF_defaults} = $accepted{$def} || $address_list;
Condition66%2/3
$accepted{$def} || $address_list
l!l&&r!l&&!r
590
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 14 my ($body, @body);
44 14 ▶ if($body = $args->{body})
Branch: 1 branch
BranchTrueFalseTotal
if ($body = $args->{"body"}) 13 1 14
45 13 ▶ { @body = ref $body eq 'ARRAY' ? @$body : ($body);
Branch: 1 branch
BranchTrueFalseTotal
ref $body eq 'ARRAY' ? : 0 13 13
46 13 ▶ return () unless @body;
Branch: 1 branch
BranchTrueFalseTotal
unless @body 0 13 13
47 }
48  
49 14 ▶ if(@body > 1 || ref $body[0])
Branch: 1 branch
BranchTrueFalseTotal
if (@body > 1 or ref $body[0]) 0 14 14
Condition33%1/3
@body > 1 or ref $body[0]
l!l&&r!l&&!r
0014
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 14 ▶ $self->SUPER::init($args) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init($args) or return 0 14 14
55 14 $self;
56 }
57  
58 #------------------------------------------
59  
60  
61 sub addAddress(@)
62 23 ▶{ my $self = shift;
Subroutine addAddress: called CC=6 SCAR=18.8
Pod documented
63 23 ▶ my $email = @_ && ref $_[0] ? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] ? : 23 0 23
Condition33%1/3
@_ and ref $_[0]
!ll&&!rl&&r
0023
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 23 my %args = @_;
65 23 ▶ my $group = delete $args{group} || '';
Condition100%2/2
delete $args{'group'} || ''
l!l
617
MC/DC100%1/1
delete $args{'group'} || ''
delete $args{'group'}
Truth table100%2/2
A
A= delete $args{'group'}
Aresult
11
00
66  
67 23 ▶ $email = Mail::Message::Field::Address->new(%args)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $email 0 23 23
68 unless defined $email;
69  
70 23 ▶ my $set = $self->group($group) || $self->addGroup(name => $group);
Condition66%2/3
$self->group($group) || $self->addGroup('name', $group)
l!l&&r!l&&!r
13010
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 23 $set->addAddress($email);
72 23 $email;
73 }
74  
75  
76 sub addGroup(@)
77 13 ▶{ my $self = shift;
Subroutine addGroup: called CC=2 SCAR=7.0
Pod documented
78 13 ▶ my $group = @_ == 1 ? shift
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 13 13
79 : Mail::Message::Field::AddrGroup->new(@_);
80  
81 13 push @{$self->{MMFF_groups}}, $group;
82 13 $group;
83 }
84  
85  
86 sub group($)
87 26 ▶{ my ($self, $name) = @_;
Subroutine group: called CC=2 SCAR=7.0
Pod documented
88 26 ▶ $name = '' unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 26 26
89 26 ▶ first { lc($_->name) eq lc($name) } $self->groups;
Subroutine __ANON__: called CC=1 SCAR=0.0
90 }
91  
92  
93 43 ▶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 9 ▶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 13 ▶{ my ($self, $string) = @_;
Subroutine parse: called CC=18 SCAR=29.4
Pod documented
113 13 my ($group, $email) = ('', undef);
114 13 $string =~ s/\s+/ /gs;
115  
116 ADDRESS:
117 13 while(1)
118 47 { (my $comment, $string) = $self->consumeComment($string);
119 47 my $start_length = length $string;
120  
121 47 ▶ if($string =~ s/^\s*\;//s ) { $group = ''; next ADDRESS } # end group
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ s/^\s*\;//s) 3 44 47
122 44 ▶ if($string =~ s/^\s*\,//s ) { next ADDRESS} # end address
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ s/^\s*\,//s) 12 32 44
123  
124 32 (my $email, $string) = $self->consumeAddress($string);
125 32 ▶ if(defined $email)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $email) { } 5 27 32
126 { # Pattern starts with e-mail address
127 5 ($comment, $string) = $self->consumeComment($string);
128 5 ▶ $email->comment($comment) if defined $comment;
Branch: 1 branch
BranchTrueFalseTotal
if defined $comment 0 5 5
129 }
130 else
131 { # Pattern not plain address
132 27 my $real_phrase = $string =~ m/^\s*\"/;
133 27 my @words;
134  
135 # In rfc2822 obs-phrase, we can have more than one word with
136 # comments inbetween.
137 WORD:
138 27 while(1)
139 47 { (my $word, $string) = $self->consumePhrase($string);
140 47 ▶ defined $word or last;
Branch: 1 branch
BranchTrueFalseTotal
defined $word or last 24 23 47
141  
142 23 ▶ push @words, $word if length $word;
Branch: 1 branch
BranchTrueFalseTotal
if length $word 23 0 23
143 23 ($comment, $string) = $self->consumeComment($string);
144  
145 23 ▶ if($string =~ s/^\s*\://s )
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ s/^\s*\://s) 3 20 23
146 3 { $group = $word;
147 # even empty groups must appear
148 3 ▶ $self->addGroup(name => $group) unless $self->group($group);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->group($group) 3 0 3
149 3 next ADDRESS;
150 }
151 }
152 24 ▶ my $phrase = @words ? join ' ', @words : undef;
Branch: 1 branch
BranchTrueFalseTotal
@words ? : 20 4 24
153  
154 24 my $angle;
155 24 ▶ if($string =~ s/^\s*\<([^>]*)\>//s) { $angle = $1 }
Branch: 3 branches
BranchTrueFalseTotal
if ($string =~ s/^\s*\<([^>]*)\>//s) { } 20 4 24
elsif ($real_phrase) { } 0 4 4
elsif (defined $phrase) { } 1 3 4
156 elsif($real_phrase)
157 0 ▶ { $self->log(ERROR => "Ignore unrelated phrase `$1'")
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ s/^\s*\"(.*?)\r?\n// 0 0 0
158 if $string =~ s/^\s*\"(.*?)\r?\n//;
159 0 next ADDRESS;
160 }
161 elsif(defined $phrase)
162 1 { ($angle = $phrase) =~ s/\s+/./g;
163 1 undef $phrase;
164 }
165  
166 24 ($comment, $string) = $self->consumeComment($string);
167  
168 # remove obsoleted route info.
169 24 ▶ return 1 unless defined $angle;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $angle 3 21 24
170 21 $angle =~ s/^\@.*?\://;
171  
172 21 ($email, $angle) = $self->consumeAddress($angle
173 , phrase => $phrase, comment => $comment);
174 }
175  
176 26 ▶ $self->addAddress($email, group => $group) if defined $email;
Branch: 1 branch
BranchTrueFalseTotal
if defined $email 23 3 26
177 26 ▶ return 1 if $string =~ m/^\s*$/s;
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ /^\s*$/s 10 16 26
178  
179 # Do not get stuck on illegal characters
180 16 ▶ last if $start_length == length $string;
Branch: 1 branch
BranchTrueFalseTotal
if $start_length == length $string 0 16 16
181 }
182  
183 0 $self->log(WARNING => 'Illegal part in address field '.$self->Name. ": $string\n");
184  
185 0 0;
186 }
187  
188 sub produceBody()
189 4 ▶{ my @groups = sort {$a->name cmp $b->name} shift->groups;
Subroutine produceBody: called CC=5 SCAR=17.0
Pod documented
190  
191 4 ▶ @groups or return '';
Branch: 1 branch
BranchTrueFalseTotal
@groups or return "" 0 4 4
192 4 ▶ @groups > 1 or return $groups[0]->string;
Branch: 1 branch
BranchTrueFalseTotal
@groups > 1 or return $groups[0]->string 2 2 4
193  
194 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
195 = $groups[0]->name eq '' && $groups[0]->addresses
196 ? (shift @groups)->string.','
197 : '';
198  
199 2 join ' ', $plain, (map $_->string, @groups);
200 }
201  
202  
203 sub consumeAddress($@)
204 53 ▶{ my ($self, $string, @options) = @_;
Subroutine consumeAddress: called CC=6 SCAR=17.9
Pod documented
205  
206 53 my ($local, $shorter, $loccomment);
207 53 ▶ if($string =~ s/^\s*"((?:\\.|[^"])*)"\s*\@/@/)
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ s/^\s*"((?:\\.|[^"])*)"\s*\@/\@/) { } 1 52 53
208 { # local part is quoted-string rfc2822
209 1 ($local, $shorter) = ($1, $string);
210 1 $local =~ s/\\"/"/g;
211 }
212 else
213 52 { ($local, $shorter, $loccomment) = $self->consumeDotAtom($string);
214 52 ▶ $local =~ s/\s//g if defined $local;
Branch: 1 branch
BranchTrueFalseTotal
if defined $local 40 12 52
215 }
216  
217 53 ▶ defined $local && $shorter =~ s/^\s*\@//
Branch: 1 branch
BranchTrueFalseTotal
defined $local and $shorter =~ s/^\s*\@// or return undef, $string 30 23 53
Condition100%3/3
defined $local and $shorter =~ s/^\s*\@//
!ll&&!rl&&r
121823
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
218 or return (undef, $string);
219
220 23 (my $domain, $shorter, my $domcomment) = $self->consumeDomain($shorter);
221 23 ▶ defined $domain
Branch: 1 branch
BranchTrueFalseTotal
defined $domain or return undef, $string 0 23 23
222 or return (undef, $string);
223  
224 # loccomment and domcomment ignored
225 23 my $email = Mail::Message::Field::Address
226 ->new(username => $local, domain => $domain, @options);
227  
228 23 ($email, $shorter);
229 }
230  
231  
232 sub consumeDomain($)
233 23 ▶{ my ($self, $string) = @_;
Subroutine consumeDomain: called CC=3 SCAR=11.3
Pod documented
234  
235 23 ▶ return ($self->stripCFWS($1), $string)
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ s/\s*(\[(?:[^[]\\]*|\\.)*\])// 0 23 23
236 if $string =~ s/\s*(\[(?:[^[]\\]*|\\.)*\])//;
237  
238 23 my ($atom, $rest, $comment) = $self->consumeDotAtom($string);
239 23 ▶ $atom =~ s/\s//g if defined $atom;
Branch: 1 branch
BranchTrueFalseTotal
if defined $atom 23 0 23
240 23 ($atom, $rest, $comment);
241 }
242  
243 #------------------------------------------
244  
245  
246 24 1;
« Message/Field/Date.pm ↑ Summary Message/Field/AuthResults.pm »