Message/Body/Encode.pm

filter: Statementstmt 80.3% 163 / 203 Branchbran 63.2% 86 / 136 Conditioncond 51.0% 51 / 100 MC/DCmcdc 18.0% 11 / 61 Subroutinesub 86.2% 25 / 29 Podpod 100% 12 / 12 totaltotal 64.3% 348 / 541 CC 101 SCAR 37.1
CC
101
Cov
68%
encode
94.2
dispositionFilename
56.4
unify
20.0
CRAP 40.8
« Message/Replace/MailInternet.pm ↑ Summary Message.pm »
1 # This code is part of Perl distribution Mail-Message version 4.05.
2 # The POD got stripped from this file by OODoc version 3.06.
3 # For contributors see file ChangeLog.
4  
5 # This software is copyright (c) 2001-2026 by Mark Overmeer.
6  
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10  
11  
12 package Mail::Message::Body;{
13 14 our $VERSION = '4.05';
14 }
15  
16 14 ▶use parent 'Mail::Reporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 14 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 14 ▶use Log::Report 'mail-message', import => [ qw/__x error panic warning/ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 14 ▶use MIME::Types ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 14 ▶use File::Basename qw/basename/;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 14 ▶use Encode qw/find_encoding from_to encode_utf8/;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 14 ▶use List::Util qw/first/;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 14 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 14 ▶use Mail::Message::Field ();
Subroutine BEGIN: called CC=1 SCAR=0.0
31 14 ▶use Mail::Message::Field::Full ();
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 # http://www.iana.org/assignments/character-sets
34 14 ▶use Encode::Alias;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 14 define_alias(qr/^unicode-?1-?1-?utf-?([78])$/i => '"UTF-$1"'); # rfc1642
36  
37 14 my $mime_types;
38  
39 #--------------------
40  
41 #--------------------
42  
43 sub charsetDetectAlgorithm(;$)
44 28 ▶{ my $self = shift;
Subroutine charsetDetectAlgorithm: called CC=3 SCAR=11.7
Pod documented
45 28 ▶ $self->{MMBE_det} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 28 28
46 28 ▶ $self->{MMBE_det} || 'charsetDetect';
Branch: 1 branch
BranchTrueFalseTotal
$self->{"MMBE_det"} or "charsetDetect" 0 28 28
47 }
48  
49 #--------------------
50  
51 sub _char_enc($)
52 136 ▶{ my ($self, $charset) = @_;
Subroutine _char_enc: called CC=4 SCAR=14.1
53 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
54  
55 50 ▶ my $enc = find_encoding $charset
Branch: 1 branch
BranchTrueFalseTotal
my $enc = &find_encoding($charset) or &warning(&__x("charset '{name}' is not known.", "name", $charset)) 50 0 50
56 or warning __x"charset '{name}' is not known.", name => $charset;
57  
58 50 $enc;
59 }
60  
61 sub encode(@)
62 71 ▶{ my ($self, %args) = @_;
Subroutine encode: called CC=39 SCAR=45.5
Pod documented
63 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
64  
65 ### The content type
66  
67 71 my $type_from = $self->type;
68 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
69 71 ▶ blessed $type_to
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($type_to) or $type_to = "Mail::Message::Field::Full"->new("Content-Type", $type_to) 71 0 71
70 or $type_to = Mail::Message::Field::Full->new('Content-Type' => $type_to);
71  
72 ### Detect specified transfer-encodings
73  
74 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
75 71 ▶ blessed $transfer
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($transfer) or $transfer = "Mail::Message::Field"->new("Content-Transfer-Encoding", $transfer) 7 64 71
76 or $transfer = Mail::Message::Field->new('Content-Transfer-Encoding' => $transfer);
77  
78 71 my $trans_was = lc $self->transferEncoding;
79 71 my $trans_to = lc $transfer;
80  
81 ### Detect specified charsets
82  
83 71 my $is_text = $type_from =~ m!^text/!i;
84 71 my ($char_was, $char_to, $from, $to);
85  
86 71 ▶ if($is_text)
Branch: 2 branches
BranchTrueFalseTotal
if ($is_text) { } 68 3 71
elsif ($trans_was eq $trans_to) { } 0 3 3
87 68 { $char_was = $type_from->attribute('charset'); # sometimes missing
88 68 $char_to = $type_to->attribute('charset'); # usually missing
89  
90 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
91 { # Explicitly stated output charset
92 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
93 42 { $char_to = $charset;
94 42 $type_to->attribute(charset => $char_to);
95 }
96 }
97 ▶ 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
98 { # By default, do not change charset
99 0 $char_to = $char_was;
100 0 $type_to->attribute(charset => $char_to);
101 }
102  
103 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
104 { # We cannot leave the body into the 'PERL' charset when transfer-encoding is applied.
105 0 warning __x"Content-Transfer-Encoding '{te}' requires explicit charset, defaulted to utf-8.", te => $trans_to;
106 0 $char_to = 'utf-8';
107 }
108  
109 68 $from = $self->_char_enc($char_was);
110 68 $to = $self->_char_enc($char_to);
111  
112 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
113 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
114 { # modify source charset into a different alias
115 0 $type_from->attribute(charset => $char_to);
116 0 $char_was = $char_to;
117 0 $from = $to;
118 }
119  
120 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
121 if $trans_was eq $trans_to && $char_was eq $char_to;
122 }
123 }
124 elsif($trans_was eq $trans_to)
125 { # No changes needed;
126 0 return $self;
127 }
128  
129 ### Apply transfer-decoding
130  
131 71 my $decoded;
132 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
133 62 { $decoded = $self }
134 elsif(my $decoder = $self->getTransferEncHandler($trans_was))
135 9 { $decoded = $decoder->decode($self, result_type => $bodytype) }
136 else
137 0 { warning __x"no decoder defined for transfer encoding '{encoding}'.", encoding => $trans_was;
138 0 return $self;
139 }
140  
141 ### Apply character-set recoding
142  
143 71 my $recoded;
144 71 ▶ if($is_text)
Branch: 1 branch
BranchTrueFalseTotal
if ($is_text) { } 68 3 71
145 68 ▶ { unless($char_was)
Branch: 1 branch
BranchTrueFalseTotal
unless ($char_was) 0 68 68
146 { # When we do not know the character-sets, try to auto-detect
147 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
148 0 $char_was = $decoded->$auto;
149 0 $from = $self->_char_enc($char_was);
150 0 $decoded->type->attribute(charset => $char_was);
151  
152 0 ▶ unless($char_to)
Branch: 1 branch
BranchTrueFalseTotal
unless ($char_to) 0 0 0
153 0 { $char_to = $char_was;
154 0 $type_to->attribute(charset => $char_to);
155 0 $to = $from;
156 }
157 }
158  
159 68 my $text = $decoded->string;
160 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
161 = $to && $char_was eq 'PERL' ? $to->encode($text)
162 : $from && $char_to eq 'PERL' ? $from->decode($text)
163 : $to && $from && $char_was ne $char_to ? $to->encode($from->decode($text))
164 : undef;
165  
166 68 ▶ $recoded
Branch: 1 branch
BranchTrueFalseTotal
$new_data ? : 42 26 68
167 = $new_data
168 ? $bodytype->new(based_on => $decoded, data => $new_data, mime_type => $type_to, checked => 1)
169 : $decoded;
170 }
171 else
172 3 { $recoded = $decoded;
173 }
174  
175 ### Apply transfer-encoding
176  
177 71 my $trans;
178 71 ▶ if($trans_to ne 'none')
Branch: 1 branch
BranchTrueFalseTotal
if ($trans_to ne "none") 36 35 71
179 36 ▶ { $trans = $self->getTransferEncHandler($trans_to)
Branch: 1 branch
BranchTrueFalseTotal
$trans = $self->getTransferEncHandler($trans_to) or &warning(&__x("no encoder defined for transfer encoding '{encoding}'.", "encoding", $trans_to)) 36 0 36
180 or warning __x"no encoder defined for transfer encoding '{encoding}'.", encoding => $trans_to;
181 }
182  
183 71 ▶ defined $trans ? $trans->encode($recoded, result_type => $bodytype) : $recoded;
Branch: 1 branch
BranchTrueFalseTotal
defined $trans ? : 36 35 71
184 }
185  
186  
187 sub charsetDetect(%)
188 28 ▶{ my ($self, %args) = @_;
Subroutine charsetDetect: called CC=7 SCAR=21.8
Pod documented
189 28 my $text = $self->string;
190  
191 # Flagged as UTF8, so certainly created by the Perl program itself:
192 # the content is not octets.
193 28 ▶ if(utf8::is_utf8($text))
Branch: 1 branch
BranchTrueFalseTotal
if (utf8::is_utf8($text)) 6 22 28
194 6 ▶ { $args{external} or return 'PERL';
Branch: 1 branch
BranchTrueFalseTotal
$args{"external"} or return "PERL" 6 0 6
195 6 $text = encode_utf8 $text;
196 }
197  
198 # Only look for normal characters, first 1920 unicode characters
199 # When there is any octet in 'utf-encoding'-space, but not an
200 # legal utf8, than it's not utf8.
201 28 ▶ return 'utf-8'
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ / [\xC0-\xDF][\x80-\xBF] # 110xxxxx, 10xxxxxx | [\xE0-\xEF][\x80-\xBF]{2} # 1110xxxx, (10xxxxxx)\302\262 | [\xF0-\xF7][\x80-\xBF]{3} # 11110xxx, (10xxxxxx)3 /x and not $text =~ /[\xC0-\xFF][^\x80-\xBF]/ and not $text =~ /[\xC0-\xFF]\z/ 0 28 28
Condition33%2/6
$text =~ / [\xC0-\xDF][\x80-\xBF] # 110xxxxx, 10xxxxxx | [\xE0-\xEF][\x80-\xBF]{2} # 1110xxxx, (10xxxxxx)\302\262 | [\xF0-\xF7][\x80-\xBF]{3} # 11110xxx, (10xxxxxx)3 /x and not $text =~ /[\xC0-\xFF][^\x80-\xBF]/ and not $text =~ /[\xC0-\xFF]\z/
!ll&&!rl&&r
2800
$text =~ / [\xC0-\xDF][\x80-\xBF] # 110xxxxx, 10xxxxxx | [\xE0-\xEF][\x80-\xBF]{2} # 1110xxxx, (10xxxxxx)\302\262 | [\xF0-\xF7][\x80-\xBF]{3} # 11110xxx, (10xxxxxx)3 /x and not $text =~ /[\xC0-\xFF][^\x80-\xBF]/
!ll&&!rl&&r
2800
MC/DC0%0/3
$text =~ / [\xC0-\xDF][\x80-\xBF] # 110xxxxx, 10xxxxxx | [\xE0-\xEF][\x80-\xBF]{2} # 1110xxxx, (10xxxxxx)\302\262 | [\xF0-\xF7][\x80-\xBF]{3} # 11110xxx, (10xxxxxx)3 /x and not $text =~ /[\xC0-\xFF][^\x80-\xBF]/ and not $text =~ /[\xC0-\xFF]\z/
$text =~ / [\xC0-\xDF][\x80-\xBF] # 110xxxxx, 10xxxxxx | [\xE0-\xEF][\x80-\xBF]{2} # 1110xxxx, (10xxxxxx)\302\262 | [\xF0-\xF7][\x80-\xBF]{3} # 11110xxx, (10xxxxxx)3 /x not $text =~ /[\xC0-\xFF][^\x80-\xBF]/ not $text =~ /[\xC0-\xFF]\z/
Truth table25%1/4
A and not(B) and not(C)
A= $text =~ / [\xC0-\xDF][\x80-\xBF] # 110xxxxx, 10xxxxxx | [\xE0-\xEF][\x80-\xBF]{2} # 1110xxxx, (10xxxxxx)\302\262 | [\xF0-\xF7][\x80-\xBF]{3} # 11110xxx, (10xxxxxx)3 /xB= not $text =~ /[\xC0-\xFF][^\x80-\xBF]/C= not $text =~ /[\xC0-\xFF]\z/
ABCresult
0XX0
10X0
1100
1111
202 if $text =~ m/ [\xC0-\xDF][\x80-\xBF] # 110xxxxx, 10xxxxxx
203 | [\xE0-\xEF][\x80-\xBF]{2} # 1110xxxx, (10xxxxxx)²
204 | [\xF0-\xF7][\x80-\xBF]{3} # 11110xxx, (10xxxxxx)3
205 /x
206 && $text !~ m/[\xC0-\xFF][^\x80-\xBF]/
207 && $text !~ m/[\xC0-\xFF]\z/;
208  
209 # Produce 'us-ascii' when it suffices: it is the RFC compliant
210 # default charset.
211 28 ▶ $text =~ m/[\x80-\xFF]/ ? 'cp1252' : 'us-ascii';
Branch: 1 branch
BranchTrueFalseTotal
$text =~ /[\x80-\xFF]/ ? : 0 28 28
212 }
213  
214  
215 sub check()
216 23 ▶{ my $self = shift;
Subroutine check: called CC=4 SCAR=14.0
Pod documented
217 23 ▶ return $self if $self->checked;
Branch: 1 branch
BranchTrueFalseTotal
if $self->checked 22 1 23
218 1 my $eol = $self->eol;
219  
220 1 my $encoding = $self->transferEncoding->body;
221 1 ▶ return $self->eol($eol)
Branch: 1 branch
BranchTrueFalseTotal
if $encoding eq "none" 0 1 1
222 if $encoding eq 'none';
223  
224 1 my $encoder = $self->getTransferEncHandler($encoding);
225 1 ▶ my $checked = $encoder ? $encoder->check($self)->eol($eol) : $self->eol($eol);
Branch: 1 branch
BranchTrueFalseTotal
$encoder ? : 1 0 1
226 1 $checked->checked(1);
227 1 $checked;
228 }
229  
230  
231 sub encoded(%)
232 57 ▶{ my ($self, %args) = @_;
Subroutine encoded: called CC=10 SCAR=23.2
Pod documented
233 57 my $mime = $self->mimeType;
234  
235 57 ▶ if($mime->isBinary)
Branch: 1 branch
BranchTrueFalseTotal
if ($mime->isBinary) 4 53 57
236 4 ▶ { return $self->transferEncoding eq 'none' ? $self->encode(transfer_encoding => $mime->encoding) : $self->check;
Branch: 1 branch
BranchTrueFalseTotal
$self->transferEncoding eq 'none' ? : 3 1 4
237 }
238  
239 53 ▶ my $charset = my $old_charset = $self->charset || '';
Condition50%1/2
$self->charset || ''
l!l
530
MC/DC0%0/1
$self->charset || ''
$self->charset
Truth table50%1/2
A
A= $self->charset
Aresult
11
00
240 53 ▶ if(!$charset || $charset eq 'PERL')
Branch: 1 branch
BranchTrueFalseTotal
if (not $charset or $charset eq "PERL") 28 25 53
Condition66%2/3
not $charset or $charset eq "PERL"
l!l&&r!l&&!r
02825
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
241 28 ▶ { my $auto = $args{charset_detect} || $self->charsetDetectAlgorithm;
Condition33%1/3
$args{'charset_detect'} || $self->charsetDetectAlgorithm
l!l&&r!l&&!r
0280
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
242 28 $charset = $self->$auto(external => 1);
243 }
244  
245 53 my $enc_was = $self->transferEncoding;
246 53 ▶ my $enc = $enc_was eq 'none' ? $mime->encoding : $enc_was;
Branch: 1 branch
BranchTrueFalseTotal
$enc_was eq 'none' ? : 29 24 53
247  
248 53 ▶ $enc_was eq $enc && $old_charset eq $charset
Branch: 1 branch
BranchTrueFalseTotal
$enc_was eq $enc && $old_charset eq $charset ? : 22 31 53
Condition100%3/3
$enc_was eq $enc and $old_charset eq $charset
!ll&&!rl&&r
29222
MC/DC100%2/2
$enc_was eq $enc and $old_charset eq $charset
$enc_was eq $enc $old_charset eq $charset
Truth table100%3/3
A and B
A= $enc_was eq $encB= $old_charset eq $charset
ABresult
0X0
100
111
249 ? $self->check
250 : $self->encode(transfer_encoding => $enc, charset => $charset);
251 }
252  
253  
254 14 ▶my $native_eol = $^O =~ m/^win/i ? 'CRLF' : $^O =~ m/^mac/i ? 'CR' : 'LF';
Branch: 2 branches
BranchTrueFalseTotal
$^O =~ /^win/i ? : 0 14 14
$^O =~ /^mac/i ? : 0 14 14
255  
256 sub eol(;$)
257 4 ▶{ my $self = shift;
Subroutine eol: called CC=12 SCAR=26.7
Pod documented
258 4 ▶ my $old_eol = $self->{MMBE_eol} ||= $native_eol;
Condition66%2/3
$self->{"MMBE_eol"} ||= $native_eol
l!l&&r!l&&!r
130
MC/DC0%0/2
$self->{"MMBE_eol"} ||= $native_eol
$self->{"MMBE_eol"} $native_eol
Truth table66%2/3
A ||= B
A= $self->{"MMBE_eol"}B= $native_eol
ABresult
1X1
011
000
259 4 ▶ @_ or return $old_eol;
Branch: 1 branch
BranchTrueFalseTotal
@_ or return $old_eol 3 1 4
260  
261 3 my $eol = shift;
262 3 ▶ $eol = $native_eol if $eol eq 'NATIVE';
Branch: 1 branch
BranchTrueFalseTotal
if $eol eq "NATIVE" 0 3 3
263  
264 3 ▶ $eol ne $old_eol || !$self->checked
Branch: 1 branch
BranchTrueFalseTotal
$eol ne $old_eol or not $self->checked or return $self 3 0 3
Condition66%2/3
$eol ne $old_eol or not $self->checked
l!l&&r!l&&!r
210
MC/DC0%0/2
$eol ne $old_eol or not $self->checked
$eol ne $old_eol not $self->checked
Truth table66%2/3
A or not(B)
A= $eol ne $old_eolB= not $self->checked
ABresult
1X1
011
000
265 or return $self;
266  
267 3 my $lines = $self->lines;
268  
269 my $wrong
270 1001 ▶ = $eol eq 'CRLF' ? first { !/\015\012$/ } @$lines
Subroutine __ANON__: called CC=1 SCAR=0.0
271 0 ▶ : $eol eq 'CR' ? first { !/\015$/ } @$lines
Subroutine __ANON__: not called CC=1 SCAR=6.9
272 3 ▶ : $eol eq 'LF' ? first { /\015\012$|\015$/ } @$lines
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 3 branches
BranchTrueFalseTotal
$eol eq 'CRLF' ? : 2 1 3
$eol eq 'CR' ? : 0 1 1
$eol eq 'LF' ? : 1 0 1
273 : error(__x"unknown line terminator '{eol}'.", eol => $eol);
274  
275 3 ▶ $wrong
Branch: 1 branch
BranchTrueFalseTotal
$wrong or return $self 1 2 3
276 or return $self;
277  
278 1 ▶ my $expect = $eol eq 'CRLF' ? "\015\012" : $eol eq 'CR' ? "\015" : "\012";
Branch: 2 branches
BranchTrueFalseTotal
$eol eq 'CRLF' ? : 1 0 1
$eol eq 'CR' ? : 0 0 0
279 1 my @new = map s/[\015\012]+$/$expect/r, @$lines;
280 1 (ref $self)->new(based_on => $self, eol => $eol, data => \@new);
281 }
282  
283  
284 sub unify($)
285 0 ▶{ my ($self, $body) = @_;
Subroutine unify: not called CC=4 SCAR=30.0
Pod documented
286 0 ▶ return $self if $self==$body;
Branch: 1 branch
BranchTrueFalseTotal
if $self == $body 0 0 0
287  
288 0 my $mime = $self->type;
289 0 my $transfer = $self->transferEncoding;
290 0 my $encoded = $body->encode(mime_type => $mime, transfer_encoding => $transfer);
291  
292 # Encode makes the best of it, but is it good enough?
293 0 ▶ $mime eq $encoded->type && $transfer eq $encoded->transferEncoding ? $encoded : undef;
Branch: 1 branch
BranchTrueFalseTotal
$mime eq $encoded->type && $transfer eq $encoded->transferEncoding ? : 0 0 0
Condition0%0/3
$mime eq $encoded->type and $transfer eq $encoded->transferEncoding
!ll&&!rl&&r
000
MC/DC0%0/2
$mime eq $encoded->type and $transfer eq $encoded->transferEncoding
$mime eq $encoded->type $transfer eq $encoded->transferEncoding
Truth table0%0/3
A and B
A= $mime eq $encoded->typeB= $transfer eq $encoded->transferEncoding
ABresult
0X0
100
111
294 }
295  
296 #--------------------
297  
298 sub isBinary()
299 11 ▶{ my $self = shift;
Subroutine isBinary: called CC=4 SCAR=14.5
Pod documented
300 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
830
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
301 11 ▶ my $type = $self->type or return 1;
Branch: 1 branch
BranchTrueFalseTotal
my $type = $self->type or return 1 11 0 11
302 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 11 0 11
303 11 $mime->isBinary;
304 }
305  
306  
307 0 ▶sub isText() { not shift->isBinary }
Subroutine isText: not called CC=1 SCAR=6.9
Pod documented
308  
309  
310 sub dispositionFilename(;$)
311 1 ▶{ my $self = shift;
Subroutine dispositionFilename: called CC=19 SCAR=40.3
Pod documented
312 1 my $raw;
313  
314 my $field;
315 1 ▶ if($field = $self->disposition)
Branch: 1 branch
BranchTrueFalseTotal
if ($field = $self->disposition) 1 0 1
316 1 ▶ { $field = $field->study if $field->can('study');
Branch: 1 branch
BranchTrueFalseTotal
if $field->can("study") 1 0 1
317 1 ▶ $raw = $field->attribute('filename') || $field->attribute('file') || $field->attribute('name');
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
318 }
319  
320 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
321 1 ▶ { $field = $field->study if $field->can('study');
Branch: 1 branch
BranchTrueFalseTotal
if $field->can("study") 1 0 1
322 1 ▶ $raw = $field->attribute('filename') || $field->attribute('file') || $field->attribute('name');
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
323 }
324  
325 1 my $base;
326 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
327 elsif(index($raw, '?') >= 0)
328 0 { eval 'require Mail::Message::Field::Full';
329 0 $base = Mail::Message::Field::Full->decode($raw);
330 }
331 else
332 0 { $base = $raw;
333 }
334  
335 1 ▶ @_ or return $base;
Branch: 1 branch
BranchTrueFalseTotal
@_ or return $base 1 0 1
336  
337 1 my $dir = shift;
338 1 my $filename = '';
339 1 ▶ if(defined $base) # RFC6266 section 4.3, very safe
Branch: 1 branch
BranchTrueFalseTotal
if (defined $base) 0 1 1
340 0 { $filename = basename $base =~ s/\s+/ /gr =~ s/ $//r =~ s/^ //r =~ s/[^\w .-]//gr;
341 }
342  
343 1 ▶ my ($filebase, $ext)
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
344 = length $filename && $filename =~ m/(.*)\.([^.]+)/ ? ($1, $2)
345 : (part => ($self->mimeType->extensions)[0] || 'raw');
346  
347 1 my $fn = File::Spec->catfile($dir, "$filebase.$ext");
348  
349 1 ▶ for(my $unique = 1; -e $fn; $unique++)
Branch: 1 branch
BranchTrueFalseTotal
if (-e $fn) 0 1 1
350 0 { $fn = File::Spec->catfile($dir, "$filebase-$unique.$ext");
351 }
352  
353 1 $fn;
354 }
355  
356 #--------------------
357  
358 14 my %transfer_encoder_classes = (
359 'base64' => 'Mail::Message::TransferEnc::Base64',
360 'binary' => 'Mail::Message::TransferEnc::Binary',
361 '8bit' => 'Mail::Message::TransferEnc::EightBit',
362 'quoted-printable' => 'Mail::Message::TransferEnc::QuotedPrint',
363 '7bit' => 'Mail::Message::TransferEnc::SevenBit',
364 );
365  
366 14 my %transfer_encoders; # they are reused.
367  
368 sub getTransferEncHandler($)
369 46 ▶{ my ($self, $type) = @_;
Subroutine getTransferEncHandler: called CC=4 SCAR=14.0
Pod documented
370  
371 return $transfer_encoders{$type}
372 46 ▶ if exists $transfer_encoders{$type}; # they are reused.
Branch: 1 branch
BranchTrueFalseTotal
if exists $transfer_encoders{$type} 35 11 46
373  
374 11 ▶ my $class = $transfer_encoder_classes{$type}
Branch: 1 branch
BranchTrueFalseTotal
my $class = $transfer_encoder_classes{$type} or return 11 0 11
375 or return;
376  
377 11 eval "require $class";
378 11 ▶ panic "Cannot load $class: $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 11 11
379  
380 11 $transfer_encoders{$type} = $class->new;
381 }
382  
383  
384 sub addTransferEncHandler($$)
385 0 ▶{ my ($this, $name, $what) = @_;
Subroutine addTransferEncHandler: not called CC=2 SCAR=17.9
Pod documented
386  
387 0 my $class;
388 0 ▶ if(ref $what)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $what) { } 0 0 0
389 0 { $transfer_encoders{$name} = $what;
390 0 $class = ref $what;
391 }
392 else
393 0 { delete $transfer_encoders{$name};
394 0 $class = $what;
395 }
396  
397 0 $transfer_encoder_classes{$name} = $class;
398 0 $this;
399 }
400  
401 14 1;
« Message/Replace/MailInternet.pm ↑ Summary Message.pm »