Sisimai/RFC3834.pm

filter: Statementstmt 100% 84 / 84 Branchbran 88.6% 39 / 44 Conditioncond 60.0% 6 / 10 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 5 / 5 Podpod 100% 2 / 2 totaltotal 90.7% 137 / 151 CC 33 SCAR 34.0
CC
33
Cov
93%
inquire
33.4
BEGIN
1.0
BEGIN
1.0
CRAP 29.9
« Sisimai/Lhost/AmazonSES.pm ↑ Summary Sisimai/SMTP/Status.pm »
1 package Sisimai::RFC3834;
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  
6 # http://tools.ietf.org/html/rfc3834
7 1 ▶sub description { 'Detector for auto replied message' }
Subroutine description: called CC=1 SCAR=0.0
Pod documented
8 95 ▶sub inquire {
Subroutine inquire: called CC=33 SCAR=35.1
Pod documented
9 # Detect auto reply message as RFC3834
10 # @param [Hash] mhead Message headers of a bounce email
11 # @param [String] mbody Message body of a bounce email
12 # @return [Hash] Bounce data list and message/rfc822 part
13 # @return [undef] failed to decode or the arguments are missing
14 # @since v4.1.28
15 95 my $class = shift;
16 95 ▶ my $mhead = shift // return undef; return undef unless keys %$mhead;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$mhead 2 92 94
Condition100%2/2
shift() // (return undef)
l!l
941
MC/DC100%1/1
shift() // (return undef)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
17 92 ▶ my $mbody = shift // return undef; return undef unless ref $mbody eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $mbody eq "SCALAR" 1 91 92
Condition50%1/2
shift() // (return undef)
l!l
920
MC/DC0%0/1
shift() // (return undef)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
18 91 my $lower = {};
19  
20 91 my $markingsof = {'boundary' => '__SISIMAI_PSEUDO_BOUNDARY__'};
21 91 my $lowerlabel = ['from', 'to', 'subject', 'auto-submitted', 'precedence', 'x-apple-action'];
22  
23 91 for my $e ( @$lowerlabel ) {
24 # Set lower-cased value of each header related to auto-response
25 546 ▶ next unless exists $mhead->{ $e };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $mhead->{$e} 261 285 546
26 285 $lower->{ $e } = lc $mhead->{ $e };
27 }
28  
29 91 state $donotparse = {
30 'from' => ['root@', 'postmaster@', 'mailer-daemon@'],
31 'to' => ['root@'],
32 'subject' => [
33 'security information for', # sudo(1)
34 'mail failure -', # Exim
35 ],
36 };
37 91 state $autoreply0 = {
38 # http://www.iana.org/assignments/auto-submitted-keywords/auto-submitted-keywords.xhtml
39 'auto-submitted' => ['auto-generated', 'auto-replied', 'auto-notified'],
40 'precedence' => ['auto_reply'],
41 'subject' => ['auto:', 'auto response:', 'automatic reply:', 'out of office:', 'out of the office:'],
42 'x-apple-action' => ['vacation'],
43 };
44 91 state $subjectset = qr{\A(?>
45 (?:.+?)?re:
46 |auto(?:[ ]response):
47 |automatic[ ]reply:
48 |out[ ]of[ ]office:
49 )
50 [ ]*(.+)\z
51 }x;
52 91 state $suspending = [
53 ["this email inbox", " is no longer in use."],
54 ];
55  
56 91 my $leave = 0; DETECT_EXCLUSION_MESSAGE: for my $e ( keys %$donotparse ) {
57 # Exclude message from root@
58 268 ▶ next unless exists $lower->{ $e };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $lower->{$e} 18 250 268
59 250 ▶ next unless grep { index($lower->{ $e }, $_) > -1 } $donotparse->{ $e }->@*;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {index($lower->{$e}, $_) != -1;} @{$$donotparse{$e}} 231 19 250
60 19 $leave = 1;
61 19 last;
62 }
63 91 ▶ return undef if $leave;
Branch: 1 branch
BranchTrueFalseTotal
if $leave 19 72 91
64  
65 72 my $match = 0; DETECT_AUTO_REPLY_MESSAGE0: for my $e ( keys %$autoreply0 ) {
66 # RFC3834 Auto-Submitted and other headers
67 259 ▶ next unless exists $lower->{ $e };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $lower->{$e} 166 93 259
68 93 ▶ next unless grep { index($lower->{ $e }, $_) == 0 } $autoreply0->{ $e }->@*;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {index($lower->{$e}, $_) == 0;} @{$$autoreply0{$e}} 62 31 93
69  
70 31 $match++;
71 31 last;
72 }
73 72 ▶ return undef unless $match;
Branch: 1 branch
BranchTrueFalseTotal
unless $match 41 31 72
74  
75 31 require Sisimai::Eb;
76 31 require Sisimai::Lhost;
77 31 my $dscontents = [Sisimai::Lhost->DELIVERYSTATUS]; my $v = $dscontents->[-1];
78 31 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
79 31 my $maxmsgline = 5; # (Integer) Max message length(lines)
80 31 my $haveloaded = 0; # (Integer) The number of lines loaded from message body
81 31 my $blanklines = 0; # (Integer) Counter for countinuous blank lines
82 31 my $countuntil = 1; # (Integer) Maximun value of blank lines in the body part
83  
84 RECIPIENT_ADDRESS: {
85 # Try to get the address of the recipient
86 31 for my $e ('reply-to', 'from', 'return-path') {
87 # Get the recipient address
88 57 ▶ next unless exists $mhead->{ $e };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $mhead->{$e} 26 31 57
89  
90 31 $v->{'recipient'} = $mhead->{ $e };
91 31 last;
92 }
93  
94 31 ▶ if( $v->{'recipient'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v->{"recipient"}) 31 0 31
95 # Clean-up the recipient address
96 31 $v->{'recipient'} = Sisimai::Address->s3s4($v->{'recipient'});
97 31 $recipients++;
98 }
99 }
100 31 ▶ return undef unless $recipients;
Branch: 1 branch
BranchTrueFalseTotal
unless $recipients 0 31 31
101  
102 31 ▶ if( $mhead->{'content-type'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mhead->{"content-type"}) 25 6 31
103 # Get the boundary string and set regular expression for matching with the boundary string.
104 25 my $q = Sisimai::RFC2045->boundary($mhead->{'content-type'}, 0);
105 25 ▶ $markingsof->{'boundary'} = $q if $q;
Branch: 1 branch
BranchTrueFalseTotal
if $q 5 20 25
106 }
107  
108 MESSAGE_BODY: {
109 # Get vacation message
110 31 for my $e ( split("\n", $$mbody) ) {
111 # Read the first 5 lines except a blank line
112 73 ▶ $countuntil += 1 if index($e, $markingsof->{'boundary'}) > -1;
Branch: 1 branch
BranchTrueFalseTotal
if index($e, $markingsof->{"boundary"}) != -1 0 73 73
113  
114 73 ▶ unless( length $e ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $e) 21 52 73
115 # Check a blank line
116 21 ▶ last if ++$blanklines > $countuntil;
Branch: 1 branch
BranchTrueFalseTotal
if ++$blanklines > $countuntil 5 16 21
117 16 next;
118 }
119 52 ▶ next if rindex($e, ' ') < 0;
Branch: 1 branch
BranchTrueFalseTotal
if rindex($e, " ") == -1 15 37 52
120 37 ▶ next if index($e, 'Content-Type') == 0 || index($e, 'Content-Transfer') == 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($e, "Content-Type") == 0 or index($e, "Content-Transfer") == 0 0 37 37
Condition33%1/3
index($e, "Content-Type") == 0 or index($e, "Content-Transfer") == 0
l!l&&r!l&&!r
0037
MC/DC0%0/2
index($e, "Content-Type") == 0 or index($e, "Content-Transfer") == 0
index($e, "Content-Type") == 0 index($e, "Content-Transfer") == 0
Truth table33%1/3
A or B
A= index($e, "Content-Type") == 0B= index($e, "Content-Transfer") == 0
ABresult
1X1
011
000
121  
122 37 $v->{'diagnosis'} .= $e.' ';
123 37 $haveloaded++;
124 37 ▶ last if $haveloaded >= $maxmsgline;
Branch: 1 branch
BranchTrueFalseTotal
if $haveloaded >= $maxmsgline 0 37 37
125 }
126 31 ▶ $v->{'diagnosis'} ||= $mhead->{'subject'};
Condition66%2/3
$v->{"diagnosis"} ||= $mhead->{"subject"}
l!l&&r!l&&!r
2650
MC/DC0%0/2
$v->{"diagnosis"} ||= $mhead->{"subject"}
$v->{"diagnosis"} $mhead->{"subject"}
Truth table66%2/3
A ||= B
A= $v->{"diagnosis"}B= $mhead->{"subject"}
ABresult
1X1
011
000
127 }
128 31 $v->{'reason'} = $Sisimai::Eb::ReAWAY;
129  
130 31 my $cv = lc $v->{'diagnosis'}; for my $e ( @$suspending ) {
131 # Check that the auto-replied message indicates the "Suspend" reason or not.
132 31 ▶ next unless Sisimai::String->aligned(\$cv, $e);
Branch: 1 branch
BranchTrueFalseTotal
unless "Sisimai::String"->aligned(\$cv, $e) 26 5 31
133 5 $v->{'reason'} = $Sisimai::Eb::ReQUIT; last;
134 }
135  
136 31 $v->{'date'} = $mhead->{'date'};
137 31 $v->{'status'} = '';
138  
139 # Get the Subject header from the original message
140 31 ▶ my $rfc822part = $lower->{'subject'} =~ $subjectset ? 'Subject: '.$1."\n" : '';
Branch: 1 branch
BranchTrueFalseTotal
$lower->{'subject'} =~ /$subjectset/u ? : 10 21 31
141 31 return {"ds" => $dscontents, "rfc822" => $rfc822part};
142 }
143  
144 8 1;
145 __END__
« Sisimai/Lhost/AmazonSES.pm ↑ Summary Sisimai/SMTP/Status.pm »