Sisimai/ARF.pm

filter: Statementstmt 96.4% 108 / 112 Branchbran 82.3% 56 / 68 Conditioncond 76.4% 26 / 34 MC/DCmcdc 60.0% 12 / 20 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 3 totaltotal 85.7% 211 / 246 CC 51 SCAR 35.7
CC
51
Cov
89%
inquire
46.7
is_arf
9.1
BEGIN
1.0
CRAP 35.5
« Sisimai/Lhost/EZweb.pm ↑ Summary Sisimai/Lhost/TrendMicro.pm »
1 package Sisimai::ARF;
2 8 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Sisimai::Eb;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Sisimai::Lhost;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Sisimai::RFC5322;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶sub description { return 'Abuse Feedback Reporting Format' }
Subroutine description: called CC=1 SCAR=0.0
Pod undocumented
10 164 ▶sub is_arf {
Subroutine is_arf: called CC=9 SCAR=22.1
Pod undocumented
11 # Email is a Feedback-Loop message or not
12 # @param [Hash] heads Email header including "Content-Type", "From" and "Subject" field
13 # @return [Integer] 1: Feedback Loop
14 # 0: is not Feedback loop
15 164 my $class = shift;
16 164 ▶ my $heads = shift || return 0;
Condition100%2/2
shift() || (return 0)
l!l
1631
MC/DC100%1/1
shift() || (return 0)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
17 163 my $abuse = ['staff@hotmail.com', 'complaints@email-abuse.amazonses.com'];
18 163 ▶ my $ctype = $heads->{"content-type"} || "";
Condition100%2/2
$heads->{'content-type'} || ''
l!l
12637
MC/DC100%1/1
$heads->{'content-type'} || ''
$heads->{'content-type'}
Truth table100%2/2
A
A= $heads->{'content-type'}
Aresult
11
00
19  
20 # Content-Type: multipart/report; report-type=feedback-report; ...
21 163 ▶ return 1 if Sisimai::String->aligned(\$ctype, ["report-type=", "feedback-report"]);
Branch: 1 branch
BranchTrueFalseTotal
if "Sisimai::String"->aligned(\$ctype, ["report-type=", "feedback-report"]) 66 97 163
22  
23 97 ▶ if( index($ctype, "multipart/mixed") > -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($ctype, "multipart/mixed") != -1) 34 63 97
24 # Microsoft (Hotmail, MSN, Live, Outlook) uses its own report format.
25 # Amazon SES Complaints bounces
26 34 ▶ if( index($heads->{"subject"}, "complaint about message from ") > -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($heads->{"subject"}, "complaint about message from ") != -1) 30 4 34
27 # From: staff@hotmail.com
28 # From: complaints@email-abuse.amazonses.com
29 # Subject: complaint about message from 192.0.2.1
30 30 ▶ return 1 if grep { index($heads->{"from"}, $_) > -1 } @$abuse;
Branch: 1 branch
BranchTrueFalseTotal
if grep {index($heads->{"from"}, $_) != -1;} @$abuse 30 0 30
31 }
32 }
33  
34 # X-Apple-Unsubscribe: true
35 67 ▶ return 0 unless exists $heads->{"x-apple-unsubscribe"};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $heads->{"x-apple-unsubscribe"} 62 5 67
36 5 ▶ return 1 if $heads->{"x-apple-unsubscribe"} eq "true";
Branch: 1 branch
BranchTrueFalseTotal
if $heads->{"x-apple-unsubscribe"} eq "true" 5 0 5
37 0 return 0;
38 }
39  
40 164 ▶sub inquire {
Subroutine inquire: called CC=43 SCAR=38.4
Pod undocumented
41 # Detect an error for Feedback Loop
42 # @param [Hash] mhead Message headers of a bounce email
43 # @param [String] mbody Message body of a bounce email
44 # @return [Hash] Bounce data list and message/rfc822 part
45 # @return [undef] failed to decode or the arguments are missing
46 164 my $class = shift;
47 164 ▶ my $mhead = shift // return undef; return undef unless is_arf(undef, $mhead);
Branch: 1 branch
BranchTrueFalseTotal
unless is_arf(undef, $mhead) 62 101 163
Condition100%2/2
shift() // (return undef)
l!l
1631
MC/DC100%1/1
shift() // (return undef)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
48 101 ▶ my $mbody = shift // return undef;
Condition50%1/2
shift() // (return undef)
l!l
1010
MC/DC0%0/1
shift() // (return undef)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
49  
50 # http://tools.ietf.org/html/rfc5965
51 # http://en.wikipedia.org/wiki/Feedback_loop_(email)
52 # http://en.wikipedia.org/wiki/Abuse_Reporting_Format
53 #
54 # Netease DMARC uses: This is a spf/dkim authentication-failure report for an email message received from IP
55 # OpenDMARC 1.3.0 uses: This is an authentication failure report for an email message received from IP
56 # Abusix ARF uses: this is an autogenerated email abuse complaint regarding your network.
57 101 state $indicators = Sisimai::Lhost->INDICATORS;
58 101 state $reportfrom = "Content-Type: message/feedback-report";
59 101 state $boundaries = [
60 "Content-Type: message/rfc822",
61 "Content-Type: text/rfc822-headers",
62 "Content-Type: text/rfc822-header", # ??
63 ];
64 101 state $arfpreface = [
65 ["this is a", "abuse report"],
66 ["this is a", "authentication", "failure report"],
67 ["this is a", " report for"],
68 ["this is an authentication", "failure report"],
69 ["this is an autogenerated email abuse complaint"],
70 ["this is an email abuse report"],
71 ];
72  
73 101 ▶ my $emailparts = Sisimai::RFC5322->part($mbody, $boundaries) // return undef; return undef unless $emailparts->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $emailparts->[0] 0 101 101
Condition50%1/2
"Sisimai::RFC5322"->part($mbody, $boundaries) // (return undef)
l!l
1010
MC/DC0%0/1
"Sisimai::RFC5322"->part($mbody, $boundaries) // (return undef)
"Sisimai::RFC5322"->part($mbody, $boundaries)
Truth table50%1/2
A
A= "Sisimai::RFC5322"->part($mbody, $boundaries)
Aresult
11
00
74 101 my $dscontents = [Sisimai::Lhost->DELIVERYSTATUS]; my $v = $dscontents->[-1];
75 101 my $reportpart = 0;
76 101 my $readcursor = 0; # Points the current cursor position
77 101 my $recipients = 0; # The number of "Final-Recipient" header
78 101 my $timestamp0 = ""; # The value of "Arrival-Date" or "Received-Date"
79 101 my $remotehost = ""; # The value of "Source-IP" field
80 101 my $reportedby = ""; # The value of "Reporting-MTA" field
81 101 my $anotherone = ""; # Other fields(append to Diagnosis)
82  
83 # 3.1. Required Fields
84 #
85 # The following report header fields MUST appear exactly once:
86 #
87 # o "Feedback-Type" contains the type of feedback report (as defined
88 # in the corresponding IANA registry and later in this memo). This
89 # is intended to let report parsers distinguish among different
90 # types of reports.
91 #
92 # o "User-Agent" indicates the name and version of the software
93 # program that generated the report. The format of this field MUST
94 # follow section 14.43 of [HTTP]. This field is for documentation
95 # only; there is no registry of user agent names or versions, and
96 # report receivers SHOULD NOT expect user agent names to belong to a
97 # known set.
98 #
99 # o "Version" indicates the version of specification that the report
100 # generator is using to generate the report. The version number in
101 # this specification is set to "1".
102 #
103 101 for my $e ( split("\n", $emailparts->[0]) ) {
104 # Read error messages and delivery status lines from the head of the email to the
105 # previous line of the beginning of the original message.
106 1721 ▶ unless( $readcursor ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($readcursor) 761 960 1721
107 # Beginning of the bounce message or message/delivery-status part
108 761 my $r = lc $e;
109 761 for my $f ( @$arfpreface ) {
110 # Hello,
111 # this is an autogenerated email abuse complaint regarding your network.
112 4276 ▶ next unless Sisimai::String->aligned(\$r, $f);
Branch: 1 branch
BranchTrueFalseTotal
unless "Sisimai::String"->aligned(\$r, $f) 4210 66 4276
113 66 $readcursor |= $indicators->{'deliverystatus'};
114 66 $v->{"diagnosis"} .= " ".$e;
115 66 last;
116 }
117 761 next;
118 }
119 960 ▶ next if ($readcursor & $indicators->{'deliverystatus'}) == 0 || $e eq "";
Branch: 1 branch
BranchTrueFalseTotal
if ($readcursor & $indicators->{"deliverystatus"}) == 0 or $e eq "" 150 810 960
Condition66%2/3
($readcursor & $indicators->{"deliverystatus"}) == 0 or $e eq ""
l!l&&r!l&&!r
0150810
MC/DC50%1/2
($readcursor & $indicators->{"deliverystatus"}) == 0 or $e eq ""
($readcursor & $indicators->{"deliverystatus"}) == 0 $e eq ""
Truth table66%2/3
A or B
A= ($readcursor & $indicators->{"deliverystatus"}) == 0B= $e eq ""
ABresult
1X1
011
000
120 810 ▶ if( $e eq $reportfrom ) { $reportpart = 1; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($e eq $reportfrom) 66 744 810
121  
122 744 ▶ if( $reportpart ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reportpart) { } 573 171 744
123 # Content-Type: message/feedback-report
124 # MIME-Version: 1.0
125 #
126 # Feedback-Type: abuse
127 # User-Agent: SomeGenerator/1.0
128 # Version: 0.1
129 # Original-Mail-From: <somespammer@example.net>
130 # Original-Rcpt-To: <kijitora@example.jp>
131 # Received-Date: Thu, 29 Apr 2009 00:00:00 JST
132 # Source-IP: 192.0.2.1
133 573 ▶ if( index($e, "Original-Rcpt-To: ") == 0 || index($e, "Removal-Recipient: ") == 0 ) {
Branch: 6 branches
BranchTrueFalseTotal
if (index($e, 'Original-Rcpt-To: ') == 0 or index($e, 'Removal-Recipient: ') == 0) { } 70 503 573
elsif (index($e, 'Feedback-Type: ') == 0) { } 66 437 503
elsif (index($e, 'Authentication-Results: ') == 0 or index($e, 'User-Agent: ') == 0 or index($e, 'Original-Mail-From: ') == 0) { } 141 296 437
elsif (index($e, 'Received-Date: ') == 0 or index($e, 'Arrival-Date: ') == 0) { } 51 245 296
elsif (index($e, 'Reporting-MTA: ') == 0) { } 0 245 245
elsif (index($e, 'Source-IP: ') == 0) { } 46 199 245
Condition100%3/3
index($e, "Original-Rcpt-To: ") == 0 or index($e, "Removal-Recipient: ") == 0
l!l&&r!l&&!r
655503
MC/DC100%2/2
index($e, "Original-Rcpt-To: ") == 0 or index($e, "Removal-Recipient: ") == 0
index($e, "Original-Rcpt-To: ") == 0 index($e, "Removal-Recipient: ") == 0
Truth table100%3/3
A or B
A= index($e, "Original-Rcpt-To: ") == 0B= index($e, "Removal-Recipient: ") == 0
ABresult
1X1
011
000
134 # Original-Rcpt-To header field is optional and may appear any number of times as appropriate:
135 # Original-Rcpt-To: <kijitora@example.jp>
136 # Removal-Recipient: user@example.com
137 70 ▶ my $cv = Sisimai::Address->s3s4(substr($e, index($e, " ") + 1,)); next unless Sisimai::Address->is_emailaddress($cv);
Branch: 1 branch
BranchTrueFalseTotal
unless "Sisimai::Address"->is_emailaddress($cv) 0 70 70
138 70 ▶ my $cw = scalar @$dscontents; next if $cw > 0 && $cv eq $dscontents->[$cw - 1]->{"recipient"};
Branch: 1 branch
BranchTrueFalseTotal
if $cw > 0 and $cv eq $$dscontents[$cw - 1]{"recipient"} 0 70 70
Condition33%1/3
$cw > 0 and $cv eq $$dscontents[$cw - 1]{"recipient"}
!ll&&!rl&&r
0700
MC/DC0%0/2
$cw > 0 and $cv eq $$dscontents[$cw - 1]{"recipient"}
$cw > 0 $cv eq $$dscontents[$cw - 1]{"recipient"}
Truth table33%1/3
A and B
A= $cw > 0B= $cv eq $$dscontents[$cw - 1]{"recipient"}
ABresult
0X0
100
111
139  
140 70 ▶ if( $v->{"recipient"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v->{"recipient"}) 35 35 70
141 # There are multiple recipient addresses in the message body.
142 35 push @$dscontents, Sisimai::Lhost->DELIVERYSTATUS;
143 35 $v = $dscontents->[-1];
144 }
145 70 $v->{"recipient"} = Sisimai::Address->s3s4(substr($e, index($e, " ") +1,));
146 70 $recipients++;
147  
148 } elsif( index($e, "Feedback-Type: ") == 0 ) {
149 # The header field MUST appear exactly once.
150 # Feedback-Type: abuse
151 66 $v->{"feedbacktype"} = substr($e, index($e, " ") + 1,);
152  
153 ▶ } elsif( index($e, "Authentication-Results: ") == 0 || index($e, "User-Agent: ") == 0 || index($e, "Original-Mail-From: ") == 0 ) {
Condition100%6/6
index($e, "Authentication-Results: ") == 0 or index($e, "User-Agent: ") == 0 or index($e, "Original-Mail-From: ") == 0
l!l&&r!l&&!r
9150296
index($e, "Authentication-Results: ") == 0 or index($e, "User-Agent: ") == 0
l!l&&r!l&&!r
2566346
MC/DC100%3/3
index($e, "Authentication-Results: ") == 0 or index($e, "User-Agent: ") == 0 or index($e, "Original-Mail-From: ") == 0
index($e, "Authentication-Results: ") == 0 index($e, "User-Agent: ") == 0 index($e, "Original-Mail-From: ") == 0
Truth table100%4/4
A or B or C
A= index($e, "Authentication-Results: ") == 0B= index($e, "User-Agent: ") == 0C= index($e, "Original-Mail-From: ") == 0
ABCresult
1XX1
01X1
0011
0000
154 # "Authentication-Results" indicates the result of one or more authentication checks
155 # run by the report generator.
156 # - Authentication-Results: mail.example.com;
157 # - spf=fail smtp.mail=somespammer@example.com
158 #
159 # The header field MUST appear exactly once.
160 # - User-Agent: SomeGenerator/1.0
161 #
162 # The header is optional and MUST NOT appear more than once.
163 # - Original-Mail-From: <somespammer@example.net>
164 141 $anotherone .= $e.", ";
165  
166 ▶ } elsif( index($e, "Received-Date: ") == 0 || index($e, "Arrival-Date: ") == 0 ) {
Condition100%3/3
index($e, "Received-Date: ") == 0 or index($e, "Arrival-Date: ") == 0
l!l&&r!l&&!r
1635245
MC/DC100%2/2
index($e, "Received-Date: ") == 0 or index($e, "Arrival-Date: ") == 0
index($e, "Received-Date: ") == 0 index($e, "Arrival-Date: ") == 0
Truth table100%3/3
A or B
A= index($e, "Received-Date: ") == 0B= index($e, "Arrival-Date: ") == 0
ABresult
1X1
011
000
167 # Arrival-Date header is optional and MUST NOT appear more than once.
168 # Received-Date: Thu, 29 Apr 2010 00:00:00 JST
169 # Arrival-Date: Thu, 29 Apr 2010 00:00:00 +0000
170 51 $timestamp0 = substr($e, index($e, " ") + 1,);
171  
172 } elsif( index($e, "Reporting-MTA: ") == 0 ) {
173 # The header is optional and MUST NOT appear more than once.
174 # Reporting-MTA: dns; mx.example.jp
175 0 ▶ my $cv = Sisimai::RFC1894->field($e); next if scalar(@$cv) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$cv == 0 0 0 0
176 0 $reportedby = $cv->[2];
177  
178 } elsif( index($e, "Source-IP: ") == 0 ) {
179 # The header is optional and MUST NOT appear more than once.
180 # Source-IP: 192.0.2.45
181 46 $remotehost = substr($e, index($e, ' ') + 1,);
182 }
183 } else {
184 # Messages before "Content-Type: message/feedback-report" part
185 171 $v->{"diagnosis"} .= " ".$e;
186 }
187 }
188  
189 101 ▶ while( $recipients == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recipients == 0) 66 71 137
190 # There is no recipient address in the message
191 66 ▶ if( exists $mhead->{"x-apple-unsubscribe"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $mhead->{'x-apple-unsubscribe'}) { } 5 61 66
192 # X-Apple-Unsubscribe: true
193 5 ▶ last if $mhead->{"x-apple-unsubscribe"} ne "true" || index($mhead->{"from"}, "@") < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $mhead->{"x-apple-unsubscribe"} ne "true" or index($mhead->{"from"}, "\@") < 1 0 5 5
Condition33%1/3
$mhead->{"x-apple-unsubscribe"} ne "true" or index($mhead->{"from"}, "\@") < 1
l!l&&r!l&&!r
005
MC/DC0%0/2
$mhead->{"x-apple-unsubscribe"} ne "true" or index($mhead->{"from"}, "\@") < 1
$mhead->{"x-apple-unsubscribe"} ne "true" index($mhead->{"from"}, "\@") < 1
Truth table33%1/3
A or B
A= $mhead->{"x-apple-unsubscribe"} ne "true"B= index($mhead->{"from"}, "\@") < 1
ABresult
1X1
011
000
194 5 $dscontents->[0]->{"recipient"} = $mhead->{"from"};
195 5 $dscontents->[0]->{"diagnosis"} = $emailparts->[0];
196 5 $dscontents->[0]->{"feedbacktype"} = "opt-out";
197  
198 # Addpend To: field as a pseudo header
199 5 ▶ $emailparts->[1] = sprintf("To: <%s>\n", $mhead->{"from"}) if $emailparts->[1] eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $emailparts->[1] eq "" 5 0 5
200  
201 } else {
202 # Pick it from the original message part
203 61 ▶ my $p1 = index($emailparts->[1], "\nTo:"); last if $p1 < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $p1 < 0 30 31 61
204 31 ▶ my $p2 = index($emailparts->[1], "\n", $p1 + 4); last if $p2 < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $p2 < 0 0 31 31
205 31 my $cv = Sisimai::Address->s3s4(substr($emailparts->[1], $p1 + 4, $p2 - $p1));
206  
207 # There is no valid email address in the To: header of the original message such as
208 # To: <Undisclosed Recipients>
209 31 ▶ $cv = Sisimai::Address->undisclosed("r") unless Sisimai::Address->is_emailaddress($cv);
Branch: 1 branch
BranchTrueFalseTotal
unless "Sisimai::Address"->is_emailaddress($cv) 10 21 31
210 31 $dscontents->[0]->{"recipient"} = $cv;
211 }
212 36 $recipients++;
213 }
214 101 ▶ return undef if $recipients == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $recipients == 0 30 71 101
215  
216 71 ▶ $anotherone = ": ".$anotherone if $anotherone ne "";
Branch: 1 branch
BranchTrueFalseTotal
if $anotherone ne "" 66 5 71
217 71 ▶ substr($anotherone, -1, 1, "") if substr($anotherone, -1, 1) eq ",";
Branch: 1 branch
BranchTrueFalseTotal
if substr($anotherone, -1, 1) eq "," 0 71 71
218  
219 71 my $j = -1; for my $e ( @$dscontents ) {
220 # Tidy up the error message in e.Diagnosis, Try to detect the bounce reason.
221 106 $j++;
222 106 $e->{"diagnosis"} = Sisimai::String->sweep($e->{"diagnosis"}.$anotherone);
223 106 $e->{"reason"} = $Sisimai::Eb::ReFEED;
224 106 $e->{"rhost"} = $remotehost;
225 106 $e->{"lhost"} = $reportedby;
226 106 $e->{"date"} = $timestamp0;
227  
228 # Copy some values from the previous element when the report have 2 or more email address
229 106 ▶ next if $j == 0 || scalar(@$dscontents) == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $j == 0 or scalar @$dscontents == 1 71 35 106
Condition66%2/3
$j == 0 or scalar @$dscontents == 1
l!l&&r!l&&!r
71035
MC/DC50%1/2
$j == 0 or scalar @$dscontents == 1
$j == 0 scalar @$dscontents == 1
Truth table66%2/3
A or B
A= $j == 0B= scalar @$dscontents == 1
ABresult
1X1
011
000
230 35 $e->{"diagnosis"} = $dscontents->[$j - 1]->{"diagnosis"};
231 35 $e->{"feedbacktype"} = $dscontents->[$j - 1]->{"feedbacktype"};
232 }
233 71 return {"ds" => $dscontents, "rfc822" => $emailparts->[1]};
234 }
235  
236 8 1;
237 __END__
« Sisimai/Lhost/EZweb.pm ↑ Summary Sisimai/Lhost/TrendMicro.pm »