Message/Field/Addresses.pm

filter: Statementstmt 93.2% 124 / 133 Branchbran 73.4% 47 / 64 Conditioncond 54.5% 12 / 22 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 90.4% 19 / 21 Podpod 91.6% 11 / 12 totaltotal 80.8% 215 / 266 CC 42 SCAR 21.7
CC
42
Cov
84%
parse
18.9
init
9.9
consumeAddress
6.0
CRAP 8.8
« Message/Construct/Text.pm ↑ Summary Message/Field/Structured.pm »
1 # This code is part of Perl distribution Mail-Message version 4.05.
2 # The POD got stripped from this file by OODoc version 3.06.
3 # For contributors see file ChangeLog.
4  
5 # This software is copyright (c) 2001-2026 by Mark Overmeer.
6  
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10  
11  
12 package Mail::Message::Field::Addresses;{
13 28 our $VERSION = '4.05';
14 }
15  
16 28 ▶use parent 'Mail::Message::Field::Structured';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 28 ▶use Log::Report 'mail-message', import => [ qw/__x error info warning/ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 28 ▶use Mail::Message::Field::AddrGroup ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 28 ▶use Mail::Message::Field::Address ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 28 ▶use List::Util qw/first/;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 28 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 #--------------------
30  
31 # what is permitted for each field.
32  
33 28 my $address_list = +{ groups => 1, multi => 1 };
34 28 my $mailbox_list = +{ multi => 1 };
35 28 my $mailbox = +{ };
36  
37 28 my %accepted = ( # defaults to $address_list
38 from => $mailbox_list,
39 sender => $mailbox,
40 );
41  
42 sub init($)
43 36 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=8 SCAR=22.9
Pod undocumented
44  
45 36 $self->{MMFF_groups} = [];
46  
47 36 my $def = lc $args->{name} =~ s/^resent\-//r;
48 36 ▶ $self->{MMFF_defaults} = $accepted{$def} || $address_list;
Condition66%2/3
$accepted{$def} || $address_list
l!l&&r!l&&!r
7290
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
49  
50 36 my ($body, @body);
51 36 ▶ if($body = $args->{body})
Branch: 1 branch
BranchTrueFalseTotal
if ($body = $args->{"body"}) 35 1 36
52 35 ▶ { @body = ref $body eq 'ARRAY' ? @$body : ($body);
Branch: 1 branch
BranchTrueFalseTotal
ref $body eq 'ARRAY' ? : 0 35 35
53 35 ▶ @body or return ();
Branch: 1 branch
BranchTrueFalseTotal
@body or return () 35 0 35
54 }
55  
56 36 ▶ if(@body > 1 || ref $body[0])
Branch: 1 branch
BranchTrueFalseTotal
if (@body > 1 or ref $body[0]) 0 36 36
Condition33%1/3
@body > 1 or ref $body[0]
l!l&&r!l&&!r
0036
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
57 0 { $self->addAddress($_) for @body;
58 0 delete $args->{body};
59 }
60  
61 36 $self->SUPER::init($args);
62 }
63  
64 #--------------------
65  
66 sub addAddress(@)
67 42 ▶{ my $self = shift;
Subroutine addAddress: called CC=5 SCAR=17.1
Pod documented
68 42 ▶ my $email = blessed $_[0] ? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) ? : 42 0 42
69 42 my %args = @_;
70 42 ▶ my $group = delete $args{group} // '';
Condition50%1/2
delete $args{"group"} // ""
l!l
420
MC/DC0%0/1
delete $args{"group"} // ""
delete $args{"group"}
Truth table50%1/2
A
A= delete $args{"group"}
Aresult
11
00
71  
72 42 ▶ $email //= Mail::Message::Field::Address->new(%args);
Condition33%1/3
$email //= "Mail::Message::Field::Address"->new(%args)
l!l&&r!l&&!r
4200
MC/DC0%0/2
$email //= "Mail::Message::Field::Address"->new(%args)
$email "Mail::Message::Field::Address"->new(%args)
Truth table33%1/3
A //= B
A= $emailB= "Mail::Message::Field::Address"->new(%args)
ABresult
1X1
011
000
73  
74 42 ▶ my $set = $self->group($group) // $self->addGroup(name => $group);
Condition66%2/3
$self->group($group) // $self->addGroup("name", $group)
l!l&&r!l&&!r
20220
MC/DC0%0/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
75 42 $set->addAddress($email);
76 42 $email;
77 }
78  
79  
80 sub addGroup(@)
81 35 ▶{ my $self = shift;
Subroutine addGroup: called CC=2 SCAR=7.0
Pod documented
82 35 ▶ my $group = @_ == 1 ? shift : Mail::Message::Field::AddrGroup->new(@_);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 35 35
83 35 push @{$self->{MMFF_groups}}, $group;
84 35 $group;
85 }
86  
87  
88 sub group($)
89 55 ▶{ my ($self, $name) = @_;
Subroutine group: called CC=2 SCAR=7.0
Pod documented
90 55 ▶ $name //= '';
Condition50%1/2
$name //= ""
l!l
550
MC/DC0%0/1
$name //= ""
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
91 55 ▶ first { lc($_->name) eq lc($name) } $self->groups;
Subroutine __ANON__: called CC=1 SCAR=0.0
92 }
93  
94  
95 94 ▶sub groups() { @{ $_[0]->{MMFF_groups}} }
Subroutine groups: called CC=1 SCAR=0.0
Pod documented
96  
97  
98 0 ▶sub groupNames() { map $_->name, $_[0]->groups }
Subroutine groupNames: not called CC=1 SCAR=6.9
Pod documented
99  
100  
101 31 ▶sub addresses() { map $_->addresses, $_[0]->groups }
Subroutine addresses: called CC=1 SCAR=0.0
Pod documented
102  
103  
104 sub addAttribute($;@)
105 0 ▶{ my $self = shift;
Subroutine addAttribute: not called CC=1 SCAR=6.9
Pod documented
106 0 error __x"no attributes for address fields.";
107 }
108  
109 #--------------------
110  
111 sub parse($)
112 35 ▶{ my ($self, $string) = @_;
Subroutine parse: called CC=18 SCAR=29.4
Pod documented
113 35 my ($group, $email) = ('', undef);
114 35 $string =~ s/\s+/ /gs;
115  
116 ADDRESS:
117 35 while(1)
118 81 { (my $comment, $string) = $self->consumeComment($string);
119 81 my $start_length = length $string;
120  
121 81 ▶ if($string =~ s/^\s*\;//s ) { $group = ''; next ADDRESS } # end group
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ s/^\s*\;//s) 3 78 81
122 78 ▶ if($string =~ s/^\s*\,//s ) { next ADDRESS} # end address
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ s/^\s*\,//s) 12 66 78
123  
124 66 (my $email, $string) = $self->consumeAddress($string);
125 66 ▶ if(defined $email)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $email) { } 13 53 66
126 { # Pattern starts with e-mail address
127 13 ($comment, $string) = $self->consumeComment($string);
128 13 ▶ $email->comment($comment) if defined $comment;
Branch: 1 branch
BranchTrueFalseTotal
if defined $comment 0 13 13
129 }
130 else
131 { # Pattern not plain address
132 53 my $real_phrase = $string =~ m/^\s*\"/;
133 53 my @words;
134  
135 # In rfc2822 obs-phrase, we can have more than one word with
136 # comments inbetween.
137 WORD:
138 53 while(1)
139 90 { (my $word, $string) = $self->consumePhrase($string);
140 90 ▶ defined $word or last;
Branch: 1 branch
BranchTrueFalseTotal
defined $word or last 50 40 90
141  
142 50 ▶ push @words, $word if length $word;
Branch: 1 branch
BranchTrueFalseTotal
if length $word 50 0 50
143 50 ($comment, $string) = $self->consumeComment($string);
144  
145 50 ▶ if($string =~ s/^\s*\://s )
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ s/^\s*\://s) 13 37 50
146 13 { $group = $word;
147 # even empty groups must appear
148 13 ▶ $self->addGroup(name => $group) unless $self->group($group);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->group($group) 13 0 13
149 13 next ADDRESS;
150 }
151 }
152 40 ▶ my $phrase = @words ? join ' ', @words : undef;
Branch: 1 branch
BranchTrueFalseTotal
@words ? : 33 7 40
153  
154 40 my $angle;
155 40 ▶ if($string =~ s/^\s*\<([^>]*)\>//s) { $angle = $1 }
Branch: 3 branches
BranchTrueFalseTotal
if ($string =~ s/^\s*\<([^>]*)\>//s) { } 31 9 40
elsif ($real_phrase) { } 0 9 9
elsif (defined $phrase) { } 4 5 9
156 elsif($real_phrase)
157 0 ▶ { warning __x"ignoring addressless phrase '{phrase}'.", 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 4 { ($angle = $phrase) =~ s/\s+/./g;
163 4 undef $phrase;
164 }
165  
166 40 ($comment, $string) = $self->consumeComment($string);
167  
168 # remove obsoleted route info.
169 40 ▶ defined $angle or return 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $angle or return 1 35 5 40
170 35 $angle =~ s/^\@.*?\://;
171  
172 35 ($email, $angle) = $self->consumeAddress($angle, phrase => $phrase, comment => $comment);
173 }
174  
175 48 ▶ $self->addAddress($email, group => $group) if defined $email;
Branch: 1 branch
BranchTrueFalseTotal
if defined $email 42 6 48
176 48 ▶ return 1 if $string =~ m/^\s*$/s;
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ /^\s*$/s 30 18 48
177  
178 # Do not get stuck on illegal characters
179 18 ▶ last if $start_length == length $string;
Branch: 1 branch
BranchTrueFalseTotal
if $start_length == length $string 0 18 18
180 }
181  
182 0 warning __x"illegal part in address field {name}: {part}.", name => $self->Name, part => $string;
183 0 0;
184 }
185  
186 sub produceBody()
187 4 ▶{ my $self = shift;
Subroutine produceBody: called CC=5 SCAR=16.8
Pod documented
188 4 my @groups = sort { $a->name cmp $b->name } $self->groups;
189  
190 4 ▶ @groups or return '';
Branch: 1 branch
BranchTrueFalseTotal
@groups or return "" 4 0 4
191 4 ▶ @groups > 1 or return $groups[0]->string;
Branch: 1 branch
BranchTrueFalseTotal
@groups > 1 or return $groups[0]->string 2 2 4
192  
193 2 ▶ my $plain = $groups[0]->name eq '' && $groups[0]->addresses ? (shift @groups)->string.',' : '';
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
194 2 join ' ', $plain, (map $_->string, @groups);
195 }
196  
197  
198 sub consumeAddress($@)
199 101 ▶{ my ($self, $string, @options) = @_;
Subroutine consumeAddress: called CC=6 SCAR=17.9
Pod documented
200  
201 101 my ($local, $shorter, $loccomment);
202 101 ▶ if($string =~ s/^\s*"((?:\\.|[^"])*)"\s*\@/@/)
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ s/^\s*"((?:\\.|[^"])*)"\s*\@/\@/) { } 1 100 101
203 { # local part is quoted-string rfc2822
204 1 ($local, $shorter) = ($1, $string);
205 1 $local =~ s/\\"/"/g;
206 }
207 else
208 100 { ($local, $shorter, $loccomment) = $self->consumeDotAtom($string);
209 100 ▶ $local =~ s/\s//g if defined $local;
Branch: 1 branch
BranchTrueFalseTotal
if defined $local 84 16 100
210 }
211  
212 101 ▶ defined $local && $shorter =~ s/^\s*\@//
Branch: 1 branch
BranchTrueFalseTotal
defined $local and $shorter =~ s/^\s*\@// or return undef, $string 42 59 101
Condition100%3/3
defined $local and $shorter =~ s/^\s*\@//
!ll&&!rl&&r
164342
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
213 or return (undef, $string);
214  
215 42 (my $domain, $shorter, my $domcomment) = $self->consumeDomain($shorter);
216 42 ▶ defined $domain
Branch: 1 branch
BranchTrueFalseTotal
defined $domain or return undef, $string 42 0 42
217 or return (undef, $string);
218  
219 # loccomment and domcomment ignored
220 42 my $email = Mail::Message::Field::Address->new(username => $local, domain => $domain, @options);
221 42 ($email, $shorter);
222 }
223  
224  
225 sub consumeDomain($)
226 42 ▶{ my ($self, $string) = @_;
Subroutine consumeDomain: called CC=3 SCAR=11.3
Pod documented
227  
228 42 ▶ return ($self->stripCFWS($1), $string)
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ s/\s*(\[(?:[^[]\\]*|\\.)*\])// 0 42 42
229 if $string =~ s/\s*(\[(?:[^[]\\]*|\\.)*\])//;
230  
231 42 my ($atom, $rest, $comment) = $self->consumeDotAtom($string);
232 42 ▶ $atom =~ s/\s//g if defined $atom;
Branch: 1 branch
BranchTrueFalseTotal
if defined $atom 42 0 42
233 42 ($atom, $rest, $comment);
234 }
235  
236 #--------------------
237  
238 28 1;
« Message/Construct/Text.pm ↑ Summary Message/Field/Structured.pm »