Courriel.pm

filter: Statementstmt 70.5% 153 / 217 Branchbran 48.1% 26 / 54 Conditioncond 59.2% 16 / 27 MC/DCmcdc 31.2% 5 / 16 Subroutinesub 66.6% 28 / 42 Podpod 85.7% 6 / 7 totaltotal 64.4% 234 / 363 CC 38 SCAR 19.0
CC
38
Cov
65%
clone_without_attachments
30.0
all_parts_matching
20.0
_parse_multipart
11.1
CRAP 6.7
« Courriel/Helpers.pm ↑ Summary Courriel/Part/Multipart.pm »
1 package Courriel;
2  
3 6 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 our $VERSION = '0.49';
10  
11 6 ▶use Courriel::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Courriel::Helpers qw( unique_boundary );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use Courriel::Part::Multipart;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Courriel::Part::Single;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use Courriel::Types qw( ArrayRef Bool Headers Maybe Part Str StringRef );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use DateTime::Format::Mail 0.403;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶use DateTime::Format::Natural;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 6 ▶use Email::Address::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 6 ▶use Encode qw( encode );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 6 ▶use List::AllUtils qw( uniq );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 6 ▶use Params::ValidationCompiler 0.26 qw( validation_for );
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 6 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 6 has top_level_part => (
28 is => 'rw',
29 writer => '_replace_top_level_part',
30 isa => Part,
31 init_arg => 'part',
32 required => 1,
33 handles => [
34 qw(
35 as_string
36 content_type
37 headers
38 is_multipart
39 stream_to
40 )
41 ],
42 );
43  
44 6 has subject => (
45 is => 'ro',
46 isa => Maybe [Str],
47 init_arg => undef,
48 lazy => 1,
49 builder => '_build_subject',
50 );
51  
52 6 has datetime => (
53 is => 'ro',
54 isa => 'DateTime',
55 init_arg => undef,
56 lazy => 1,
57 builder => '_build_datetime',
58 );
59  
60 6 has _to => (
61 traits => ['Array'],
62 isa => ArrayRef ['Email::Address::XS'],
63 init_arg => undef,
64 lazy => 1,
65 builder => '_build_to',
66 handles => {
67 to => 'elements',
68 },
69 );
70  
71 6 has _cc => (
72 traits => ['Array'],
73 isa => ArrayRef ['Email::Address::XS'],
74 init_arg => undef,
75 lazy => 1,
76 builder => '_build_cc',
77 handles => {
78 cc => 'elements',
79 },
80 );
81  
82 6 has from => (
83 is => 'ro',
84 isa => Maybe ['Email::Address::XS'],
85 init_arg => undef,
86 lazy => 1,
87 builder => '_build_from',
88 );
89  
90 6 has _participants => (
91 traits => ['Array'],
92 isa => ArrayRef ['Email::Address::XS'],
93 init_arg => undef,
94 lazy => 1,
95 builder => '_build_participants',
96 handles => {
97 participants => 'elements',
98 },
99 );
100  
101 6 has _recipients => (
102 traits => ['Array'],
103 isa => ArrayRef ['Email::Address::XS'],
104 init_arg => undef,
105 lazy => 1,
106 builder => '_build_recipients',
107 handles => {
108 recipients => 'elements',
109 },
110 );
111  
112 6 has plain_body_part => (
113 is => 'ro',
114 isa => Maybe ['Courriel::Part::Single'],
115 init_arg => undef,
116 lazy => 1,
117 builder => '_build_plain_body_part',
118 );
119  
120 6 has html_body_part => (
121 is => 'ro',
122 isa => Maybe ['Courriel::Part::Single'],
123 init_arg => undef,
124 lazy => 1,
125 builder => '_build_html_body_part',
126 );
127  
128 0 ▶sub part_count {
Subroutine part_count: not called CC=2 SCAR=17.9
Pod documented
129 0 my $self = shift;
130  
131 0 ▶ return $self->is_multipart
Branch: 1 branch
BranchTrueFalseTotal
$self->is_multipart ? : 0 0 0
132 ? $self->top_level_part->part_count
133 : 1;
134 }
135  
136 0 ▶sub parts {
Subroutine parts: not called CC=2 SCAR=17.9
Pod documented
137 0 my $self = shift;
138  
139 0 ▶ return $self->is_multipart
Branch: 1 branch
BranchTrueFalseTotal
$self->is_multipart ? : 0 0 0
140 ? $self->top_level_part->parts
141 : $self->top_level_part;
142 }
143  
144 0 ▶sub clone_without_attachments {
Subroutine clone_without_attachments: not called CC=5 SCAR=34.0
Pod documented
145 0 my $self = shift;
146  
147 0 my $plain_body = $self->plain_body_part;
148 0 my $html_body = $self->html_body_part;
149  
150 0 my $headers = $self->headers;
151  
152 0 ▶ if ( $plain_body && $html_body ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($plain_body and $html_body) { } 0 0 0
elsif ($plain_body) { } 0 0 0
elsif ($html_body) { } 0 0 0
Condition0%0/3
$plain_body and $html_body
!ll&&!rl&&r
000
MC/DC0%0/2
$plain_body and $html_body
$plain_body $html_body
Truth table0%0/3
A and B
A= $plain_bodyB= $html_body
ABresult
0X0
100
111
153 0 my $ct = Courriel::Header::ContentType->new(
154 mime_type => 'multipart/alternative',
155 attributes => { boundary => unique_boundary },
156 );
157  
158 0 return Courriel->new(
159 part => Courriel::Part::Multipart->new(
160 content_type => $ct,
161 headers => $headers,
162 parts => [ $plain_body, $html_body ],
163 )
164 );
165 }
166 elsif ($plain_body) {
167 0 return Courriel->new(
168 part => Courriel::Part::Single->new(
169 content_type => $plain_body->content_type,
170 headers => $headers,
171 encoding => $plain_body->encoding,
172 encoded_content => $plain_body->encoded_content,
173 )
174 );
175 }
176 elsif ($html_body) {
177 0 return Courriel->new(
178 part => Courriel::Part::Single->new(
179 content_type => $html_body->content_type,
180 headers => $headers,
181 encoding => $html_body->encoding,
182 encoded_content => $html_body->encoded_content,
183 )
184 );
185 }
186  
187 0 die 'Cannot find a text or html body in this email!';
188 }
189  
190 0 ▶sub _build_subject {
Subroutine _build_subject: not called CC=2 SCAR=17.9
191 0 my $self = shift;
192  
193 0 my $subject = $self->headers->get('Subject');
194  
195 0 ▶ return $subject ? $subject->value : undef;
Branch: 1 branch
BranchTrueFalseTotal
$subject ? : 0 0 0
196 }
197  
198 {
199 6 my $mail_parser = DateTime::Format::Mail->new( loose => 1 );
200 6 my $natural_parser = DateTime::Format::Natural->new( time_zone => 'UTC' );
201  
202 2 ▶ sub _build_datetime {
Subroutine _build_datetime: called CC=6 SCAR=18.8
203 2 my $self = shift;
204  
205 my @possible = (
206 2 ( map { $_->value } $self->headers->get('Date') ),
207 (
208 reverse
209 7 map { $self->_find_date_received( $_->value ) }
210 $self->headers->get('Received')
211 ),
212 0 ( map { $_->value } $self->headers->get('Resent-Date') ),
213 );
214  
215 # Stolen from Email::Date and then modified
216 2 for my $possible (@possible) {
217 5 ▶ next unless defined $possible && length $possible;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $possible and length $possible 0 5 5
Condition33%1/3
defined $possible and length $possible
!ll&&!rl&&r
005
MC/DC0%0/2
defined $possible and length $possible
defined $possible length $possible
Truth table33%1/3
A and B
A= defined $possibleB= length $possible
ABresult
0X0
100
111
218  
219 5 my $dt = eval { $mail_parser->parse_datetime($possible) };
220  
221 5 ▶ unless ($dt) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dt) 3 2 5
222 3 $dt = $natural_parser->parse_datetime($possible);
223 3 ▶ next unless $natural_parser->success;
Branch: 1 branch
BranchTrueFalseTotal
unless $natural_parser->success 3 0 3
224 }
225  
226 2 $dt->set_time_zone('UTC');
227 2 return $dt;
228 }
229  
230 0 return DateTime->now( time_zone => 'UTC' );
231 }
232 }
233  
234 # Stolen from Email::Date and modified
235 6 ▶sub _find_date_received {
Subroutine _find_date_received: called CC=3 SCAR=11.6
236 7 shift;
237 7 my $received = shift;
238  
239 7 ▶ return unless defined $received && length $received;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $received and length $received 0 7 7
Condition33%1/3
defined $received and length $received
!ll&&!rl&&r
007
MC/DC0%0/2
defined $received and length $received
defined $received length $received
Truth table33%1/3
A and B
A= defined $receivedB= length $received
ABresult
0X0
100
111
240  
241 7 $received =~ s/.+;//;
242  
243 7 return $received;
244 }
245  
246 0 ▶sub _build_to {
Subroutine _build_to: not called CC=1 SCAR=6.9
247 0 my $self = shift;
248  
249 0 my @addresses = map { Email::Address::XS->parse( $_->value ) }
250 $self->headers->get('To');
251  
252 0 return $self->_unique_addresses( \@addresses );
253 }
254  
255 0 ▶sub _build_cc {
Subroutine _build_cc: not called CC=1 SCAR=6.9
256 0 my $self = shift;
257  
258 0 my @addresses = map { Email::Address::XS->parse( $_->value ) }
259 $self->headers->get('CC');
260  
261 0 return $self->_unique_addresses( \@addresses );
262 }
263  
264 0 ▶sub _build_from {
Subroutine _build_from: not called CC=1 SCAR=6.9
265 0 my $self = shift;
266  
267 0 my @addresses = map { Email::Address::XS->parse( $_->value ) }
268 $self->headers->get('From');
269  
270 0 return $addresses[0];
271 }
272  
273 0 ▶sub _build_recipients {
Subroutine _build_recipients: not called CC=1 SCAR=6.9
274 0 my $self = shift;
275  
276 0 my @addresses = ( $self->to, $self->cc );
277  
278 0 return $self->_unique_addresses( \@addresses );
279 }
280  
281 0 ▶sub _build_participants {
Subroutine _build_participants: not called CC=1 SCAR=6.9
282 0 my $self = shift;
283  
284 0 my @addresses = grep {defined} ( $self->from, $self->to, $self->cc );
285  
286 0 return $self->_unique_addresses( \@addresses );
287 }
288  
289 0 ▶sub _unique_addresses {
Subroutine _unique_addresses: not called CC=1 SCAR=6.9
290 0 my $self = shift;
291 0 my $addresses = shift;
292  
293 0 my %seen;
294 0 return [ grep { !$seen{ $_->original }++ } @{$addresses} ];
295 }
296  
297 3 ▶sub _build_plain_body_part {
Subroutine _build_plain_body_part: called CC=1 SCAR=0.0
298 3 my $self = shift;
299  
300 return $self->first_part_matching(
301 5 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
302 5 ▶ $_[0]->mime_type eq 'text/plain'
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->mime_type eq "text/plain" and $_[0]->is_inline 3 2 5
303 && $_[0]->is_inline;
304 }
305 3 );
306 }
307  
308 0 ▶sub _build_html_body_part {
Subroutine _build_html_body_part: not called CC=1 SCAR=6.9
309 0 my $self = shift;
310  
311 return $self->first_part_matching(
312 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
313 0 ▶ $_[0]->mime_type eq 'text/html'
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->mime_type eq "text/html" and $_[0]->is_inline 0 0 0
314 && $_[0]->is_inline;
315 }
316 0 );
317 }
318  
319 3 ▶sub first_part_matching {
Subroutine first_part_matching: called CC=4 SCAR=14.0
Pod documented
320 3 my $self = shift;
321 3 my $match = shift;
322  
323 3 my @parts = $self->top_level_part;
324  
325 ## no critic (ControlStructures::ProhibitCStyleForLoops)
326 3 ▶ for ( my $part = shift @parts; $part; $part = shift @parts ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part) 5 0 5
327 5 ▶ return $part if $match->($part);
Branch: 1 branch
BranchTrueFalseTotal
if &$match($part) 3 2 5
328  
329 2 ▶ push @parts, $part->parts if $part->is_multipart;
Branch: 1 branch
BranchTrueFalseTotal
if $part->is_multipart 2 0 2
330 }
331 }
332  
333 0 ▶sub all_parts_matching {
Subroutine all_parts_matching: not called CC=4 SCAR=30.0
Pod documented
334 0 my $self = shift;
335 0 my $match = shift;
336  
337 0 my @parts = $self->top_level_part;
338  
339 0 my @match;
340 ## no critic (ControlStructures::ProhibitCStyleForLoops)
341 0 ▶ for ( my $part = shift @parts; $part; $part = shift @parts ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part) 0 0 0
342 0 ▶ push @match, $part if $match->($part);
Branch: 1 branch
BranchTrueFalseTotal
if &$match($part) 0 0 0
343  
344 0 ▶ push @parts, $part->parts if $part->is_multipart;
Branch: 1 branch
BranchTrueFalseTotal
if $part->is_multipart 0 0 0
345 }
346  
347 0 return @match;
348 }
349  
350 {
351 6 my $validator = validation_for(
352 params => [
353 text => { type => StringRef },
354 ],
355 named_to_list => 1,
356 );
357  
358 # This is needed for Email::Abstract compatibility but it's a godawful
359 # idea, and even Email::Abstract says not to do this.
360 #
361 # It's much safer to just make a new Courriel object from scratch.
362 0 ▶ sub replace_body {
Subroutine replace_body: not called CC=1 SCAR=6.9
Pod undocumented
363 0 my $self = shift;
364 0 my ($text) = $validator->(@_);
365  
366 0 my $part = Courriel::Part::Single->new(
367 headers => $self->headers,
368 encoded_content => $text,
369 );
370  
371 0 $self->_replace_top_level_part($part);
372  
373 0 return;
374 }
375 }
376  
377 {
378 6 my $validator = validation_for(
379 params => [
380 text => { type => StringRef },
381 is_character => { type => Bool, default => 0 },
382 ],
383 named_to_list => 1,
384 );
385  
386 65 ▶ sub parse {
Subroutine parse: called CC=2 SCAR=8.5
Pod documented
387 65 my $class = shift;
388 65 my ( $text, $is_character ) = $validator->(@_);
389  
390 65 ▶ if ($is_character) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_character) 0 65 65
391 0 ${$text} = encode( 'UTF-8', ${$text} );
392 }
393  
394 65 return $class->new( part => $class->_parse($text) );
395 }
396 }
397  
398 6 ▶sub _parse {
Subroutine _parse: called CC=1 SCAR=0.0
399 143 my $class = shift;
400 143 my $text = shift;
401  
402 143 my ( $sep_idx, $headers ) = $class->_parse_headers($text);
403  
404 143 substr( ${$text}, 0, $sep_idx, q{} );
405  
406 143 return $class->_parse_parts( $text, $headers );
407 }
408  
409 143 ▶sub _parse_headers {
Subroutine _parse_headers: called CC=2 SCAR=6.9
410 143 my $class = shift;
411 143 my $text = shift;
412  
413 143 my $header_text;
414 my $sep_idx;
415  
416 # We want to ignore mbox message separators - this is a pretty lax parser,
417 # but we may find broken lines. The key is that it starts with From
418 # followed by space, not a colon.
419 143 ${$text} =~ s/^From\s+.+$Courriel::Helpers::LINE_SEP_RE//;
420  
421 # Some broken emails may split the From line in an arbitrary spot
422 143 ${$text} =~ s/^[^:]+$Courriel::Helpers::LINE_SEP_RE//g;
423  
424 143 ▶ if ( ${$text} =~ /^(.+?)($Courriel::Helpers::LINE_SEP_RE)\g{2}/s ) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$text;} =~ /^(.+?)($Courriel::Helpers::LINE_SEP_RE)\g{2}/s) { } 142 1 143
425 142 $header_text = $1 . $2;
426 142 $sep_idx = ( length $header_text ) + ( length $2 );
427 }
428 else {
429 1 return ( 0, Courriel::Headers::->new );
430 }
431  
432 142 my $headers = Courriel::Headers::->parse(
433 text => \$header_text,
434 );
435  
436 142 return ( $sep_idx, $headers );
437 }
438  
439 {
440 6 my $fake_ct = Courriel::Header::ContentType->new_from_value(
441 name => 'Content-Type',
442 value => 'text/plain'
443 );
444  
445 143 ▶ sub _parse_parts {
Subroutine _parse_parts: called CC=4 SCAR=14.1
446 143 my $class = shift;
447 143 my $text = shift;
448 143 my $headers = shift;
449  
450 143 my @ct = $headers->get('Content-Type');
451 143 ▶ if ( @ct > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ct > 1) 0 143 143
452 0 die 'This email defines more than one Content-Type header.';
453 }
454  
455 143 ▶ my $ct = $ct[0] // $fake_ct;
Condition66%2/3
$ct[0] // $fake_ct
l!l&&r!l&&!r
132110
MC/DC0%0/2
$ct[0] // $fake_ct
$ct[0] $fake_ct
Truth table66%2/3
A // B
A= $ct[0]B= $fake_ct
ABresult
1X1
011
000
456  
457 143 ▶ if ( $ct->mime_type !~ /^multipart/i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ct->mime_type =~ /^multipart/i) 107 36 143
458 107 return Courriel::Part::Single->new(
459 headers => $headers,
460 encoded_content => $text,
461 );
462 }
463  
464 36 return $class->_parse_multipart( $text, $headers, $ct );
465 }
466 }
467  
468 6 ▶sub _parse_multipart {
Subroutine _parse_multipart: called CC=11 SCAR=24.1
469 36 my $class = shift;
470 36 my $text = shift;
471 36 my $headers = shift;
472 36 my $ct = shift;
473  
474 36 my $boundary = $ct->attribute_value('boundary');
475  
476 36 ▶ die q{The message's mime type claims this is a multipart message (}
Branch: 1 branch
BranchTrueFalseTotal
unless defined $boundary and length $boundary 0 36 36
Condition33%1/3
defined $boundary and length $boundary
!ll&&!rl&&r
0036
MC/DC0%0/2
defined $boundary and length $boundary
defined $boundary length $boundary
Truth table33%1/3
A and B
A= defined $boundaryB= length $boundary
ABresult
0X0
100
111
477 . $ct->mime_type
478 . q{) but it does not specify a boundary.}
479 unless defined $boundary && length $boundary;
480  
481 36 my ( $preamble, $all_parts, $epilogue ) = ${$text} =~ /
482 (.*?) # preamble
483 ^--\Q$boundary\E\s*
484 (.+) # all parts
485 ^--\Q$boundary\E--\s*
486 (.*) # epilogue
487 /smx;
488  
489 36 my @part_text;
490  
491 36 ▶ if ( defined $all_parts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $all_parts) 31 5 36
492 31 @part_text = split /^--\Q$boundary\E\s*/m, $all_parts;
493 }
494  
495 36 ▶ unless (@part_text) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@part_text) 5 31 36
496 5 ${$text} =~ s/^--\Q$boundary\E\s*//m;
497 5 push @part_text, ${$text};
498 }
499  
500 return Courriel::Part::Multipart->new(
501 headers => $headers,
502 (
503 defined $preamble
504 && length $preamble
505 && $preamble =~ /\S/ ? ( preamble => $preamble ) : ()
506 ),
507 (
508 defined $epilogue
509 && length $epilogue
510 && $epilogue =~ /\S/ ? ( epilogue => $epilogue ) : ()
511 ),
512 boundary => $boundary,
513 36 ▶ parts => [ map { $class->_parse( \$_ ) } @part_text ],
Branch: 2 branches
BranchTrueFalseTotal
defined $preamble && length $preamble && $preamble =~ /\S/ ? : 3 33 36
defined $epilogue && length $epilogue && $epilogue =~ /\S/ ? : 6 30 36
Condition91%11/12
defined $preamble and length $preamble and $preamble =~ /\S/
!ll&&!rl&&r
21123
defined $preamble and length $preamble
!ll&&!rl&&r
51615
defined $epilogue and length $epilogue and $epilogue =~ /\S/
!ll&&!rl&&r
3006
defined $epilogue and length $epilogue
!ll&&!rl&&r
5256
MC/DC100%3/3
defined $preamble and length $preamble and $preamble =~ /\S/
defined $preamble length $preamble $preamble =~ /\S/
MC/DC66%2/3
defined $epilogue and length $epilogue and $epilogue =~ /\S/
defined $epilogue length $epilogue $epilogue =~ /\S/
Truth table100%4/4
A and B and C
A= defined $preambleB= length $preambleC= $preamble =~ /\S/
ABCresult
0XX0
10X0
1100
1111
Truth table75%3/4
A and B and C
A= defined $epilogueB= length $epilogueC= $epilogue =~ /\S/
ABCresult
0XX0
10X0
1100
1111
514 );
515 }
516  
517 6 __PACKAGE__->meta->make_immutable;
518  
519 6 1;
520  
521 # ABSTRACT: High level email parsing and manipulation
522  
523 __END__
« Courriel/Helpers.pm ↑ Summary Courriel/Part/Multipart.pm »