Sisimai/RFC2045.pm

filter: Statementstmt 100% 188 / 188 Branchbran 91.7% 123 / 134 Conditioncond 84.8% 56 / 66 MC/DCmcdc 67.5% 25 / 37 Subroutinesub 100% 16 / 16 Podpod 100% 9 / 9 totaltotal 92.6% 417 / 450 CC 99 SCAR 28.3
CC
99
Cov
95%
makeflat
26.1
levelout
21.4
haircut
20.0
CRAP 16.9
« Sisimai/Lhost/X6.pm ↑ Summary Sisimai/Rhost/GSuite.pm »
1 package Sisimai::RFC2045;
2 90 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 90 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 90 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 90 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 90 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 90 ▶use MIME::QuotedPrint ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 90 ▶use Sisimai::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 10496 ▶sub is_encoded {
Subroutine is_encoded: called CC=5 SCAR=16.1
Pod documented
11 # Check that the argument is MIME-Encoded string or not
12 # @param [String] argv0 String to be checked
13 # @return [Boolean] 0: Not MIME encoded string
14 # 1: MIME encoded string
15 10496 my $class = shift;
16 10496 ▶ my $argv0 = shift || return 0;
Condition100%2/2
shift() || (return 0)
l!l
104951
MC/DC100%1/1
shift() || (return 0)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
17 10495 my $text1 = $$argv0; $text1 =~ y/"//d;
18 10495 my @piece = ($text1);
19 10495 my $mime1 = 0;
20  
21 # Multiple MIME-Encoded strings in a line
22 10495 ▶ @piece = split(' ', $text1) if rindex($text1, ' ') > -1;
Branch: 1 branch
BranchTrueFalseTotal
if rindex($text1, " ") != -1 4373 6122 10495
23 10495 ▶ while( my $e = shift @piece ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = shift @piece) 24571 10495 35066
24 # Check all the string in the array
25 24571 ▶ next unless $e =~ /[ \t]*=[?][-_0-9A-Za-z]+[?][BbQq][?].+[?]=?[ \t]*/;
Branch: 1 branch
BranchTrueFalseTotal
unless $e =~ /[ \t]*=[?][-_0-9A-Za-z]+[?][BbQq][?].+[?]=?[ \t]*/u 23276 1295 24571
26 1295 $mime1 = 1;
27 }
28 10495 return $mime1;
29 }
30  
31 3297 ▶sub decodeH {
Subroutine decodeH: called CC=16 SCAR=27.8
Pod documented
32 # Decode MIME-Encoded string in an email header
33 # @param [Array] argvs Reference to an array including MIME-Encoded text
34 # @return [String] MIME-Decoded text
35 3297 my $class = shift;
36 3297 ▶ my $argvs = shift || return '';
Condition100%2/2
shift() || (return '')
l!l
32961
MC/DC100%1/1
shift() || (return '')
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
37  
38 3296 my $ctxcharset = '';
39 3296 my $qbencoding = '';
40 3296 my @textblocks;
41  
42 3296 ▶ while( my $e = shift @$argvs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = shift @$argvs) 3515 3296 6811
43 # Check and decode each element
44 3515 s/\A[ \t]+//g, s/[ \t]+\z//g, y/"//d for $e;
45  
46 3515 ▶ if( __PACKAGE__->is_encoded(\$e) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (__PACKAGE__->is_encoded(\$e)) { } 496 3019 3515
47 # =?utf-8?B?55m954yr44Gr44KD44KT44GT?=
48 496 ▶ next unless $e =~ m{\A(.*)=[?]([-_0-9A-Za-z]+)[?]([BbQq])[?](.+)[?]=?(.*)\z};
Branch: 1 branch
BranchTrueFalseTotal
unless $e =~ /\A(.*)=[?]([-_0-9A-Za-z]+)[?]([BbQq])[?](.+)[?]=?(.*)\z/u 20 476 496
49 476 ▶ $ctxcharset ||= lc $2;
Condition66%2/3
$ctxcharset ||= lc $2
l!l&&r!l&&!r
644120
MC/DC0%0/2
$ctxcharset ||= lc $2
$ctxcharset lc $2
Truth table66%2/3
A ||= B
A= $ctxcharsetB= lc $2
ABresult
1X1
011
000
50 476 ▶ $qbencoding ||= uc $3;
Condition66%2/3
$qbencoding ||= uc $3
l!l&&r!l&&!r
644120
MC/DC0%0/2
$qbencoding ||= uc $3
$qbencoding uc $3
Truth table66%2/3
A ||= B
A= $qbencodingB= uc $3
ABresult
1X1
011
000
51  
52 476 push @textblocks, $1;
53 476 ▶ push @textblocks, $qbencoding eq 'B'
Branch: 1 branch
BranchTrueFalseTotal
$qbencoding eq 'B' ? : 388 88 476
54 ? MIME::Base64::decode($4)
55 : MIME::QuotedPrint::decode($4);
56 476 $textblocks[-1] =~ y/\r\n//d;
57 476 push @textblocks, $5;
58  
59 } else {
60 3019 ▶ push @textblocks, scalar @textblocks ? ' '.$e : $e;
Branch: 1 branch
BranchTrueFalseTotal
scalar @textblocks ? : 130 2889 3019
61 }
62 }
63 3296 ▶ return '' unless scalar @textblocks;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @textblocks 0 3296 3296
64  
65 3296 my $p = join('', @textblocks);
66 3296 ▶ if( $ctxcharset && $qbencoding ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctxcharset and $qbencoding) 412 2884 3296
Condition66%2/3
$ctxcharset and $qbencoding
!ll&&!rl&&r
28840412
MC/DC50%1/2
$ctxcharset and $qbencoding
$ctxcharset $qbencoding
Truth table66%2/3
A and B
A= $ctxcharsetB= $qbencoding
ABresult
0X0
100
111
67 # utf-8 => utf8
68 412 ▶ $ctxcharset = 'utf8' if $ctxcharset eq 'utf-8';
Branch: 1 branch
BranchTrueFalseTotal
if $ctxcharset eq "utf-8" 284 128 412
69  
70 412 ▶ unless( $ctxcharset eq 'utf8' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ctxcharset eq "utf8") 128 284 412
71 # Characterset is not UTF-8
72 128 eval { Encode::from_to($p, $ctxcharset, 'utf8') };
73 128 ▶ $p = 'FAILED TO CONVERT THE SUBJECT' if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 128 128
74 }
75 }
76 3296 return $p;
77 }
78  
79 47 ▶sub decodeB {
Subroutine decodeB: called CC=3 SCAR=11.0
Pod documented
80 # Decode MIME BASE64 Encoded string
81 # @param [String] argv0 MIME Encoded text
82 # @return [String] MIME-Decoded text
83 47 my $class = shift;
84 47 ▶ my $argv0 = shift // return "";
Condition100%2/2
shift() // (return "")
l!l
461
MC/DC100%1/1
shift() // (return "")
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
85  
86 46 ▶ my $p = $$argv0 =~ m|([+/=0-9A-Za-z\r\n]+)| ? MIME::Base64::decode($1) : '';
Branch: 1 branch
BranchTrueFalseTotal
$$argv0 =~ m[([+/=0-9A-Za-z\r\n]+)]u ? : 46 0 46
87 46 return \$p;
88 }
89  
90 286 ▶sub decodeQ {
Subroutine decodeQ: called CC=3 SCAR=11.0
Pod documented
91 # Decode MIME Quoted-Printable Encoded string
92 # @param [String] argv0 Entire MIME-Encoded text
93 # @param [String] argv1 The value of Content-Type: header
94 # @return [String] MIME Decoded text
95 286 my $class = shift;
96 286 ▶ my $argv0 = shift // return "";
Condition100%2/2
shift() // (return "")
l!l
2851
MC/DC100%1/1
shift() // (return "")
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
97  
98 285 ▶ my $p = MIME::QuotedPrint::decode($$argv0) || '';
Condition100%2/2
&MIME::QuotedPrint::decode($$argv0) || ''
l!l
27411
MC/DC100%1/1
&MIME::QuotedPrint::decode($$argv0) || ''
&MIME::QuotedPrint::decode($$argv0)
Truth table100%2/2
A
A= &MIME::QuotedPrint::decode($$argv0)
Aresult
11
00
99 285 return \$p;
100 }
101  
102 13212 ▶sub parameter {
Subroutine parameter: called CC=7 SCAR=19.5
Pod documented
103 # Find a value of specified field name from Content-Type: header
104 # @param [String] argv0 The value of Content-Type: header
105 # @param [String] argv1 Lower-cased attribute name of the parameter
106 # @return [String] The value of the parameter
107 # @since v5.0.0
108 13212 my $class = shift;
109 13212 ▶ my $argv0 = shift || return "";
Condition100%2/2
shift() || (return '')
l!l
107962416
MC/DC100%1/1
shift() || (return '')
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
110 10796 ▶ my $argv1 = shift || '';
Condition100%2/2
shift() || ''
l!l
46476149
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
111  
112 10796 ▶ my $parameterq = length $argv1 > 0 ? $argv1.'=' : '';
Branch: 1 branch
BranchTrueFalseTotal
length $argv1 > 0 ? : 4647 6149 10796
113 10796 ▶ my $paramindex = length $argv1 > 0 ? index($argv0, $parameterq) : 0;
Branch: 1 branch
BranchTrueFalseTotal
length $argv1 > 0 ? : 4647 6149 10796
114 10796 ▶ return '' if $paramindex == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $paramindex == -1 151 10645 10796
115  
116 # Find the value of the parameter name specified in $argv1
117 10645 my $foundtoken = [split(';', substr($argv0, $paramindex + length($parameterq)), 2)]->[0];
118 10645 ▶ $foundtoken = lc $foundtoken unless $argv1 eq 'boundary';
Branch: 1 branch
BranchTrueFalseTotal
unless $argv1 eq "boundary" 6150 4495 10645
119 10645 $foundtoken =~ y/"'//d;
120 10645 return $foundtoken;
121 }
122  
123 4684 ▶sub boundary {
Subroutine boundary: called CC=6 SCAR=17.9
Pod documented
124 # Get a boundary string
125 # @param [String] argv0 The value of Content-Type header
126 # @param [Integer] start -1: boundary string itself
127 # 0: Start of boundary
128 # 1: End of boundary
129 # @return [String] Boundary string
130 4684 my $class = shift;
131 4684 ▶ my $argv0 = shift || return "";
Condition100%2/2
shift() || (return '')
l!l
464341
MC/DC100%1/1
shift() || (return '')
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
132 4643 ▶ my $start = shift // -1;
Condition100%2/2
shift() // -1
l!l
4517126
MC/DC100%1/1
shift() // -1
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
133 4643 ▶ my $btext = __PACKAGE__->parameter($argv0, 'boundary') || return '';
Condition100%2/2
__PACKAGE__->parameter($argv0, 'boundary') || (return '')
l!l
4494149
MC/DC100%1/1
__PACKAGE__->parameter($argv0, 'boundary') || (return '')
__PACKAGE__->parameter($argv0, 'boundary')
Truth table100%2/2
A
A= __PACKAGE__->parameter($argv0, 'boundary')
Aresult
11
00
134  
135 # Content-Type: multipart/mixed; boundary=Apple-Mail-5--931376066
136 # Content-Type: multipart/report; report-type=delivery-status;
137 # boundary="n6H9lKZh014511.1247824040/mx.example.jp"
138 4494 ▶ $btext = '--'.$btext if $start > -1;
Branch: 1 branch
BranchTrueFalseTotal
if $start > -1 3830 664 4494
139 4494 ▶ $btext = $btext.'--' if $start > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $start > 0 13 4481 4494
140 4494 return $btext;
141 }
142  
143 9190 ▶sub haircut {
Subroutine haircut: called CC=20 SCAR=30.0
Pod documented
144 # Cut header fields except Content-Type, Content-Transfer-Encoding from multipart/* block
145 # @param [String] block multipart/* block text
146 # @param [Boolean] heads 1 = Returns only Content-(Type|Transfer-Encoding) headers
147 # @return [Array] Two headers and body part of multipart/* block
148 # @since v5.0.0
149 9190 my $class = shift;
150 9190 ▶ my $block = shift // return undef;
Condition100%2/2
shift() // (return undef)
l!l
91891
MC/DC100%1/1
shift() // (return undef)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
151 9189 ▶ my $heads = shift // undef;
Condition100%2/2
shift() // undef
l!l
19188
MC/DC100%1/1
shift() // undef
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
152  
153 9189 my($upperchunk, $lowerchunk) = split("\n\n", $$block, 2);
154 9189 ▶ return ['', ''] unless $upperchunk;
Branch: 1 branch
BranchTrueFalseTotal
unless $upperchunk 0 9189 9189
155 9189 ▶ return ['', ''] unless $lowerchunk;
Branch: 1 branch
BranchTrueFalseTotal
unless $lowerchunk 1568 7621 9189
156 7621 ▶ return ['', ''] unless index($upperchunk, 'Content-Type:') > -1;
Branch: 1 branch
BranchTrueFalseTotal
unless index($upperchunk, "Content-Type:") != -1 848 6773 7621
157  
158 6773 my $headerpart = ['', '']; # ["text/plain; charset=iso-2022-jp; ...", "quoted-printable"]
159 6773 my $multipart1 = []; # [@$headerpart, "body"]
160  
161 6773 for my $e ( split("\n", $upperchunk) ) {
162 # Remove fields except Content-Type:, and Content-Transfer-Encoding: in each part
163 # of multipart/* block such as the following:
164 # Date: Thu, 29 Apr 2018 22:22:22 +0900
165 # MIME-Version: 1.0
166 # Message-ID: ...
167 # Content-Transfer-Encoding: quoted-printable
168 # Content-Type: text/plain; charset=us-ascii
169 12791 ▶ if( index($e, 'Content-Type:') == 0 ) {
Branch: 3 branches
BranchTrueFalseTotal
if (index($e, 'Content-Type:') == 0) { } 6773 6018 12791
elsif (index($e, 'Content-Transfer-Encoding:') == 0) { } 1463 4555 6018
elsif (index($e, 'boundary=') != -1 or index($e, 'charset=') != -1) { } 273 4282 4555
170 # Content-Type: ***
171 6773 my $v = [split(' ', $e, 2)]->[-1];
172 6773 ▶ $headerpart->[0] = index($v, 'boundary=') > -1 ? $v : lc $v;
Branch: 1 branch
BranchTrueFalseTotal
index($v, 'boundary=') != -1 ? : 464 6309 6773
173  
174 } elsif( index($e, 'Content-Transfer-Encoding:') == 0 ) {
175 # Content-Transfer-Encoding: ***
176 1463 $headerpart->[1] = lc [split(' ', $e, 2)]->[-1];
177  
178 ▶ } elsif( index($e, 'boundary=') > -1 || index($e, 'charset=') > -1 ) {
Condition100%3/3
index($e, "boundary=") != -1 or index($e, "charset=") != -1
l!l&&r!l&&!r
1631104282
MC/DC100%2/2
index($e, "boundary=") != -1 or index($e, "charset=") != -1
index($e, "boundary=") != -1 index($e, "charset=") != -1
Truth table100%3/3
A or B
A= index($e, "boundary=") != -1B= index($e, "charset=") != -1
ABresult
1X1
011
000
179 # "Content-Type" field has boundary="..." or charset="utf-8"
180 273 ▶ next unless length $headerpart->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless length $headerpart->[0] 0 273 273
181 273 $headerpart->[0] .= " ".$e;
182 273 $headerpart->[0] =~ s/\s\s+/ /g;
183 }
184 }
185 6773 ▶ return $headerpart if $heads;
Branch: 1 branch
BranchTrueFalseTotal
if $heads 1 6772 6773
186  
187 6772 my $mediatypev = lc $headerpart->[0];
188 6772 my $ctencoding = $headerpart->[1];
189 6772 push @$multipart1, @$headerpart, '';
190  
191 6772 UPPER: while(1) {
192 # Make a body part at the 2nd element of $multipart1
193 6772 $multipart1->[2] = sprintf("Content-Type: %s\n", $headerpart->[0]);
194  
195 # Do not append Content-Transfer-Encoding: header when the part is the original message:
196 # Content-Type is message/rfc822 or text/rfc822-headers, or message/delivery-status or
197 # message/feedback-report
198 6772 ▶ last if index($mediatypev, '/rfc822') > -1;
Branch: 1 branch
BranchTrueFalseTotal
if index($mediatypev, "/rfc822") != -1 2194 4578 6772
199 4578 ▶ last if index($mediatypev, '/delivery-status') > -1;
Branch: 1 branch
BranchTrueFalseTotal
if index($mediatypev, "/delivery-status") != -1 2003 2575 4578
200 2575 ▶ last if index($mediatypev, '/feedback-report') > -1;
Branch: 1 branch
BranchTrueFalseTotal
if index($mediatypev, "/feedback-report") != -1 66 2509 2575
201 2509 ▶ last if length $ctencoding == 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $ctencoding == 0 1714 795 2509
202  
203 795 $multipart1->[2] .= sprintf("Content-Transfer-Encoding: %s\n", $ctencoding);
204 795 last;
205 }
206  
207 # Append LF before the lower chunk into the 2nd element of $multipart1
208 6772 ▶ $multipart1->[2] .= "\n" if $lowerchunk ne "" && substr($lowerchunk, 0, 1) ne "\n";
Branch: 1 branch
BranchTrueFalseTotal
if $lowerchunk ne "" and substr($lowerchunk, 0, 1) ne "\n" 6466 306 6772
Condition66%2/3
$lowerchunk ne "" and substr($lowerchunk, 0, 1) ne "\n"
!ll&&!rl&&r
03066466
MC/DC50%1/2
$lowerchunk ne "" and substr($lowerchunk, 0, 1) ne "\n"
$lowerchunk ne "" substr($lowerchunk, 0, 1) ne "\n"
Truth table66%2/3
A and B
A= $lowerchunk ne ""B= substr($lowerchunk, 0, 1) ne "\n"
ABresult
0X0
100
111
209 6772 $multipart1->[2] .= $lowerchunk;
210 6772 return $multipart1;
211 }
212  
213 3044 ▶sub levelout {
Subroutine levelout: called CC=21 SCAR=30.6
Pod documented
214 # Split argv1: multipart/* blocks by a boundary string in argv0
215 # @param [String] argv0 The value of Content-Type header
216 # @param [String] argv1 A pointer to multipart/* message blocks
217 # @param [Integer] depth Depth of MIME parts
218 # @return [Array] List of each part of multipart/*
219 # @since v5.0.0
220 3044 my $class = shift;
221 3044 ▶ my $argv0 = shift || return []; return [] unless length $argv0;
Branch: 1 branch
BranchTrueFalseTotal
unless length $argv0 0 3043 3043
Condition100%2/2
shift() || (return [])
l!l
30431
MC/DC100%1/1
shift() || (return [])
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
222 3043 ▶ my $argv1 = shift || return []; return [] unless length $$argv1;
Branch: 1 branch
BranchTrueFalseTotal
unless length $$argv1 0 3042 3042
Condition100%2/2
shift() || (return [])
l!l
30421
MC/DC100%1/1
shift() || (return [])
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
223 3042 ▶ my $depth = shift // 0; return [] if $depth > 64;
Branch: 1 branch
BranchTrueFalseTotal
if $depth > 64 1 3041 3042
Condition50%1/2
shift() // 0
l!l
30420
MC/DC0%0/1
shift() // 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
224  
225 3041 ▶ my $boundary01 = __PACKAGE__->boundary($argv0, 0) || return [];
Condition100%2/2
__PACKAGE__->boundary($argv0, 0) || (return [])
l!l
302615
MC/DC100%1/1
__PACKAGE__->boundary($argv0, 0) || (return [])
__PACKAGE__->boundary($argv0, 0)
Truth table100%2/2
A
A= __PACKAGE__->boundary($argv0, 0)
Aresult
11
00
226 3026 my $multiparts = [split(/\Q$boundary01\E\n/, $$argv1)];
227 3026 my $partstable = [];
228 3026 my $partsdepth = $depth + 1;
229  
230 # Remove empty or useless preamble and epilogue of multipart/* block
231 3026 ▶ shift @$multiparts if length $multiparts->[0] < 8;
Branch: 1 branch
BranchTrueFalseTotal
if length $multiparts->[0] < 8 1452 1574 3026
232 3026 ▶ return [] if scalar @$multiparts == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$multiparts == 0 2 3024 3026
233 3024 ▶ pop @$multiparts if length $multiparts->[-1] < 8;
Branch: 1 branch
BranchTrueFalseTotal
if length $multiparts->[-1] < 8 184 2840 3024
234  
235 3024 ▶ while( my $e = shift @$multiparts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = shift @$multiparts) 9187 3024 12211
236 # Check each part and breaks up internal multipart/* block
237 9187 my $f = __PACKAGE__->haircut(\$e);
238 9187 ▶ if( index($f->[0], 'multipart/') > -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($f->[0], 'multipart/') != -1) { } 622 8565 9187
239 # There is nested multipart/* block
240 622 ▶ my $boundary02 = __PACKAGE__->boundary($f->[0], -1) || next;
Condition50%1/2
__PACKAGE__->boundary($f->[0], -1) || (next)
l!l
6220
MC/DC0%0/1
__PACKAGE__->boundary($f->[0], -1) || (next)
__PACKAGE__->boundary($f->[0], -1)
Truth table50%1/2
A
A= __PACKAGE__->boundary($f->[0], -1)
Aresult
11
00
241 622 my $bodyinside = [split(/\n\n/, $f->[-1], 2)]->[-1];
242 622 ▶ next if length $bodyinside < 9 || index($bodyinside, $boundary02) < 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $bodyinside < 9 or index($bodyinside, $boundary02) == -1 0 622 622
Condition33%1/3
length $bodyinside < 9 or index($bodyinside, $boundary02) == -1
l!l&&r!l&&!r
00622
MC/DC0%0/2
length $bodyinside < 9 or index($bodyinside, $boundary02) == -1
length $bodyinside < 9 index($bodyinside, $boundary02) == -1
Truth table33%1/3
A or B
A= length $bodyinside < 9B= index($bodyinside, $boundary02) == -1
ABresult
1X1
011
000
243  
244 622 my $v = __PACKAGE__->levelout($f->[0], \$bodyinside, $partsdepth);
245 622 ▶ push @$partstable, @$v if scalar @$v;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$v 557 65 622
246  
247 } else {
248 # The part is not a multipart/* block
249 8565 ▶ my $b = length $f->[-1] ? $f->[-1] : $e;
Branch: 1 branch
BranchTrueFalseTotal
length $f->[-1] ? : 6149 2416 8565
250 8565 ▶ my $v = [$f->[0], $f->[1], length $f->[0] ? [split("\n\n", $b, 2)]->[-1] : $b];
Branch: 1 branch
BranchTrueFalseTotal
length $f->[0] ? : 6149 2416 8565
251 8565 push @$partstable, $v;
252 }
253 }
254 3024 ▶ return [] unless scalar @$partstable;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$partstable 64 2960 3024
255  
256 # Remove $boundary01.'--' and strings from the boundary to the end of the body part.
257 2960 chomp $boundary01;
258 2960 my $b = $partstable->[-1]->[2];
259 2960 my $p = index($b, $boundary01.'--');
260 2960 ▶ substr($partstable->[-1]->[2], $p, length $b, "") if $p > -1;
Branch: 1 branch
BranchTrueFalseTotal
if $p > -1 2422 538 2960
261  
262 2960 return $partstable;
263 }
264  
265 2421 ▶sub makeflat {
Subroutine makeflat: called CC=26 SCAR=32.6
Pod documented
266 # Make flat multipart/* part blocks and decode
267 # @param [String] argv0 The value of Content-Type header
268 # @param [String] argv1 A pointer to multipart/* message blocks
269 # @return [String] Message body
270 2421 my $class = shift;
271 2421 ▶ my $argv0 = shift // return undef;
Condition100%2/2
shift() // (return undef)
l!l
24201
MC/DC100%1/1
shift() // (return undef)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
272 2420 ▶ my $argv1 = shift // return undef;
Condition100%2/2
shift() // (return undef)
l!l
24191
MC/DC100%1/1
shift() // (return undef)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
273 2419 ▶ return undef if index(lc $argv0, 'multipart/') < 0 || index(lc $argv0, 'boundary=') < 0;
Branch: 1 branch
BranchTrueFalseTotal
if index(lc $argv0, "multipart/") == -1 or index(lc $argv0, "boundary=") == -1 0 2419 2419
Condition33%1/3
index(lc $argv0, "multipart/") == -1 or index(lc $argv0, "boundary=") == -1
l!l&&r!l&&!r
002419
MC/DC0%0/2
index(lc $argv0, "multipart/") == -1 or index(lc $argv0, "boundary=") == -1
index(lc $argv0, "multipart/") == -1 index(lc $argv0, "boundary=") == -1
Truth table33%1/3
A or B
A= index(lc $argv0, "multipart/") == -1B= index(lc $argv0, "boundary=") == -1
ABresult
1X1
011
000
274  
275 2419 my $iso2022set = qr/charset=["']?(iso-2022-[-a-z0-9]+)['"]?\b/;
276 2419 my $multiparts = __PACKAGE__->levelout($argv0, $argv1, 0);
277 2419 my $flattenout = '';
278 2419 my $delimiters = ["/delivery-status", "/rfc822", "/feedback-report", "/partial"];
279  
280 2419 ▶ while( my $e = shift @$multiparts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = shift @$multiparts) 8563 2419 10982
281 # Pick only the following parts Sisimai::Lhost will use, and decode each part
282 # - text/plain, text/rfc822-headers
283 # - message/delivery-status, message/rfc822, message/partial, message/feedback-report
284 8563 my $istexthtml = 0;
285 8563 ▶ my $mediatypev = __PACKAGE__->parameter($e->[0]) || 'text/plain';
Condition100%2/2
__PACKAGE__->parameter($e->[0]) || 'text/plain'
l!l
61472416
MC/DC100%1/1
__PACKAGE__->parameter($e->[0]) || 'text/plain'
__PACKAGE__->parameter($e->[0])
Truth table100%2/2
A
A= __PACKAGE__->parameter($e->[0])
Aresult
11
00
286 8563 ▶ next if index($mediatypev, 'text/') != 0 && index($mediatypev, 'message/') != 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($mediatypev, "text/") != 0 and index($mediatypev, "message/") != 0 34 8529 8563
Condition100%3/3
index($mediatypev, "text/") != 0 and index($mediatypev, "message/") != 0
!ll&&!rl&&r
4761376834
MC/DC100%2/2
index($mediatypev, "text/") != 0 and index($mediatypev, "message/") != 0
index($mediatypev, "text/") != 0 index($mediatypev, "message/") != 0
Truth table100%3/3
A and B
A= index($mediatypev, "text/") != 0B= index($mediatypev, "message/") != 0
ABresult
0X0
100
111
287  
288 8529 ▶ if( $mediatypev eq 'text/html' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mediatypev eq "text/html") 12 8517 8529
289 # Skip text/html part when the value of Content-Type: header in an internal part of
290 # multipart/* includes multipart/alternative;
291 12 ▶ next if index($argv0, 'multipart/alternative') > -1;
Branch: 1 branch
BranchTrueFalseTotal
if index($argv0, "multipart/alternative") != -1 0 12 12
292 12 $istexthtml = 1;
293 }
294 8529 ▶ my $ctencoding = $e->[1] || '';
Condition100%2/2
$e->[1] || ''
l!l
14277102
MC/DC100%1/1
$e->[1] || ''
$e->[1]
Truth table100%2/2
A
A= $e->[1]
Aresult
11
00
295 8529 my $bodyinside = $e->[2];
296 8529 my $bodystring = '';
297  
298 8529 ▶ if( length $ctencoding ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $ctencoding) { } 1427 7102 8529
299 # Check the value of Content-Transfer-Encoding: header
300 1427 ▶ if( $ctencoding eq 'base64' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ctencoding eq 'base64') { } 34 1393 1427
elsif ($ctencoding eq 'quoted-printable') { } 222 1171 1393
elsif ($ctencoding eq '7bit') { } 882 289 1171
301 # Content-Transfer-Encoding: base64
302 34 $bodystring = __PACKAGE__->decodeB(\$bodyinside)->$*;
303 34 ▶ my $dontset = 0; while( my $first10 = substr($bodystring, 0, 10) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $first10 = substr($bodystring, 0, 10)) 34 0 34
304 # Don't pick the decoded part as an error message when the part is
305 # - BASE64 encoded.
306 # - the value of the charset is not utf-8.
307 # - NOT a plain text.
308 34 ▶ last if Sisimai::String->aligned(\$e->[0], ['charset', '=', 'utf-8']);
Branch: 1 branch
BranchTrueFalseTotal
if "Sisimai::String"->aligned(\$e->[0], ["charset", "=", "utf-8"]) 24 10 34
309 10 ▶ last unless $first10 =~ /[\x00-\x08\x0E-\x1F\x7F-]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $first10 =~ /[\x00-\x08\x0E-\x1F\x7F-]/u 5 5 10
310 5 $dontset = 1; last;
311 }
312 34 ▶ next if $dontset;
Branch: 1 branch
BranchTrueFalseTotal
if $dontset 5 29 34
313  
314 } elsif( $ctencoding eq 'quoted-printable') {
315 # Content-Transfer-Encoding: quoted-printable
316 222 $bodystring = __PACKAGE__->decodeQ(\$bodyinside)->$*;
317  
318 } elsif( $ctencoding eq '7bit' ) {
319 # Content-Transfer-Encoding: 7bit
320 882 ▶ if( lc $e->[0] =~ $iso2022set ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $e->[0] =~ /$iso2022set/u) { } 67 815 882
321 # Content-Type: text/plain; charset=ISO-2022-JP
322 67 $bodystring = Sisimai::String->to_utf8(\$bodyinside, $1)->$*;
323  
324 } else {
325 # No "charset" parameter in the value of Content-Type: header
326 815 $bodystring = $bodyinside;
327 }
328 } else {
329 # Content-Transfer-Encoding: 8bit, binary, and so on
330 289 $bodystring = $bodyinside;
331 }
332  
333 # Try to delete HTML tags inside of text/html part whenever possible
334 1422 ▶ $bodystring = Sisimai::String->to_plain(\$bodystring)->$* if $istexthtml;
Branch: 1 branch
BranchTrueFalseTotal
if $istexthtml 12 1410 1422
335 1422 ▶ next unless $bodystring;
Branch: 1 branch
BranchTrueFalseTotal
unless $bodystring 11 1411 1422
336 1411 ▶ $bodystring =~ s|\r\n|\n|g if index($bodystring, "\r\n") > -1; # Convert CRLF to LF
Branch: 1 branch
BranchTrueFalseTotal
if index($bodystring, "\r\n") != -1 5 1406 1411
337  
338 } else {
339 # There is no Content-Transfer-Encoding header in the part
340 7102 $bodystring .= $bodyinside;
341 }
342
343 8513 ▶ if( grep { index($mediatypev, $_) > 0 } @$delimiters ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {index($mediatypev, $_) > 0;} @$delimiters) 4268 4245 8513
344 # Add Content-Type: header of each part (will be used as a delimiter at Sisimai::Lhost) into
345 # the body inside when the value of Content-Type: is message/delivery-status, message/rfc822,
346 # or text/rfc822-headers
347 4268 $bodystring = sprintf("Content-Type: %s\n%s", $mediatypev, $bodystring);
348 }
349  
350 # Append "\n" when the last character of $bodystring is not LF
351 8513 ▶ $bodystring .= "\n\n" unless substr($bodystring, -2, 2) eq "\n\n";
Branch: 1 branch
BranchTrueFalseTotal
unless substr($bodystring, -2, 2) eq "\n\n" 333 8180 8513
352 8513 $flattenout .= $bodystring;
353 }
354 2419 return \$flattenout;
355 }
356  
357 90 1;
358 __END__
« Sisimai/Lhost/X6.pm ↑ Summary Sisimai/Rhost/GSuite.pm »