Sisimai/Lhost/qmail.pm

filter: Statementstmt 100% 81 / 81 Branchbran 95.2% 40 / 42 Conditioncond 69.5% 16 / 23 MC/DCmcdc 50.0% 7 / 14 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 90.4% 152 / 168 CC 38 SCAR 35.3
CC
38
Cov
94%
inquire
38.4
BEGIN
1.0
BEGIN
1.0
CRAP 34.1
« Sisimai/RFC5322.pm ↑ Summary Sisimai/Lhost/Notes.pm »
1 package Sisimai::Lhost::qmail;
2 43 ▶use parent 'Sisimai::Lhost';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 43 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 43 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 43 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶sub description { 'qmail: https://cr.yp.to/qmail.html' }
Subroutine description: called CC=1 SCAR=0.0
Pod documented
8 1129 ▶sub inquire {
Subroutine inquire: called CC=38 SCAR=36.5
Pod documented
9 # Detect an error from qmail
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.0.0
15 1129 my $class = shift;
16 1129 ▶ my $mhead = shift // return undef;
Condition100%2/2
shift() // (return undef)
l!l
11263
MC/DC100%1/1
shift() // (return undef)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
17 1126 ▶ my $mbody = shift // return undef;
Condition100%2/2
shift() // (return undef)
l!l
11233
MC/DC100%1/1
shift() // (return undef)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
18  
19 # Pre process email headers and the body part of the message which generated by qmail.
20 # see https://cr.yp.to/qmail.html
21 # e.g.) Received: (qmail 12345 invoked for bounce); 29 Apr 2009 12:34:56 -0000
22 # Subject: failure notice
23 1123 my $emailtitle = [
24 "failure notice", # qmail-send.c:Subject: failure notice\n\
25 "Failure Notice", # Yahoo
26 ];
27 1123 ▶ my $proceedsto = 0; $proceedsto++ if grep { $mhead->{"subject"} eq $_ } @$emailtitle;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$mhead->{"subject"} eq $_;} @$emailtitle 242 881 1123
28  
29 1123 for my $e ( $mhead->{"received"}->@* ) {
30 # Received: (qmail 2222 invoked for bounce);29 Apr 2017 23:34:45 +0900
31 # Received: (qmail 2202 invoked from network); 29 Apr 2018 00:00:00 +0900
32 2143 ▶ $proceedsto ||= 1 if Sisimai::String->aligned(\$e, ["(qmail", " invoked "]);
Branch: 1 branch
BranchTrueFalseTotal
if "Sisimai::String"->aligned(\$e, ["(qmail", " invoked "]) 147 1996 2143
Condition50%1/2
$proceedsto ||= 1
l!l
1470
MC/DC0%0/1
$proceedsto ||= 1
$proceedsto
Truth table50%1/2
A
A= $proceedsto
Aresult
11
00
33 }
34 1123 ▶ return undef if $proceedsto == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $proceedsto == 0 881 242 1123
35  
36 242 require Sisimai::Eb;
37 242 require Sisimai::SMTP::Command;
38 242 state $indicators = __PACKAGE__->INDICATORS;
39 242 state $boundaries = [
40 # qmail-send.c:qmail_puts(&qqt,*sender.s ? "--- Below this line is a copy of the message.\n\n" :...
41 "--- Below this line is a copy of the message.", # qmail-1.03
42 "--- Below this line is a copy of the mail header.",
43 "--- Below the next line is a copy of the message.", # The followings are the qmail clone
44 "--- Mensaje original adjunto.",
45 "Content-Type: message/rfc822",
46 "Original message follows.",
47 ];
48 242 state $startingof = {
49 # qmail-remote.c:248| if (code >= 500) {
50 # qmail-remote.c:249| out("h"); outhost(); out(" does not like recipient.\n");
51 # qmail-remote.c:265| if (code >= 500) quit("D"," failed on DATA command");
52 # qmail-remote.c:271| if (code >= 500) quit("D"," failed after I sent the message");
53 #
54 # Characters: K,Z,D in qmail-qmqpc.c, qmail-send.c, qmail-rspawn.c
55 # K = success, Z = temporary error, D = permanent error
56 "error" => ["Remote host said:"],
57 "message" => [
58 "Hi. This is the qmail", # qmail-send.c:Hi. This is the qmail-send program at ");
59 "He/Her is not ", # The followings are the qmail clone
60 "unable to deliver your message to the following addresses",
61 "Su mensaje no pudo ser entregado",
62 "Sorry, we were unable to deliver your message to the following address",
63 "This is the machine generated message from mail service",
64 "This is the mail delivery agent at",
65 "Unable to deliver message to the following address",
66 "unable to deliver your message to the following addresses",
67 "Unfortunately, your mail was not delivered to the following address:",
68 "Your mail message to the following address",
69 "Your message to the following addresses",
70 "We're sorry.",
71 ],
72 "rhost" => ['Giving up on ', 'Connected to ', 'remote host '],
73 };
74 242 state $commandset = {
75 # Error text regular expressions which defined in qmail-remote.c
76 # qmail-remote.c:225| if (smtpcode() != 220) quit("ZConnected to "," but greeting failed");
77 $Sisimai::Eb::CeCONN => [" but greeting failed."],
78 # qmail-remote.c:231| if (smtpcode() != 250) quit("ZConnected to "," but my name was rejected");
79 $Sisimai::Eb::CeEHLO => [" but my name was rejected."],
80 # qmail-remote.c:238| if (code >= 500) quit("DConnected to "," but sender was rejected");
81 # reason = Rejected
82 $Sisimai::Eb::CeMAIL => [" but sender was rejected."],
83 # qmail-remote.c:249| out("h"); outhost(); out(" does not like recipient.\n");
84 # qmail-remote.c:253| out("s"); outhost(); out(" does not like recipient.\n");
85 # reason = UserUnknown
86 $Sisimai::Eb::CeRCPT => [" does not like recipient."],
87 # qmail-remote.c:265| if (code >= 500) quit("D"," failed on DATA command");
88 # qmail-remote.c:266| if (code >= 400) quit("Z"," failed on DATA command");
89 # qmail-remote.c:271| if (code >= 500) quit("D"," failed after I sent the message");
90 # qmail-remote.c:272| if (code >= 400) quit("Z"," failed after I sent the message");
91 $Sisimai::Eb::CeDATA => [" failed on DATA command", " failed after I sent the message"],
92 };
93  
94 242 state $messagesof = {
95 # notqmail 1.08 returns the following error message when the destination MX is NullMX
96 $Sisimai::Eb::Re00MX => ["Sorry, I couldn't find a mail exchanger or IP address"],
97 $Sisimai::Eb::ReUSER => ["no mailbox here by that name"],
98 };
99  
100 242 ▶ my $emailparts = Sisimai::RFC5322->part($mbody, $boundaries) // return undef; return undef unless $emailparts->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $emailparts->[0] 0 242 242
Condition50%1/2
"Sisimai::RFC5322"->part($mbody, $boundaries) // (return undef)
l!l
2420
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
101 242 my $dscontents = [__PACKAGE__->DELIVERYSTATUS]; my $v = undef;
102 242 my $readcursor = 0; # (Integer) Points the current cursor position
103 242 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
104  
105 242 for my $e ( split("\n", $emailparts->[0]) ) {
106 # Read error messages and delivery status lines from the head of the email to the previous
107 # line of the beginning of the original message.
108 2265 ▶ unless( $readcursor ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($readcursor) 928 1337 2265
109 # Beginning of the bounce message or message/delivery-status part
110 928 ▶ $readcursor |= $indicators->{'deliverystatus'} if grep { index($e, $_) > -1 } $startingof->{'message'}->@*;
Branch: 1 branch
BranchTrueFalseTotal
if grep {index($e, $_) != -1;} @{$$startingof{"message"}} 215 713 928
111 928 next;
112 }
113 1337 ▶ next if ($readcursor & $indicators->{'deliverystatus'}) == 0 || $e eq "";
Branch: 1 branch
BranchTrueFalseTotal
if ($readcursor & $indicators->{"deliverystatus"}) == 0 or $e eq "" 255 1082 1337
Condition66%2/3
($readcursor & $indicators->{"deliverystatus"}) == 0 or $e eq ""
l!l&&r!l&&!r
02551082
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
114  
115 # <kijitora@example.jp>:
116 # 192.0.2.153 does not like recipient.
117 # Remote host said: 550 5.1.1 <kijitora@example.jp>... User Unknown
118 # Giving up on 192.0.2.153.
119 1082 $v = $dscontents->[-1];
120  
121 1082 ▶ if( index($e, '<') == 0 && Sisimai::String->aligned(\$e, ['<', '@', '>:']) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (index($e, '<') == 0 and 'Sisimai::String'->aligned(\$e, ['<', '@', '>:'])) { } 230 852 1082
elsif (scalar @$dscontents == $recipients) { } 558 294 852
Condition66%2/3
index($e, "<") == 0 and "Sisimai::String"->aligned(\$e, ["<", "\@", ">:"])
!ll&&!rl&&r
8520230
MC/DC50%1/2
index($e, "<") == 0 and "Sisimai::String"->aligned(\$e, ["<", "\@", ">:"])
index($e, "<") == 0 "Sisimai::String"->aligned(\$e, ["<", "\@", ">:"])
Truth table66%2/3
A and B
A= index($e, "<") == 0B= "Sisimai::String"->aligned(\$e, ["<", "\@", ">:"])
ABresult
0X0
100
111
122 # <kijitora@example.jp>:
123 230 ▶ if( $v->{'recipient'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v->{"recipient"}) 15 215 230
124 # There are multiple recipient addresses in the message body.
125 15 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
126 15 $v = $dscontents->[-1];
127 }
128 230 $v->{'recipient'} = Sisimai::Address->s3s4(substr($e, index($e, '<'),));
129 230 $recipients++;
130  
131 } elsif( scalar @$dscontents == $recipients ) {
132 # Append error message
133 558 $v->{'diagnosis'} .= $e.' ';
134 558 ▶ $v->{'alterrors'} = $e if index($e, $startingof->{'error'}->[0]) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($e, $startingof->{"error"}[0]) == 0 147 411 558
135  
136 558 ▶ next if $v->{'rhost'};
Branch: 1 branch
BranchTrueFalseTotal
if $v->{"rhost"} 57 501 558
137 501 for my $r ( $startingof->{'rhost'}->@* ) {
138 # Find a remote host name
139 1348 ▶ my $p1 = index($e, $r); next if $p1 == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $p1 == -1 1257 91 1348
140 91 my $cm = length $r;
141 91 ▶ my $p2 = index($e, ' ', $p1 + $cm + 1); $p2 = rindex($e, '.') if $p2 == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $p2 == -1 70 21 91
142  
143 91 $v->{'rhost'} = substr($e, $p1 + $cm, $p2 - $p1 - $cm);
144 91 last;
145 }
146 }
147 }
148 242 ▶ return undef unless $recipients;
Branch: 1 branch
BranchTrueFalseTotal
unless $recipients 27 215 242
149  
150 215 for my $e ( @$dscontents ) {
151 # Get the SMTP command name for the session
152 230 SMTP: for my $r ( keys %$commandset ) {
153 # Get the last SMTP Command
154 964 ▶ next unless grep { index($e->{"diagnosis"}, $_) > 0 } $commandset->{ $r }->@*;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {index($e->{"diagnosis"}, $_) > 0;} @{$$commandset{$r}} 853 111 964
155 111 $e->{"command"} = $r;
156 111 last;
157 }
158 230 ▶ if( index($e->{"diagnosis"}, "no SMTP connection got far enough") > -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($e->{"diagnosis"}, "no SMTP connection got far enough") != -1) 6 224 230
159 # Sorry, no SMTP connection got far enough; most progress was RCPT TO response; ...
160 6 ▶ $e->{"command"} ||= Sisimai::SMTP::Command->find($e->{"diagnosis"});
Condition33%1/3
$e->{"command"} ||= "Sisimai::SMTP::Command"->find($e->{"diagnosis"})
l!l&&r!l&&!r
060
MC/DC0%0/2
$e->{"command"} ||= "Sisimai::SMTP::Command"->find($e->{"diagnosis"})
$e->{"command"} "Sisimai::SMTP::Command"->find($e->{"diagnosis"})
Truth table33%1/3
A ||= B
A= $e->{"command"}B= "Sisimai::SMTP::Command"->find($e->{"diagnosis"})
ABresult
1X1
011
000
161 }
162  
163 # Detect the reason of bounce
164 230 ▶ if( $e->{"command"} eq $Sisimai::Eb::CeHELO || $e->{"command"} eq $Sisimai::Eb::CeEHLO ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->{'command'} eq $Sisimai::Eb::CeHELO or $e->{'command'} eq $Sisimai::Eb::CeEHLO) { } 5 225 230
Condition66%2/3
$e->{"command"} eq $Sisimai::Eb::CeHELO or $e->{"command"} eq $Sisimai::Eb::CeEHLO
l!l&&r!l&&!r
05225
MC/DC50%1/2
$e->{"command"} eq $Sisimai::Eb::CeHELO or $e->{"command"} eq $Sisimai::Eb::CeEHLO
$e->{"command"} eq $Sisimai::Eb::CeHELO $e->{"command"} eq $Sisimai::Eb::CeEHLO
Truth table66%2/3
A or B
A= $e->{"command"} eq $Sisimai::Eb::CeHELOB= $e->{"command"} eq $Sisimai::Eb::CeEHLO
ABresult
1X1
011
000
165 # HELO | Connected to 192.0.2.135 but my name was rejected.
166 5 $e->{"reason"} = $Sisimai::Eb::ReBLOC;
167  
168 } else {
169 # The error message includes any of patterns defined in the variable avobe
170 225 FINDREASON: for my $f ( $e->{"alterrors"}, $e->{"diagnosis"} ) {
171 # Check that the error message includes any of message patterns or not
172 450 ▶ last if $e->{"reason"};
Branch: 1 branch
BranchTrueFalseTotal
if $e->{"reason"} 0 450 450
173 450 ▶ next unless $f;
Branch: 1 branch
BranchTrueFalseTotal
unless $f 83 367 450
174 367 MESG: for my $r ( keys %$messagesof ) {
175 # The key is a bounce reason name
176 724 ▶ next unless grep { index($f, $_) > -1 } $messagesof->{ $r }->@*;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {index($f, $_) != -1;} @{$$messagesof{$r}} 709 15 724
177 15 $e->{"reason"} = $r;
178 15 last FINDREASON;
179 }
180 }
181 }
182 230 ▶ $e->{"command"} ||= Sisimai::SMTP::Command->find($e->{"diagnosis"});
Condition100%3/3
$e->{"command"} ||= "Sisimai::SMTP::Command"->find($e->{"diagnosis"})
l!l&&r!l&&!r
1173182
MC/DC100%2/2
$e->{"command"} ||= "Sisimai::SMTP::Command"->find($e->{"diagnosis"})
$e->{"command"} "Sisimai::SMTP::Command"->find($e->{"diagnosis"})
Truth table100%3/3
A ||= B
A= $e->{"command"}B= "Sisimai::SMTP::Command"->find($e->{"diagnosis"})
ABresult
1X1
011
000
183 }
184 215 return {"ds" => $dscontents, "rfc822" => $emailparts->[1]};
185 }
186  
187 43 1;
188 __END__
« Sisimai/RFC5322.pm ↑ Summary Sisimai/Lhost/Notes.pm »