Sisimai/Message.pm

filter: Statementstmt 100% 252 / 252 Branchbran 90.6% 107 / 118 Conditioncond 86.8% 53 / 61 MC/DCmcdc 74.2% 26 / 35 Subroutinesub 100% 16 / 16 Podpod 20.0% 1 / 5 totaltotal 93.4% 455 / 487 CC 100 SCAR 30.2
CC
100
Cov
96%
sift
29.3
tidy
26.0
rise
19.1
CRAP 20.6
« Sisimai/Reason.pm ↑ Summary Sisimai/Lhost/EinsUndEins.pm »
1 package Sisimai::Message;
2 89 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 89 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 89 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 89 ▶use Sisimai::RFC1894;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 89 ▶use Sisimai::RFC2045;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 89 ▶use Sisimai::RFC5322;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 89 ▶use Sisimai::RFC5965;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 89 ▶use Sisimai::Address;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 89 ▶use Sisimai::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 89 ▶use Sisimai::Order;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 89 ▶use Sisimai::Lhost;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 89 state $Fields1894 = Sisimai::RFC1894->FIELDINDEX;
15 89 state $Fields5322 = Sisimai::RFC5322->FIELDINDEX;
16 89 state $Fields5965 = Sisimai::RFC5965->FIELDINDEX;
17 89 state $FieldTable = { map { lc $_ => $_ } ($Fields1894->@*, $Fields5322->@*, $Fields5965->@*) };
18 89 state $Boundaries = ["Content-Type: message/rfc822", "Content-Type: text/rfc822-headers"];
19 89 state $MediaTypes = [
20 ["message/xdelivery-status", "message/delivery-status"],
21 ["message/disposition-notification", "message/delivery-status"],
22 ["message/global-delivery-status", "message/delivery-status"],
23 ["message/global-disposition-notification", "message/delivery-status"],
24 ["message/global-delivery-status", "message/delivery-status"],
25 ["message/global-headers", "text/rfc822-headers"],
26 ["message/global", "message/rfc822"],
27 ];
28  
29 89 my $TryOnFirst = [];
30  
31 3529 ▶sub rise {
Subroutine rise: called CC=19 SCAR=29.5
Pod documented
32 # Constructor of Sisimai::Message
33 # @param [Hash] argvs Email text data
34 # @options argvs [String] data Entire email message
35 # @options argvs [Code] hook Reference to callback method
36 # @return [Hash] Structured email data
37 # [Undef] If each value of the arguments are missing
38 3529 my $class = shift;
39 3529 ▶ my $argvs = shift || return undef;
Condition100%2/2
shift() || (return undef)
l!l
35281
MC/DC100%1/1
shift() || (return undef)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
40 3528 ▶ my $email = $argvs->{'data'} || return undef;
Condition100%2/2
$argvs->{'data'} || (return undef)
l!l
35271
MC/DC100%1/1
$argvs->{'data'} || (return undef)
$argvs->{'data'}
Truth table100%2/2
A
A= $argvs->{'data'}
Aresult
11
00
41 3527 my $thing = {'from' => '', 'header' => {}, 'rfc822' => '', 'ds' => [], 'catch' => undef};
42 3527 my $param = {};
43  
44 3527 my $aftersplit = undef;
45 3527 my $beforefact = undef;
46 3527 my $parseagain = 0;
47  
48 3527 ▶ while($parseagain < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parseagain < 2) 3546 0 3546
49 # 1. Split email data to headers and a body part.
50 3546 ▶ last unless $aftersplit = __PACKAGE__->part(\$email);
Branch: 1 branch
BranchTrueFalseTotal
unless $aftersplit = __PACKAGE__->part(\$email) 1 3545 3546
51  
52 # 2. Convert email headers from text to hash reference
53 3545 $thing->{'from'} = $aftersplit->[0];
54 3545 $thing->{'header'} = __PACKAGE__->makemap(\$aftersplit->[1]);
55  
56 # 3. Decode and rewrite the "Subject:" header
57 3545 ▶ if( $thing->{'header'}->{'subject'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($thing->{"header"}{"subject"}) 3527 18 3545
58 # Decode MIME-Encoded "Subject:" header
59 3527 my $cv = $thing->{'header'}->{'subject'};
60 3527 ▶ my $cq = Sisimai::RFC2045->is_encoded(\$cv) ? Sisimai::RFC2045->decodeH([split(/[ ]/, $cv)]) : $cv;
Branch: 1 branch
BranchTrueFalseTotal
'Sisimai::RFC2045'->is_encoded(\$cv) ? : 153 3374 3527
61 3527 my $cl = lc $cq;
62 3527 ▶ my $p1 = index($cl, 'fwd:'); $p1 = index($cl, 'fw:') if $p1 < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $p1 < 0 3517 10 3527
63  
64 # Remove "Fwd:" string from the "Subject:" header
65 3527 ▶ if( $p1 > -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p1 > -1) 30 3497 3527
66 # Delete quoted strings, quote symbols(>)
67 30 $cq = substr($cq, index($cq, ':') + 1,);
68 30 s/^[>][ ]//gm, s/^[>]$//gm for $aftersplit->[2];
69 }
70 3527 $thing->{'header'}->{'subject'} = $cq;
71 }
72  
73 # 4. Rewrite message body for detecting the bounce reason
74 3545 $TryOnFirst = Sisimai::Order->make($thing->{'header'}->{'subject'});
75 3545 ▶ $param = {'hook' => $argvs->{'hook'} || undef, 'mail' => $thing, 'body' => \$aftersplit->[2]};
Condition100%2/2
$argvs->{'hook'} || undef
l!l
7162829
MC/DC100%1/1
$argvs->{'hook'} || undef
$argvs->{'hook'}
Truth table100%2/2
A
A= $argvs->{'hook'}
Aresult
11
00
76 3545 ▶ last if $beforefact = __PACKAGE__->sift(%$param);
Branch: 1 branch
BranchTrueFalseTotal
if $beforefact = __PACKAGE__->sift(%$param) 3485 60 3545
77 60 ▶ last unless grep { index($aftersplit->[2], $_) > -1 } @$Boundaries;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {index($aftersplit->[2], $_) != -1;} @$Boundaries 41 19 60
78  
79 # 5. Try to sift again
80 # There is a bounce message inside of mutipart/*, try to sift the first message/rfc822
81 # part as a entire message body again. rfc3464/1086-a847b090.eml is the email but the
82 # results decodd by sisimai are unstable.
83 19 $parseagain++;
84 19 ▶ my $v = Sisimai::RFC5322->part(\$aftersplit->[2], $Boundaries, 1); last unless $v;
Branch: 1 branch
BranchTrueFalseTotal
unless $v 0 19 19
85 19 $email = $v->[1]; $email =~ s/\A[\r\n\s]+//m;
86 19 ▶ last unless length $email > 128;
Branch: 1 branch
BranchTrueFalseTotal
unless length $email > 128 0 19 19
87 }
88 3527 ▶ return undef unless $beforefact;
Branch: 1 branch
BranchTrueFalseTotal
unless $beforefact 42 3485 3527
89  
90 # 6. Rewrite headers of the original message in the body part
91 3485 $thing->{ $_ } = $beforefact->{ $_ } for ('ds', 'catch', 'rfc822');
92 3485 ▶ my $r = $beforefact->{'rfc822'} || $aftersplit->[2];
Condition66%2/3
$beforefact->{'rfc822'} || $aftersplit->[2]
l!l&&r!l&&!r
33161690
MC/DC0%0/2
$beforefact->{'rfc822'} || $aftersplit->[2]
$beforefact->{'rfc822'} $aftersplit->[2]
Truth table66%2/3
A || B
A= $beforefact->{'rfc822'}B= $aftersplit->[2]
ABresult
1X1
011
000
93 3485 ▶ $thing->{'rfc822'} = ref $r ? $r : __PACKAGE__->makemap(\$r, 1);
Branch: 1 branch
BranchTrueFalseTotal
ref $r ? : 0 3485 3485
94  
95 3485 return $thing;
96 }
97  
98 3548 ▶sub part {
Subroutine part: called CC=12 SCAR=24.9
Pod undocumented
99 # Divide email data up headers and a body part.
100 # @param [String] email Email data
101 # @return [Array] Email data after split
102 # @since v4.14.0
103 3548 my $class = shift;
104 3548 ▶ my $email = shift // return undef;
Condition100%2/2
shift() // (return undef)
l!l
35462
MC/DC100%1/1
shift() // (return undef)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
105 3546 my $parts = ['', '', '']; # 0:From, 1:Header, 2:Body
106  
107 3546 $$email =~ s/\A\s+//m;
108 3546 ▶ $$email =~ s/\r\n/\n/gm if rindex($$email, "\r\n") > -1;
Branch: 1 branch
BranchTrueFalseTotal
if rindex($$email, "\r\n") != -1 775 2771 3546
109  
110 3546 ▶ ($parts->[1], $parts->[2]) = split(/\n\n/, $$email, 2); $parts->[2] ||= "";
Condition100%2/2
$parts->[2] ||= ""
l!l
35451
MC/DC100%1/1
$parts->[2] ||= ""
$parts->[2]
Truth table100%2/2
A
A= $parts->[2]
Aresult
11
00
111 3546 ▶ return undef if $parts->[1] eq "" || $parts->[2] eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $parts->[1] eq "" or $parts->[2] eq "" 1 3545 3546
Condition66%2/3
$parts->[1] eq "" or $parts->[2] eq ""
l!l&&r!l&&!r
013545
MC/DC50%1/2
$parts->[1] eq "" or $parts->[2] eq ""
$parts->[1] eq "" $parts->[2] eq ""
Truth table66%2/3
A or B
A= $parts->[1] eq ""B= $parts->[2] eq ""
ABresult
1X1
011
000
112  
113 3545 ▶ if( substr($parts->[1], 0, 5) eq 'From ' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($parts->[1], 0, 5) eq 'From ') { } 463 3082 3545
114 # From MAILER-DAEMON Tue Feb 11 00:00:00 2014
115 463 $parts->[0] = [split(/\n/, $parts->[1], 2)]->[0];
116 463 $parts->[0] =~ y/\r\n//d;
117  
118 } else {
119 # Set pseudo UNIX From line
120 3082 $parts->[0] = 'MAILER-DAEMON Tue Feb 11 00:00:00 2014';
121 }
122 3545 ▶ $parts->[1] .= "\n" unless substr($parts->[1], -1, 1) eq "\n";
Branch: 1 branch
BranchTrueFalseTotal
unless substr($parts->[1], -1, 1) eq "\n" 3545 0 3545
123  
124 3545 for my $e ('image/', 'application/', 'text/html') {
125 # https://github.com/sisimai/p5-sisimai/issues/492, Reduce email size
126 10635 ▶ my $p0 = 0; my $p1 = 0; my $ep = $e eq 'text/html' ? '</html>' : "--\n";
Branch: 1 branch
BranchTrueFalseTotal
$e eq 'text/html' ? : 3545 7090 10635
127 10635 while(1) {
128 # Remove each part from "Content-Type: image/..." to "--\n" (the end of each boundary)
129 11249 ▶ $p0 = index($parts->[2], 'Content-Type: '.$e, $p0); last if $p0 < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $p0 < 0 10412 837 11249
130 837 ▶ $p1 = index($parts->[2], $ep, $p0 + 32); last if $p1 < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $p1 < 0 223 614 837
131 614 substr($parts->[2], $p0, $p1 - $p0, '');
132 }
133 }
134 3545 $parts->[2] .= "\n";
135 3545 return $parts;
136 }
137  
138 7031 ▶sub makemap {
Subroutine makemap: called CC=18 SCAR=28.9
Pod undocumented
139 # Convert a text including email headers to a hash reference
140 # @param [String] argv0 Email header data
141 # @param [Bool] argv1 Decode "Subject:" header
142 # @return [Hash] Structured email header data
143 # @since v4.25.6
144 7031 my $class = shift;
145 7031 ▶ my $argv0 = shift || return {}; $$argv0 =~ s/^[>]+[ ]//mg; # Remove '>' indent symbols
Condition100%2/2
shift() || (return {})
l!l
70301
MC/DC100%1/1
shift() || (return {})
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
146 7030 ▶ my $argv1 = shift || 0;
Condition100%2/2
shift() || 0
l!l
34853545
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
147  
148 # Select and convert all the headers in $argv0. The following regular expression is based on
149 # https://gist.github.com/xtetsuji/b080e1f5551d17242f6415aba8a00239
150 7030 my $firstpairs = {$$argv0 =~ /^([\w-]+):[ ]*(.*?)\n(?![\s\t])/gms};
151 7030 my $headermaps = {'subject' => ''};
152 7030 $headermaps->{ lc $_ } = $firstpairs->{ $_ } for keys %$firstpairs;
153 7030 my $receivedby = [];
154  
155 7030 for my $e ( values %$headermaps ) { s/\n\s+/ /, y/\t / /s for $e }
156 7030 ▶ if( index($$argv0, "\nReceived:") > 0 || index($$argv0, "Received:") == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($$argv0, "\nReceived:") > 0 or index($$argv0, "Received:") == 0) 6232 798 7030
Condition100%3/3
index($$argv0, "\nReceived:") > 0 or index($$argv0, "Received:") == 0
l!l&&r!l&&!r
5544688798
MC/DC100%2/2
index($$argv0, "\nReceived:") > 0 or index($$argv0, "Received:") == 0
index($$argv0, "\nReceived:") > 0 index($$argv0, "Received:") == 0
Truth table100%3/3
A or B
A= index($$argv0, "\nReceived:") > 0B= index($$argv0, "Received:") == 0
ABresult
1X1
011
000
157 # Capture values of each Received: header
158 6232 my $re = [$$argv0 =~ /^Received:[ ]*(.*?)\n(?![\s\t])/gms];
159 6232 for my $e ( @$re ) {
160 # 1. Exclude the Received header including "(qmail ** invoked from network)".
161 # 2. Convert all consecutive spaces and line breaks into a single space character.
162 11759 ▶ next if index($e, Sisimai::RFC5322->woReceived->[0]) > 0 || index($e, Sisimai::RFC5322->woReceived->[1]) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($e, "Sisimai::RFC5322"->woReceived->[0]) > 0 or index($e, "Sisimai::RFC5322"->woReceived->[1]) > 0 255 11504 11759
Condition100%3/3
index($e, "Sisimai::RFC5322"->woReceived->[0]) > 0 or index($e, "Sisimai::RFC5322"->woReceived->[1]) > 0
l!l&&r!l&&!r
11514011504
MC/DC100%2/2
index($e, "Sisimai::RFC5322"->woReceived->[0]) > 0 or index($e, "Sisimai::RFC5322"->woReceived->[1]) > 0
index($e, "Sisimai::RFC5322"->woReceived->[0]) > 0 index($e, "Sisimai::RFC5322"->woReceived->[1]) > 0
Truth table100%3/3
A or B
A= index($e, "Sisimai::RFC5322"->woReceived->[0]) > 0B= index($e, "Sisimai::RFC5322"->woReceived->[1]) > 0
ABresult
1X1
011
000
163  
164 11504 $e =~ s/\n\s+/ /;
165 11504 $e =~ y/\n\t / /s;
166 11504 push @$receivedby, $e;
167 }
168 }
169 7030 $headermaps->{'received'} = $receivedby;
170  
171 7030 ▶ return $headermaps unless $argv1;
Branch: 1 branch
BranchTrueFalseTotal
unless $argv1 3545 3485 7030
172 3485 ▶ return $headermaps unless length $headermaps->{'subject'};
Branch: 1 branch
BranchTrueFalseTotal
unless length $headermaps->{"subject"} 306 3179 3485
173  
174 # Convert MIME-Encoded subject
175 3179 ▶ if( Sisimai::String->is_8bit(\$headermaps->{'subject'}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('Sisimai::String'->is_8bit(\$headermaps->{'subject'})) { } 46 3133 3179
176 # The value of ``Subject'' header is including multibyte character, is not MIME-Encoded text.
177 46 eval {
178 # Remove invalid byte sequence
179 46 Encode::decode_utf8($headermaps->{'subject'});
180 46 Encode::encode_utf8($headermaps->{'subject'});
181 };
182 46 ▶ $headermaps->{'subject'} = 'MULTIBYTE CHARACTERS HAVE BEEN REMOVED' if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 46 46
183  
184 } else {
185 # MIME-Encoded subject field or ASCII characters only
186 3133 my $r = [];
187 3133 ▶ if( Sisimai::RFC2045->is_encoded(\$headermaps->{'subject'}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('Sisimai::RFC2045'->is_encoded(\$headermaps->{'subject'})) { } 251 2882 3133
188 # split the Subject: field by " "
189 251 for my $v ( split(/ /, $headermaps->{'subject'}) ) {
190 # Insert value to the array if the string is MIME encoded text
191 313 ▶ push @$r, $v if Sisimai::RFC2045->is_encoded(\$v);
Branch: 1 branch
BranchTrueFalseTotal
if "Sisimai::RFC2045"->is_encoded(\$v) 308 5 313
192 }
193 } else {
194 # Subject line is not MIME encoded
195 2882 $r = [$headermaps->{'subject'}];
196 }
197 3133 $headermaps->{'subject'} = Sisimai::RFC2045->decodeH($r);
198 }
199 3179 return $headermaps;
200 }
201  
202 3567 ▶sub tidy {
Subroutine tidy: called CC=26 SCAR=32.6
Pod undocumented
203 # Tidy up each field name and format
204 # @param [String] argv0 Strings including field and value used at an email
205 # @return [String] Strings tidied up
206 # @since v5.0.0
207 3567 my $class = shift;
208 3567 ▶ my $argv0 = shift || return '';
Condition100%2/2
shift() || (return '')
l!l
35661
MC/DC100%1/1
shift() || (return '')
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
209 3566 my $email = '';
210 3566 my @lines = split("\n", $$argv0);
211 3566 my $index = -1;
212  
213 3566 for my $e ( @lines ) {
214 # Find and tidy up fields defined in RFC5322, RFC1894, and RFC5965
215 # 1. Find a field label defined in RFC5322, RFC1894, or RFC5965 from this line
216 222212 my $p0 = index($e, ':');
217 222212 ▶ my $cf = substr(lc $e, 0, $p0); chop $cf if substr($cf, -1, 1) eq ' ';
Branch: 1 branch
BranchTrueFalseTotal
if substr($cf, -1, 1) eq " " 1023 221189 222212
218 222212 ▶ my $fn = $FieldTable->{ $cf } || '';
Condition100%2/2
$FieldTable->{$cf} || ''
l!l
67648154564
MC/DC100%1/1
$FieldTable->{$cf} || ''
$FieldTable->{$cf}
Truth table100%2/2
A
A= $FieldTable->{$cf}
Aresult
11
00
219  
220 # There is neither ":" character nor the field listed in $FieldTable
221 222212 $index++;
222 222212 ▶ if( $fn eq '' ){ $email .= $e."\n"; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($fn eq "") 154564 67648 222212
223  
224 # 2. Tidy up a sub type of each field defined in RFC1894 such as Reporting-MTA: DNS;...
225 67648 my $ab = [];
226 67648 my $bf = substr($e, $p0 + 1,);
227 67648 my $p1 = index($bf, ';');
228 67648 while(1) {
229 # Such as Diagnostic-Code, Remote-MTA, and so on
230 # - Before: Diagnostic-Code: SMTP;550 User unknown
231 # - After: Diagnostic-Code: smtp; 550 User unknown
232 67648 ▶ last unless grep { $fn eq $_ } (@$Fields1894, 'Content-Type');
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$fn eq $_;} @$Fields1894, "Content-Type" 35251 32397 67648
233  
234 32397 ▶ if( $p1 > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p1 > 0) { } 19905 12492 32397
235 # The field including one or more ";"
236 19905 for my $f (split(';', $bf)) {
237 # 2-1. Trim leading and trailing space characters from the current buffer
238 39265 ▶ while( index($f, ' ') == 0 ) { $f = substr($f, 1,) }
Branch: 1 branch
BranchTrueFalseTotal
if (index($f, " ") == 0) 37157 39265 76422
239 39265 ▶ while( substr($f, -1, 1) eq ' '){ $f = substr($f, 0, length($f) - 1) }
Branch: 1 branch
BranchTrueFalseTotal
if (substr($f, -1, 1) eq " ") 61 39265 39326
240 39265 my $ps = '';
241  
242 # 2-2. Convert some parameters to the lower-cased string
243 39265 while(1) {
244 # For example,
245 # - Content-Type: Message/delivery-status => message/delivery-status
246 # - Content-Type: Charset=UTF8 => charset=utf8
247 # - Reporting-MTA: DNS; ... => dns
248 # - Final-Recipient: RFC822; ... => rfc822
249 39265 ▶ last if index($f, ' ') > 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($f, " ") > 0 2326 36939 39265
250  
251 36939 ▶ my $p2 = index($f, '='); if( $p2 > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p2 > 0) 9866 27073 36939
252 # charset=, boundary=, and other pairs divided by "="
253 9866 $ps = lc substr($f, 0, $p2);
254 9866 substr($f, 0, $p2, $ps);
255 }
256 36939 ▶ $f = lc $f if $ps ne 'boundary';
Branch: 1 branch
BranchTrueFalseTotal
if $ps ne "boundary" 31315 5624 36939
257 36939 ▶ $f = 'rfc822' if $f eq 'rfc/822';
Branch: 1 branch
BranchTrueFalseTotal
if $f eq "rfc/822" 10 36929 36939
258 36939 last;
259 }
260 39265 push @$ab, $f;
261 }
262  
263 19905 ▶ if( $fn eq 'Diagnostic-Code' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fn eq "Diagnostic-Code") 2028 17877 19905
264 # Diagnostic-Code: x-unix;
265 # /var/email/kijitora/Maildir/tmp/1000000000.A000000B00000.neko22:
266 # Disk quota exceeded
267 2028 ▶ push @$ab, '' if scalar @$ab == 1 && index($lines[$index + 1], " ") == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$ab == 1 and index($lines[$index + 1], " ") == 0 5 2023 2028
Condition66%2/3
scalar @$ab == 1 and index($lines[$index + 1], " ") == 0
!ll&&!rl&&r
202305
MC/DC50%1/2
scalar @$ab == 1 and index($lines[$index + 1], " ") == 0
scalar @$ab == 1 index($lines[$index + 1], " ") == 0
Truth table66%2/3
A and B
A= scalar @$ab == 1B= index($lines[$index + 1], " ") == 0
ABresult
0X0
100
111
268 }
269 19905 $bf = join('; ', @$ab); $ab = []; # Insert " " (space characer) immediately after ";"
270  
271 } else {
272 # There is no ";" in the field
273 # Arrival-Date, Last-Attempt-Date
274 # X-Original-Message-ID
275 12492 ▶ last if index($fn, '-Date') > 0 || index($fn, '-Message-ID') > 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($fn, "-Date") > 0 or index($fn, "-Message-ID") > 0 3021 9471 12492
Condition100%3/3
index($fn, "-Date") > 0 or index($fn, "-Message-ID") > 0
l!l&&r!l&&!r
28551669471
MC/DC100%2/2
index($fn, "-Date") > 0 or index($fn, "-Message-ID") > 0
index($fn, "-Date") > 0 index($fn, "-Message-ID") > 0
Truth table100%3/3
A or B
A= index($fn, "-Date") > 0B= index($fn, "-Message-ID") > 0
ABresult
1X1
011
000
276 9471 $bf = lc $bf;
277 }
278 29376 last;
279 }
280  
281 # 3. Tidy up a value, and a parameter of Content-Type: field
282 67648 ▶ if( $fn eq "Content-Type" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fn eq "Content-Type") 15400 52248 67648
283 # Replace the value of "Content-Type" field
284 15400 for my $f ( @$MediaTypes ) {
285 # - Before: Content-Type: message/xdelivery-status; ...
286 # - After: Content-Type: message/delivery-status; ...
287 107800 ▶ $p1 = index($bf, $f->[0]); next if $p1 < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $p1 < 0 107794 6 107800
288 6 substr($bf, $p1, length $f->[0], $f->[1]);
289 }
290 }
291  
292 # 4. Concatenate the field name and the field value
293 67648 for my $f ( split(' ', $bf) ) {
294 # Remove redundant space characters
295 172904 ▶ push @$ab, $f if length $f > 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $f > 0 172904 0 172904
296 }
297 67648 $email .= sprintf("%s: %s\n", $fn, join(' ', @$ab));
298 }
299  
300 # 5. Convert the lower-cased SMTP command to the upper-cased.
301 3566 $email =~ s/after end of data:/after end of DATA:/g;
302 3566 ▶ $email .= "\n" if substr($email, -2, 2) ne "\n\n";
Branch: 1 branch
BranchTrueFalseTotal
if substr($email, -2, 2) ne "\n\n" 3566 0 3566
303 3566 return \$email;
304 }
305  
306 3547 ▶sub sift {
Subroutine sift: called CC=29 SCAR=33.8
Pod undocumented
307 # Sift a bounce mail with each MTA module
308 # @param [Hash] argvs Processing message entity.
309 # @param options argvs [Hash] mail Email message entity
310 # @param options mail [String] from From line of mbox
311 # @param options mail [Hash] header Email header data
312 # @param options mail [String] rfc822 Original message part
313 # @param options mail [Array] ds Delivery status list(decoded data)
314 # @param options argvs [String] body Email message body
315 # @param options argvs [Code] hook Hook method to be called
316 # @return [Hash] Decoded and structured bounce mails
317 3547 my $class = shift;
318 3547 my $argvs = { @_ };
319  
320 3547 ▶ my $mailheader = $argvs->{'mail'}->{'header'} || return undef;
Condition100%2/2
$argvs->{'mail'}{'header'} || (return undef)
l!l
35461
MC/DC100%1/1
$argvs->{'mail'}{'header'} || (return undef)
$argvs->{'mail'}{'header'}
Truth table100%2/2
A
A= $argvs->{'mail'}{'header'}
Aresult
11
00
321 3546 ▶ my $bodystring = $argvs->{'body'} || return undef;
Condition100%2/2
$argvs->{'body'} || (return undef)
l!l
35451
MC/DC100%1/1
$argvs->{'body'} || (return undef)
$argvs->{'body'}
Truth table100%2/2
A
A= $argvs->{'body'}
Aresult
11
00
322 3545 ▶ my $hookmethod = $argvs->{'hook'} || undef;
Condition100%2/2
$argvs->{'hook'} || undef
l!l
7162829
MC/DC100%1/1
$argvs->{'hook'} || undef
$argvs->{'hook'}
Truth table100%2/2
A
A= $argvs->{'hook'}
Aresult
11
00
323 3545 my $havecaught = undef;
324  
325 3545 state $defaultset = Sisimai::Order->another;
326 3545 state $lhosttable = Sisimai::Lhost->path;
327  
328 3545 ▶ $mailheader->{'from'} //= '';
Condition100%2/2
$mailheader->{"from"} //= ""
l!l
352223
MC/DC100%1/1
$mailheader->{"from"} //= ""
$mailheader->{"from"}
Truth table100%2/2
A
A= $mailheader->{"from"}
Aresult
11
00
329 3545 ▶ $mailheader->{'subject'} //= '';
Condition50%1/2
$mailheader->{"subject"} //= ""
l!l
35450
MC/DC0%0/1
$mailheader->{"subject"} //= ""
$mailheader->{"subject"}
Truth table50%1/2
A
A= $mailheader->{"subject"}
Aresult
11
00
330 3545 ▶ $mailheader->{'content-type'} //= '';
Condition100%2/2
$mailheader->{"content-type"} //= ""
l!l
2877668
MC/DC100%1/1
$mailheader->{"content-type"} //= ""
$mailheader->{"content-type"}
Truth table100%2/2
A
A= $mailheader->{"content-type"}
Aresult
11
00
331  
332 # Tidy up each field name and value in the entire message body
333 3545 $$bodystring = __PACKAGE__->tidy($bodystring)->$*;
334  
335 # Decode BASE64 Encoded message body
336 3545 ▶ my $mesgformat = lc($mailheader->{'content-type'} || '');
Condition100%2/2
$mailheader->{'content-type'} || ''
l!l
2877668
MC/DC100%1/1
$mailheader->{'content-type'} || ''
$mailheader->{'content-type'}
Truth table100%2/2
A
A= $mailheader->{'content-type'}
Aresult
11
00
337 3545 ▶ my $ctencoding = lc($mailheader->{'content-transfer-encoding'} || '');
Condition100%2/2
$mailheader->{'content-transfer-encoding'} || ''
l!l
6162929
MC/DC100%1/1
$mailheader->{'content-transfer-encoding'} || ''
$mailheader->{'content-transfer-encoding'}
Truth table100%2/2
A
A= $mailheader->{'content-transfer-encoding'}
Aresult
11
00
338  
339 3545 ▶ if( index($mesgformat, 'text/plain') == 0 || index($mesgformat, 'text/html') == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (index($mesgformat, 'text/plain') == 0 or index($mesgformat, 'text/html') == 0) { } 459 3086 3545
elsif (index($mesgformat, 'multipart/') == 0) { } 2418 668 3086
Condition66%2/3
index($mesgformat, "text/plain") == 0 or index($mesgformat, "text/html") == 0
l!l&&r!l&&!r
45903086
MC/DC50%1/2
index($mesgformat, "text/plain") == 0 or index($mesgformat, "text/html") == 0
index($mesgformat, "text/plain") == 0 index($mesgformat, "text/html") == 0
Truth table66%2/3
A or B
A= index($mesgformat, "text/plain") == 0B= index($mesgformat, "text/html") == 0
ABresult
1X1
011
000
340 # Content-Type: text/plain; charset=UTF-8
341 459 ▶ if( $ctencoding eq 'base64' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ctencoding eq 'base64') { } 11 448 459
elsif ($ctencoding eq 'quoted-printable') { } 60 388 448
342 # Content-Transfer-Encoding: base64
343 11 $bodystring = Sisimai::RFC2045->decodeB($bodystring);
344  
345 } elsif( $ctencoding eq 'quoted-printable' ) {
346 # Content-Transfer-Encoding: quoted-printable
347 60 $bodystring = Sisimai::RFC2045->decodeQ($bodystring);
348 }
349  
350 # Content-Type: text/html;...
351 459 ▶ $bodystring = Sisimai::String->to_plain($bodystring, 1) if index($mesgformat, 'text/html') > -1;
Branch: 1 branch
BranchTrueFalseTotal
if index($mesgformat, "text/html") != -1 0 459 459
352  
353 } elsif( index($mesgformat, 'multipart/') == 0 ) {
354 # In case of Content-Type: multipart/*
355 2418 my $p = Sisimai::RFC2045->makeflat($mailheader->{'content-type'}, $bodystring);
356 2418 ▶ $bodystring = $p if defined $p && length $$p;
Branch: 1 branch
BranchTrueFalseTotal
if defined $p and length $$p 2401 17 2418
Condition66%2/3
defined $p and length $$p
!ll&&!rl&&r
0172401
MC/DC50%1/2
defined $p and length $$p
defined $p length $$p
Truth table66%2/3
A and B
A= defined $pB= length $$p
ABresult
0X0
100
111
357 }
358 3545 $$bodystring =~ tr/\r//d;
359 3545 $$bodystring =~ s/\t/ /g;
360 3545 ▶ return undef unless $$bodystring;
Branch: 1 branch
BranchTrueFalseTotal
unless $$bodystring 0 3545 3545
361  
362 3545 ▶ if( ref $hookmethod eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hookmethod eq "CODE") 716 2829 3545
363 # Call hook method
364 716 my $p = {'headers' => $mailheader, 'message' => $$bodystring};
365 716 eval { $havecaught = $hookmethod->($p) };
366 716 ▶ warn sprintf(" ***warning: Something is wrong in hook method 'hook': %s", $@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 716 716
367 }
368  
369 3545 my $haveloaded = {};
370 3545 my $havesifted = undef;
371 3545 my $modulename = '';
372 3545 DECODER: while(1) {
373 # 1. MTA Module Candidates to be tried on first
374 # 2. Sisimai::Lhost::*
375 # 3. Sisimai::RFC3464
376 # 4. Sisimai::ARF
377 # 5. Sisimai::RFC3834
378 3545 TRY_ON_FIRST_AND_DEFAULTS: for my $r ( @$TryOnFirst, @$defaultset ) {
379 # Try MTA module candidates
380 39658 ▶ next if exists $haveloaded->{ $r };
Branch: 1 branch
BranchTrueFalseTotal
if exists $haveloaded->{$r} 3372 36286 39658
381 36286 require $lhosttable->{ $r };
382 36286 $havesifted = $r->inquire($mailheader, $bodystring);
383 36286 $haveloaded->{ $r } = 1;
384 36286 $modulename = $r;
385 36286 ▶ last(DECODER) if $havesifted;
Branch: 1 branch
BranchTrueFalseTotal
if $havesifted 2690 33596 36286
386 }
387  
388 # When the all of Sisimai::Lhost::* modules did not return bounce data, call Sisimai::RFC3464;
389 855 require Sisimai::RFC3464;
390 855 $havesifted = Sisimai::RFC3464->inquire($mailheader, $bodystring);
391 855 ▶ if( $havesifted ){ $modulename = 'RFC3464'; last(DECODER) }
Branch: 1 branch
BranchTrueFalseTotal
if ($havesifted) 739 116 855
392  
393 # Feedback Loop message
394 116 require Sisimai::ARF;
395 116 $havesifted = Sisimai::ARF->inquire($mailheader, $bodystring);
396 116 ▶ if( $havesifted ){ $modulename = "ARF"; last(DECODER) }
Branch: 1 branch
BranchTrueFalseTotal
if ($havesifted) 40 76 116
397  
398 # Try to sift the message as auto reply message defined in RFC3834
399 76 require Sisimai::RFC3834;
400 76 $havesifted = Sisimai::RFC3834->inquire($mailheader, $bodystring);
401 76 ▶ if( $havesifted ){ $modulename = 'RFC3834'; last(DECODER) }
Branch: 1 branch
BranchTrueFalseTotal
if ($havesifted) 16 60 76
402  
403 60 last; # as of now, we have no sample email for coding this block
404  
405 } # End of while(DECODER)
406 3545 ▶ return undef unless $havesifted;
Branch: 1 branch
BranchTrueFalseTotal
unless $havesifted 60 3485 3545
407  
408 3485 $havesifted->{'catch'} = $havecaught;
409 3485 ▶ $modulename =~ s/\A.+:://; $_->{'agent'} ||= $modulename for $havesifted->{'ds'}->@*;
Condition33%1/3
$_->{"agent"} ||= $modulename
l!l&&r!l&&!r
036720
MC/DC0%0/2
$_->{"agent"} ||= $modulename
$_->{"agent"} $modulename
Truth table33%1/3
A ||= B
A= $_->{"agent"}B= $modulename
ABresult
1X1
011
000
410 3485 return $havesifted;
411 }
412  
413 89 1;
414 __END__
« Sisimai/Reason.pm ↑ Summary Sisimai/Lhost/EinsUndEins.pm »