MIME/Header/AddressList.pm

filter: Statementstmt 85.2% 104 / 122 Branchbran 59.3% 19 / 32 Conditioncond 61.1% 11 / 18 MC/DCmcdc 41.6% 5 / 12 Subroutinesub 88.8% 16 / 18 Podpod 100% 13 / 13 totaltotal 78.1% 168 / 215 CC 30 SCAR 19.9
CC
30
Cov
78%
new_mime_groups
15.3
as_mime_string
8.1
append_addresses
6.0
CRAP 7.3
« MIME.pm ↑ Summary MIME/Encode.pm »
1 # Copyright (c) 2016-2017 by Pali <pali@cpan.org>
2  
3 package Email::MIME::Header::AddressList 1.954;
4  
5 # ABSTRACT: MIME support for list of Email::Address::XS objects
6  
7 3 ▶use v5.12.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Email::Address::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Email::MIME::Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 #pod =encoding utf8
15 #pod
16 #pod =head1 SYNOPSIS
17 #pod
18 #pod my $address1 = Email::Address::XS->new('Name1' => 'address1@host.com');
19 #pod my $address2 = Email::Address::XS->new("Name2 \N{U+263A}" => 'address2@host.com');
20 #pod my $mime_address = Email::Address::XS->new('=?UTF-8?B?TmFtZTIg4pi6?=' => 'address2@host.com');
21 #pod
22 #pod my $list1 = Email::MIME::Header::AddressList->new($address1, $address2);
23 #pod
24 #pod $list1->append_groups('undisclosed-recipients' => []);
25 #pod
26 #pod $list1->first_address();
27 #pod # returns $address1
28 #pod
29 #pod $list1->groups();
30 #pod # returns (undef, [ $address1, $address2 ], 'undisclosed-recipients', [])
31 #pod
32 #pod $list1->as_string();
33 #pod # returns 'Name1 <address1@host.com>, "Name2 ☺" <address2@host.com>, undisclosed-recipients:;'
34 #pod
35 #pod $list1->as_mime_string();
36 #pod # returns 'Name1 <address1@host.com>, =?UTF-8?B?TmFtZTIg4pi6?= <address2@host.com>, undisclosed-recipients:;'
37 #pod
38 #pod my $list2 = Email::MIME::Header::AddressList->new_groups(Group => [ $address1, $address2 ]);
39 #pod
40 #pod $list2->append_addresses($address2);
41 #pod
42 #pod $list2->addresses();
43 #pod # returns ($address2, $address1, $address2)
44 #pod
45 #pod $list2->groups();
46 #pod # returns (undef, [ $address2 ], 'Group', [ $address1, $address2 ])
47 #pod
48 #pod my $list3 = Email::MIME::Header::AddressList->new_mime_groups('=?UTF-8?B?4pi6?=' => [ $mime_address ]);
49 #pod $list3->as_string();
50 #pod # returns '☺: "Name2 ☺" <address2@host.com>;'
51 #pod
52 #pod my $list4 = Email::MIME::Header::AddressList->from_string('Name1 <address1@host.com>, "Name2 ☺" <address2@host.com>, undisclosed-recipients:;');
53 #pod my $list5 = Email::MIME::Header::AddressList->from_string('Name1 <address1@host.com>', '"Name2 ☺" <address2@host.com>', 'undisclosed-recipients:;');
54 #pod
55 #pod my $list6 = Email::MIME::Header::AddressList->from_mime_string('Name1 <address1@host.com>, =?UTF-8?B?TmFtZTIg4pi6?= <address2@host.com>, undisclosed-recipients:;');
56 #pod my $list7 = Email::MIME::Header::AddressList->from_mime_string('Name1 <address1@host.com>', '=?UTF-8?B?TmFtZTIg4pi6?= <address2@host.com>', 'undisclosed-recipients:;');
57 #pod
58 #pod =head1 DESCRIPTION
59 #pod
60 #pod This module implements object representation for the list of the
61 #pod L<Email::Address::XS|Email::Address::XS> objects. It provides methods for
62 #pod L<RFC 2047|https://tools.ietf.org/html/rfc2047> MIME encoding and decoding
63 #pod suitable for L<RFC 2822|https://tools.ietf.org/html/rfc2822> address-list
64 #pod structure.
65 #pod
66 #pod =head2 EXPORT
67 #pod
68 #pod None
69 #pod
70 #pod =head2 Class Methods
71 #pod
72 #pod =over 4
73 #pod
74 #pod =item new_empty
75 #pod
76 #pod Construct new empty C<Email::MIME::Header::AddressList> object.
77 #pod
78 #pod =cut
79  
80 59 ▶sub new_empty {
Subroutine new_empty: called CC=1 SCAR=0.0
Pod documented
81 59 my ($class) = @_;
82 59 return bless { addresses => [], groups => [] }, $class;
83 }
84  
85 #pod =item new
86 #pod
87 #pod Construct new C<Email::MIME::Header::AddressList> object from list of
88 #pod L<Email::Address::XS|Email::Address::XS> objects.
89 #pod
90 #pod =cut
91  
92 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
93 0 my ($class, @addresses) = @_;
94 0 my $self = $class->new_empty();
95 0 $self->append_addresses(@addresses);
96 0 return $self;
97 }
98  
99 #pod =item new_groups
100 #pod
101 #pod Construct new C<Email::MIME::Header::AddressList> object from named groups of
102 #pod L<Email::Address::XS|Email::Address::XS> objects.
103 #pod
104 #pod =cut
105  
106 59 ▶sub new_groups {
Subroutine new_groups: called CC=1 SCAR=0.0
Pod documented
107 59 my ($class, @groups) = @_;
108 59 my $self = $class->new_empty();
109 59 $self->append_groups(@groups);
110 59 return $self;
111 }
112  
113 #pod =item new_mime_groups
114 #pod
115 #pod Like L<C<new_groups>|/new_groups> but in this method group names and objects properties are
116 #pod expected to be already MIME encoded, thus ASCII strings.
117 #pod
118 #pod =cut
119  
120 35 ▶sub new_mime_groups {
Subroutine new_mime_groups: called CC=13 SCAR=27.3
Pod documented
121 35 my ($class, @groups) = @_;
122 35 ▶ if (scalar @groups % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @groups % 2) 0 35 35
123 0 Carp::carp 'Odd number of elements in argument list';
124 0 return;
125 }
126 35 foreach (0 .. scalar @groups / 2 - 1) {
127 35 ▶ $groups[2 * $_] = Email::MIME::Encode::mime_decode($groups[2 * $_])
Branch: 1 branch
BranchTrueFalseTotal
if defined $groups[2 * $_] and $groups[2 * $_] =~ /=\?/u 7 28 35
Condition100%3/3
defined $groups[2 * $_] and $groups[2 * $_] =~ /=\?/u
!ll&&!rl&&r
2537
MC/DC100%2/2
defined $groups[2 * $_] and $groups[2 * $_] =~ /=\?/u
defined $groups[2 * $_] $groups[2 * $_] =~ /=\?/u
Truth table100%3/3
A and B
A= defined $groups[2 * $_]B= $groups[2 * $_] =~ /=\?/u
ABresult
0X0
100
111
128 if defined $groups[2 * $_] and $groups[2 * $_] =~ /=\?/;
129 35 $groups[2 * $_ + 1] = [ @{$groups[2 * $_ + 1]} ];
130 35 foreach (@{$groups[2 * $_ + 1]}) {
131 44 ▶ next unless Email::Address::XS->is_obj($_);
Branch: 1 branch
BranchTrueFalseTotal
unless "Email::Address::XS"->is_obj($_) 0 44 44
132 44 ▶ my $decode_phrase = (defined $_->phrase and $_->phrase =~ /=\?/);
Condition66%2/3
defined $_->phrase && $_->phrase =~ /=\?/u
!ll&&!rl&&r
0143
MC/DC50%1/2
defined $_->phrase && $_->phrase =~ /=\?/u
defined $_->phrase $_->phrase =~ /=\?/u
Truth table66%2/3
A && B
A= defined $_->phraseB= $_->phrase =~ /=\?/u
ABresult
0X0
100
111
133 44 ▶ my $decode_comment = (defined $_->comment and $_->comment =~ /=\?/);
Condition33%1/3
defined $_->comment && $_->comment =~ /=\?/u
!ll&&!rl&&r
4400
MC/DC0%0/2
defined $_->comment && $_->comment =~ /=\?/u
defined $_->comment $_->comment =~ /=\?/u
Truth table33%1/3
A && B
A= defined $_->commentB= $_->comment =~ /=\?/u
ABresult
0X0
100
111
134 44 ▶ next unless $decode_phrase or $decode_comment;
Branch: 1 branch
BranchTrueFalseTotal
unless $decode_phrase or $decode_comment 1 43 44
Condition66%2/3
$decode_phrase or $decode_comment
l!l&&r!l&&!r
4301
MC/DC50%1/2
$decode_phrase or $decode_comment
$decode_phrase $decode_comment
Truth table66%2/3
A or B
A= $decode_phraseB= $decode_comment
ABresult
1X1
011
000
135 43 $_ = ref($_)->new(copy => $_);
136 43 ▶ $_->phrase(Email::MIME::Encode::mime_decode($_->phrase))
Branch: 1 branch
BranchTrueFalseTotal
if $decode_phrase 43 0 43
137 if $decode_phrase;
138 43 ▶ $_->comment(Email::MIME::Encode::mime_decode($_->comment))
Branch: 1 branch
BranchTrueFalseTotal
if $decode_comment 0 43 43
139 if $decode_comment;
140 }
141 }
142 35 return $class->new_groups(@groups);
143 }
144  
145 #pod =item from_string
146 #pod
147 #pod Construct new C<Email::MIME::Header::AddressList> object from input string arguments.
148 #pod Calls L<Email::Address::XS::parse_email_groups|Email::Address::XS/parse_email_groups>.
149 #pod
150 #pod =cut
151  
152 22 ▶sub from_string {
Subroutine from_string: called CC=1 SCAR=0.0
Pod documented
153 22 my ($class, @strings) = @_;
154 22 return $class->new_groups(map { Email::Address::XS::parse_email_groups($_) } @strings);
155 }
156  
157 #pod =item from_mime_string
158 #pod
159 #pod Like L<C<from_string>|/from_string> but input string arguments are expected to
160 #pod be already MIME encoded.
161 #pod
162 #pod =cut
163  
164 35 ▶sub from_mime_string {
Subroutine from_mime_string: called CC=1 SCAR=0.0
Pod documented
165 35 my ($class, @strings) = @_;
166 35 return $class->new_mime_groups(map { Email::Address::XS::parse_email_groups($_) } @strings);
167 }
168  
169 #pod =back
170 #pod
171 #pod =head2 Object Methods
172 #pod
173 #pod =over 4
174 #pod
175 #pod =item as_string
176 #pod
177 #pod Returns string representation of C<Email::MIME::Header::AddressList> object.
178 #pod Calls L<Email::Address::XS::format_email_groups|Email::Address::XS/format_email_groups>.
179 #pod
180 #pod =cut
181  
182 21 ▶sub as_string {
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
183 21 my ($self) = @_;
184 21 return Email::Address::XS::format_email_groups($self->groups());
185 }
186  
187 #pod =item as_mime_string
188 #pod
189 #pod Like L<C<as_string>|/as_string> but output string will be properly and
190 #pod unambiguously MIME encoded. MIME encoding is done before calling
191 #pod L<Email::Address::XS::format_email_groups|Email::Address::XS/format_email_groups>.
192 #pod
193 #pod =cut
194  
195 24 ▶sub as_mime_string {
Subroutine as_mime_string: called CC=8 SCAR=20.9
Pod documented
196 24 my ($self, $arg) = @_;
197 24 my $charset = $arg->{charset};
198 24 my $header_name_length = $arg->{header_name_length};
199  
200 24 my @groups = $self->groups();
201 24 foreach (0 .. scalar @groups / 2 - 1) {
202 26 ▶ $groups[2 * $_] = Email::MIME::Encode::mime_encode($groups[2 * $_], $charset)
Branch: 1 branch
BranchTrueFalseTotal
if Email::MIME::Encode::_needs_mime_encode_addr($groups[2 * $_]) 2 24 26
203 if Email::MIME::Encode::_needs_mime_encode_addr($groups[2 * $_]);
204 26 $groups[2 * $_ + 1] = [ @{$groups[2 * $_ + 1]} ];
205 26 foreach (@{$groups[2 * $_ + 1]}) {
206 27 my $encode_phrase = Email::MIME::Encode::_needs_mime_encode_addr($_->phrase);
207 27 my $encode_comment = Email::MIME::Encode::_needs_mime_encode_addr($_->comment);
208 27 ▶ next unless $encode_phrase or $encode_comment;
Branch: 1 branch
BranchTrueFalseTotal
unless $encode_phrase or $encode_comment 4 23 27
Condition66%2/3
$encode_phrase or $encode_comment
l!l&&r!l&&!r
2304
MC/DC50%1/2
$encode_phrase or $encode_comment
$encode_phrase $encode_comment
Truth table66%2/3
A or B
A= $encode_phraseB= $encode_comment
ABresult
1X1
011
000
209 23 $_ = ref($_)->new(copy => $_);
210 23 ▶ $_->phrase(Email::MIME::Encode::mime_encode($_->phrase, $charset))
Branch: 1 branch
BranchTrueFalseTotal
if $encode_phrase 23 0 23
211 if $encode_phrase;
212 23 ▶ $_->comment(Email::MIME::Encode::mime_encode($_->comment, $charset))
Branch: 1 branch
BranchTrueFalseTotal
if $encode_comment 0 23 23
213 if $encode_comment;
214 }
215 }
216 24 return Email::Address::XS::format_email_groups(@groups);
217 }
218  
219 #pod =item first_address
220 #pod
221 #pod Returns first L<Email::Address::XS|Email::Address::XS> object.
222 #pod
223 #pod =cut
224  
225 7 ▶sub first_address {
Subroutine first_address: called CC=4 SCAR=14.2
Pod documented
226 7 my ($self) = @_;
227 7 ▶ return $self->{addresses}->[0] if @{$self->{addresses}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"addresses"};} 0 7 7
228 7 my $groups = $self->{groups};
229 7 foreach (0 .. @{$groups} / 2 - 1) {
230 7 ▶ next unless @{$groups->[2 * $_ + 1]};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$$groups[2 * $_ + 1];} 0 7 7
231 7 return $groups->[2 * $_ + 1]->[0];
232 }
233 0 return undef;
234 }
235  
236 #pod =item addresses
237 #pod
238 #pod Returns list of all L<Email::Address::XS|Email::Address::XS> objects.
239 #pod
240 #pod =cut
241  
242 2 ▶sub addresses {
Subroutine addresses: called CC=1 SCAR=0.0
Pod documented
243 2 my ($self) = @_;
244 2 my $t = 1;
245 2 my @addresses = @{$self->{addresses}};
246 2 push @addresses, map { @{$_} } grep { $t ^= 1 } @{$self->{groups}};
247 2 return @addresses;
248 }
249  
250 #pod =item groups
251 #pod
252 #pod Like L<C<addresses>|/addresses> but returns objects with named groups.
253 #pod
254 #pod =cut
255  
256 51 ▶sub groups {
Subroutine groups: called CC=3 SCAR=11.2
Pod documented
257 51 my ($self) = @_;
258 51 my @groups = @{$self->{groups}};
259 53 $groups[2 * $_ + 1] = [ @{$groups[2 * $_ + 1]} ]
260 51 foreach 0 .. scalar @groups / 2 - 1;
261 0 unshift @groups, undef, [ @{$self->{addresses}} ]
262 51 ▶ if @{$self->{addresses}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"addresses"};} 0 51 51
263 51 return @groups;
264 }
265  
266 #pod =item append_addresses
267 #pod
268 #pod Append L<Email::Address::XS|Email::Address::XS> objects.
269 #pod
270 #pod =cut
271  
272 0 ▶sub append_addresses {
Subroutine append_addresses: not called CC=2 SCAR=17.9
Pod documented
273 0 my ($self, @addresses) = @_;
274 0 my @valid_addresses = grep { Email::Address::XS->is_obj($_) } @addresses;
275 0 ▶ Carp::carp 'Argument is not an Email::Address::XS object' if scalar @valid_addresses != scalar @addresses;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @valid_addresses != scalar @addresses 0 0 0
276 0 push @{$self->{addresses}}, @valid_addresses;
277 }
278  
279 #pod =item append_groups
280 #pod
281 #pod Like L<C<append_addresses>|/append_addresses> but arguments are pairs of name of
282 #pod group and array reference of L<Email::Address::XS|Email::Address::XS> objects.
283 #pod
284 #pod =cut
285  
286 59 ▶sub append_groups {
Subroutine append_groups: called CC=5 SCAR=17.5
Pod documented
287 59 my ($self, @groups) = @_;
288 59 ▶ if (scalar @groups % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @groups % 2) 0 59 59
289 0 Carp::carp 'Odd number of elements in argument list';
290 0 return;
291 }
292 59 my $carp_invalid = 1;
293 59 my @valid_groups;
294 59 foreach (0 .. scalar @groups / 2 - 1) {
295 61 push @valid_groups, $groups[2 * $_];
296 61 my $addresses = $groups[2 * $_ + 1];
297 61 my @valid_addresses = grep { Email::Address::XS->is_obj($_) } @{$addresses};
298 61 ▶ if ($carp_invalid and scalar @valid_addresses != scalar @{$addresses}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($carp_invalid and scalar @valid_addresses != scalar @{$addresses;}) 0 61 61
Condition33%1/3
$carp_invalid and scalar @valid_addresses != scalar @{$addresses;}
!ll&&!rl&&r
0610
MC/DC0%0/2
$carp_invalid and scalar @valid_addresses != scalar @{$addresses;}
$carp_invalid scalar @valid_addresses != scalar @{$addresses;}
Truth table33%1/3
A and B
A= $carp_invalidB= scalar @valid_addresses != scalar @{$addresses;}
ABresult
0X0
100
111
299 0 Carp::carp 'Array element is not an Email::Address::XS object';
300 0 $carp_invalid = 0;
301 }
302 61 push @valid_groups, \@valid_addresses;
303 }
304 59 push @{$self->{groups}}, @valid_groups;
305 }
306  
307 #pod =back
308 #pod
309 #pod =head1 SEE ALSO
310 #pod
311 #pod L<RFC 2047|https://tools.ietf.org/html/rfc2047>,
312 #pod L<RFC 2822|https://tools.ietf.org/html/rfc2822>,
313 #pod L<Email::MIME>,
314 #pod L<Email::Address::XS>
315 #pod
316 #pod =head1 AUTHOR
317 #pod
318 #pod Pali E<lt>pali@cpan.orgE<gt>
319 #pod
320 #pod =cut
321  
322 3 1;
323  
324 __END__
« MIME.pm ↑ Summary MIME/Encode.pm »