| 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)
MC/DC100%1/1
shift() || (return 0)
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 23 | 10495 | ▶ | while( my $e = shift @piece ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | # Check all the string in the array | ||||||||||||||||||||||||||||
| 25 | 24571 | ▶ | next unless $e =~ /[ \t]*=[?][-_0-9A-Za-z]+[?][BbQq][?].+[?]=?[ \t]*/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 '')
MC/DC100%1/1
shift() || (return '')
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 3296 | my $ctxcharset = ''; | |||||||||||||||||||||||||||
| 39 | 3296 | my $qbencoding = ''; | |||||||||||||||||||||||||||
| 40 | 3296 | my @textblocks; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 3296 | ▶ | while( my $e = shift @$argvs ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 47 | # =?utf-8?B?55m954yr44Gr44KD44KT44GT?= | ||||||||||||||||||||||||||||
| 48 | 496 | ▶ | next unless $e =~ m{\A(.*)=[?]([-_0-9A-Za-z]+)[?]([BbQq])[?](.+)[?]=?(.*)\z}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 476 | ▶ | $ctxcharset ||= lc $2; | ||||||||||||||||||||||||||
|
Condition66%2/3
$ctxcharset ||= lc $2
MC/DC0%0/2
$ctxcharset ||= lc $2
$ctxcharset
lc $2
Truth table66%2/3
A ||= B
A= $ctxcharsetB= lc $2
| |||||||||||||||||||||||||||||
| 50 | 476 | ▶ | $qbencoding ||= uc $3; | ||||||||||||||||||||||||||
|
Condition66%2/3
$qbencoding ||= uc $3
MC/DC0%0/2
$qbencoding ||= uc $3
$qbencoding
uc $3
Truth table66%2/3
A ||= B
A= $qbencodingB= uc $3
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 476 | push @textblocks, $1; | |||||||||||||||||||||||||||
| 53 | 476 | ▶ | push @textblocks, $qbencoding eq 'B' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | 3296 | ▶ | return '' unless scalar @textblocks; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 3296 | my $p = join('', @textblocks); | |||||||||||||||||||||||||||
| 66 | 3296 | ▶ | if( $ctxcharset && $qbencoding ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$ctxcharset and $qbencoding
MC/DC50%1/2
$ctxcharset and $qbencoding
$ctxcharset
$qbencoding
Truth table66%2/3
A and B
A= $ctxcharsetB= $qbencoding
| |||||||||||||||||||||||||||||
| 67 | # utf-8 => utf8 | ||||||||||||||||||||||||||||
| 68 | 412 | ▶ | $ctxcharset = 'utf8' if $ctxcharset eq 'utf-8'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 412 | ▶ | unless( $ctxcharset eq 'utf8' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 "")
MC/DC100%1/1
shift() // (return "")
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 46 | ▶ | my $p = $$argv0 =~ m|([+/=0-9A-Za-z\r\n]+)| ? MIME::Base64::decode($1) : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 "")
MC/DC100%1/1
shift() // (return "")
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 285 | ▶ | my $p = MIME::QuotedPrint::decode($$argv0) || ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
&MIME::QuotedPrint::decode($$argv0) || ''
MC/DC100%1/1
&MIME::QuotedPrint::decode($$argv0) || ''
&MIME::QuotedPrint::decode($$argv0)
Truth table100%2/2
A
A= &MIME::QuotedPrint::decode($$argv0)
| |||||||||||||||||||||||||||||
| 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 '')
MC/DC100%1/1
shift() || (return '')
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 110 | 10796 | ▶ | my $argv1 = shift || ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || ''
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 10796 | ▶ | my $parameterq = length $argv1 > 0 ? $argv1.'=' : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 10796 | ▶ | my $paramindex = length $argv1 > 0 ? index($argv0, $parameterq) : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 10796 | ▶ | return '' if $paramindex == -1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 '')
MC/DC100%1/1
shift() || (return '')
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 132 | 4643 | ▶ | my $start = shift // -1; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() // -1
MC/DC100%1/1
shift() // -1
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 133 | 4643 | ▶ | my $btext = __PACKAGE__->parameter($argv0, 'boundary') || return ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
__PACKAGE__->parameter($argv0, 'boundary') || (return '')
MC/DC100%1/1
__PACKAGE__->parameter($argv0, 'boundary') || (return '')
__PACKAGE__->parameter($argv0, 'boundary')
Truth table100%2/2
A
A= __PACKAGE__->parameter($argv0, 'boundary')
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 139 | 4494 | ▶ | $btext = $btext.'--' if $start > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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)
MC/DC100%1/1
shift() // (return undef)
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 151 | 9189 | ▶ | my $heads = shift // undef; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() // undef
MC/DC100%1/1
shift() // undef
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 9189 | my($upperchunk, $lowerchunk) = split("\n\n", $$block, 2); | |||||||||||||||||||||||||||
| 154 | 9189 | ▶ | return ['', ''] unless $upperchunk; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 9189 | ▶ | return ['', ''] unless $lowerchunk; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 7621 | ▶ | return ['', ''] unless index($upperchunk, 'Content-Type:') > -1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||
| 179 | # "Content-Type" field has boundary="..." or charset="utf-8" | ||||||||||||||||||||||||||||
| 180 | 273 | ▶ | next unless length $headerpart->[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 273 | $headerpart->[0] .= " ".$e; | |||||||||||||||||||||||||||
| 182 | 273 | $headerpart->[0] =~ s/\s\s+/ /g; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | 6773 | ▶ | return $headerpart if $heads; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 199 | 4578 | ▶ | last if index($mediatypev, '/delivery-status') > -1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | 2575 | ▶ | last if index($mediatypev, '/feedback-report') > -1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 2509 | ▶ | last if length $ctencoding == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
$lowerchunk ne "" and substr($lowerchunk, 0, 1) ne "\n"
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"
| |||||||||||||||||||||||||||||
| 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
Condition100%2/2
shift() || (return [])
MC/DC100%1/1
shift() || (return [])
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 222 | 3043 | ▶ | my $argv1 = shift || return []; return [] unless length $$argv1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
shift() || (return [])
MC/DC100%1/1
shift() || (return [])
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 223 | 3042 | ▶ | my $depth = shift // 0; return [] if $depth > 64; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
shift() // 0
MC/DC0%0/1
shift() // 0
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 3041 | ▶ | my $boundary01 = __PACKAGE__->boundary($argv0, 0) || return []; | ||||||||||||||||||||||||||
|
Condition100%2/2
__PACKAGE__->boundary($argv0, 0) || (return [])
MC/DC100%1/1
__PACKAGE__->boundary($argv0, 0) || (return [])
__PACKAGE__->boundary($argv0, 0)
Truth table100%2/2
A
A= __PACKAGE__->boundary($argv0, 0)
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 232 | 3026 | ▶ | return [] if scalar @$multiparts == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 233 | 3024 | ▶ | pop @$multiparts if length $multiparts->[-1] < 8; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 3024 | ▶ | while( my $e = shift @$multiparts ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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)
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)
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
length $bodyinside < 9 or index($bodyinside, $boundary02) == -1
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
| |||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | 622 | my $v = __PACKAGE__->levelout($f->[0], \$bodyinside, $partsdepth); | |||||||||||||||||||||||||||
| 245 | 622 | ▶ | push @$partstable, @$v if scalar @$v; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | } else { | ||||||||||||||||||||||||||||
| 248 | # The part is not a multipart/* block | ||||||||||||||||||||||||||||
| 249 | 8565 | ▶ | my $b = length $f->[-1] ? $f->[-1] : $e; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 250 | 8565 | ▶ | my $v = [$f->[0], $f->[1], length $f->[0] ? [split("\n\n", $b, 2)]->[-1] : $b]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 251 | 8565 | push @$partstable, $v; | |||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||
| 254 | 3024 | ▶ | return [] unless scalar @$partstable; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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)
MC/DC100%1/1
shift() // (return undef)
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 272 | 2420 | ▶ | my $argv1 = shift // return undef; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() // (return undef)
MC/DC100%1/1
shift() // (return undef)
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 273 | 2419 | ▶ | return undef if index(lc $argv0, 'multipart/') < 0 || index(lc $argv0, 'boundary=') < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
index(lc $argv0, "multipart/") == -1 or index(lc $argv0, "boundary=") == -1
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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'
MC/DC100%1/1
__PACKAGE__->parameter($e->[0]) || 'text/plain'
__PACKAGE__->parameter($e->[0])
Truth table100%2/2
A
A= __PACKAGE__->parameter($e->[0])
| |||||||||||||||||||||||||||||
| 286 | 8563 | ▶ | next if index($mediatypev, 'text/') != 0 && index($mediatypev, 'message/') != 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
index($mediatypev, "text/") != 0 and index($mediatypev, "message/") != 0
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
| |||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | 8529 | ▶ | if( $mediatypev eq 'text/html' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 292 | 12 | $istexthtml = 1; | |||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||
| 294 | 8529 | ▶ | my $ctencoding = $e->[1] || ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$e->[1] || ''
MC/DC100%1/1
$e->[1] || ''
$e->[1]
Truth table100%2/2
A
A= $e->[1]
| |||||||||||||||||||||||||||||
| 295 | 8529 | my $bodyinside = $e->[2]; | |||||||||||||||||||||||||||
| 296 | 8529 | my $bodystring = ''; | |||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | 8529 | ▶ | if( length $ctencoding ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 299 | # Check the value of Content-Transfer-Encoding: header | ||||||||||||||||||||||||||||
| 300 | 1427 | ▶ | if( $ctencoding eq 'base64' ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 309 | 10 | ▶ | last unless $first10 =~ /[\x00-\x08\x0E-\x1F\x7F-]/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 310 | 5 | $dontset = 1; last; | |||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||
| 312 | 34 | ▶ | next if $dontset; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 335 | 1422 | ▶ | next unless $bodystring; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 336 | 1411 | ▶ | $bodystring =~ s|\r\n|\n|g if index($bodystring, "\r\n") > -1; # Convert CRLF to LF | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 352 | 8513 | $flattenout .= $bodystring; | |||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||
| 354 | 2419 | return \$flattenout; | |||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | 90 | 1; | |||||||||||||||||||||||||||
| 358 | __END__ | ||||||||||||||||||||||||||||