Message/Body/Encode.pm

filter: Statementstmt 75.6% 146 / 193 Branchbran 62.0% 72 / 116 Conditioncond 55.3% 52 / 94 MC/DCmcdc 24.1% 14 / 58 Subroutinesub 87.5% 21 / 24 Podpod 100% 11 / 11 totaltotal 63.7% 316 / 496 SCAR 59.8
CC
92
Cov
67%
encode
94.2
dispositionFilename
72.1
unify
20.0
CRAP 396.2
« Message/Head/Complete.pm ↑ Summary Box/Parser/Perl.pm »
1 # Copyrights 2001-2023 by [Mark Overmeer <markov@cpan.org>].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.03.
5 # This code is part of distribution Mail-Message. Meta-POD processed with
6 # OODoc into POD and HTML manual-pages. See README.md
7 # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8  
9 package Mail::Message::Body;
10 13 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 $VERSION = '3.013';
12  
13 13 ▶use base 'Mail::Reporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 13 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 13 ▶use MIME::Types ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 13 ▶use File::Basename 'basename';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 13 ▶use Encode qw/find_encoding from_to encode_utf8/;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 13 ▶use Mail::Message::Field ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 13 ▶use Mail::Message::Field::Full ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 # http://www.iana.org/assignments/character-sets
28 13 ▶use Encode::Alias;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 13 define_alias(qr/^unicode-?1-?1-?utf-?([78])$/i => '"UTF-$1"'); # rfc1642
30  
31 13 my $mime_types;
32  
33  
34 sub _char_enc($)
35 136 ▶{ my ($self, $charset) = @_;
Subroutine _char_enc: called CC=4 SCAR=14.1
36 136 ▶ return undef if !$charset || $charset eq 'PERL';
Branch: 1 branch
BranchTrueFalseTotal
if not $charset or $charset eq "PERL" 86 50 136
Condition66%2/3
not $charset or $charset eq "PERL"
l!l&&r!l&&!r
08650
MC/DC50%1/2
not $charset or $charset eq "PERL"
not $charset $charset eq "PERL"
Truth table66%2/3
not(A) or B
A= not $charsetB= $charset eq "PERL"
ABresult
1X1
011
000
37  
38 50 ▶ my $enc = find_encoding $charset
Branch: 1 branch
BranchTrueFalseTotal
my $enc = &find_encoding($charset) or $self->log("WARNING", "Charset `${charset}' is not known.") 0 50 50
39 or $self->log(WARNING => "Charset `$charset' is not known.");
40  
41 50 $enc;
42 }
43  
44 sub encode(@)
45 71 ▶{ my ($self, %args) = @_;
Subroutine encode: called CC=39 SCAR=45.5
Pod documented
46  
47 71 ▶ my $bodytype = $args{result_type} || ref $self;
Condition33%1/3
$args{'result_type'} || ref $self
l!l&&r!l&&!r
0710
MC/DC0%0/2
$args{'result_type'} || ref $self
$args{'result_type'} ref $self
Truth table33%1/3
A || B
A= $args{'result_type'}B= ref $self
ABresult
1X1
011
000
48  
49 ### The content type
50  
51 71 my $type_from = $self->type;
52 71 ▶ my $type_to = $args{mime_type} || $type_from->clone->study;
Condition33%1/3
$args{'mime_type'} || $type_from->clone->study
l!l&&r!l&&!r
0710
MC/DC0%0/2
$args{'mime_type'} || $type_from->clone->study
$args{'mime_type'} $type_from->clone->study
Truth table33%1/3
A || B
A= $args{'mime_type'}B= $type_from->clone->study
ABresult
1X1
011
000
53 71 ▶ $type_to = Mail::Message::Field::Full->new('Content-Type' => $type_to)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $type_to 0 71 71
54 unless ref $type_to;
55  
56 ### Detect specified transfer-encodings
57  
58 71 ▶ my $transfer = $args{transfer_encoding} || $self->transferEncoding->clone;
Condition66%2/3
$args{'transfer_encoding'} || $self->transferEncoding->clone
l!l&&r!l&&!r
6650
MC/DC0%0/2
$args{'transfer_encoding'} || $self->transferEncoding->clone
$args{'transfer_encoding'} $self->transferEncoding->clone
Truth table66%2/3
A || B
A= $args{'transfer_encoding'}B= $self->transferEncoding->clone
ABresult
1X1
011
000
59 71 ▶ $transfer = Mail::Message::Field->new('Content-Transfer-Encoding' => $transfer)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $transfer 64 7 71
60 unless ref $transfer;
61  
62 71 my $trans_was = lc $self->transferEncoding;
63 71 my $trans_to = lc $transfer;
64  
65 ### Detect specified charsets
66  
67 71 my $is_text = $type_from =~ m!^text/!i;
68 71 my ($char_was, $char_to, $from, $to);
69 71 ▶ if($is_text)
Branch: 2 branches
BranchTrueFalseTotal
if ($is_text) { } 68 3 71
elsif ($trans_was eq $trans_to) { } 0 3 3
70 68 { $char_was = $type_from->attribute('charset'); # sometimes missing
71 68 $char_to = $type_to->attribute('charset'); # usually missing
72  
73 68 ▶ if(my $charset = delete $args{charset})
Branch: 2 branches
BranchTrueFalseTotal
if (my $charset = delete $args{'charset'}) { } 63 5 68
elsif (not $char_to and $char_was) { } 0 10 10
74 { # Explicitly stated output charset
75 63 ▶ if(!$char_to || $char_to ne $charset)
Branch: 1 branch
BranchTrueFalseTotal
if (not $char_to or $char_to ne $charset) 42 21 63
Condition66%2/3
not $char_to or $char_to ne $charset
l!l&&r!l&&!r
04221
MC/DC50%1/2
not $char_to or $char_to ne $charset
not $char_to $char_to ne $charset
Truth table66%2/3
not(A) or B
A= not $char_toB= $char_to ne $charset
ABresult
1X1
011
000
76 42 { $char_to = $charset;
77 42 $type_to->attribute(charset => $char_to);
78 }
79 }
80 ▶ elsif(!$char_to && $char_was)
Condition33%1/3
not $char_to and $char_was
!ll&&!rl&&r
500
MC/DC0%0/2
not $char_to and $char_was
not $char_to $char_was
Truth table33%1/3
not(A) and B
A= not $char_toB= $char_was
ABresult
0X0
100
111
81 { # By default, do not change charset
82 0 $char_to = $char_was;
83 0 $type_to->attribute(charset => $char_to);
84 }
85  
86 68 ▶ if($char_to && $trans_to ne 'none' && $char_to eq 'PERL')
Branch: 1 branch
BranchTrueFalseTotal
if ($char_to and $trans_to ne "none" and $char_to eq "PERL") 0 68 68
Condition66%4/6
$char_to and $trans_to ne "none" and $char_to eq "PERL"
!ll&&!rl&&r
35330
$char_to and $trans_to ne "none"
!ll&&!rl&&r
03533
MC/DC0%0/3
$char_to and $trans_to ne "none" and $char_to eq "PERL"
$char_to $trans_to ne "none" $char_to eq "PERL"
Truth table50%2/4
A and B and C
A= $char_toB= $trans_to ne "none"C= $char_to eq "PERL"
ABCresult
0XX0
10X0
1100
1111
87 { # We cannot leave the body into the 'PERL' charset when transfer-
88 # encoding is applied.
89 0 $self->log(WARNING => "Transfer-Encoding `$trans_to' requires "
90 . "explicit charset, defaulted to utf-8");
91 0 $char_to = 'utf-8';
92 }
93  
94 68 $from = $self->_char_enc($char_was);
95 68 $to = $self->_char_enc($char_to);
96  
97 68 ▶ if($from && $to)
Branch: 1 branch
BranchTrueFalseTotal
if ($from and $to) 4 64 68
Condition100%3/3
$from and $to
!ll&&!rl&&r
5594
MC/DC100%2/2
$from and $to
$from $to
Truth table100%3/3
A and B
A= $fromB= $to
ABresult
0X0
100
111
98 4 ▶ { if($char_was ne $char_to && $from->name eq $to->name)
Branch: 1 branch
BranchTrueFalseTotal
if ($char_was ne $char_to and $from->name eq $to->name) 0 4 4
Condition33%1/3
$char_was ne $char_to and $from->name eq $to->name
!ll&&!rl&&r
400
MC/DC0%0/2
$char_was ne $char_to and $from->name eq $to->name
$char_was ne $char_to $from->name eq $to->name
Truth table33%1/3
A and B
A= $char_was ne $char_toB= $from->name eq $to->name
ABresult
0X0
100
111
99 { # modify source charset into a different alias
100 0 $type_from->attribute(charset => $char_to);
101 0 $char_was = $char_to;
102 0 $from = $to;
103 }
104  
105 4 ▶ return $self
Branch: 1 branch
BranchTrueFalseTotal
if $trans_was eq $trans_to and $char_was eq $char_to 0 4 4
Condition33%1/3
$trans_was eq $trans_to and $char_was eq $char_to
!ll&&!rl&&r
400
MC/DC0%0/2
$trans_was eq $trans_to and $char_was eq $char_to
$trans_was eq $trans_to $char_was eq $char_to
Truth table33%1/3
A and B
A= $trans_was eq $trans_toB= $char_was eq $char_to
ABresult
0X0
100
111
106 if $trans_was eq $trans_to && $char_was eq $char_to;
107 }
108 }
109 elsif($trans_was eq $trans_to)
110 { # No changes needed;
111 0 return $self;
112 }
113  
114 ### Apply transfer-decoding
115  
116 71 my $decoded;
117 71 ▶ if($trans_was eq 'none')
Branch: 2 branches
BranchTrueFalseTotal
if ($trans_was eq 'none') { } 62 9 71
elsif (my $decoder = $self->getTransferEncHandler($trans_was)) { } 9 0 9
118 62 { $decoded = $self }
119 elsif(my $decoder = $self->getTransferEncHandler($trans_was))
120 9 { $decoded = $decoder->decode($self, result_type => $bodytype) }
121 else
122 0 { $self->log(WARNING =>
123 "No decoder defined for transfer encoding $trans_was.");
124 0 return $self;
125 }
126  
127 ### Apply character-set recoding
128  
129 71 my $recoded;
130 71 ▶ if($is_text)
Branch: 1 branch
BranchTrueFalseTotal
if ($is_text) { } 68 3 71
131 68 ▶ { unless($char_was)
Branch: 1 branch
BranchTrueFalseTotal
unless ($char_was) 0 68 68
132 { # When we do not know the character-sets, try to auto-detect
133 0 ▶ my $auto = $args{charset_detect} || $self->charsetDetectAlgorithm;
Condition0%0/3
$args{'charset_detect'} || $self->charsetDetectAlgorithm
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'charset_detect'} || $self->charsetDetectAlgorithm
$args{'charset_detect'} $self->charsetDetectAlgorithm
Truth table0%0/3
A || B
A= $args{'charset_detect'}B= $self->charsetDetectAlgorithm
ABresult
1X1
011
000
134 0 $char_was = $decoded->$auto;
135 0 $from = $self->_char_enc($char_was);
136 0 $decoded->type->attribute(charset => $char_was);
137  
138 0 ▶ unless($char_to)
Branch: 1 branch
BranchTrueFalseTotal
unless ($char_to) 0 0 0
139 0 { $char_to = $char_was;
140 0 $type_to->attribute(charset => $char_to);
141 0 $to = $from;
142 }
143 }
144  
145 68 ▶ my $new_data
Branch: 3 branches
BranchTrueFalseTotal
$to && $char_was eq 'PERL' ? : 33 35 68
$from && $char_to eq 'PERL' ? : 9 26 35
$to && $from && $char_was ne $char_to ? : 0 26 26
Condition83%10/12
$to and $char_was eq "PERL"
!ll&&!rl&&r
31433
$from and $char_to eq "PERL"
!ll&&!rl&&r
2249
$to and $from and $char_was ne $char_to
!ll&&!rl&&r
2240
$to and $from
!ll&&!rl&&r
2204
MC/DC100%2/2
$to and $char_was eq "PERL"
$to $char_was eq "PERL"
MC/DC100%2/2
$from and $char_to eq "PERL"
$from $char_to eq "PERL"
MC/DC0%0/3
$to and $from and $char_was ne $char_to
$to $from $char_was ne $char_to
Truth table100%3/3
A and B
A= $toB= $char_was eq "PERL"
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $fromB= $char_to eq "PERL"
ABresult
0X0
100
111
Truth table50%2/4
A and B and C
A= $toB= $fromC= $char_was ne $char_to
ABCresult
0XX0
10X0
1100
1111
146 = $to && $char_was eq 'PERL' ? $to->encode($decoded->string)
147 : $from && $char_to eq 'PERL' ? $from->decode($decoded->string)
148 : $to && $from && $char_was ne $char_to ? $to->encode($from->decode($decoded->string))
149 : undef;
150  
151 68 ▶ $recoded
Branch: 1 branch
BranchTrueFalseTotal
$new_data ? : 42 26 68
152 = $new_data
153 ? $bodytype->new(based_on => $decoded, data => $new_data,
154 mime_type => $type_to, checked => 1)
155 : $decoded;
156 }
157 else
158 3 { $recoded = $decoded;
159 }
160  
161 ### Apply transfer-encoding
162  
163 71 my $trans;
164 71 ▶ if($trans_to ne 'none')
Branch: 1 branch
BranchTrueFalseTotal
if ($trans_to ne "none") 36 35 71
165 36 ▶ { $trans = $self->getTransferEncHandler($trans_to)
Branch: 1 branch
BranchTrueFalseTotal
$trans = $self->getTransferEncHandler($trans_to) or $self->log("WARNING", "No encoder defined for transfer encoding `${trans_to}'.") 0 36 36
166 or $self->log(WARNING =>
167 "No encoder defined for transfer encoding `$trans_to'.");
168 }
169  
170 71 ▶ my $encoded = defined $trans
Branch: 1 branch
BranchTrueFalseTotal
defined $trans ? : 36 35 71
171 ? $trans->encode($recoded, result_type => $bodytype)
172 : $recoded;
173  
174 71 $encoded;
175 }
176  
177  
178 sub charsetDetectAlgorithm(;$)
179 32 ▶{ my $self = shift;
Subroutine charsetDetectAlgorithm: called CC=3 SCAR=11.7
Pod documented
180 32 ▶ $self->{MMBE_det} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 32 32
181 32 ▶ $self->{MMBE_det} || 'charsetDetect';
Branch: 1 branch
BranchTrueFalseTotal
$self->{"MMBE_det"} or "charsetDetect" 32 0 32
182 }
183  
184  
185 sub charsetDetect(%)
186 32 ▶{ my ($self, %args) = @_;
Subroutine charsetDetect: called CC=6 SCAR=19.1
Pod documented
187 32 my $text = $self->string;
188  
189 # Flagged as UTF8, so certainly created by the Perl program itself:
190 # the content is not octets.
191 32 ▶ if(utf8::is_utf8($text))
Branch: 1 branch
BranchTrueFalseTotal
if (utf8::is_utf8($text)) 6 26 32
192 6 ▶ { $args{external} or return 'PERL';
Branch: 1 branch
BranchTrueFalseTotal
$args{"external"} or return "PERL" 0 6 6
193 6 $text = encode_utf8 $text;
194 }
195  
196 # Only look for normal characters, first 1920 unicode characters
197 # When there is any octet in 'utf-encoding'-space, but not an
198 # legal utf8, than it's not utf8.
199 #XXX Use the fact that cp1252 does not define (0x81, 0x8d, 0x8f, 0x90, 0x9d) ?
200 32 ▶ return 'utf-8'
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /[\0xC0-\xDF][\x80-\xBF]/ and not $text =~ /[\0xC0-\xFF]([^\0x80-\xBF]|$)/ 0 32 32
Condition33%1/3
$text =~ /[\0xC0-\xDF][\x80-\xBF]/ and not $text =~ /[\0xC0-\xFF]([^\0x80-\xBF]|$)/
!ll&&!rl&&r
3200
MC/DC0%0/2
$text =~ /[\0xC0-\xDF][\x80-\xBF]/ and not $text =~ /[\0xC0-\xFF]([^\0x80-\xBF]|$)/
$text =~ /[\0xC0-\xDF][\x80-\xBF]/ not $text =~ /[\0xC0-\xFF]([^\0x80-\xBF]|$)/
Truth table33%1/3
A and not(B)
A= $text =~ /[\0xC0-\xDF][\x80-\xBF]/B= not $text =~ /[\0xC0-\xFF]([^\0x80-\xBF]|$)/
ABresult
0X0
100
111
201 if $text =~ m/[\0xC0-\xDF][\x80-\xBF]/ # 110xxxxx, 10xxxxxx
202 && $text !~ m/[\0xC0-\xFF]([^\0x80-\xBF]|$)/;
203  
204 # Produce 'us-ascii' when it suffices: it is the RFC compliant
205 # default charset.
206 32 ▶ $text =~ m/[\x80-\xFF]/ ? 'cp1252' : 'us-ascii';
Branch: 1 branch
BranchTrueFalseTotal
$text =~ /[\x80-\xFF]/ ? : 0 32 32
207 }
208  
209  
210  
211 sub check()
212 23 ▶{ my $self = shift;
Subroutine check: called CC=4 SCAR=14.0
Pod documented
213 23 ▶ return $self if $self->checked;
Branch: 1 branch
BranchTrueFalseTotal
if $self->checked 22 1 23
214 1 my $eol = $self->eol;
215  
216 1 my $encoding = $self->transferEncoding->body;
217 1 ▶ return $self->eol($eol)
Branch: 1 branch
BranchTrueFalseTotal
if $encoding eq "none" 0 1 1
218 if $encoding eq 'none';
219  
220 1 my $encoder = $self->getTransferEncHandler($encoding);
221  
222 1 ▶ my $checked
Branch: 1 branch
BranchTrueFalseTotal
$encoder ? : 1 0 1
223 = $encoder
224 ? $encoder->check($self)->eol($eol)
225 : $self->eol($eol);
226  
227 1 $checked->checked(1);
228 1 $checked;
229 }
230  
231 #------------------------------------------
232  
233  
234 sub encoded(%)
235 57 ▶{ my ($self, %args) = @_;
Subroutine encoded: called CC=12 SCAR=25.1
Pod documented
236  
237 57 ▶ $mime_types ||= MIME::Types->new;
Condition66%2/3
$mime_types ||= "MIME::Types"->new
l!l&&r!l&&!r
5070
MC/DC0%0/2
$mime_types ||= "MIME::Types"->new
$mime_types "MIME::Types"->new
Truth table66%2/3
A ||= B
A= $mime_typesB= "MIME::Types"->new
ABresult
1X1
011
000
238 57 my $mime = $mime_types->type($self->type->body);
239  
240 57 ▶ my $charset = my $old_charset = $self->charset || '';
Condition100%2/2
$self->charset || ''
l!l
534
MC/DC100%1/1
$self->charset || ''
$self->charset
Truth table100%2/2
A
A= $self->charset
Aresult
11
00
241 57 ▶ if(!$charset || $charset eq 'PERL')
Branch: 1 branch
BranchTrueFalseTotal
if (not $charset or $charset eq "PERL") 32 25 57
Condition100%3/3
not $charset or $charset eq "PERL"
l!l&&r!l&&!r
42825
MC/DC100%2/2
not $charset or $charset eq "PERL"
not $charset $charset eq "PERL"
Truth table100%3/3
not(A) or B
A= not $charsetB= $charset eq "PERL"
ABresult
1X1
011
000
242 32 ▶ { my $auto = $args{charset_detect} || $self->charsetDetectAlgorithm;
Condition33%1/3
$args{'charset_detect'} || $self->charsetDetectAlgorithm
l!l&&r!l&&!r
0320
MC/DC0%0/2
$args{'charset_detect'} || $self->charsetDetectAlgorithm
$args{'charset_detect'} $self->charsetDetectAlgorithm
Truth table33%1/3
A || B
A= $args{'charset_detect'}B= $self->charsetDetectAlgorithm
ABresult
1X1
011
000
243 32 $charset = $self->$auto(external => 1);
244 }
245  
246 57 my $enc_was = $self->transferEncoding;
247 57 my $enc = $enc_was;
248 57 ▶ $enc = defined $mime ? $mime->encoding : 'base64'
Branch: 2 branches
BranchTrueFalseTotal
if $enc eq "none" 32 25 57
defined $mime ? : 32 0 32
249 if $enc eq 'none';
250  
251 # we could (expensively) try to autodetect character-set used,
252 # but everything is a subset of utf-8.
253 57 ▶ my $new_charset = (!$mime || $mime !~ m!^text/!i) ? '' : $charset;
Branch: 1 branch
BranchTrueFalseTotal
!($mime && $mime =~ m[^text/]i) ? : 4 53 57
Condition66%2/3
$mime && $mime =~ m[^text/]i
!ll&&!rl&&r
0453
MC/DC50%1/2
$mime && $mime =~ m[^text/]i
$mime $mime =~ m[^text/]i
Truth table66%2/3
A && B
A= $mimeB= $mime =~ m[^text/]i
ABresult
0X0
100
111
254  
255 57 ▶ ($enc_was ne 'none' && $old_charset eq $new_charset)
Branch: 1 branch
BranchTrueFalseTotal
$enc_was ne 'none' && $old_charset eq $new_charset ? : 23 34 57
Condition100%3/3
$enc_was ne "none" and $old_charset eq $new_charset
!ll&&!rl&&r
32223
MC/DC100%2/2
$enc_was ne "none" and $old_charset eq $new_charset
$enc_was ne "none" $old_charset eq $new_charset
Truth table100%3/3
A and B
A= $enc_was ne "none"B= $old_charset eq $new_charset
ABresult
0X0
100
111
256 ? $self->check
257 : $self->encode(transfer_encoding => $enc, charset => $new_charset);
258 }
259  
260 #------------------------------------------
261  
262  
263 sub unify($)
264 0 ▶{ my ($self, $body) = @_;
Subroutine unify: not called CC=4 SCAR=30.0
Pod documented
265 0 ▶ return $self if $self==$body;
Branch: 1 branch
BranchTrueFalseTotal
if $self == $body 0 0 0
266  
267 0 my $mime = $self->type;
268 0 my $transfer = $self->transferEncoding;
269  
270 0 my $encoded = $body->encode
271 ( mime_type => $mime
272 , transfer_encoding => $transfer
273 );
274  
275 # Encode makes the best of it, but is it good enough?
276  
277 0 my $newmime = $encoded->type;
278 0 ▶ return unless $newmime eq $mime;
Branch: 1 branch
BranchTrueFalseTotal
unless $newmime eq $mime 0 0 0
279 0 ▶ return unless $transfer eq $encoded->transferEncoding;
Branch: 1 branch
BranchTrueFalseTotal
unless $transfer eq $encoded->transferEncoding 0 0 0
280 0 $encoded;
281 }
282  
283 #------------------------------------------
284  
285  
286 sub isBinary()
287 11 ▶{ my $self = shift;
Subroutine isBinary: called CC=4 SCAR=14.5
Pod documented
288 11 ▶ $mime_types ||= MIME::Types->new(only_complete => 1);
Condition66%2/3
$mime_types ||= "MIME::Types"->new("only_complete", 1)
l!l&&r!l&&!r
1010
MC/DC0%0/2
$mime_types ||= "MIME::Types"->new("only_complete", 1)
$mime_types "MIME::Types"->new("only_complete", 1)
Truth table66%2/3
A ||= B
A= $mime_typesB= "MIME::Types"->new("only_complete", 1)
ABresult
1X1
011
000
289 11 ▶ my $type = $self->type or return 1;
Branch: 1 branch
BranchTrueFalseTotal
my $type = $self->type or return 1 0 11 11
290 11 ▶ my $mime = $mime_types->type($type->body) or return 1;
Branch: 1 branch
BranchTrueFalseTotal
my $mime = $mime_types->type($type->body) or return 1 0 11 11
291 11 $mime->isBinary;
292 }
293
294  
295 0 ▶sub isText() { not shift->isBinary }
Subroutine isText: not called CC=1 SCAR=6.9
Pod documented
296  
297  
298 sub dispositionFilename(;$)
299 1 ▶{ my $self = shift;
Subroutine dispositionFilename: called CC=20 SCAR=42.8
Pod documented
300 1 my $raw;
301  
302 my $field;
303 1 ▶ if($field = $self->disposition)
Branch: 1 branch
BranchTrueFalseTotal
if ($field = $self->disposition) 1 0 1
304 1 ▶ { $field = $field->study if $field->can('study');
Branch: 1 branch
BranchTrueFalseTotal
if $field->can("study") 1 0 1
305 1 ▶ $raw = $field->attribute('filename')
Condition33%2/6
$field->attribute('filename') || $field->attribute('file') || $field->attribute('name')
l!l&&r!l&&!r
001
$field->attribute("filename") or $field->attribute("file")
l!l&&r!l&&!r
001
MC/DC0%0/3
$field->attribute('filename') || $field->attribute('file') || $field->attribute('name')
$field->attribute("filename") $field->attribute("file") $field->attribute('name')
Truth table25%1/4
A or B || C
A= $field->attribute("filename")B= $field->attribute("file")C= $field->attribute('name')
ABCresult
1XX1
01X1
0011
0000
306 || $field->attribute('file')
307 || $field->attribute('name');
308 }
309  
310 1 ▶ if(!defined $raw && ($field = $self->type))
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $raw and $field = $self->type) 1 0 1
Condition33%1/3
not defined $raw and $field = $self->type
!ll&&!rl&&r
001
MC/DC0%0/2
not defined $raw and $field = $self->type
not defined $raw $field = $self->type
Truth table33%1/3
not(A) and B
A= not defined $rawB= $field = $self->type
ABresult
0X0
100
111
311 1 ▶ { $field = $field->study if $field->can('study');
Branch: 1 branch
BranchTrueFalseTotal
if $field->can("study") 1 0 1
312 1 ▶ $raw = $field->attribute('filename')
Condition33%2/6
$field->attribute('filename') || $field->attribute('file') || $field->attribute('name')
l!l&&r!l&&!r
001
$field->attribute("filename") or $field->attribute("file")
l!l&&r!l&&!r
001
MC/DC0%0/3
$field->attribute('filename') || $field->attribute('file') || $field->attribute('name')
$field->attribute("filename") $field->attribute("file") $field->attribute('name')
Truth table25%1/4
A or B || C
A= $field->attribute("filename")B= $field->attribute("file")C= $field->attribute('name')
ABCresult
1XX1
01X1
0011
0000
313 || $field->attribute('file')
314 || $field->attribute('name');
315 }
316  
317 1 my $base;
318 1 ▶ if(!defined $raw || !length $raw) {}
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $raw && length $raw) { } 1 0 1
elsif (index($raw, '?') != -1) { } 0 0 0
Condition33%1/3
defined $raw && length $raw
!ll&&!rl&&r
100
MC/DC0%0/2
defined $raw && length $raw
defined $raw length $raw
Truth table33%1/3
A && B
A= defined $rawB= length $raw
ABresult
0X0
100
111
319 elsif(index($raw, '?') >= 0)
320 0 { eval 'require Mail::Message::Field::Full';
321 0 $base = Mail::Message::Field::Full->decode($raw);
322 }
323 else
324 0 { $base = $raw;
325 }
326  
327 1 ▶ return $base
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 1 1
328 unless @_;
329  
330 1 my $dir = shift;
331 1 my $filename = '';
332 1 ▶ if(defined $base) # RFC6266 section 4.3, very safe
Branch: 1 branch
BranchTrueFalseTotal
if (defined $base) 0 1 1
333 0 { $filename = basename $base;
334 0 for($filename)
335 0 { s/\s+/ /g; s/ $//; s/^ //;
336 0 s/[^\w .-]//g;
337 }
338 }
339  
340 1 ▶ my ($filebase, $ext) = length $filename && $filename =~ m/(.*)\.([^.]+)/
Branch: 1 branch
BranchTrueFalseTotal
length $filename && $filename =~ /(.*)\.([^.]+)/ ? : 0 1 1
Condition40%2/5
length $filename and $filename =~ /(.*)\.([^.]+)/
!ll&&!rl&&r
100
($self->mimeType->extensions)[0] or "raw"
l!l
10
MC/DC0%0/2
length $filename and $filename =~ /(.*)\.([^.]+)/
length $filename $filename =~ /(.*)\.([^.]+)/
MC/DC0%0/1
($self->mimeType->extensions)[0] or "raw"
($self->mimeType->extensions)[0]
Truth table33%1/3
A and B
A= length $filenameB= $filename =~ /(.*)\.([^.]+)/
ABresult
0X0
100
111
Truth table50%1/2
A
A= ($self->mimeType->extensions)[0]
Aresult
11
00
341 ? ($1, $2) : (part => ($self->mimeType->extensions)[0] || 'raw');
342  
343 1 my $fn = File::Spec->catfile($dir, "$filebase.$ext");
344  
345 1 ▶ for(my $unique = 1; -e $fn; $unique++)
Branch: 1 branch
BranchTrueFalseTotal
if (-e $fn) 0 1 1
346 0 { $fn = File::Spec->catfile($dir, "$filebase-$unique.$ext");
347 }
348  
349 1 $fn;
350 }
351  
352 #------------------------------------------
353  
354  
355 13 my %transfer_encoder_classes =
356 ( base64 => 'Mail::Message::TransferEnc::Base64'
357 , binary => 'Mail::Message::TransferEnc::Binary'
358 , '8bit' => 'Mail::Message::TransferEnc::EightBit'
359 , 'quoted-printable' => 'Mail::Message::TransferEnc::QuotedPrint'
360 , '7bit' => 'Mail::Message::TransferEnc::SevenBit'
361 );
362  
363 13 my %transfer_encoders; # they are reused.
364  
365 sub getTransferEncHandler($)
366 46 ▶{ my ($self, $type) = @_;
Subroutine getTransferEncHandler: called CC=4 SCAR=14.0
Pod documented
367  
368 return $transfer_encoders{$type}
369 46 ▶ if exists $transfer_encoders{$type}; # they are reused.
Branch: 1 branch
BranchTrueFalseTotal
if exists $transfer_encoders{$type} 35 11 46
370  
371 11 my $class = $transfer_encoder_classes{$type};
372 11 ▶ return unless $class;
Branch: 1 branch
BranchTrueFalseTotal
unless $class 0 11 11
373  
374 11 eval "require $class";
375 11 ▶ confess "Cannot load $class: $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 11 11
376  
377 11 $transfer_encoders{$type} = $class->new;
378 }
379  
380  
381 sub addTransferEncHandler($$)
382 0 ▶{ my ($this, $name, $what) = @_;
Subroutine addTransferEncHandler: not called CC=2 SCAR=17.9
Pod documented
383  
384 0 my $class;
385 0 ▶ if(ref $what)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $what) { } 0 0 0
386 0 { $transfer_encoders{$name} = $what;
387 0 $class = ref $what;
388 }
389 else
390 0 { delete $transfer_encoders{$name};
391 0 $class = $what;
392 }
393  
394 0 $transfer_encoder_classes{$name} = $class;
395 0 $this;
396 }
397  
398 13 1;
« Message/Head/Complete.pm ↑ Summary Box/Parser/Perl.pm »