lib/Data/JSEmail.pm

filter: Statementstmt 94.3% 623 / 660 Branchbran 84.8% 307 / 362 Conditioncond 70.5% 158 / 224 MC/DCmcdc 43.7% 56 / 128 Subroutinesub 94.0% 79 / 84 Podpod 25.0% 7 / 28 totaltotal 82.7% 1230 / 1486 CC 290 SCAR 28.7
CC
290
Cov
87%
_make
59.4
parseStructure
40.2
_leaf_part
30.6
CRAP 17.7
↑ Summary
1 #!/usr/bin/perl -cw
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package Data::JSEmail;
7  
8 9 our $VERSION = '0.06';
9  
10 9 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use HTML::Strip;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Email::MIME;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use Email::MIME::ContentType;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use Email::Address::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶use Encode::MIME::Header;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 9 ▶use Unicode::Normalize;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 9 ▶use Date::Format;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 9 ▶use Date::Parse;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 9 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 9 ▶use DateTime::Format::Mail;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 9 ▶use DateTime::Format::ISO8601::Format;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 9 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 9 ▶use Data::UUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 9 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 9 my $json = JSON::XS->new->utf8->canonical();
30  
31 # The Email::MIME settings this module is written against. They are
32 # package globals, so a caller may have set them either way for its own
33 # reasons; every entry point runs with these values instead, so what this
34 # module returns does not depend on who calls it.
35 # STRICT_PARAMS = 0: accept the unquoted and otherwise sloppy
36 # Content-Type/Content-Disposition parameters real mail is full of
37 # (strict mode drops them, or the whole header, with a warning).
38 # PRE_2231_FORM = 1: write a plain name=/filename= next to an RFC 2231
39 # one, for readers that do not know RFC 2231.
40 # GROUCHY = 0: do not die on a message without a header/body separator.
41 171 ▶sub _with_settings {
Subroutine _with_settings: called CC=1 SCAR=0.0
42 171 my $code = shift;
43 171 local $Email::MIME::ContentType::STRICT_PARAMS = 0;
44 171 local $Email::MIME::ContentType::PRE_2231_FORM = 1;
45 171 local $Email::Simple::GROUCHY = 0;
46 171 return $code->(@_);
47 }
48  
49 113 ▶sub parse { _with_settings(\&_parse, @_) }
Subroutine parse: called CC=1 SCAR=0.0
Pod undocumented
50  
51 113 ▶sub _parse {
Subroutine _parse: called CC=2 SCAR=7.0
52 113 my $rfc822 = shift;
53 113 ▶ my $id = shift || Digest::SHA::sha256_hex($rfc822);
Condition66%2/3
shift() || &Digest::SHA::sha256_hex($rfc822)
l!l&&r!l&&!r
10490
MC/DC0%0/2
shift() || &Digest::SHA::sha256_hex($rfc822)
shift() &Digest::SHA::sha256_hex($rfc822)
Truth table66%2/3
A || B
A= shift()B= &Digest::SHA::sha256_hex($rfc822)
ABresult
1X1
011
000
54 113 my $eml = Data::JSEmail::MIMEPart->new($rfc822);
55 113 my $res = _parse_email($id, $eml);
56 113 $res->{size} = length($rfc822);
57 113 return $res;
58 }
59  
60 0 ▶sub parse_email { _with_settings(\&_parse_email, @_) }
Subroutine parse_email: not called CC=1 SCAR=6.9
Pod undocumented
61  
62 113 ▶sub _parse_email {
Subroutine _parse_email: called CC=4 SCAR=13.9
63 113 my $id = shift;
64 113 my $eml = shift;
65 113 my $part = shift;
66  
67 113 my $preview = preview($eml);
68 113 my $headers = headers($eml);
69  
70 113 my %values;
71 113 my $bodystructure = bodystructure(\%values, $id, $eml);
72 113 my $textBody = [];
73 113 my $htmlBody = [];
74 113 my $attachments = [];
75 113 parseStructure([$bodystructure], 'mixed', 0, $textBody, $htmlBody, $attachments);
76  
77 113 ▶ my ($hasAtt) = grep { ($_->{disposition} // '') ne 'inline' } @$attachments;
Condition100%2/2
$_->{"disposition"} // ""
l!l
6031
MC/DC100%1/1
$_->{"disposition"} // ""
$_->{"disposition"}
Truth table100%2/2
A
A= $_->{"disposition"}
Aresult
11
00
78  
79 # S4.1.3: each convenience property is its header:{name}:as{form}, so
80 # it is computed from the same Raw values (Email::MIME's header() has
81 # already decoded them, and returns the first instance, not the last).
82 113 my %raw;
83 113 push @{ $raw{ lc $_->{name} } }, $_->{value} for @$headers;
84 113 ▶ my $hv = sub { my ($form, $name) = @_; header_value($form, $raw{ lc $name } || []) };
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%2/2
$raw{lc $name} || []
l!l
498745
MC/DC100%1/1
$raw{lc $name} || []
$raw{lc $name}
Truth table100%2/2
A
A= $raw{lc $name}
Aresult
11
00
85 113 ▶ my $data = {
Branch: 1 branch
BranchTrueFalseTotal
$hasAtt ? : 37 76 113
86 id => $id,
87 sender => $hv->(asAddresses => 'Sender'),
88 to => $hv->(asAddresses => 'To'),
89 cc => $hv->(asAddresses => 'Cc'),
90 bcc => $hv->(asAddresses => 'Bcc'),
91 from => $hv->(asAddresses => 'From'),
92 replyTo => $hv->(asAddresses => 'Reply-To'),
93 subject => $hv->(asText => 'Subject'),
94 sentAt => $hv->(asDate => 'Date'),
95 messageId => $hv->(asMessageIds => 'Message-Id'),
96 references => $hv->(asMessageIds => 'References'),
97 inReplyTo => $hv->(asMessageIds => 'In-Reply-To'),
98 preview => $preview,
99 hasAttachment => $hasAtt ? $JSON::true : $JSON::false,
100 headers => $headers,
101 bodyStructure => $bodystructure,
102 bodyValues => \%values,
103 textBody => $textBody,
104 htmlBody => $htmlBody,
105 attachments => $attachments,
106 };
107  
108 113 return $data;
109 }
110  
111 352 ▶sub bodystructure {
Subroutine bodystructure: called CC=17 SCAR=28.4
Pod undocumented
112 352 my $values = shift;
113 352 my $id = shift;
114 352 my $eml = shift;
115 352 my $partno = shift;
116  
117 352 my $type = {
118 'subtype' => 'plain',
119 'type' => 'text'
120 };
121 352 ▶ if (my $val = $eml->header('Content-Type')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $val = $eml->header("Content-Type")) 335 17 352
122 335 $type = parse_content_type($val);
123 }
124 352 my @parts = $eml->subparts();
125 352 ▶ if (@parts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts) { } 91 261 352
126 91 my @sub;
127 91 ▶ for (my $n = 1; $n <= @parts; $n++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n <= @parts) 239 91 330
128 239 ▶ my $subno = $partno ? "$partno.$n" : $n;
Branch: 1 branch
BranchTrueFalseTotal
$partno ? : 100 139 239
129 # One part that cannot be read must not cost the reader the rest
130 # of the message: it becomes an opaque leaf.
131 239 my $node = eval { bodystructure($values, $id, $parts[$n-1], $subno) };
132 239 ▶ $node //= _opaque_leaf($id, $parts[$n-1], $subno);
Condition33%1/3
$node //= _opaque_leaf($id, $parts[$n - 1], $subno)
l!l&&r!l&&!r
23900
MC/DC0%0/2
$node //= _opaque_leaf($id, $parts[$n - 1], $subno)
$node _opaque_leaf($id, $parts[$n - 1], $subno)
Truth table33%1/3
A //= B
A= $nodeB= _opaque_leaf($id, $parts[$n - 1], $subno)
ABresult
1X1
011
000
133 239 push @sub, $node;
134 }
135 return {
136 partId => undef,
137 blobId => undef,
138 type => "$type->{type}/$type->{subtype}",
139 size => 0,
140 headers => headers($eml),
141 name => undef,
142 cid => asOneURL($eml->header('Content-Id')),
143 charset => $type->{attributes}{charset},
144 91 language => asCommaList($eml->header('Content-Language')),
145 location => undef,
146 disposition => undef,
147 subParts => \@sub,
148 };
149 }
150 else {
151 261 my $disposition = {};
152 261 ▶ if (my $val = $eml->header('Content-Disposition')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $val = $eml->header("Content-Disposition")) 82 179 261
153 82 my $orig = $val;
154 82 $val =~ s{^(.*filename=\s*)([^\s"][^;]+)}{$1"$2"};
155 82 $disposition = parse_content_disposition($val);
156 }
157 261 ▶ $partno ||= '1';
Condition100%2/2
$partno ||= 1
l!l
19764
MC/DC100%1/1
$partno ||= 1
$partno
Truth table100%2/2
A
A= $partno
Aresult
11
00
158 # A multipart that yielded no parts (its boundary never appears, or
159 # it is nested or split past Email::MIME's limits) is kept whole as
160 # one opaque leaf: a part with a multipart type must have subParts
161 # (RFC 8621 S4.1.4), and this one's content cannot be split into any.
162 $type = { type => 'application', subtype => 'octet-stream', attributes => {} }
163 261 ▶ if lc $type->{type} eq 'multipart';
Branch: 1 branch
BranchTrueFalseTotal
if lc $type->{"type"} eq "multipart" 6 255 261
164 261 my $body = $eml->body();
165 261 my $raw_size = length($body); # size in octets of decoded content
166 261 ▶ if ($type->{type} eq 'text') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->{"type"} eq "text") 177 84 261
167 # Decode charset to Perl character string
168 177 my $decoded = eval { $eml->body_str() };
169 177 ▶ if (defined $decoded) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $decoded) { } 171 6 177
170 171 $body = $decoded;
171 } else {
172 # Fallback: try UTF-8 decode
173 6 ▶ $body = eval { Encode::decode('UTF-8', $body) } // $body;
Condition33%1/3
eval { do { &Encode::decode("UTF-8", $body) } } // $body
l!l&&r!l&&!r
600
MC/DC0%0/2
eval { do { &Encode::decode("UTF-8", $body) } } // $body
eval { do { &Encode::decode("UTF-8", $body) } } $body
Truth table33%1/3
A // B
A= eval { do { &Encode::decode("UTF-8", $body) } }B= $body
ABresult
1X1
011
000
174 }
175 # RFC 8621: line endings in bodyValues MUST be \n not \r\n
176 177 my $text_body = $body;
177 177 $text_body =~ s/\r\n/\n/g;
178 # RFC 8621 S4.1.4: isEncodingProblem is also true when the
179 # content-transfer-encoding was unknown. Email::MIME::body()
180 # quietly treats an unknown one as 7bit, so check it ourselves;
181 # the value stays the best-effort (undecoded) body.
182 177 ▶ my $problem = !defined($decoded) || !_known_cte($eml);
Condition100%3/3
defined $decoded && _known_cte($eml)
!ll&&!rl&&r
68163
MC/DC100%2/2
defined $decoded && _known_cte($eml)
defined $decoded _known_cte($eml)
Truth table100%3/3
A && B
A= defined $decodedB= _known_cte($eml)
ABresult
0X0
100
111
183 177 ▶ $values->{$partno} = {
Branch: 1 branch
BranchTrueFalseTotal
$problem ? : 14 163 177
184 value => $text_body,
185 isEncodingProblem => ($problem ? $JSON::true : $JSON::false),
186 isTruncated => $JSON::false,
187 };
188 }
189 261 my $charset = $type->{attributes}{charset};
190 261 ▶ if ($type->{type} eq 'text' and not $charset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->{"type"} eq "text" and not $charset) 31 230 261
Condition100%3/3
$type->{"type"} eq "text" and not $charset
!ll&&!rl&&r
8414631
MC/DC100%2/2
$type->{"type"} eq "text" and not $charset
$type->{"type"} eq "text" not $charset
Truth table100%3/3
A and not(B)
A= $type->{"type"} eq "text"B= not $charset
ABresult
0X0
100
111
191 31 $charset = 'us-ascii';
192 }
193 return {
194 partId => "$partno",
195 blobId => "m-$id-$partno",
196 type => "$type->{type}/$type->{subtype}",
197 size => $raw_size,
198 headers => headers($eml),
199 name => $disposition->{attributes}{filename} // $type->{attributes}{name},
200 cid => asOneURL($eml->header('Content-Id')),
201 charset => $charset,
202 language => asCommaList($eml->header('Content-Language')),
203 location => asText($eml->header('Content-Location')),
204 disposition => $disposition->{type},
205 261 ▶ };
Condition100%3/3
$disposition->{"attributes"}{"filename"} // $type->{"attributes"}{"name"}
l!l&&r!l&&!r
3713211
MC/DC100%2/2
$disposition->{"attributes"}{"filename"} // $type->{"attributes"}{"name"}
$disposition->{"attributes"}{"filename"} $type->{"attributes"}{"name"}
Truth table100%3/3
A // B
A= $disposition->{"attributes"}{"filename"}B= $type->{"attributes"}{"name"}
ABresult
1X1
011
000
206 }
207 }
208  
209 # The leaf a part that could not be read at all is reported as.
210 0 ▶sub _opaque_leaf {
Subroutine _opaque_leaf: not called CC=3 SCAR=24.8
211 0 my ($id, $eml, $partno) = @_;
212 0 ▶ my $raw = eval { $eml->body_raw } // '';
Condition0%0/2
eval { do { $eml->body_raw } } // ""
l!l
00
MC/DC0%0/1
eval { do { $eml->body_raw } } // ""
eval { do { $eml->body_raw } }
Truth table0%0/2
A
A= eval { do { $eml->body_raw } }
Aresult
11
00
213 return {
214 partId => "$partno",
215 blobId => "m-$id-$partno",
216 type => 'application/octet-stream',
217 size => length($raw),
218 0 ▶ headers => (eval { headers($eml) } || []),
Condition0%0/2
eval { do { headers($eml) } } || []
l!l
00
MC/DC0%0/1
eval { do { headers($eml) } } || []
eval { do { headers($eml) } }
Truth table0%0/2
A
A= eval { do { headers($eml) } }
Aresult
11
00
219 name => undef,
220 cid => undef,
221 charset => undef,
222 language => undef,
223 location => undef,
224 disposition => undef,
225 };
226 }
227  
228 136 ▶sub asDate {
Subroutine asDate: called CC=4 SCAR=14.0
Pod undocumented
229 136 my $val = shift;
230 136 ▶ return undef unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 136 136
231 136 $val =~ s/\(.*//; # strip comments
232 136 $val =~ s/^\s+//; # strip leading whitespace
233 136 $val =~ s/\s+$//; # strip trailing whitespace
234 # Add :00 seconds if missing (e.g. "23:32 -0330" → "23:32:00 -0330")
235 136 $val =~ s/(\s\d{2}:\d{2})\s+([-+]\d{4})/$1:00 $2/;
236 136 my $dt = eval { DateTime::Format::Mail->parse_datetime($val) };
237 136 ▶ return undef unless $dt;
Branch: 1 branch
BranchTrueFalseTotal
unless $dt 10 126 136
238 126 my $tz = $dt->time_zone;
239 126 ▶ if ($tz->isa('DateTime::TimeZone::Floating')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tz->isa("DateTime::TimeZone::Floating")) 0 126 126
240 0 $dt->set_time_zone('UTC');
241 }
242 126 return DateTime::Format::ISO8601::Format->new->format_datetime($dt);
243 }
244  
245 144 ▶sub asMessageIds {
Subroutine asMessageIds: called CC=3 SCAR=11.0
Pod undocumented
246 144 my $val = shift;
247 144 ▶ return undef unless $val;
Branch: 1 branch
BranchTrueFalseTotal
unless $val 1 143 144
248 143 my @list = $val =~ m{<([^\>]+)>}gs;
249 143 ▶ return undef unless @list;
Branch: 1 branch
BranchTrueFalseTotal
unless @list 1 142 143
250 142 return \@list;
251 }
252  
253 352 ▶sub asCommaList {
Subroutine asCommaList: called CC=2 SCAR=6.9
Pod undocumented
254 352 my $val = shift;
255 352 ▶ return undef unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 350 2 352
256 2 $val =~ s/^\s+//;
257 2 $val =~ s/\s+$//;
258 2 my @list = split /\s*,\s*/, $val;
259 2 return \@list;
260 }
261  
262 358 ▶sub asURLs {
Subroutine asURLs: called CC=6 SCAR=18.1
Pod undocumented
263 358 my $val = shift;
264 358 ▶ return undef unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 338 20 358
265 20 $val =~ s/^\s+//;
266 20 $val =~ s/\s+$//;
267 20 ▶ return undef unless length($val);
Branch: 1 branch
BranchTrueFalseTotal
unless length $val 1 19 20
268  
269 # Extract URLs from angle brackets
270 19 my @list;
271 19 ▶ while ($val =~ m/<([^>]+)>/gs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /<([^>]+)>/gs) 22 19 41
272 22 push @list, $1;
273 }
274 19 ▶ return \@list if @list;
Branch: 1 branch
BranchTrueFalseTotal
if @list 18 1 19
275  
276 # No angle brackets — treat whole value as a URL if it looks like one
277 1 ▶ return undef unless $val =~ m{^[a-zA-Z][a-zA-Z0-9+.-]*:};
Branch: 1 branch
BranchTrueFalseTotal
unless $val =~ /^[a-zA-Z][a-zA-Z0-9+.-]*:/ 1 0 1
278 0 $val =~ s/ .*//; # strip after first whitespace
279 0 return [$val];
280 }
281  
282 352 ▶sub asOneURL {
Subroutine asOneURL: called CC=2 SCAR=6.9
Pod undocumented
283 352 my $val = shift;
284 352 ▶ my $list = asURLs($val) || [];
Condition100%2/2
asURLs($val) || []
l!l
14338
MC/DC100%1/1
asURLs($val) || []
asURLs($val)
Truth table100%2/2
A
A= asURLs($val)
Aresult
11
00
285 352 return $list->[-1];
286 }
287  
288 698 ▶sub asText {
Subroutine asText: called CC=6 SCAR=18.0
Pod undocumented
289 698 my $val = shift;
290 698 ▶ return undef unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 310 388 698
291 # Decode MIME-Header encoded words, then NFC normalize
292 388 my $decoded = eval { decode('MIME-Header', $val) };
293 388 ▶ $decoded = $val unless defined $decoded;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $decoded 0 388 388
294 # If still raw bytes (not flagged as UTF-8), try UTF-8 decode
295 388 ▶ if (!Encode::is_utf8($decoded) && $decoded =~ /[\x80-\xff]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not &Encode::is_utf8($decoded) and $decoded =~ /[\x80-\xff]/) 26 362 388
Condition100%3/3
not &Encode::is_utf8($decoded) and $decoded =~ /[\x80-\xff]/
!ll&&!rl&&r
7229026
MC/DC100%2/2
not &Encode::is_utf8($decoded) and $decoded =~ /[\x80-\xff]/
not &Encode::is_utf8($decoded) $decoded =~ /[\x80-\xff]/
Truth table100%3/3
not(A) and B
A= not &Encode::is_utf8($decoded)B= $decoded =~ /[\x80-\xff]/
ABresult
0X0
100
111
296 26 ▶ $decoded = eval { Encode::decode('UTF-8', $decoded) } // $decoded;
Condition33%1/3
eval { do { &Encode::decode("UTF-8", $decoded) } } // $decoded
l!l&&r!l&&!r
2600
MC/DC0%0/2
eval { do { &Encode::decode("UTF-8", $decoded) } } // $decoded
eval { do { &Encode::decode("UTF-8", $decoded) } } $decoded
Truth table33%1/3
A // B
A= eval { do { &Encode::decode("UTF-8", $decoded) } }B= $decoded
ABresult
1X1
011
000
297 }
298 388 my $res = NFC($decoded);
299 388 $res =~ s/^\s*//;
300 388 $res =~ s/\s*$//;
301 388 return $res;
302 }
303  
304 # An addr-spec as JMAP reports it: RFC 6532 lets the local part and domain
305 # be UTF-8, which arrives as octets and must become characters (or it is
306 # encoded a second time on output); the domain is lower-cased. Octets that
307 # are not UTF-8 are left as they are.
308 288 ▶sub _addr_spec {
Subroutine _addr_spec: called CC=5 SCAR=16.1
309 288 my ($email) = @_;
310 288 ▶ return $email unless defined $email;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $email 4 284 288
311 284 ▶ if ($email =~ /[\x80-\xff]/ && $email !~ /[^\x00-\xff]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($email =~ /[\x80-\xff]/ and not $email =~ /[^\x00-\xff]/) 18 266 284
Condition66%2/3
$email =~ /[\x80-\xff]/ and not $email =~ /[^\x00-\xff]/
!ll&&!rl&&r
266018
MC/DC50%1/2
$email =~ /[\x80-\xff]/ and not $email =~ /[^\x00-\xff]/
$email =~ /[\x80-\xff]/ not $email =~ /[^\x00-\xff]/
Truth table66%2/3
A and not(B)
A= $email =~ /[\x80-\xff]/B= not $email =~ /[^\x00-\xff]/
ABresult
0X0
100
111
312 18 my $copy = $email;
313 18 my $chars = eval { Encode::decode('UTF-8', $copy, Encode::FB_CROAK) };
314 18 ▶ $email = NFC($chars) if defined $chars;
Branch: 1 branch
BranchTrueFalseTotal
if defined $chars 17 1 18
315 }
316 284 $email =~ s/\@(.*)/"@" . lc($1)/e;
317 284 return $email;
318 }
319  
320 # The (group, [mailbox...]) pairs of an address-list Raw value, with the
321 # phrases still encoded: asText decodes them, once. (Email::MIME's
322 # from_mime_string decodes them first, so they were decoded twice.)
323 253 ▶sub _address_groups {
Subroutine _address_groups: called CC=2 SCAR=7.0
324 253 my $raw = shift;
325 253 my @groups = eval { Email::Address::XS::parse_email_groups($raw) };
326 253 ▶ return $@ ? undef : \@groups;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 253 253
327 }
328  
329 247 ▶sub asAddresses {
Subroutine asAddresses: called CC=2 SCAR=7.0
Pod documented
330 247 my $emails = shift;
331  
332 247 my $res = asGroupAddresses($emails);
333 247 ▶ return undef unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 247 247
334  
335 247 my $arr = [ grep { defined $_->{email} } @$res ];
336 247 return $arr;
337 }
338  
339 6 ▶sub asGroupedAddresses {
Subroutine asGroupedAddresses: called CC=6 SCAR=18.0
Pod undocumented
340 # RFC 8621: returns EmailAddressGroup[] — [{name, addresses}]
341 6 my $emails = shift;
342 6 ▶ return undef unless defined $emails;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $emails 0 6 6
343 6 ▶ my $groups = _address_groups($emails) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $groups = _address_groups($emails) or return undef 6 0 6
344 6 my @addrs = @$groups;
345 6 my @res;
346 6 ▶ while (@addrs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@addrs) 11 6 17
347 11 my $group = shift @addrs;
348 11 my $list = shift @addrs;
349 11 my @addresses;
350 11 foreach my $addr (@$list) {
351 12 my $name = $addr->phrase();
352 12 my $email = _addr_spec($addr->address());
353 12 push @addresses, {
354 name => asText($name),
355 email => $email,
356 };
357 }
358 11 ▶ push @res, {
Branch: 1 branch
BranchTrueFalseTotal
defined $group ? : 6 5 11
359 name => defined $group ? asText($group) : undef,
360 addresses => \@addresses,
361 };
362 }
363 6 return \@res;
364 }
365  
366 247 ▶sub asGroupAddresses {
Subroutine asGroupAddresses: called CC=7 SCAR=19.5
Pod undocumented
367 # Internal format with sentinel entries (backward compat)
368 247 my $emails = shift;
369  
370 247 ▶ return undef unless defined $emails;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $emails 0 247 247
371 247 ▶ my $groups = _address_groups($emails) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $groups = _address_groups($emails) or return undef 247 0 247
372 247 my @addrs = @$groups;
373 247 my @res;
374 247 ▶ while (@addrs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@addrs) 246 247 493
375 246 my $group = shift @addrs;
376 246 my $list = shift @addrs;
377 246 ▶ if (defined $group) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $group) 2 244 246
378 2 push @res, {
379 name => asText($group),
380 email => undef,
381 };
382 }
383 246 foreach my $addr (@$list) {
384 276 my $name = $addr->phrase();
385 276 my $email = _addr_spec($addr->address());
386 276 push @res, {
387 name => asText($name),
388 email => $email,
389 };
390 }
391 246 ▶ if (defined $group) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $group) 2 244 246
392 2 push @res, {
393 name => undef,
394 email => undef,
395 };
396 }
397 }
398  
399 247 return \@res;
400 }
401  
402 465 ▶sub headers {
Subroutine headers: called CC=7 SCAR=19.7
Pod undocumented
403 465 my $eml = shift;
404 # Parse raw header string to preserve exact values including
405 # leading whitespace after colon (RFC 8621 raw header form)
406 465 my $raw = $eml->header_obj->as_string;
407 465 my @res;
408 # Unfold continuation lines first, then split on header boundaries
409 my @lines;
410 465 for my $line (split /\r?\n/, $raw) {
411 2213 ▶ if ($line =~ /^\s/ && @lines) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^\s/ and @lines) { } 86 2127 2213
elsif ($line =~ /^([^:]+):(.*)/) { } 2127 0 2127
Condition66%2/3
$line =~ /^\s/ and @lines
!ll&&!rl&&r
2127086
MC/DC50%1/2
$line =~ /^\s/ and @lines
$line =~ /^\s/ @lines
Truth table66%2/3
A and B
A= $line =~ /^\s/B= @lines
ABresult
0X0
100
111
412 # Continuation line — append to previous
413 86 $lines[-1] .= "\r\n$line";
414 }
415 elsif ($line =~ /^([^:]+):(.*)/) {
416 2127 push @lines, $line;
417 }
418 }
419 465 for my $line (@lines) {
420 2127 ▶ if ($line =~ /^([^:]+):(.*)$/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^([^:]+):(.*)$/s) 2127 0 2127
421 2127 push @res, { name => $1, value => $2 };
422 }
423 }
424 465 return \@res;
425 }
426  
427 # The content-transfer-encodings RFC 2045 S6.1 defines; anything else
428 # (an x-token, a typo) is one we cannot decode.
429 9 my %KNOWN_CTE = map { $_ => 1 } qw(7bit 8bit binary base64 quoted-printable);
430  
431 # True if the part has no Content-Transfer-Encoding or one we know.
432 171 ▶sub _known_cte {
Subroutine _known_cte: called CC=4 SCAR=13.9
433 171 my $eml = shift;
434 171 my $cte = $eml->header('Content-Transfer-Encoding');
435 171 ▶ return 1 unless defined $cte;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cte 99 72 171
436 72 $cte =~ s/;.*//s; # drop any parameters (S/MIME etc.)
437 72 $cte =~ s/\A\s+|\s+\z//g;
438 72 ▶ return 1 if $cte eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $cte eq "" 0 72 72
439 72 ▶ return $KNOWN_CTE{lc $cte} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$KNOWN_CTE{lc $cte} ? : 64 8 72
440 }
441  
442 108 ▶sub _clean {
Subroutine _clean: called CC=1 SCAR=0.0
443 108 my ($type, $text) = @_;
444 108 return $text;
445 }
446  
447 108 ▶sub _body_str {
Subroutine _body_str: called CC=2 SCAR=6.9
448 108 my $eml = shift;
449 108 my $str = eval { $eml->body_str() };
450 108 ▶ return $str if $str;
Branch: 1 branch
BranchTrueFalseTotal
if $str 104 4 108
451 4 return Encode::decode('us-ascii', $eml->body_raw());
452 }
453  
454 # XXX: re-define on top of bodyStructure?
455 169 ▶sub preview {
Subroutine preview: called CC=6 SCAR=17.9
Pod undocumented
456 169 my $eml = shift;
457 169 ▶ my $type = $eml->content_type() || 'text/plain';
Condition100%2/2
$eml->content_type || 'text/plain'
l!l
1609
MC/DC100%1/1
$eml->content_type || 'text/plain'
$eml->content_type
Truth table100%2/2
A
A= $eml->content_type
Aresult
11
00
458 169 ▶ if ($type =~ m{text/plain}i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ m[text/plain]i) 98 71 169
459 98 my $text = _clean($type, _body_str($eml));
460 98 return make_preview($text);
461 }
462 71 ▶ if ($type =~ m{text/html}i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ m[text/html]i) 10 61 71
463 10 my $text = _clean($type, _body_str($eml));
464 10 return make_preview(htmltotext($text));
465 }
466 61 foreach my $sub ($eml->subparts()) {
467 56 my $res = eval { preview($sub) };
468 56 ▶ return $res if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 56 0 56
469 }
470 5 return undef;
471 }
472  
473 108 ▶sub make_preview {
Subroutine make_preview: called CC=1 SCAR=0.0
Pod undocumented
474 108 my $text = shift;
475 108 $text =~ s/\s+/ /gs;
476 108 return substr($text, 0, 256);
477 }
478  
479 0 ▶sub hasatt {
Subroutine hasatt: not called CC=5 SCAR=34.0
Pod undocumented
480 0 my $bs = shift;
481 0 ▶ if ($bs->{subParts}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bs->{"subParts"}) 0 0 0
482 0 foreach my $sub (@{$bs->{subParts}}) {
483 0 ▶ return 1 if hasatt($sub);
Branch: 1 branch
BranchTrueFalseTotal
if hasatt($sub) 0 0 0
484 }
485 }
486 0 ▶ return 1 if $bs->{type} =~ m{(image|video|application)/}; # others?
Branch: 1 branch
BranchTrueFalseTotal
if $bs->{"type"} =~ m[(image|video|application)/] 0 0 0
487 0 return 0;
488 }
489  
490 352 ▶sub isInlineMediaType {
Subroutine isInlineMediaType: called CC=4 SCAR=14.1
Pod undocumented
491 352 my $type = shift;
492 352 ▶ return 1 if $type =~ m{^image/};
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ m[^image/] 34 318 352
493 318 ▶ return 1 if $type =~ m{^audio/};
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ m[^audio/] 0 318 318
494 318 ▶ return 1 if $type =~ m{^video/};
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ m[^video/] 0 318 318
495 318 return 0;
496 }
497  
498 204 ▶sub parseStructure {
Subroutine parseStructure: called CC=38 SCAR=36.9
Pod undocumented
499 204 my $parts = shift;
500 204 my $multipartType = shift;
501 204 my $inAlternative = shift;
502 204 my $textBody = shift;
503 204 my $htmlBody = shift;
504 204 my $attachments = shift;
505  
506 204 ▶ my $textCount = $textBody ? scalar @$textBody : -1;
Branch: 1 branch
BranchTrueFalseTotal
$textBody ? : 204 0 204
507 204 ▶ my $htmlCount = $htmlBody ? scalar @$htmlBody : -1;
Branch: 1 branch
BranchTrueFalseTotal
$htmlBody ? : 204 0 204
508  
509 204 ▶ for (my $i = 0; $i < @$parts; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$parts) 352 204 556
510 352 my $part = $parts->[$i];
511 352 ▶ my $isMultipart = $part->{subParts} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$part->{'subParts'} ? : 91 261 352
512 352 ▶ my $subMultiType = $isMultipart && $part->{type} =~ m{^multipart/(.*)}i ? lc $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$isMultipart && $part->{'type'} =~ m[^multipart/(.*)]i ? : 91 261 352
Condition66%2/3
$isMultipart and $part->{"type"} =~ m[^multipart/(.*)]i
!ll&&!rl&&r
261091
MC/DC50%1/2
$isMultipart and $part->{"type"} =~ m[^multipart/(.*)]i
$isMultipart $part->{"type"} =~ m[^multipart/(.*)]i
Truth table66%2/3
A and B
A= $isMultipartB= $part->{"type"} =~ m[^multipart/(.*)]i
ABresult
0X0
100
111
513  
514 # Determine part type (matching Cyrus classification)
515 my $isPlain = $part->{type} eq 'text/plain'
516 || $part->{type} eq 'text/richtext'
517 352 ▶ || $part->{type} eq 'text/enriched';
Condition66%4/6
$part->{'type'} eq 'text/plain' || $part->{'type'} eq 'text/richtext' || $part->{'type'} eq 'text/enriched'
l!l&&r!l&&!r
1500202
$part->{"type"} eq "text/plain" or $part->{"type"} eq "text/richtext"
l!l&&r!l&&!r
1500202
MC/DC33%1/3
$part->{'type'} eq 'text/plain' || $part->{'type'} eq 'text/richtext' || $part->{'type'} eq 'text/enriched'
$part->{"type"} eq "text/plain" $part->{"type"} eq "text/richtext" $part->{'type'} eq 'text/enriched'
Truth table50%2/4
A or B || C
A= $part->{"type"} eq "text/plain"B= $part->{"type"} eq "text/richtext"C= $part->{'type'} eq 'text/enriched'
ABCresult
1XX1
01X1
0011
0000
518 352 my $isHTML = $part->{type} eq 'text/html';
519 352 my $isInlineMedia = isInlineMediaType($part->{type});
520  
521 my $isInline = ($part->{disposition} // '') ne 'attachment' &&
522 ($isPlain || $isHTML || $isInlineMedia) &&
523 ($i == 0 ||
524 ( $multipartType ne 'related' &&
525 352 ▶ ( $isInlineMedia || !$part->{name} ) ) );
Condition100%23/23
($part->{'disposition'} // '') ne 'attachment' && ($isPlain || $isHTML || $isInlineMedia) && ($i == 0 || $multipartType ne 'related' && ($isInlineMedia || !$part->{'name'}))
!ll&&!rl&&r
16018174
($part->{"disposition"} // "") ne "attachment" and $isPlain || $isHTML || $isInlineMedia
!ll&&!rl&&r
46114192
$part->{"disposition"} // ""
l!l
82270
$isPlain or $isHTML or $isInlineMedia
l!l&&r!l&&!r
16824114
$isPlain or $isHTML
l!l&&r!l&&!r
14226138
$i == 0 or $multipartType ne "related" and $isInlineMedia || !$part->{"name"}
l!l&&r!l&&!r
1314318
$multipartType ne "related" and $isInlineMedia || !$part->{"name"}
!ll&&!rl&&r
13543
$isInlineMedia or not $part->{"name"}
l!l&&r!l&&!r
11325
MC/DC37%3/8
($part->{'disposition'} // '') ne 'attachment' && ($isPlain || $isHTML || $isInlineMedia) && ($i == 0 || $multipartType ne 'related' && ($isInlineMedia || !$part->{'name'}))
$part->{"disposition"} $isPlain $isHTML $isInlineMedia $i == 0 $multipartType ne "related" $isInlineMedia not $part->{"name"}
Truth table29%5/17
A and B or C or D && E or F and G or not(H)
A= $part->{"disposition"}B= $isPlainC= $isHTMLD= $isInlineMediaE= $i == 0F= $multipartType ne "related"G= $isInlineMediaH= not $part->{"name"}
ABCDEFGHresult
0XXXXXXX0
1000XXXX0
11XX00XX0
11XX01000
101X00XX0
101X01000
100100XX0
100101000
11XX1XXX1
11XX011X1
11XX01011
101X1XXX1
101X011X1
101X01011
10011XXX1
1001011X1
100101011
526  
527 352 ▶ if ($isMultipart) {
Branch: 2 branches
BranchTrueFalseTotal
if ($isMultipart) { } 91 261 352
elsif ($isInline) { } 174 87 261
528 91 ▶ parseStructure($part->{subParts}, $subMultiType,
Condition100%3/3
$inAlternative || $subMultiType eq 'alternative'
l!l&&r!l&&!r
132058
MC/DC100%2/2
$inAlternative || $subMultiType eq 'alternative'
$inAlternative $subMultiType eq 'alternative'
Truth table100%3/3
A || B
A= $inAlternativeB= $subMultiType eq 'alternative'
ABresult
1X1
011
000
529 $inAlternative || ( $subMultiType eq 'alternative' ),
530 $textBody, $htmlBody, $attachments);
531 }
532 elsif ($isInline) {
533 174 ▶ if ($multipartType eq 'alternative') {
Branch: 1 branch
BranchTrueFalseTotal
if ($multipartType eq "alternative") 23 151 174
534 23 ▶ if ($isPlain && $textBody) {
Branch: 2 branches
BranchTrueFalseTotal
if ($isPlain and $textBody) { } 12 11 23
elsif ($isHTML and $htmlBody) { } 11 0 11
Condition66%2/3
$isPlain and $textBody
!ll&&!rl&&r
11012
MC/DC50%1/2
$isPlain and $textBody
$isPlain $textBody
Truth table66%2/3
A and B
A= $isPlainB= $textBody
ABresult
0X0
100
111
535 12 push @$textBody, $part;
536 }
537 ▶ elsif ($isHTML && $htmlBody) {
Condition33%1/3
$isHTML and $htmlBody
!ll&&!rl&&r
0011
MC/DC0%0/2
$isHTML and $htmlBody
$isHTML $htmlBody
Truth table33%1/3
A and B
A= $isHTMLB= $htmlBody
ABresult
0X0
100
111
538 11 push @$htmlBody, $part;
539 }
540 else {
541 0 push @$attachments, $part;
542 }
543 23 next;
544 }
545 # Inside an alternative ancestor: seeing a text part means
546 # we can't also use it for HTML, and vice versa
547 151 ▶ if ($inAlternative) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inAlternative) 21 130 151
548 21 ▶ if ($isPlain) {
Branch: 2 branches
BranchTrueFalseTotal
if ($isPlain) { } 8 13 21
elsif ($isHTML) { } 9 4 13
549 8 $htmlBody = undef;
550 }
551 elsif ($isHTML) {
552 9 $textBody = undef;
553 }
554 }
555 151 ▶ push @$textBody, $part if $textBody;
Branch: 1 branch
BranchTrueFalseTotal
if $textBody 142 9 151
556 151 ▶ push @$htmlBody, $part if $htmlBody;
Branch: 1 branch
BranchTrueFalseTotal
if $htmlBody 139 12 151
557 151 ▶ if ((!$textBody || !$htmlBody) && $isInlineMedia) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $textBody && $htmlBody and $isInlineMedia) 4 147 151
Condition100%6/6
not $textBody && $htmlBody and $isInlineMedia
!ll&&!rl&&r
130174
$textBody && $htmlBody
!ll&&!rl&&r
912130
MC/DC66%2/3
not $textBody && $htmlBody and $isInlineMedia
$textBody $htmlBody $isInlineMedia
Truth table80%4/5
not(A && B) and C
A= $textBodyB= $htmlBodyC= $isInlineMedia
ABCresult
11X0
0X00
1000
0X11
1011
558 4 push @$attachments, $part;
559 }
560 }
561 else {
562 87 push @$attachments, $part;
563 }
564 }
565  
566 # Alternative fallback: if only one type was found, copy to the other
567 204 ▶ if ($multipartType eq 'alternative' && $textBody && $htmlBody) {
Branch: 1 branch
BranchTrueFalseTotal
if ($multipartType eq "alternative" and $textBody and $htmlBody) 20 184 204
Condition66%4/6
$multipartType eq "alternative" and $textBody and $htmlBody
!ll&&!rl&&r
184020
$multipartType eq "alternative" and $textBody
!ll&&!rl&&r
184020
MC/DC33%1/3
$multipartType eq "alternative" and $textBody and $htmlBody
$multipartType eq "alternative" $textBody $htmlBody
Truth table50%2/4
A and B and C
A= $multipartType eq "alternative"B= $textBodyC= $htmlBody
ABCresult
0XX0
10X0
1100
1111
568 20 ▶ if ($textCount == scalar @$textBody) {
Branch: 1 branch
BranchTrueFalseTotal
if ($textCount == scalar @$textBody) 4 16 20
569 # No text parts found, copy HTML parts to text
570 4 push @$textBody, @{$htmlBody}[$htmlCount .. $#$htmlBody];
571 }
572 20 ▶ if ($htmlCount == scalar @$htmlBody) {
Branch: 1 branch
BranchTrueFalseTotal
if ($htmlCount == scalar @$htmlBody) 0 20 20
573 # No HTML parts found, copy text parts to HTML
574 0 push @$htmlBody, @{$textBody}[$textCount .. $#$textBody];
575 }
576 }
577 }
578  
579 # --- header forms (RFC 8621 S4.1.2, S4.1.3) --------------------------------
580 #
581 # One codec for the parsed header forms, used in both directions: the
582 # get side (header_value) reads the Raw value of each instance, and the
583 # create side (format_header) writes a Raw value that header_value reads
584 # back as what was given. make() and parse() use these for every header
585 # they read or write, the convenience properties included.
586  
587 my %GET_FORM = (
588 6 ▶ asRaw => sub { $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
589 9 asText => \&asText,
590 asAddresses => \&asAddresses,
591 asGroupedAddresses => \&asGroupedAddresses,
592 asMessageIds => \&asMessageIds,
593 asDate => \&asDate,
594 asURLs => \&asURLs,
595 );
596  
597 2000 ▶sub _form_name {
Subroutine _form_name: called CC=4 SCAR=13.9
598 2000 my $form = shift;
599 2000 ▶ return 'asRaw' if !defined $form || $form eq '' || $form eq 'Raw';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $form or $form eq "" or $form eq "Raw" 18 1982 2000
Condition83%5/6
not defined $form or $form eq "" or $form eq "Raw"
l!l&&r!l&&!r
1711982
not defined $form or $form eq ""
l!l&&r!l&&!r
1701983
MC/DC66%2/3
not defined $form or $form eq "" or $form eq "Raw"
not defined $form $form eq "" $form eq "Raw"
Truth table75%3/4
not(A) or B or C
A= not defined $formB= $form eq ""C= $form eq "Raw"
ABCresult
1XX1
01X1
0011
0000
600 1982 return $form;
601 }
602  
603 1270 ▶sub header_value {
Subroutine header_value: called CC=5 SCAR=16.1
Pod documented
604 1270 my ($form, $raw_values, %opt) = @_;
605 1270 $form = _form_name($form);
606 1270 ▶ my $conv = $GET_FORM{$form} or die "Unknown header form '$form'\n";
Branch: 1 branch
BranchTrueFalseTotal
my $conv = $GET_FORM{$form} or die "Unknown header form '${form}'\n" 1269 1 1270
607 1269 ▶ my @values = grep { defined } (ref $raw_values eq 'ARRAY' ? @$raw_values : ($raw_values));
Branch: 1 branch
BranchTrueFalseTotal
ref $raw_values eq 'ARRAY' ? : 1267 2 1269
608 1269 ▶ return [ map { $conv->($_) } @values ] if $opt{all};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"all"} 3 1266 1269
609 1266 ▶ return @values ? $conv->($values[-1]) : undef;
Branch: 1 branch
BranchTrueFalseTotal
@values ? : 520 746 1266
610 }
611  
612 2 ▶sub header_property {
Subroutine header_property: called CC=4 SCAR=14.3
Pod documented
613 2 my ($headers, $prop) = @_;
614 2 ▶ my ($name, $form, $all) = parse_header_property($prop)
Branch: 1 branch
BranchTrueFalseTotal
() = parse_header_property($prop) or die "Invalid header property '${prop}'\n" 2 0 2
615 or die "Invalid header property '$prop'\n";
616 4 my @values = map { $_->{value} }
617 2 ▶ grep { lc($_->{name} // '') eq lc $name } @{ $headers || [] };
Condition50%2/4
$_->{"name"} // ""
l!l
40
$headers || []
l!l
20
MC/DC0%0/1
$_->{"name"} // ""
$_->{"name"}
MC/DC0%0/1
$headers || []
$headers
Truth table50%1/2
A
A= $_->{"name"}
Aresult
11
00
Truth table50%1/2
A
A= $headers
Aresult
11
00
618 2 return header_value($form, \@values, all => $all);
619 }
620  
621 38 ▶sub parse_header_property {
Subroutine parse_header_property: called CC=5 SCAR=16.1
Pod documented
622 38 my $prop = shift;
623 38 ▶ return () unless defined $prop
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prop and $prop =~ /\Aheader:([\x21-\x39\x3b-\x7e]+?)(?::(as[A-Za-z]+))?(:all)?\z/ 4 34 38
Condition66%2/3
defined $prop and $prop =~ /\Aheader:([\x21-\x39\x3b-\x7e]+?)(?::(as[A-Za-z]+))?(:all)?\z/
!ll&&!rl&&r
0434
MC/DC50%1/2
defined $prop and $prop =~ /\Aheader:([\x21-\x39\x3b-\x7e]+?)(?::(as[A-Za-z]+))?(:all)?\z/
defined $prop $prop =~ /\Aheader:([\x21-\x39\x3b-\x7e]+?)(?::(as[A-Za-z]+))?(:all)?\z/
Truth table66%2/3
A and B
A= defined $propB= $prop =~ /\Aheader:([\x21-\x39\x3b-\x7e]+?)(?::(as[A-Za-z]+))?(:all)?\z/
ABresult
0X0
100
111
624 && $prop =~ /\Aheader:([\x21-\x39\x3b-\x7e]+?)(?::(as[A-Za-z]+))?(:all)?\z/;
625 34 ▶ my ($name, $form, $all) = ($1, _form_name($2), $3 ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$3 ? : 6 28 34
626 34 ▶ return () unless $GET_FORM{$form};
Branch: 1 branch
BranchTrueFalseTotal
unless $GET_FORM{$form} 2 32 34
627 32 return ($name, $form, $all);
628 }
629  
630 # RFC 8621 S4.1.2: the header fields each parsed form may be used on.
631 # A field not defined in RFC 5322 or RFC 2369 may use every form.
632 9 my %FORM_FIELDS = (
633 asText => [qw(subject comments keywords list-id)],
634 asAddresses => [qw(from sender reply-to to cc bcc resent-from resent-sender
635 resent-reply-to resent-to resent-cc resent-bcc)],
636 asMessageIds => [qw(message-id in-reply-to references resent-message-id)],
637 asDate => [qw(date resent-date)],
638 asURLs => [qw(list-help list-unsubscribe list-subscribe list-post
639 list-owner list-archive)],
640 );
641 9 $FORM_FIELDS{asGroupedAddresses} = $FORM_FIELDS{asAddresses};
642 9 my %FORM_ALLOWED = map { my $f = $_; ($f => { map { $_ => 1 } @{ $FORM_FIELDS{$f} } }) } keys %FORM_FIELDS;
643 9 my %DEFINED_FIELD = map { $_ => 1 } qw(
644 date from sender reply-to to cc bcc message-id in-reply-to references subject
645 comments keywords resent-date resent-from resent-sender resent-to resent-cc
646 resent-bcc resent-message-id return-path received
647 list-help list-unsubscribe list-subscribe list-post list-owner list-archive
648 );
649  
650 47 ▶sub header_form_allowed {
Subroutine header_form_allowed: called CC=6 SCAR=17.9
Pod documented
651 47 my ($field, $form) = @_;
652 47 $form = _form_name($form);
653 47 ▶ return 0 unless $GET_FORM{$form};
Branch: 1 branch
BranchTrueFalseTotal
unless $GET_FORM{$form} 1 46 47
654 46 ▶ return 1 if $form eq 'asRaw';
Branch: 1 branch
BranchTrueFalseTotal
if $form eq "asRaw" 15 31 46
655 31 ▶ return 1 unless $DEFINED_FIELD{ lc($field // '') };
Branch: 1 branch
BranchTrueFalseTotal
unless $DEFINED_FIELD{lc($field // "")} 10 21 31
Condition50%1/2
$field // ""
l!l
310
MC/DC0%0/1
$field // ""
$field
Truth table50%1/2
A
A= $field
Aresult
11
00
656 21 ▶ return $FORM_ALLOWED{$form}{ lc $field } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$FORM_ALLOWED{$form}{lc $field} ? : 13 8 21
657 }
658  
659 9 my %SET_FORM = (
660 asRaw => \&_fmt_raw,
661 asText => \&_fmt_text,
662 asAddresses => \&_fmt_addresses,
663 asGroupedAddresses => \&_fmt_grouped,
664 asMessageIds => \&_fmt_msgids,
665 asDate => \&_fmt_date,
666 asURLs => \&_fmt_urls,
667 );
668  
669 649 ▶sub format_header {
Subroutine format_header: called CC=5 SCAR=16.1
Pod documented
670 649 my ($form, $value) = @_;
671 649 $form = _form_name($form);
672 649 ▶ my $fmt = $SET_FORM{$form} or die "Unknown header form '$form'\n";
Branch: 1 branch
BranchTrueFalseTotal
my $fmt = $SET_FORM{$form} or die "Unknown header form '${form}'\n" 648 1 649
673 648 ▶ return undef unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 539 109 648
674 109 my $res = $fmt->($value);
675 94 ▶ return undef unless defined $res;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $res 2 92 94
676 92 ▶ return $res if $form eq 'asRaw';
Branch: 1 branch
BranchTrueFalseTotal
if $form eq "asRaw" 18 74 92
677 74 return " $res";
678 }
679  
680 15 ▶sub _bad { my ($form, $why) = @_; die "Invalid $form header value: $why\n" }
Subroutine _bad: called CC=1 SCAR=0.0
681  
682 20 ▶sub _fmt_raw {
Subroutine _fmt_raw: called CC=4 SCAR=13.9
683 20 my $v = shift;
684 20 ▶ _bad(asRaw => 'not a string') if ref $v;
Branch: 1 branch
BranchTrueFalseTotal
if ref $v 0 20 20
685 20 ▶ _bad(asRaw => 'NUL') if $v =~ /\0/;
Branch: 1 branch
BranchTrueFalseTotal
if $v =~ /\0/ 1 19 20
686 # A line break is only allowed as a fold: CRLF then whitespace.
687 19 (my $t = $v) =~ s/\r?\n(?=[ \t])//g;
688 19 ▶ _bad(asRaw => 'line break that is not a fold') if $t =~ /[\r\n]/;
Branch: 1 branch
BranchTrueFalseTotal
if $t =~ /[\r\n]/ 1 18 19
689 18 return $v;
690 }
691  
692 9 my $ATEXT = qr{[A-Za-z0-9!#\$%&'*+/=?^_`{|}~-]};
693  
694 # RFC 2047 encoded-words for text that cannot go in a header as it is:
695 # non-ASCII, control characters (a line break would end the header), or
696 # something a reader would take for an encoded-word.
697 51 ▶sub _needs_encoding { $_[0] =~ /[^\x20-\x7e\t]|=\?/ }
Subroutine _needs_encoding: called CC=1 SCAR=0.0
698  
699 17 ▶sub _encode_words {
Subroutine _encode_words: called CC=1 SCAR=0.0
700 17 my $text = shift;
701 17 my $enc = Encode::encode('MIME-Q', $text);
702 17 $enc =~ s/\r?\n(?=[ \t])/\r\n/g;
703 17 return $enc;
704 }
705  
706 15 ▶sub _fmt_text {
Subroutine _fmt_text: called CC=3 SCAR=11.0
707 15 my $v = shift;
708 15 ▶ _bad(asText => 'not a string') if ref $v;
Branch: 1 branch
BranchTrueFalseTotal
if ref $v 1 14 15
709 14 $v =~ s/[\r\n]+/ /g; # a header field cannot hold a line break
710 14 ▶ return _needs_encoding($v) ? _encode_words($v) : $v;
Branch: 1 branch
BranchTrueFalseTotal
_needs_encoding($v) ? : 8 6 14
711 }
712  
713 # A phrase (display-name or group name): as-is if it is atoms, RFC 2047
714 # encoded-words if it needs them, else a quoted-string.
715 37 ▶sub _fmt_phrase {
Subroutine _fmt_phrase: called CC=3 SCAR=11.0
716 37 my $phrase = shift;
717 37 ▶ return _encode_words($phrase) if _needs_encoding($phrase);
Branch: 1 branch
BranchTrueFalseTotal
if _needs_encoding($phrase) 9 28 37
718 28 ▶ return $phrase if $phrase =~ m{\A$ATEXT+(?: $ATEXT+)*\z};
Branch: 1 branch
BranchTrueFalseTotal
if $phrase =~ /\A$ATEXT+(?: $ATEXT+)*\z/ 19 9 28
719 9 (my $q = $phrase) =~ s/(["\\])/\\$1/g;
720 9 return qq{"$q"};
721 }
722  
723 # An addr-spec. RFC 8621 S4.1.2.3 lets "email" lack the '@' (and S4.6
724 # lets a draft be invalid), so such a value is written as given; a local
725 # part that is not a dot-atom is quoted. RFC 6532 UTF-8 is kept.
726 57 ▶sub _fmt_addr_spec {
Subroutine _fmt_addr_spec: called CC=10 SCAR=23.1
727 57 my $email = shift;
728 57 ▶ _bad(asAddresses => 'email is not a string') if !defined $email || ref $email;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $email or ref $email 1 56 57
Condition66%2/3
not defined $email or ref $email
l!l&&r!l&&!r
1056
MC/DC50%1/2
not defined $email or ref $email
not defined $email ref $email
Truth table66%2/3
not(A) or B
A= not defined $emailB= ref $email
ABresult
1X1
011
000
729 56 $email =~ s/[\r\n]+/ /g; # nothing in it may end the header line
730 56 ▶ _bad(asAddresses => "email '$email' has control characters") if $email =~ /[\x00-\x1f\x7f]/;
Branch: 1 branch
BranchTrueFalseTotal
if $email =~ /[\x00-\x1f\x7f]/ 0 56 56
731 56 ▶ my ($local, $domain) = $email =~ /\A(.*)\@([^@]*)\z/s ? ($1, $2) : ($email, undef);
Branch: 1 branch
BranchTrueFalseTotal
$email =~ /\A(.*)\@([^\@]*)\z/s ? : 36 20 56
732 56 my $atom = qr{(?:$ATEXT|[^\x00-\x7f])+};
733 56 ▶ if ($local !~ /\A"(?:[^"\\]|\\.)*"\z/ && $local !~ /\A$atom(?:\.$atom)*\z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($local =~ /\A"(?:[^"\\]|\\.)*"\z/ or $local =~ /\A$atom(?:\.$atom)*\z/) 2 54 56
Condition100%3/3
$local =~ /\A"(?:[^"\\]|\\.)*"\z/ or $local =~ /\A$atom(?:\.$atom)*\z/
l!l&&r!l&&!r
1532
MC/DC100%2/2
$local =~ /\A"(?:[^"\\]|\\.)*"\z/ or $local =~ /\A$atom(?:\.$atom)*\z/
$local =~ /\A"(?:[^"\\]|\\.)*"\z/ $local =~ /\A$atom(?:\.$atom)*\z/
Truth table100%3/3
A or B
A= $local =~ /\A"(?:[^"\\]|\\.)*"\z/B= $local =~ /\A$atom(?:\.$atom)*\z/
ABresult
1X1
011
000
734 2 $local =~ s/(["\\])/\\$1/g;
735 2 $local = qq{"$local"};
736 }
737 56 ▶ if (defined $domain) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $domain) 36 20 56
738 36 ▶ _bad(asAddresses => "bad domain in '$email'")
Branch: 1 branch
BranchTrueFalseTotal
if not $domain =~ /\A\[[^\[\]\\]*\]\z/ and $domain =~ /[\s<>()\[\],;:\\"]/ 1 35 36
Condition66%2/3
not $domain =~ /\A\[[^\[\]\\]*\]\z/ and $domain =~ /[\s<>()\[\],;:\\"]/
!ll&&!rl&&r
0351
MC/DC50%1/2
not $domain =~ /\A\[[^\[\]\\]*\]\z/ and $domain =~ /[\s<>()\[\],;:\\"]/
not $domain =~ /\A\[[^\[\]\\]*\]\z/ $domain =~ /[\s<>()\[\],;:\\"]/
Truth table66%2/3
not(A) and B
A= not $domain =~ /\A\[[^\[\]\\]*\]\z/B= $domain =~ /[\s<>()\[\],;:\\"]/
ABresult
0X0
100
111
739 if $domain !~ /\A\[[^\[\]\\]*\]\z/ && $domain =~ /[\s<>()\[\],;:\\"]/;
740 35 return "$local\@$domain";
741 }
742 20 return $local;
743 }
744  
745 57 ▶sub _fmt_mailbox {
Subroutine _fmt_mailbox: called CC=5 SCAR=16.5
746 57 my $a = shift;
747 57 ▶ _bad(asAddresses => 'not an EmailAddress') unless ref $a eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $a eq "HASH" 0 57 57
748 57 my $spec = _fmt_addr_spec($a->{email});
749 55 my $name = $a->{name};
750 55 ▶ _bad(asAddresses => 'name is not a string') if ref $name;
Branch: 1 branch
BranchTrueFalseTotal
if ref $name 0 55 55
751 55 ▶ return (defined $name && $name ne '') ? _fmt_phrase($name) . " <$spec>" : $spec;
Branch: 1 branch
BranchTrueFalseTotal
defined $name && $name ne '' ? : 32 23 55
Condition66%2/3
defined $name and $name ne ""
!ll&&!rl&&r
23032
MC/DC50%1/2
defined $name and $name ne ""
defined $name $name ne ""
Truth table66%2/3
A and B
A= defined $nameB= $name ne ""
ABresult
0X0
100
111
752 }
753  
754 44 ▶sub _fmt_addresses {
Subroutine _fmt_addresses: called CC=2 SCAR=6.9
755 44 my $v = shift;
756 44 ▶ _bad(asAddresses => 'not a list') unless ref $v eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $v eq "ARRAY" 1 43 44
757 43 return join(', ', map { _fmt_mailbox($_) } @$v);
758 }
759  
760 5 ▶sub _fmt_grouped {
Subroutine _fmt_grouped: called CC=8 SCAR=20.9
761 5 my $v = shift;
762 5 ▶ _bad(asGroupedAddresses => 'not a list') unless ref $v eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $v eq "ARRAY" 0 5 5
763 5 my @out;
764 5 for my $g (@$v) {
765 _bad(asGroupedAddresses => 'not an EmailAddressGroup')
766 7 ▶ unless ref $g eq 'HASH' && ref $g->{addresses} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $g eq "HASH" and ref $g->{"addresses"} eq "ARRAY" 1 6 7
Condition66%2/3
ref $g eq "HASH" and ref $g->{"addresses"} eq "ARRAY"
!ll&&!rl&&r
016
MC/DC50%1/2
ref $g eq "HASH" and ref $g->{"addresses"} eq "ARRAY"
ref $g eq "HASH" ref $g->{"addresses"} eq "ARRAY"
Truth table66%2/3
A and B
A= ref $g eq "HASH"B= ref $g->{"addresses"} eq "ARRAY"
ABresult
0X0
100
111
767 6 my @mbox = map { _fmt_mailbox($_) } @{ $g->{addresses} };
768 6 ▶ if (defined $g->{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $g->{'name'}) { } 5 1 6
769 5 ▶ _bad(asGroupedAddresses => 'name is not a string') if ref $g->{name};
Branch: 1 branch
BranchTrueFalseTotal
if ref $g->{"name"} 0 5 5
770 5 ▶ push @out, _fmt_phrase($g->{name}) . ':' . (@mbox ? ' ' . join(', ', @mbox) : '') . ';';
Branch: 1 branch
BranchTrueFalseTotal
@mbox ? : 3 2 5
771 }
772 else {
773 1 push @out, @mbox;
774 }
775 }
776 4 return join(', ', @out);
777 }
778  
779 9 ▶sub _fmt_msgids {
Subroutine _fmt_msgids: called CC=8 SCAR=21.3
780 9 my $v = shift;
781 9 ▶ _bad(asMessageIds => 'not a list') unless ref $v eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $v eq "ARRAY" 1 8 9
782 8 ▶ return undef unless @$v; # no msg-id at all reads back as null
Branch: 1 branch
BranchTrueFalseTotal
unless @$v 1 7 8
783 7 for my $id (@$v) {
784 10 ▶ _bad(asMessageIds => 'not a string') if !defined $id || ref $id;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $id or ref $id 0 10 10
Condition33%1/3
not defined $id or ref $id
l!l&&r!l&&!r
0010
MC/DC0%0/2
not defined $id or ref $id
not defined $id ref $id
Truth table33%1/3
not(A) or B
A= not defined $idB= ref $id
ABresult
1X1
011
000
785 10 ▶ _bad(asMessageIds => "'$id' is not a msg-id") if $id eq '' || $id =~ /[\s<>\x00-\x1f\x7f]/;
Branch: 1 branch
BranchTrueFalseTotal
if $id eq "" or $id =~ /[\s<>\x00-\x1f\x7f]/ 2 8 10
Condition66%2/3
$id eq "" or $id =~ /[\s<>\x00-\x1f\x7f]/
l!l&&r!l&&!r
028
MC/DC50%1/2
$id eq "" or $id =~ /[\s<>\x00-\x1f\x7f]/
$id eq "" $id =~ /[\s<>\x00-\x1f\x7f]/
Truth table66%2/3
A or B
A= $id eq ""B= $id =~ /[\s<>\x00-\x1f\x7f]/
ABresult
1X1
011
000
786 }
787 5 return join(' ', map { "<$_>" } @$v);
788 }
789  
790 # RFC 3339 (a JMAP Date) in, RFC 5322 S3.3 date-time out, keeping the
791 # offset; asDate reads it back as the same instant and offset.
792 12 ▶sub _fmt_date {
Subroutine _fmt_date: called CC=5 SCAR=16.1
793 12 my $v = shift;
794 12 ▶ _bad(asDate => 'not a string') if ref $v;
Branch: 1 branch
BranchTrueFalseTotal
if ref $v 0 12 12
795 12 ▶ my ($y, $mo, $d, $h, $mi, $s, $tz) = $v =~
Branch: 1 branch
BranchTrueFalseTotal
() = $v =~ /\A(\d{4})-(\d\d)-(\d\d)[Tt](\d\d):(\d\d):(\d\d)(?:\.\d+)?([Zz]|[-+]\d\d:\d\d)\z/ or _bad("asDate", "'${v}' is not an RFC 3339 date-time") 9 3 12
796 /\A(\d{4})-(\d\d)-(\d\d)[Tt](\d\d):(\d\d):(\d\d)(?:\.\d+)?([Zz]|[-+]\d\d:\d\d)\z/
797 or _bad(asDate => "'$v' is not an RFC 3339 date-time");
798 9 ▶ $tz = $tz =~ /^[Zz]$/ ? '+0000' : $tz =~ s/://r;
Branch: 1 branch
BranchTrueFalseTotal
$tz =~ /^[Zz]$/ ? : 5 4 9
799 9 ▶ my $dt = eval {
Branch: 1 branch
BranchTrueFalseTotal
my $dt = eval { do { "DateTime"->new("year", $y, "month", $mo, "day", $d, "hour", $h, "minute", $mi, "second", $s, "time_zone", $tz) } } or _bad("asDate", "'${v}' is not a valid date-time") 8 1 9
800 9 DateTime->new(year => $y, month => $mo, day => $d, hour => $h,
801 minute => $mi, second => $s, time_zone => $tz);
802 } or _bad(asDate => "'$v' is not a valid date-time");
803 8 return $dt->strftime('%a, %d %b %Y %H:%M:%S %z');
804 }
805  
806 4 ▶sub _fmt_urls {
Subroutine _fmt_urls: called CC=8 SCAR=21.7
807 4 my $v = shift;
808 4 ▶ _bad(asURLs => 'not a list') unless ref $v eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $v eq "ARRAY" 0 4 4
809 4 ▶ return undef unless @$v; # no URL at all reads back as null
Branch: 1 branch
BranchTrueFalseTotal
unless @$v 1 3 4
810 3 for my $u (@$v) {
811 5 ▶ _bad(asURLs => 'not a string') if !defined $u || ref $u;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $u or ref $u 0 5 5
Condition33%1/3
not defined $u or ref $u
l!l&&r!l&&!r
005
MC/DC0%0/2
not defined $u or ref $u
not defined $u ref $u
Truth table33%1/3
not(A) or B
A= not defined $uB= ref $u
ABresult
1X1
011
000
812 5 ▶ _bad(asURLs => "'$u' is not a URL") if $u eq '' || $u =~ /[\s<>\x00-\x1f\x7f]/;
Branch: 1 branch
BranchTrueFalseTotal
if $u eq "" or $u =~ /[\s<>\x00-\x1f\x7f]/ 1 4 5
Condition66%2/3
$u eq "" or $u =~ /[\s<>\x00-\x1f\x7f]/
l!l&&r!l&&!r
014
MC/DC50%1/2
$u eq "" or $u =~ /[\s<>\x00-\x1f\x7f]/
$u eq "" $u =~ /[\s<>\x00-\x1f\x7f]/
Truth table66%2/3
A or B
A= $u eq ""B= $u =~ /[\s<>\x00-\x1f\x7f]/
ABresult
1X1
011
000
813 }
814 2 return join(', ', map { "<$_>" } @$v);
815 }
816  
817 # The header fields given by header:{name}[:as{form}][:all] properties
818 # on an Email or EmailBodyPart, as [Name, [Raw values]] in a stable
819 # order. Dies on a property that is not valid for create.
820 134 ▶sub _header_props {
Subroutine _header_props: called CC=9 SCAR=22.0
821 134 my $obj = shift;
822 134 my @res;
823 134 for my $key (sort grep { /^header:/ } keys %$obj) {
824 29 ▶ my ($name, $form, $all) = parse_header_property($key)
Branch: 1 branch
BranchTrueFalseTotal
() = parse_header_property($key) or die "Invalid header property '${key}'\n" 28 1 29
825 or die "Invalid header property '$key'\n";
826 28 ▶ die "$form is not allowed for header $name\n" unless header_form_allowed($name, $form);
Branch: 1 branch
BranchTrueFalseTotal
unless header_form_allowed($name, $form) 2 26 28
827 26 my $val = $obj->{$key};
828 26 ▶ next unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 26 26
829 26 my @vals;
830 26 ▶ if ($all) {
Branch: 2 branches
BranchTrueFalseTotal
if ($all) { } 4 22 26
elsif ($form eq 'asRaw' and ref $val eq 'ARRAY') { } 2 20 22
831 4 ▶ die "$key must be a list\n" unless ref $val eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $val eq "ARRAY" 1 3 4
832 3 @vals = @$val;
833 }
834 ▶ elsif ($form eq 'asRaw' && ref $val eq 'ARRAY') {
Condition100%3/3
$form eq "asRaw" and ref $val eq "ARRAY"
!ll&&!rl&&r
10102
MC/DC100%2/2
$form eq "asRaw" and ref $val eq "ARRAY"
$form eq "asRaw" ref $val eq "ARRAY"
Truth table100%3/3
A and B
A= $form eq "asRaw"B= ref $val eq "ARRAY"
ABresult
0X0
100
111
835 2 @vals = @$val; # a list of Raw values: one header field each
836 }
837 else {
838 20 @vals = ($val);
839 }
840 25 push @res, [ $name, [ grep { defined } map { format_header($form, $_) } @vals ] ];
841 }
842 130 return @res;
843 }
844  
845 # Write a Raw value (format_header's output, or one given) as is: the
846 # writer puts a space after the colon itself, so one leading space is
847 # dropped, and the value is written as UTF-8 octets.
848 116 ▶sub _raw_for_write {
Subroutine _raw_for_write: called CC=2 SCAR=6.9
849 116 my $v = shift;
850 116 $v =~ s/\A //;
851 116 ▶ $v = Encode::encode('UTF-8', $v) if Encode::is_utf8($v);
Branch: 1 branch
BranchTrueFalseTotal
if &Encode::is_utf8($v) 9 107 116
852 116 return $v;
853 }
854  
855 111 ▶sub _set_raw_header {
Subroutine _set_raw_header: called CC=1 SCAR=0.0
856 111 my ($mime, $name, $vals) = @_;
857 111 $mime->header_raw_set($name => map { _raw_for_write($_) } @$vals);
858 }
859  
860 # The Content-Transfer-Encoding for a leaf's octets. make() ends by
861 # rewriting every line ending to CRLF, so whatever is written raw must
862 # survive that: text is 7bit, or quoted-printable when it has 8-bit or
863 # control octets or a line too long for SMTP; message/* is 7bit or 8bit
864 # (RFC 2046 S5.2.1 forbids anything else); everything else is base64,
865 # so binary octets never meet the rewrite.
866 71 ▶sub _cte {
Subroutine _cte: called CC=6 SCAR=17.9
867 71 my ($octets, $type) = @_;
868 71 ▶ my $plain = $octets !~ /[^\x09\x0a\x0d\x20-\x7e]/ && $octets !~ /[^\n]{999}/;
Condition100%3/3
$octets =~ /[^\x09\x0a\x0d\x20-\x7e]/ || $octets =~ /[^\n]{999}/
l!l&&r!l&&!r
20150
MC/DC100%2/2
$octets =~ /[^\x09\x0a\x0d\x20-\x7e]/ || $octets =~ /[^\n]{999}/
$octets =~ /[^\x09\x0a\x0d\x20-\x7e]/ $octets =~ /[^\n]{999}/
Truth table100%3/3
A || B
A= $octets =~ /[^\x09\x0a\x0d\x20-\x7e]/B= $octets =~ /[^\n]{999}/
ABresult
1X1
011
000
869 71 ▶ return $plain ? '7bit' : '8bit' if $type =~ m{^message/}i;
Branch: 2 branches
BranchTrueFalseTotal
if $type =~ m[^message/]i 1 70 71
$plain ? : 0 1 1
870 70 ▶ return $plain ? '7bit' : 'quoted-printable' if $type =~ m{^text/}i;
Branch: 2 branches
BranchTrueFalseTotal
if $type =~ m[^text/]i 61 9 70
$plain ? : 48 13 61
871 9 return 'base64';
872 }
873  
874 2 ▶sub _valid_cid {
Subroutine _valid_cid: called CC=5 SCAR=17.2
875 2 my $cid = shift;
876 2 ▶ return 1 unless defined $cid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cid 0 2 2
877 # Content-ID msg-id: no angle brackets, no whitespace, no NUL
878 2 ▶ die "Invalid cid: must not contain < or >\n" if $cid =~ /[<>]/;
Branch: 1 branch
BranchTrueFalseTotal
if $cid =~ /[<>]/ 0 2 2
879 2 ▶ die "Invalid cid: must not contain whitespace\n" if $cid =~ /\s/;
Branch: 1 branch
BranchTrueFalseTotal
if $cid =~ /\s/ 0 2 2
880 2 ▶ die "Invalid cid: must not be empty\n" if $cid eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $cid eq "" 0 2 2
881 2 return 1;
882 }
883  
884 # One leaf EmailBodyPart as a MIME part: the only place make() encodes
885 # body content, used for bodyStructure leaves and for the textBody,
886 # htmlBody and attachments shorthand alike.
887 #
888 # The content is the part's bodyValues value (characters, written as
889 # UTF-8) if it has a partId there, else its blob (octets, written as they
890 # are), else empty. $opt{type} and $opt{disposition} are the defaults
891 # for a part that gives none; $opt{headers} (default true) applies the
892 # part's own header:* properties to it.
893 71 ▶sub _leaf_part {
Subroutine _leaf_part: called CC=28 SCAR=34.2
894 71 my ($node, $bodyValues, $getblob, %opt) = @_;
895 71 ▶ my $type = $node->{type} || $opt{type} || 'application/octet-stream';
Condition40%2/5
$node->{'type'} || $opt{'type'} || 'application/octet-stream'
l!l
710
$node->{"type"} or $opt{"type"}
l!l&&r!l&&!r
7100
MC/DC0%0/2
$node->{'type'} || $opt{'type'} || 'application/octet-stream'
$node->{"type"} $opt{"type"}
Truth table33%1/3
A or B
A= $node->{"type"}B= $opt{"type"}
ABresult
1X1
011
000
896 71 my $is_text = $type =~ m{^text/}i;
897  
898 71 my ($octets, $charset);
899 71 my $partId = $node->{partId};
900 71 ▶ if (defined $partId && $bodyValues->{$partId}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $partId and $bodyValues->{$partId}) { } 55 16 71
elsif ($node->{'blobId'} and $getblob) { } 16 0 16
Condition66%2/3
defined $partId and $bodyValues->{$partId}
!ll&&!rl&&r
16055
MC/DC50%1/2
defined $partId and $bodyValues->{$partId}
defined $partId $bodyValues->{$partId}
Truth table66%2/3
A and B
A= defined $partIdB= $bodyValues->{$partId}
ABresult
0X0
100
111
901 55 ▶ my $value = $bodyValues->{$partId}{value} // '';
Condition50%1/2
$bodyValues->{$partId}{"value"} // ""
l!l
550
MC/DC0%0/1
$bodyValues->{$partId}{"value"} // ""
$bodyValues->{$partId}{"value"}
Truth table50%1/2
A
A= $bodyValues->{$partId}{"value"}
Aresult
11
00
902 55 $octets = Encode::encode('UTF-8', $value);
903 55 ▶ $charset = $value =~ /[^\x00-\x7f]/ ? 'UTF-8' : 'us-ascii';
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /[^\x00-\x7f]/ ? : 8 47 55
904 }
905 ▶ elsif ($node->{blobId} && $getblob) {
Condition33%1/3
$node->{"blobId"} and $getblob
!ll&&!rl&&r
0016
MC/DC0%0/2
$node->{"blobId"} and $getblob
$node->{"blobId"} $getblob
Truth table33%1/3
A and B
A= $node->{"blobId"}B= $getblob
ABresult
0X0
100
111
906 16 my $btype;
907 16 ($btype, $octets) = $getblob->($node->{blobId});
908 16 ▶ $octets //= '';
Condition50%1/2
$octets //= ""
l!l
160
MC/DC0%0/1
$octets //= ""
$octets
Truth table50%1/2
A
A= $octets
Aresult
11
00
909 # Octets already; a wide character means the getblob gave text.
910 16 ▶ $octets = Encode::encode('UTF-8', $octets) if $octets =~ /[^\x00-\xff]/;
Branch: 1 branch
BranchTrueFalseTotal
if $octets =~ /[^\x00-\xff]/ 0 16 16
911 16 ▶ if ($is_text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_text) 7 9 16
912 # S4.6: with a blobId, charset says what the octets are. Else the
913 # blob's own type may, else they are ASCII or UTF-8, or unknown.
914 7 ▶ ($charset) = ($btype // '') =~ /;\s*charset\s*=\s*"?([^";\s]+)/i;
Condition50%1/2
$btype // ""
l!l
70
MC/DC0%0/1
$btype // ""
$btype
Truth table50%1/2
A
A= $btype
Aresult
11
00
915 7 ▶ $charset = $node->{charset} if $node->{charset};
Branch: 1 branch
BranchTrueFalseTotal
if $node->{"charset"} 1 6 7
916 7 ▶ $charset //= $octets !~ /[^\x00-\x7f]/ ? 'us-ascii'
Branch: 2 branches
BranchTrueFalseTotal
!($octets =~ /[^\x00-\x7f]/) ? : 2 3 5
_is_utf8_octets($octets) ? : 2 1 3
Condition100%2/2
$charset //= !($octets =~ /[^\x00-\x7f]/) ? "us-ascii" : (_is_utf8_octets($octets) ? "UTF-8" : undef)
l!l
25
MC/DC100%1/1
$charset //= !($octets =~ /[^\x00-\x7f]/) ? "us-ascii" : (_is_utf8_octets($octets) ? "UTF-8" : undef)
$charset
Truth table100%2/2
A
A= $charset
Aresult
11
00
917 : _is_utf8_octets($octets) ? 'UTF-8' : undef;
918 }
919 }
920 else {
921 0 $octets = '';
922 0 $charset = 'us-ascii';
923 }
924 # Text is written in canonical form, CRLF line ends (RFC 2046 S4.1.1):
925 # the final rewrite makes every line end CRLF.
926 71 ▶ $octets =~ s/\r\n/\n/g if $is_text;
Branch: 1 branch
BranchTrueFalseTotal
if $is_text 61 10 71
927  
928 71 my %attrs = (content_type => $type, encoding => _cte($octets, $type));
929 71 ▶ $attrs{charset} = $charset if $is_text && defined $charset;
Branch: 1 branch
BranchTrueFalseTotal
if $is_text and defined $charset 60 11 71
Condition100%3/3
$is_text and defined $charset
!ll&&!rl&&r
10160
MC/DC100%2/2
$is_text and defined $charset
$is_text defined $charset
Truth table100%3/3
A and B
A= $is_textB= defined $charset
ABresult
0X0
100
111
930 71 ▶ my $disp = $node->{disposition} || $opt{disposition};
Condition100%3/3
$node->{'disposition'} || $opt{'disposition'}
l!l&&r!l&&!r
41057
MC/DC100%2/2
$node->{'disposition'} || $opt{'disposition'}
$node->{'disposition'} $opt{'disposition'}
Truth table100%3/3
A || B
A= $node->{'disposition'}B= $opt{'disposition'}
ABresult
1X1
011
000
931 71 ▶ $attrs{disposition} = $disp if $disp;
Branch: 1 branch
BranchTrueFalseTotal
if $disp 14 57 71
932 71 ▶ $attrs{filename} = $attrs{name} = $node->{name} if $node->{name};
Branch: 1 branch
BranchTrueFalseTotal
if $node->{"name"} 11 60 71
933  
934 71 my $mime = Email::MIME->create(attributes => \%attrs, body => $octets);
935  
936 71 ▶ if (defined $node->{cid}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $node->{"cid"}) 2 69 71
937 2 _valid_cid($node->{cid});
938 2 $mime->header_str_set('Content-ID' => "<$node->{cid}>");
939 }
940 71 ▶ if ($node->{language} && ref($node->{language}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"language"} and ref $node->{"language"} eq "ARRAY") 2 69 71
Condition66%2/3
$node->{"language"} and ref $node->{"language"} eq "ARRAY"
!ll&&!rl&&r
6902
MC/DC50%1/2
$node->{"language"} and ref $node->{"language"} eq "ARRAY"
$node->{"language"} ref $node->{"language"} eq "ARRAY"
Truth table66%2/3
A and B
A= $node->{"language"}B= ref $node->{"language"} eq "ARRAY"
ABresult
0X0
100
111
941 2 $mime->header_str_set('Content-Language' => join(', ', @{$node->{language}}));
942 }
943 71 ▶ _apply_bodystructure_headers($mime, $node) if $opt{headers} // 1;
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"headers"} // 1 21 50 71
Condition100%2/2
$opt{"headers"} // 1
l!l
5021
MC/DC100%1/1
$opt{"headers"} // 1
$opt{"headers"}
Truth table100%2/2
A
A= $opt{"headers"}
Aresult
11
00
944 71 return $mime;
945 }
946  
947 3 ▶sub _is_utf8_octets {
Subroutine _is_utf8_octets: called CC=1 SCAR=0.0
948 3 my $copy = shift;
949 3 return defined eval { Encode::decode('UTF-8', $copy, Encode::FB_CROAK) };
950 }
951  
952 9 my $_uuid_gen;
953 46 ▶sub default_header_defaults {
Subroutine default_header_defaults: called CC=4 SCAR=14.1
Pod undocumented
954 46 my ($name) = @_;
955 46 ▶ if (lc($name) eq 'date') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $name eq "date") 23 23 46
956 23 return Date::Format::time2str("%a, %d %b %Y %H:%M:%S %z", time());
957 }
958 23 ▶ if (lc($name) eq 'message-id') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $name eq "message-id") 23 0 23
959 23 ▶ $_uuid_gen ||= Data::UUID->new;
Condition66%2/3
$_uuid_gen ||= "Data::UUID"->new
l!l&&r!l&&!r
2210
MC/DC0%0/2
$_uuid_gen ||= "Data::UUID"->new
$_uuid_gen "Data::UUID"->new
Truth table66%2/3
A ||= B
A= $_uuid_genB= "Data::UUID"->new
ABresult
1X1
011
000
960 23 my $uuid = $_uuid_gen->to_string($_uuid_gen->create);
961 23 my $host = hostname();
962 23 return "<$uuid\@$host>";
963 }
964 0 return undef;
965 }
966  
967 13 ▶sub _build_bodystructure {
Subroutine _build_bodystructure: called CC=5 SCAR=16.2
968 13 my ($node, $bodyValues, $getblob) = @_;
969  
970 13 ▶ my $type = $node->{type} || 'text/plain';
Condition50%1/2
$node->{'type'} || 'text/plain'
l!l
130
MC/DC0%0/1
$node->{'type'} || 'text/plain'
$node->{'type'}
Truth table50%1/2
A
A= $node->{'type'}
Aresult
11
00
971  
972 13 ▶ if ($type =~ m{^multipart/}i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ m[^multipart/]i) 4 9 13
973 # Multipart: recursively build subParts
974 4 my @parts;
975 4 ▶ for my $sub (@{$node->{subParts} || []}) {
Condition50%1/2
$node->{'subParts'} || []
l!l
40
MC/DC0%0/1
$node->{'subParts'} || []
$node->{'subParts'}
Truth table50%1/2
A
A= $node->{'subParts'}
Aresult
11
00
976 8 push @parts, _build_bodystructure($sub, $bodyValues, $getblob);
977 }
978 4 my $mime = Email::MIME->create(
979 attributes => { content_type => $type },
980 parts => \@parts,
981 );
982 # Apply header:* from this node
983 4 _apply_bodystructure_headers($mime, $node);
984 4 return $mime;
985 }
986  
987 9 return _leaf_part($node, $bodyValues, $getblob, type => 'text/plain');
988 }
989  
990 25 ▶sub _apply_bodystructure_headers {
Subroutine _apply_bodystructure_headers: called CC=3 SCAR=11.1
991 25 my ($mime, $node) = @_;
992 25 for my $h (_header_props($node)) {
993 2 my ($hname, $vals) = @$h;
994 # Skip Content-* headers that are managed by Email::MIME
995 2 ▶ next if $hname =~ /^Content-/i;
Branch: 1 branch
BranchTrueFalseTotal
if $hname =~ /^Content-/i 0 2 2
996 2 _set_raw_header($mime, $hname, $vals);
997 }
998 }
999  
1000 58 ▶sub make { _with_settings(\&_make, @_) }
Subroutine make: called CC=1 SCAR=0.0
Pod documented
1001  
1002 58 ▶sub _make {
Subroutine _make: called CC=42 SCAR=40.8
1003 58 my $args = shift;
1004 58 my $getblob = shift;
1005 58 ▶ my $defaults_cb = shift || \&default_header_defaults;
Condition100%2/2
shift() || \&Data::JSEmail::default_header_defaults
l!l
3226
MC/DC100%1/1
shift() || \&Data::JSEmail::default_header_defaults
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1006  
1007 # RFC 8621 Email/set create format:
1008 # from, to, cc, bcc, replyTo, sender: EmailAddress[]
1009 # subject: String
1010 # sentAt: Date (optional)
1011 # messageId, inReplyTo, references: String[]
1012 # textBody: EmailBodyPart[] (each with partId referencing bodyValues)
1013 # htmlBody: EmailBodyPart[] (each with partId referencing bodyValues)
1014 # bodyValues: Id[EmailBodyValue] (partId => {value: String})
1015 # attachments: EmailBodyPart[] (each with blobId)
1016  
1017 # Follow RFC 8621 / Cyrus precedence:
1018 # 1. header:* properties (already converted from typed forms by caller)
1019 # 2. Convenience properties (only if header:* didn't set that header)
1020 # 3. Defaults (only if neither header:* nor convenience set the header)
1021  
1022 # Every header field is written through format_header, the inverse
1023 # of the header forms parse() reads, as [Name, [Raw values]].
1024 58 my @header = _header_props($args);
1025 54 my %have = map { lc($_->[0]) => 1 } @header;
1026  
1027 # Step 2: Convenience properties (skip if header:* already provides it)
1028 54 my @convenience = (
1029 [ from => From => 'asAddresses' ],
1030 [ to => To => 'asAddresses' ],
1031 [ cc => Cc => 'asAddresses' ],
1032 [ bcc => Bcc => 'asAddresses' ],
1033 [ subject => Subject => 'asText' ],
1034 [ sentAt => Date => 'asDate' ],
1035 [ messageId => 'Message-ID' => 'asMessageIds' ],
1036 [ inReplyTo => 'In-Reply-To' => 'asMessageIds' ],
1037 [ references => References => 'asMessageIds' ],
1038 [ replyTo => 'Reply-To' => 'asAddresses' ],
1039 [ sender => Sender => 'asAddresses' ],
1040 );
1041 54 for my $c (@convenience) {
1042 589 my ($prop, $name, $form) = @$c;
1043 589 ▶ next if $have{lc $name};
Branch: 1 branch
BranchTrueFalseTotal
if $have{lc $name} 10 579 589
1044 579 my $val = format_header($form, $args->{$prop});
1045 578 ▶ next unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 538 40 578
1046 40 push @header, [ $name, [ $val ] ];
1047 40 $have{lc $name} = 1;
1048 }
1049 53 ▶ if (!$have{date} && $args->{msgdate}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $have{"date"} and $args->{"msgdate"}) 0 53 53
Condition66%2/3
not $have{"date"} and $args->{"msgdate"}
!ll&&!rl&&r
4490
MC/DC0%0/2
not $have{"date"} and $args->{"msgdate"}
not $have{"date"} $args->{"msgdate"}
Truth table66%2/3
not(A) and B
A= not $have{"date"}B= $args->{"msgdate"}
ABresult
0X0
100
111
1050 0 push @header, [ Date => [ Date::Format::time2str("%a, %d %b %Y %H:%M:%S %z", $args->{msgdate}) ] ];
1051 0 $have{date} = 1;
1052 }
1053  
1054 # header:* on the textBody/htmlBody part go on the message, unless the
1055 # message already has that field.
1056 53 for my $partlist ($args->{textBody}, $args->{htmlBody}) {
1057 106 ▶ next unless $partlist && ref($partlist) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless $partlist and ref $partlist eq "ARRAY" 56 50 106
Condition66%2/3
$partlist and ref $partlist eq "ARRAY"
!ll&&!rl&&r
56050
MC/DC50%1/2
$partlist and ref $partlist eq "ARRAY"
$partlist ref $partlist eq "ARRAY"
Truth table66%2/3
A and B
A= $partlistB= ref $partlist eq "ARRAY"
ABresult
0X0
100
111
1058 50 for my $part (@$partlist) {
1059 51 for my $h (_header_props($part)) {
1060 0 ▶ next if $have{lc $h->[0]};
Branch: 1 branch
BranchTrueFalseTotal
if $have{lc $h->[0]} 0 0 0
1061 0 push @header, $h;
1062 0 $have{lc $h->[0]} = 1;
1063 }
1064 }
1065 }
1066  
1067 # Step 3: Defaults via callback (only if nothing set them)
1068 53 for my $name (qw(Date Message-ID)) {
1069 106 ▶ next if $have{lc $name};
Branch: 1 branch
BranchTrueFalseTotal
if $have{lc $name} 7 99 106
1070 99 my $val = $defaults_cb->($name);
1071 99 ▶ push @header, [ $name, [ $val ] ] if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 46 53 99
1072 }
1073  
1074 53 ▶ my $bodyValues = $args->{bodyValues} || {};
Condition100%2/2
$args->{'bodyValues'} || {}
l!l
503
MC/DC100%1/1
$args->{'bodyValues'} || {}
$args->{'bodyValues'}
Truth table100%2/2
A
A= $args->{'bodyValues'}
Aresult
11
00
1075  
1076 # bodyStructure mode: build MIME tree from bodyStructure recursively
1077 53 ▶ if ($args->{bodyStructure}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"bodyStructure"}) 5 48 53
1078 5 my $MIME = _build_bodystructure($args->{bodyStructure}, $bodyValues, $getblob);
1079  
1080 # Apply message-level headers to the top-level MIME part
1081 5 _set_raw_header($MIME, @$_) for @header;
1082  
1083 5 my $res = $MIME->as_string();
1084 5 $res =~ s/\r?\n/\r\n/gs;
1085 5 return $res;
1086 }
1087  
1088 # textBody/htmlBody: the first part with content (a partId in
1089 # bodyValues, or a blobId). Its header:* properties went on the
1090 # message above.
1091 96 ▶ my $first_with_content = sub {
Subroutine __ANON__: called CC=8 SCAR=22.5
1092 96 my $list = shift;
1093 96 ▶ return undef unless $list && ref($list) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless $list and ref $list eq "ARRAY" 46 50 96
Condition66%2/3
$list and ref $list eq "ARRAY"
!ll&&!rl&&r
46050
MC/DC50%1/2
$list and ref $list eq "ARRAY"
$list ref $list eq "ARRAY"
Truth table66%2/3
A and B
A= $listB= ref $list eq "ARRAY"
ABresult
0X0
100
111
1094 50 for my $part (@$list) {
1095 50 ▶ return $part if defined $part->{partId} && $bodyValues->{$part->{partId}};
Branch: 1 branch
BranchTrueFalseTotal
if defined $part->{"partId"} and $$bodyValues{$part->{"partId"}} 49 1 50
Condition66%2/3
defined $part->{"partId"} and $$bodyValues{$part->{"partId"}}
!ll&&!rl&&r
1049
MC/DC50%1/2
defined $part->{"partId"} and $$bodyValues{$part->{"partId"}}
defined $part->{"partId"} $$bodyValues{$part->{"partId"}}
Truth table66%2/3
A and B
A= defined $part->{"partId"}B= $$bodyValues{$part->{"partId"}}
ABresult
0X0
100
111
1096 1 ▶ return $part if $part->{blobId} && $getblob;
Branch: 1 branch
BranchTrueFalseTotal
if $part->{"blobId"} and $getblob 1 0 1
Condition33%1/3
$part->{"blobId"} and $getblob
!ll&&!rl&&r
001
MC/DC0%0/2
$part->{"blobId"} and $getblob
$part->{"blobId"} $getblob
Truth table33%1/3
A and B
A= $part->{"blobId"}B= $getblob
ABresult
0X0
100
111
1097 }
1098 0 return undef;
1099 48 };
1100  
1101 # Note: do NOT auto-generate text/plain from HTML. Per RFC 8621,
1102 # if only htmlBody is provided, the server creates a single text/html
1103 # part that appears in both textBody and htmlBody of the response.
1104 # Generating a multipart/alternative would change the structure.
1105  
1106 48 my $MIME;
1107 48 my ($textpart, $htmlpart);
1108 48 ▶ if (my $part = $first_with_content->($args->{textBody})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $part = &$first_with_content($args->{"textBody"})) 47 1 48
1109 47 $textpart = _leaf_part({ %$part, type => 'text/plain' }, $bodyValues, $getblob, headers => 0);
1110 }
1111 48 ▶ if (my $part = $first_with_content->($args->{htmlBody})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $part = &$first_with_content($args->{"htmlBody"})) 3 45 48
1112 3 $htmlpart = _leaf_part({ %$part, type => 'text/html' }, $bodyValues, $getblob, headers => 0);
1113 }
1114  
1115 48 my @attparts;
1116 48 ▶ if ($args->{attachments} && ref($args->{attachments}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"attachments"} and ref $args->{"attachments"} eq "ARRAY") 6 42 48
Condition66%2/3
$args->{"attachments"} and ref $args->{"attachments"} eq "ARRAY"
!ll&&!rl&&r
4206
MC/DC50%1/2
$args->{"attachments"} and ref $args->{"attachments"} eq "ARRAY"
$args->{"attachments"} ref $args->{"attachments"} eq "ARRAY"
Truth table66%2/3
A and B
A= $args->{"attachments"}B= ref $args->{"attachments"} eq "ARRAY"
ABresult
0X0
100
111
1117 6 for my $att (@{$args->{attachments}}) {
1118 next unless (defined $att->{partId} && $bodyValues->{$att->{partId}})
1119 12 ▶ || ($att->{blobId} && $getblob);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $att->{"partId"} and $$bodyValues{$att->{"partId"}} or $att->{"blobId"} and $getblob 0 12 12
Condition55%5/9
defined $att->{"partId"} and $$bodyValues{$att->{"partId"}} or $att->{"blobId"} and $getblob
l!l&&r!l&&!r
2100
defined $att->{"partId"} and $$bodyValues{$att->{"partId"}}
!ll&&!rl&&r
1002
$att->{"blobId"} and $getblob
!ll&&!rl&&r
0010
MC/DC0%0/4
defined $att->{"partId"} and $$bodyValues{$att->{"partId"}} or $att->{"blobId"} and $getblob
defined $att->{"partId"} $$bodyValues{$att->{"partId"}} $att->{"blobId"} $getblob
Truth table28%2/7
A and B or C and D
A= defined $att->{"partId"}B= $$bodyValues{$att->{"partId"}}C= $att->{"blobId"}D= $getblob
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
1120 push @attparts, _leaf_part($att, $bodyValues, $getblob,
1121 12 ▶ disposition => ($att->{isInline} ? 'inline' : 'attachment'));
Branch: 1 branch
BranchTrueFalseTotal
$att->{'isInline'} ? : 0 12 12
1122 }
1123 }
1124  
1125 # Assemble the MIME structure
1126 48 ▶ if (@attparts) {
Branch: 4 branches
BranchTrueFalseTotal
if (@attparts) { } 6 42 48
elsif ($textpart and $htmlpart) { } 0 42 42
elsif ($textpart) { } 41 1 42
elsif ($htmlpart) { } 1 0 1
1127 6 my $body;
1128 6 ▶ if ($textpart && $htmlpart) {
Branch: 1 branch
BranchTrueFalseTotal
if ($textpart and $htmlpart) { } 2 4 6
Condition66%2/3
$textpart and $htmlpart
!ll&&!rl&&r
042
MC/DC50%1/2
$textpart and $htmlpart
$textpart $htmlpart
Truth table66%2/3
A and B
A= $textpartB= $htmlpart
ABresult
0X0
100
111
1129 2 $body = Email::MIME->create(
1130 attributes => { content_type => 'multipart/alternative' },
1131 parts => [$textpart, $htmlpart],
1132 );
1133 }
1134 else {
1135 4 ▶ $body = $textpart || $htmlpart;
Condition33%1/3
$textpart || $htmlpart
l!l&&r!l&&!r
400
MC/DC0%0/2
$textpart || $htmlpart
$textpart $htmlpart
Truth table33%1/3
A || B
A= $textpartB= $htmlpart
ABresult
1X1
011
000
1136 }
1137 6 $MIME = Email::MIME->create(
1138 attributes => { content_type => 'multipart/mixed' },
1139 parts => [$body, @attparts],
1140 );
1141 6 _set_raw_header($MIME, @$_) for @header;
1142 }
1143 ▶ elsif ($textpart && $htmlpart) {
Condition66%2/3
$textpart and $htmlpart
!ll&&!rl&&r
1410
MC/DC0%0/2
$textpart and $htmlpart
$textpart $htmlpart
Truth table66%2/3
A and B
A= $textpartB= $htmlpart
ABresult
0X0
100
111
1144 0 $MIME = Email::MIME->create(
1145 attributes => { content_type => 'multipart/alternative' },
1146 parts => [$textpart, $htmlpart],
1147 );
1148 0 _set_raw_header($MIME, @$_) for @header;
1149 }
1150 elsif ($textpart) {
1151 # Add message headers to the existing part (preserves Content-ID etc.)
1152 41 _set_raw_header($textpart, @$_) for @header;
1153 41 $MIME = $textpart;
1154 }
1155 elsif ($htmlpart) {
1156 1 _set_raw_header($htmlpart, @$_) for @header;
1157 1 $MIME = $htmlpart;
1158 }
1159 else {
1160 0 $MIME = Email::MIME->create(
1161 attributes => { content_type => 'text/plain', charset => 'us-ascii' },
1162 body => '',
1163 );
1164 0 _set_raw_header($MIME, @$_) for @header;
1165 }
1166  
1167 48 my $res = $MIME->as_string();
1168 48 $res =~ s/\r?\n/\r\n/gs;
1169  
1170 48 return $res;
1171 }
1172  
1173 # Email::MIME, made to degrade one part instead of the whole message.
1174 # Email::MIME splits every multipart while it is constructed, and dies
1175 # (attempting the whole parse) when a part is nested deeper than
1176 # $Email::MIME::MAX_DEPTH or the message has more than $MAX_PARTS parts.
1177 # Here the part where that happens is kept whole as a single part, with
1178 # its body intact, and every other part parses as before. Subparts are
1179 # made with ref($self)->_new, so the whole tree is of this class.
1180 package Data::JSEmail::MIMEPart;
1181 9 our @ISA = ('Email::MIME');
1182  
1183 107 ▶sub parts_multipart {
Subroutine parts_multipart: called CC=2 SCAR=6.9
1184 107 my $self = shift;
1185 107 my $body = $self->Email::Simple::body;
1186 107 my @parts = eval { $self->SUPER::parts_multipart(@_) };
1187 107 ▶ return @parts unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 105 2 107
1188 2 $self->Email::Simple::body_set($body);
1189 2 delete $self->{body_raw};
1190 2 return $self->parts_single_part;
1191 }
1192  
1193 package Data::JSEmail;
1194  
1195 0 ▶sub isodate {
Subroutine isodate: not called CC=2 SCAR=17.9
Pod undocumented
1196 0 ▶ my $epoch = shift || time();
Condition0%0/2
shift() || time
l!l
00
MC/DC0%0/1
shift() || time
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1197 0 my $date = DateTime->from_epoch( epoch => $epoch );
1198 0 return $date->iso8601() . 'Z';
1199 }
1200  
1201 0 ▶sub parse_date {
Subroutine parse_date: not called CC=1 SCAR=6.9
Pod undocumented
1202 0 my $date = shift;
1203 0 return str2time($date);
1204 }
1205  
1206 12 ▶sub htmltotext {
Subroutine htmltotext: called CC=1 SCAR=0.0
Pod undocumented
1207 12 my $html = shift;
1208 12 my $hs = HTML::Strip->new();
1209 12 my $clean_text = $hs->parse( $html );
1210 12 $hs->eof;
1211 12 return $clean_text;
1212 }
1213  
1214 9 1;
1215  
1216 __END__
↑ Summary