Sisimai/RFC5322.pm

filter: Statementstmt 100% 112 / 112 Branchbran 89.7% 61 / 68 Conditioncond 95.8% 46 / 48 MC/DCmcdc 92.3% 24 / 26 Subroutinesub 100% 11 / 11 Podpod 66.6% 2 / 3 totaltotal 95.5% 256 / 268 CC 61 SCAR 35.5
CC
61
Cov
96%
received
48.1
part
12.0
HEADERFIELDS
3.0
CRAP 34.8
« Sisimai/Lhost/TrendMicro.pm ↑ Summary Sisimai/Lhost/qmail.pm »
1 package Sisimai::RFC5322;
2 93 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 93 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 93 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 93 ▶use Sisimai::RFC791;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 93 ▶use Sisimai::Address;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 93 ▶use constant HEADERTABLE => {
Subroutine BEGIN: called CC=1 SCAR=0.0
9 'messageid' => ['message-id'],
10 'subject' => ['subject'],
11 'listid' => ['list-id'],
12 'date' => [qw|date posted-date posted resent-date|],
13 'addresser' => [qw|from return-path reply-to errors-to reverse-path x-postfix-sender envelope-from x-envelope-from|],
14 'recipient' => [qw|to delivered-to forward-path envelope-to x-envelope-to resent-to apparently-to|],
15 93 };
16 93 ▶use constant FIELDINDEX => [
Subroutine BEGIN: called CC=1 SCAR=0.0
17 # The following fields are not referred in Sisimai
18 # Resent-From Resent-Sender Resent-Cc Cc Bcc Resent-Bcc In-Reply-To References
19 # Comments Keywords
20 qw|Resent-Date From Sender Reply-To To Message-ID Subject Return-Path Received Date X-Mailer
21 Content-Type Content-Transfer-Encoding Content-Description Content-Disposition|
22 93 ];
23 # The part of "Received:" headers generated by qmail or qmail-clone
24 # - Received: (qmail 2202 invoked from network); 15 Oct 2015 06:22:22 -0000
25 # - Received: (qmail 2220 invoked by uid 2); 17 Jul 2014 08:30:40 -0000
26 # Do not exclude the following strings:
27 # - Received: (qmail 2204 invoked for bounce); 29 Apr 2010 00:00:00 -000
28 93 ▶use constant woReceived => [" invoked by uid", " invoked from network"];
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 2 ▶sub HEADERFIELDS {
Subroutine HEADERFIELDS: called CC=3 SCAR=11.0
Pod undocumented
31 # Grouped RFC822 headers
32 # @param [String] group RFC822 Header group name
33 # @return [Array] RFC822 Header list
34 2 my $class = shift;
35 2 ▶ my $group = shift || return [];
Condition50%1/2
shift() || (return [])
l!l
20
MC/DC0%0/1
shift() || (return [])
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
36 2 ▶ return HEADERTABLE->{ $group } if exists HEADERTABLE->{ $group };
Branch: 1 branch
BranchTrueFalseTotal
if exists {"addresser" => ["from","return-path","reply-to","errors-to","reverse-path","x-postfix-sender","envelope-from","x-envelope-from"],"date" => ["date","posted-date","posted","resent-date"],"listid" => ["list-id"],"messageid" => ["message-id"],"recipient" => ["to","delivered-to","forward-path","envelope-to","x-envelope-to","resent-to","apparently-to"],"subject" => ["subject"]}->{$group} 1 1 2
37 1 return [];
38 }
39  
40 4530 ▶sub received {
Subroutine received: called CC=48 SCAR=38.7
Pod documented
41 # Convert Received headers to a structured data
42 # @param [String] argv1 Received header
43 # @return [Array] Each item in the Received header order by the following:
44 # 0: (from) "hostname"
45 # 1: (by) "hostname"
46 # 2: (via) "protocol/tcp"
47 # 3: (with) "protocol/smtp"
48 # 4: (id) "queue-id"
49 # 5: (for) "envelope-to address"
50 4530 my $class = shift;
51 4530 ▶ my $argv1 = shift || return []; return [] if ref $argv1;
Branch: 1 branch
BranchTrueFalseTotal
if ref $argv1 1 4523 4524
Condition100%2/2
shift() || (return [])
l!l
45246
MC/DC100%1/1
shift() || (return [])
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
52 4523 ▶ return [] if index($argv1, woReceived->[0]) > 0 || index($argv1, woReceived->[1]) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($argv1, [" invoked by uid"," invoked from network"]->[0]) > 0 or index($argv1, [" invoked by uid"," invoked from network"]->[1]) > 0 2 4521 4523
Condition100%3/3
index($argv1, [" invoked by uid"," invoked from network"]->[0]) > 0 or index($argv1, [" invoked by uid"," invoked from network"]->[1]) > 0
l!l&&r!l&&!r
114521
MC/DC100%2/2
index($argv1, [" invoked by uid"," invoked from network"]->[0]) > 0 or index($argv1, [" invoked by uid"," invoked from network"]->[1]) > 0
index($argv1, [" invoked by uid"," invoked from network"]->[0]) > 0 index($argv1, [" invoked by uid"," invoked from network"]->[1]) > 0
Truth table100%3/3
A or B
A= index($argv1, [" invoked by uid"," invoked from network"]->[0]) > 0B= index($argv1, [" invoked by uid"," invoked from network"]->[1]) > 0
ABresult
1X1
011
000
53  
54 # - https://datatracker.ietf.org/doc/html/rfc5322
55 # received = "Received:" *received-token ";" date-time CRLF
56 # received-token = word / angle-addr / addr-spec / domain
57 #
58 # - Appendix A.4. Message with Trace Fields
59 # Received:
60 # from x.y.test
61 # by example.net
62 # via TCP
63 # with ESMTP
64 # id ABC12345
65 # for <mary@example.net>; 21 Nov 1997 10:05:43 -0600
66 4521 my $recvd = [split(' ', $argv1)];
67 4521 my $label = [qw|from by via with id for|];
68 4521 my $token = {};
69 4521 my $other = [];
70 4521 my $alter = [];
71 4521 my $right = 0;
72 4521 my $range = scalar @$recvd;
73 4521 my $index = -1;
74  
75 4521 for my $e ( @$recvd ) {
76 # Look up each label defined in $label from Received header
77 80138 ▶ last unless ++$index < $range; my $f = lc $e;
Branch: 1 branch
BranchTrueFalseTotal
unless ++$index < $range 0 80138 80138
78 80138 ▶ next unless grep { $f eq $_ } @$label;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$f eq $_;} @$label 61376 18762 80138
79  
80 18762 ▶ $token->{ $f } = $recvd->[$index + 1] || next;
Condition100%2/2
$$recvd[$index + 1] || (next)
l!l
1875111
MC/DC100%1/1
$$recvd[$index + 1] || (next)
$$recvd[$index + 1]
Truth table100%2/2
A
A= $$recvd[$index + 1]
Aresult
11
00
81 18751 $token->{ $f } = lc $token->{ $f };
82 18751 $token->{ $f } =~ y/();//d;
83  
84 18751 ▶ next unless $f eq 'from';
Branch: 1 branch
BranchTrueFalseTotal
unless $f eq "from" 14963 3788 18751
85 3788 ▶ last unless $index + 2 < $range;
Branch: 1 branch
BranchTrueFalseTotal
unless $index + 2 < $range 5 3783 3788
86 3783 ▶ next unless index($recvd->[$index + 2], '(') == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless index($$recvd[$index + 2], "(") == 0 587 3196 3783
87  
88 # Get and keep a hostname in the comment as follows:
89 # from mx1.example.com (c213502.kyoto.example.ne.jp [192.0.2.135]) by mx.example.jp (V8/cf)
90 # [
91 # "from", # index + 0
92 # "mx1.example.com", # index + 1
93 # "(c213502.kyoto.example.ne.jp", # index + 2
94 # "[192.0.2.135])", # index + 3
95 # "by",
96 # "mx.example.jp",
97 # "(V8/cf)",
98 # ...
99 # ]
100 # The 2nd element after the current element is NOT a continuation of the current element
101 # such as "(c213502.kyoto.example.ne.jp)"
102 3196 push @$other, $recvd->[$index + 2]; $other->[0] =~ y/();//d;
103  
104 # The 2nd element after the current element is a continuation of the current element.
105 # such as "(c213502.kyoto.example.ne.jp", "[192.0.2.135])"
106 3196 ▶ last unless $index + 3 < $range;
Branch: 1 branch
BranchTrueFalseTotal
unless $index + 3 < $range 0 3196 3196
107 3196 push @$other, $recvd->[$index + 3];
108 3196 $other->[1] =~ y/();//d;
109 }
110  
111 4521 for my $e ( @$other ) {
112 # Check alternatives in $other, and then delete uninformative values.
113 6392 ▶ next if length $e < 4 || $e eq 'unknown';
Branch: 1 branch
BranchTrueFalseTotal
if length $e < 4 or $e eq "unknown" 983 5409 6392
Condition100%3/3
length $e < 4 or $e eq "unknown"
l!l&&r!l&&!r
7612225409
MC/DC100%2/2
length $e < 4 or $e eq "unknown"
length $e < 4 $e eq "unknown"
Truth table100%3/3
A or B
A= length $e < 4B= $e eq "unknown"
ABresult
1X1
011
000
114 5409 ▶ next if $e eq 'localhost' || $e eq '[127.0.0.1]' || $e eq '[IPv6:::1]';
Branch: 1 branch
BranchTrueFalseTotal
if $e eq "localhost" or $e eq "[127.0.0.1]" or $e eq "[IPv6:::1]" 862 4547 5409
Condition100%6/6
$e eq "localhost" or $e eq "[127.0.0.1]" or $e eq "[IPv6:::1]"
l!l&&r!l&&!r
85754547
$e eq "localhost" or $e eq "[127.0.0.1]"
l!l&&r!l&&!r
3614964552
MC/DC100%3/3
$e eq "localhost" or $e eq "[127.0.0.1]" or $e eq "[IPv6:::1]"
$e eq "localhost" $e eq "[127.0.0.1]" $e eq "[IPv6:::1]"
Truth table100%4/4
A or B or C
A= $e eq "localhost"B= $e eq "[127.0.0.1]"C= $e eq "[IPv6:::1]"
ABCresult
1XX1
01X1
0011
0000
115 4547 ▶ next if index($e, '.') == -1 || index($e, '=') > 1;
Branch: 1 branch
BranchTrueFalseTotal
if index($e, ".") == -1 or index($e, "=") > 1 578 3969 4547
Condition100%3/3
index($e, ".") == -1 or index($e, "=") > 1
l!l&&r!l&&!r
552263969
MC/DC100%2/2
index($e, ".") == -1 or index($e, "=") > 1
index($e, ".") == -1 index($e, "=") > 1
Truth table100%3/3
A or B
A= index($e, ".") == -1B= index($e, "=") > 1
ABresult
1X1
011
000
116 3969 push @$alter, $e;
117 }
118  
119 4521 for my $e ('from', 'by') {
120 # Remove square brackets from the IP address such as "[192.0.2.25]"
121 9042 ▶ next unless defined $token->{ $e };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token->{$e} 879 8163 9042
122 8163 ▶ next unless length $token->{ $e };
Branch: 1 branch
BranchTrueFalseTotal
unless length $token->{$e} 0 8163 8163
123 8163 ▶ next unless index($token->{ $e }, '[') == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless index($token->{$e}, "[") == 0 7928 235 8163
124 235 ▶ $token->{ $e } = shift Sisimai::RFC791->find($token->{ $e })->@* || '';
Condition100%2/2
shift @{'Sisimai::RFC791'->find($token->{$e})} || ''
l!l
22411
MC/DC100%1/1
shift @{'Sisimai::RFC791'->find($token->{$e})} || ''
shift @{'Sisimai::RFC791'->find($token->{$e})}
Truth table100%2/2
A
A= shift @{'Sisimai::RFC791'->find($token->{$e})}
Aresult
11
00
125 }
126  
127 4521 ▶ $token->{'from'} ||= '';
Condition100%2/2
$token->{"from"} ||= ""
l!l
3777744
MC/DC100%1/1
$token->{"from"} ||= ""
$token->{"from"}
Truth table100%2/2
A
A= $token->{"from"}
Aresult
11
00
128 4521 while(1) {
129 # Prefer hostnames over IP addresses, except for localhost.localdomain and similar.
130 4521 ▶ last if $token->{'from'} eq 'localhost' || $token->{'from'} eq 'localhost.localdomain';
Branch: 1 branch
BranchTrueFalseTotal
if $token->{"from"} eq "localhost" or $token->{"from"} eq "localhost.localdomain" 387 4134 4521
Condition100%3/3
$token->{"from"} eq "localhost" or $token->{"from"} eq "localhost.localdomain"
l!l&&r!l&&!r
377104134
MC/DC100%2/2
$token->{"from"} eq "localhost" or $token->{"from"} eq "localhost.localdomain"
$token->{"from"} eq "localhost" $token->{"from"} eq "localhost.localdomain"
Truth table100%3/3
A or B
A= $token->{"from"} eq "localhost"B= $token->{"from"} eq "localhost.localdomain"
ABresult
1X1
011
000
131 4134 ▶ last if index($token->{'from'}, '.') < 0; # A hostname without a domain name
Branch: 1 branch
BranchTrueFalseTotal
if index($token->{"from"}, ".") == -1 1186 2948 4134
132 2948 ▶ last if scalar Sisimai::RFC791->find($token->{'from'})->@*;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{"Sisimai::RFC791"->find($token->{"from"})} 415 2533 2948
133  
134 # No need to rewrite $token->{'from'}
135 2533 $right = 1;
136 2533 last;
137 }
138 4521 while(1) {
139 # Try to rewrite uninformative hostnames and IP addresses in $token->{'from'}
140 4521 ▶ last if $right; # There is no need to rewrite
Branch: 1 branch
BranchTrueFalseTotal
if $right 2533 1988 4521
141 1988 ▶ last if scalar @$alter == 0; # There is no alternative to rewriting
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$alter == 0 1404 584 1988
142 584 ▶ last if index($alter->[0], $token->{'from'}) > -1;
Branch: 1 branch
BranchTrueFalseTotal
if index($alter->[0], $token->{"from"}) != -1 226 358 584
143  
144 358 ▶ if( index($token->{'from'}, 'localhost') == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (index($token->{'from'}, 'localhost') == 0) { } 25 333 358
elsif (index($token->{'from'}, '.') == -1) { } 80 253 333
145 # localhost or localhost.localdomain
146 25 $token->{'from'} = $alter->[0];
147  
148 } elsif( index($token->{'from'}, '.') == -1 ) {
149 # A hostname without a domain name such as "mail", "mx", or "mbox"
150 80 ▶ $token->{'from'} = $alter->[0] if index($alter->[0], '.') > 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($alter->[0], ".") > 0 80 0 80
151  
152 } else {
153 # An IPv4 address
154 253 $token->{'from'} = $alter->[0];
155 }
156 358 last;
157 }
158  
159 4521 ▶ delete $token->{'by'} unless defined $token->{'by'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token->{"by"} 146 4375 4521
160 4521 ▶ delete $token->{'from'} unless defined $token->{'from'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token->{"from"} 0 4521 4521
161 4521 ▶ $token->{'for'} = Sisimai::Address->s3s4($token->{'for'}) if exists $token->{'for'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $token->{"for"} 2941 1580 4521
162 4521 for my $e ( keys %$token ) {
163 # Delete an invalid value
164 19414 ▶ $token->{ $e } = '' if index($token->{ $e }, ' ') > -1;
Branch: 1 branch
BranchTrueFalseTotal
if index($token->{$e}, " ") != -1 0 19414 19414
165 19414 $token->{ $e } =~ y/[]//d; # Remove "[]" from the IP address
166 }
167  
168 return [
169 $token->{'from'} || '',
170 $token->{'by'} || '',
171 $token->{'via'} || '',
172 $token->{'with'} || '',
173 $token->{'id'} || '',
174 4521 ▶ $token->{'for'} || '',
Condition100%12/12
$token->{'from'} || ''
l!l
3777744
$token->{'by'} || ''
l!l
4375146
$token->{'via'} || ''
l!l
614460
$token->{'with'} || ''
l!l
3838683
$token->{'id'} || ''
l!l
3678843
$token->{'for'} || ''
l!l
29411580
MC/DC100%1/1
$token->{'from'} || ''
$token->{'from'}
MC/DC100%1/1
$token->{'by'} || ''
$token->{'by'}
MC/DC100%1/1
$token->{'via'} || ''
$token->{'via'}
MC/DC100%1/1
$token->{'with'} || ''
$token->{'with'}
MC/DC100%1/1
$token->{'id'} || ''
$token->{'id'}
MC/DC100%1/1
$token->{'for'} || ''
$token->{'for'}
Truth table100%2/2
A
A= $token->{'from'}
Aresult
11
00
Truth table100%2/2
A
A= $token->{'by'}
Aresult
11
00
Truth table100%2/2
A
A= $token->{'via'}
Aresult
11
00
Truth table100%2/2
A
A= $token->{'with'}
Aresult
11
00
Truth table100%2/2
A
A= $token->{'id'}
Aresult
11
00
Truth table100%2/2
A
A= $token->{'for'}
Aresult
11
00
175 ];
176 }
177  
178 4480 ▶sub part {
Subroutine part: called CC=12 SCAR=24.9
Pod documented
179 # Split given entire message body into error message lines and the original message part only
180 # include email headers
181 # @param [String] email Entire message body
182 # @param [Array] cutby List of strings which is a boundary of the original message part
183 # @param [Bool] keeps Flag for keeping strings after "\n\n"
184 # @return [Array] [Error message lines, The original message]
185 # @since v5.0.0
186 4480 my $class = shift;
187 4480 ▶ my $email = shift || return undef;
Condition100%2/2
shift() || (return undef)
l!l
44791
MC/DC100%1/1
shift() || (return undef)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
188 4479 ▶ my $cutby = shift || return undef;
Condition100%2/2
shift() || (return undef)
l!l
44781
MC/DC100%1/1
shift() || (return undef)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
189 4478 ▶ my $keeps = shift // 0;
Condition100%2/2
shift() // 0
l!l
334445
MC/DC100%1/1
shift() // 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
190  
191 4478 my $boundaryor = ''; # A boundary string divides the error message part and the original message part
192 4478 my $positionor = -1; # A Position of the boundary string
193 4478 my $formerpart = ''; # The error message part
194 4478 my $latterpart = ''; # The original message part
195  
196 4478 for my $e ( @$cutby ) {
197 # Find a boundary string(2nd argument) from the 1st argument
198 6403 ▶ $positionor = index($$email, $e); next if $positionor == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $positionor == -1 2941 3462 6403
199 3462 $boundaryor = $e;
200 3462 last;
201 }
202  
203 4478 ▶ if( $positionor > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($positionor > 0) { } 3417 1061 4478
204 # There is the boundary string in the message body
205 3417 $formerpart = substr($$email, 0, $positionor);
206 3417 ▶ $latterpart = substr($$email, ($positionor + length($boundaryor) + 1), ) || '';
Condition50%1/2
substr($$email, $positionor + length($boundaryor) + 1) || ''
l!l
34170
MC/DC0%0/1
substr($$email, $positionor + length($boundaryor) + 1) || ''
substr($$email, $positionor + length($boundaryor) + 1)
Truth table50%1/2
A
A= substr($$email, $positionor + length($boundaryor) + 1)
Aresult
11
00
207  
208 } else {
209 # Substitute the entire message to the former part when the boundary string is not included
210 # the $$email
211 1061 $formerpart = $$email;
212 }
213  
214 4478 ▶ if( length $latterpart > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $latterpart > 0) 3417 1061 4478
215 # Remove blank lines, the message body of the original message, and append "\n" at the end
216 # of the original message headers
217 # 1. Remove leading blank lines
218 # 2. Remove text after the first blank line: \n\n
219 # 3. Append "\n" at the end of test block when the last character is not "\n"
220 3417 $latterpart =~ s/\A[\r\n\s]+//m;
221 3417 ▶ if( $keeps == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keeps == 0) 3397 20 3417
222 # Remove text after the first blank line: \n\n when $keeps is 0
223 3397 ▶ substr($latterpart, index($latterpart, "\n\n") + 1, length($latterpart), '') if index($latterpart, "\n\n");
Branch: 1 branch
BranchTrueFalseTotal
if index $latterpart, "\n\n" 3397 0 3397
224 }
225 3417 ▶ $latterpart .= "\n" unless substr($latterpart, -1, 1) eq "\n";
Branch: 1 branch
BranchTrueFalseTotal
unless substr($latterpart, -1, 1) eq "\n" 27 3390 3417
226 }
227 4478 return [$formerpart, $latterpart];
228 }
229  
230 93 1;
231 __END__
« Sisimai/Lhost/TrendMicro.pm ↑ Summary Sisimai/Lhost/qmail.pm »