Sisimai/RFC1894.pm

filter: Statementstmt 97.3% 72 / 74 Branchbran 84.3% 27 / 32 Conditioncond 72.0% 18 / 25 MC/DCmcdc 53.8% 7 / 13 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 87.2% 137 / 157 CC 30 SCAR 25.3
CC
30
Cov
89%
field
19.7
match
10.1
label
3.2
CRAP 12.6
« Sisimai/Rhost/NTTDOCOMO.pm ↑ Summary Sisimai/Lhost/OpenSMTPD.pm »
1 package Sisimai::RFC1894;
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 Sisimai::Eb;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 90 ▶use Sisimai::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 90 ▶use constant FIELDINDEX => [qw|
Subroutine BEGIN: called CC=1 SCAR=0.0
9 Action Arrival-Date Diagnostic-Code Final-Recipient Last-Attempt-Date Original-Recipient
10 Received-From-MTA Remote-MTA Reporting-MTA Status X-Actual-Recipient X-Original-Message-ID
11 90 |];
12 90 ▶use constant FIELDTABLE => {
Subroutine BEGIN: called CC=1 SCAR=0.0
13 # Return pairs that a field name and key name defined in Sisimai::Lhost class
14 'action' => 'action',
15 'arrival-date' => 'date',
16 'diagnostic-code' => 'diagnosis',
17 'final-recipient' => 'recipient',
18 'last-attempt-date' => 'date',
19 'original-recipient'=> 'alias',
20 'received-from-mta' => 'lhost',
21 'remote-mta' => 'rhost',
22 'reporting-mta' => 'lhost',
23 'status' => 'status',
24 'x-actual-recipient'=> 'alias',
25 90 };
26  
27 27735 ▶sub match {
Subroutine match: called CC=10 SCAR=23.1
Pod documented
28 # Check the argument matches with a field defined in RFC3464
29 # @param [String] argv0 A line including field and value defined in RFC3464
30 # @return [Integer] 0: did not matched, 1,2: matched
31 # @since v4.25.0
32 27735 my $class = shift;
33 27735 ▶ my $argv0 = shift || return 0;
Condition50%1/2
shift() || (return 0)
l!l
277350
MC/DC0%0/1
shift() || (return 0)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
34 27735 ▶ my $label = __PACKAGE__->label($argv0) || return 0;
Condition50%1/2
__PACKAGE__->label($argv0) || (return 0)
l!l
277350
MC/DC0%0/1
__PACKAGE__->label($argv0) || (return 0)
__PACKAGE__->label($argv0)
Truth table50%1/2
A
A= __PACKAGE__->label($argv0)
Aresult
11
00
35 27735 my $match = 0;
36  
37 27735 state $fieldnames = [
38 # https://tools.ietf.org/html/rfc3464#section-2.2
39 # Some fields of a DSN apply to all of the delivery attempts described by that DSN. At
40 # most, these fields may appear once in any DSN. These fields are used to correlate the
41 # DSN with the original message transaction and to provide additional information which
42 # may be useful to gateways.
43 #
44 # The following fields (not defined in RFC 3464) are used in Sisimai
45 # - X-Original-Message-ID: <....> (GSuite)
46 #
47 # The following fields are not used in Sisimai:
48 # - Original-Envelope-Id
49 # - DSN-Gateway
50 {
51 'arrival-date' => ':',
52 'received-from-mta' => ';',
53 'reporting-mta' => ';',
54 'x-original-message-id' => '@',
55 },
56  
57 # https://tools.ietf.org/html/rfc3464#section-2.3
58 # A DSN contains information about attempts to deliver a message to one or more recipi-
59 # ents. The delivery information for any particular recipient is contained in a group of
60 # contiguous per-recipient fields. Each group of per-recipient fields is preceded by a
61 # blank line.
62 #
63 # The following fields (not defined in RFC 3464) are used in Sisimai
64 # - X-Actual-Recipient: RFC822; ....
65 #
66 # The following fields are not used in Sisimai:
67 # - Will-Retry-Until
68 # - Final-Log-ID
69 {
70 'action' => 'e',
71 'diagnostic-code' => ';',
72 'final-recipient' => ';',
73 'last-attempt-date' => ':',
74 'original-recipient' => ';',
75 'remote-mta' => ';',
76 'status' => '.',
77 'x-actual-recipient' => ';',
78 },
79 ];
80  
81 27735 FIELDS0: for my $e ( keys $fieldnames->[0]->%* ) {
82 # Per-Message fields
83 106293 ▶ next unless $label eq $e;
Branch: 1 branch
BranchTrueFalseTotal
unless $label eq $e 101924 4369 106293
84 4369 ▶ next unless index($argv0, $fieldnames->[0]->{ $label }) > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless index($argv0, $fieldnames->[0]{$label}) > 1 0 4369 4369
85 4369 $match = 1; last;
86 }
87 27735 ▶ return $match if $match > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $match > 0 4369 23366 27735
88  
89 23366 FIELDS1: for my $e ( keys $fieldnames->[1]->%* ) {
90 # Per-Recipient fields
91 157990 ▶ next unless $label eq $e;
Branch: 1 branch
BranchTrueFalseTotal
unless $label eq $e 147618 10372 157990
92 10372 ▶ next unless index($argv0, $fieldnames->[1]->{ $label }) > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless index($argv0, $fieldnames->[1]{$label}) > 1 62 10310 10372
93 10310 $match = 2; last;
94 }
95 23366 return $match;
96 }
97  
98 44124 ▶sub label {
Subroutine label: called CC=3 SCAR=11.7
Pod documented
99 # Returns a field name as a label from the given string
100 # @param [String] argv0 A line including field and value defined in RFC3464
101 # @return [String] Field name as a label
102 # @since v4.25.15
103 44124 my $class = shift;
104 44124 ▶ my $argv0 = shift || return "";
Condition50%1/2
shift() || (return '')
l!l
441240
MC/DC0%0/1
shift() || (return '')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
105 44124 ▶ return lc((split(':', $argv0, 2))[0]) || "";
Condition50%1/2
lc((split(/:/u, $argv0, 2))[0]) || ''
l!l
441240
MC/DC0%0/1
lc((split(/:/u, $argv0, 2))[0]) || ''
lc((split(/:/u, $argv0, 2))[0])
Truth table50%1/2
A
A= lc((split(/:/u, $argv0, 2))[0])
Aresult
11
00
106 }
107  
108 16375 ▶sub field {
Subroutine field: called CC=19 SCAR=29.8
Pod documented
109 # Check the argument is including field defined in RFC3464 and return values
110 # @param [String] argv0 A line including field and value defined in RFC3464
111 # @return [Array] ['field-name', 'value-type', 'Value', 'field-group']
112 # @since v4.25.0
113 16375 my $class = shift;
114 16375 ▶ my $argv0 = shift || return undef;
Condition100%2/2
shift() || (return undef)
l!l
163741
MC/DC100%1/1
shift() || (return undef)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
115  
116 16374 state $subtypeset = {"addr" => "RFC822", "cdoe" => "SMTP", "host" => "DNS"};
117 16374 state $actionlist = [$Sisimai::Eb::AeFAIL, $Sisimai::Eb::AeSTAY, $Sisimai::Eb::AeSENT,
118 $Sisimai::Eb::AePASS, $Sisimai::Eb::AeEXPN];
119 16374 state $correction = {
120 'deliverable' => $Sisimai::Eb::AeSENT,
121 'expired' => $Sisimai::Eb::AeFAIL,
122 'failure' => $Sisimai::Eb::AeFAIL,
123 };
124 16374 state $fieldgroup = {
125 'original-recipient' => 'addr',
126 'final-recipient' => 'addr',
127 'x-actual-recipient' => 'addr',
128 'diagnostic-code' => 'code',
129 'arrival-date' => 'date',
130 'last-attempt-date' => 'date',
131 'received-from-mta' => 'host',
132 'remote-mta' => 'host',
133 'reporting-mta' => 'host',
134 'action' => 'list',
135 'status' => 'stat',
136 'x-original-message-id' => 'text',
137 };
138 16374 state $captureson = {
139 "addr" => ["Final-Recipient", "Original-Recipient", "X-Actual-Recipient"],
140 "code" => ["Diagnostic-Code"],
141 "date" => ["Arrival-Date", "Last-Attempt-Date"],
142 "host" => ["Received-From-MTA", "Remote-MTA", "Reporting-MTA"],
143 "list" => ["Action"],
144 "stat" => ["Status"],
145 #"text" => ["X-Original-Message-ID", "Final-Log-ID", "Original-Envelope-ID"],
146 };
147  
148 16374 my $parts = [split(":", $argv0, 2)]; # ["Final-Recipient", " rfc822; <neko@example.jp>"]
149 16374 ▶ my $label = __PACKAGE__->label($argv0) || return undef;
Condition50%1/2
__PACKAGE__->label($argv0) || (return undef)
l!l
163740
MC/DC0%0/1
__PACKAGE__->label($argv0) || (return undef)
__PACKAGE__->label($argv0)
Truth table50%1/2
A
A= __PACKAGE__->label($argv0)
Aresult
11
00
150 16374 ▶ my $group = $fieldgroup->{ $label } || return undef;
Condition100%2/2
$fieldgroup->{$label} || (return undef)
l!l
163695
MC/DC100%1/1
$fieldgroup->{$label} || (return undef)
$fieldgroup->{$label}
Truth table100%2/2
A
A= $fieldgroup->{$label}
Aresult
11
00
151 16369 ▶ return undef unless exists $captureson->{ $group };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $captureson->{$group} 165 16204 16369
152  
153 # Try to match with each pattern of Per-Message field, Per-Recipient field
154 # - 0: Field-Name
155 # - 1: Sub Type: RFC822, DNS, X-Unix, and so on)
156 # - 2: Value
157 # - 3: Field Group(addr, code, date, host, stat, text)
158 # - 4: Comment
159 16204 my $table = [$label, "", "", $group, ""]; $parts->[1] = Sisimai::String->sweep($parts->[1]);
160  
161 16204 ▶ if( $group eq 'addr' || $group eq 'code' || $group eq 'host' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($group eq 'addr' or $group eq 'code' or $group eq 'host') { } 8170 8034 16204
elsif ($group eq 'list') { } 3631 4403 8034
Condition100%6/6
$group eq "addr" or $group eq "code" or $group eq "host"
l!l&&r!l&&!r
448936818034
$group eq "addr" or $group eq "code"
l!l&&r!l&&!r
2694179511715
MC/DC100%3/3
$group eq "addr" or $group eq "code" or $group eq "host"
$group eq "addr" $group eq "code" $group eq "host"
Truth table100%4/4
A or B or C
A= $group eq "addr"B= $group eq "code"C= $group eq "host"
ABCresult
1XX1
01X1
0011
0000
162 # - Final-Recipient: RFC822; kijitora@nyaan.jp
163 # - Diagnostic-Code: SMTP; 550 5.1.1 <kijitora@example.jp>... User Unknown
164 # - Remote-MTA: DNS; mx.example.jp
165 8170 ▶ if( index($parts->[1], ";" ) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($parts->[1], ';') > 0) { } 8170 0 8170
166 # There is a valid sub type (including ";")
167 8170 my $v = [split(";", $parts->[1], 2)];
168 8170 ▶ $table->[1] = uc Sisimai::String->sweep($v->[0]) if scalar @$v > 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$v > 0 8170 0 8170
169 8170 ▶ $table->[2] = Sisimai::String->sweep($v->[1]) if scalar @$v > 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$v > 1 8170 0 8170
170  
171 } else {
172 # There is no sub type like "Diagnostic-Code: 550 5.1.1 <kijitora@example.jp>..."
173 0 $table->[2] = $parts->[1];
174 0 ▶ $table->[1] = $subtypeset->{ $group } || "";
Condition0%0/2
$subtypeset->{$group} || ''
l!l
00
MC/DC0%0/1
$subtypeset->{$group} || ''
$subtypeset->{$group}
Truth table0%0/2
A
A= $subtypeset->{$group}
Aresult
11
00
175 }
176 8170 ▶ $table->[2] = lc $table->[2] if $group eq "host";
Branch: 1 branch
BranchTrueFalseTotal
if $group eq "host" 3681 4489 8170
177 8170 ▶ $table->[2] = '' if $table->[2] =~ /\A\s+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $table->[2] =~ /\A\s+\z/u 0 8170 8170
178  
179 } elsif( $group eq "list" ) {
180 # Action: failed
181 # Check that the value is an available value defined in "actionlist" or not.
182 # When the value is invalid, convert to an available value defined in "correction"
183 3631 my $v = lc $parts->[1];
184 3631 ▶ $table->[2] = $v if grep { $v eq $_ } @$actionlist;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$v eq $_;} @$actionlist 1930 1701 3631
185 3631 ▶ $table->[2] ||= $correction->{ $v };
Condition100%3/3
$table->[2] ||= $correction->{$v}
l!l&&r!l&&!r
1930111690
MC/DC100%2/2
$table->[2] ||= $correction->{$v}
$table->[2] $correction->{$v}
Truth table100%3/3
A ||= B
A= $table->[2]B= $correction->{$v}
ABresult
1X1
011
000
186  
187 } else {
188 # Other groups such as Status:, Arrival-Date:, or X-Original-Message-ID:.
189 # There is no ";" character in the field.
190 # - Status: 5.2.2
191 # - Arrival-Date: Mon, 21 May 2018 16:09:59 +0900
192 4403 ▶ $table->[2] = $group eq "date" ? $parts->[1] : lc $parts->[1];
Branch: 1 branch
BranchTrueFalseTotal
$group eq 'date' ? : 2487 1916 4403
193 }
194  
195 16204 ▶ if( Sisimai::String->aligned(\$table->[2], [" (", ")"]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Sisimai::String"->aligned(\$table->[2], [" (", ")"])) 1743 14461 16204
196 # Extract text enclosed in parentheses as comments
197 # Reporting-MTA: dns; mr21p30im-asmtp004.me.example.com (tcp-daemon)
198 1743 my $p1 = index($table->[2], " (");
199 1743 my $p2 = index($table->[2], ")");
200 1743 $table->[4] = substr($table->[2], $p1 + 2, $p2 - $p1 - 2);
201 1743 $table->[2] = substr($table->[2], 0, $p1);
202 }
203 16204 return $table;
204 }
205  
206 90 1;
207 __END__
« Sisimai/Rhost/NTTDOCOMO.pm ↑ Summary Sisimai/Lhost/OpenSMTPD.pm »