Sisimai/Lhost/Sendmail.pm

filter: Statementstmt 98.1% 108 / 110 Branchbran 90.7% 69 / 76 Conditioncond 76.2% 45 / 59 MC/DCmcdc 42.4% 14 / 33 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 85.3% 244 / 286 CC 65 SCAR 41.6
CC
65
Cov
91%
inquire
69.1
BEGIN
1.0
BEGIN
1.0
CRAP 64.3
« Sisimai/RFC3464.pm ↑ Summary Sisimai/Address.pm »
1 package Sisimai::Lhost::Sendmail;
2 55 ▶use parent 'Sisimai::Lhost';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 55 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 55 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 55 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶sub description { 'Sendmail Open Source: https://sendmail.org/' }
Subroutine description: called CC=1 SCAR=0.0
Pod documented
8 1509 ▶sub inquire {
Subroutine inquire: called CC=65 SCAR=42.4
Pod documented
9 # Decode bounce messages from Sendmail Open Source
10 # @param [Hash] mhead Message headers of the bounce email
11 # @param [String] mbody Message body of the bounce email
12 # @return [Hash] The list of decoded bounces and a message/rfc822 part block
13 # @return [undef] failed to decode or the arguments are missing
14 # @see https://www.proofpoint.com/us/products/email-protection/open-source-email-solution
15 # @since v4.0.0
16 1509 my $class = shift;
17 1509 ▶ my $mhead = shift // return undef;
Condition100%2/2
shift() // (return undef)
l!l
15081
MC/DC100%1/1
shift() // (return undef)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
18 1508 ▶ my $mbody = shift // return undef;
Condition100%2/2
shift() // (return undef)
l!l
15071
MC/DC100%1/1
shift() // (return undef)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
19  
20 1507 ▶ return undef if $mhead->{'x-aol-ip'}; # X-AOL-IP is a header defined in AOL
Branch: 1 branch
BranchTrueFalseTotal
if $mhead->{"x-aol-ip"} 37 1470 1507
21 1470 ▶ my $match = 0; $match ||= 1 if index($mhead->{'subject'}, 'see transcript for details') > -1;
Branch: 1 branch
BranchTrueFalseTotal
if index($mhead->{"subject"}, "see transcript for details") != -1 524 946 1470
Condition50%1/2
$match ||= 1
l!l
0524
MC/DC0%0/1
$match ||= 1
$match
Truth table50%1/2
A
A= $match
Aresult
11
00
22 1470 ▶ $match ||= 1 if index($mhead->{'subject'}, 'Warning: ') == 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($mhead->{"subject"}, "Warning: ") == 0 20 1450 1470
Condition50%1/2
$match ||= 1
l!l
020
MC/DC0%0/1
$match ||= 1
$match
Truth table50%1/2
A
A= $match
Aresult
11
00
23 1470 ▶ return undef unless $match > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $match > 0 926 544 1470
24  
25 544 require Sisimai::RFC1123;
26 544 require Sisimai::SMTP::Reply;
27 544 require Sisimai::SMTP::Status;
28 544 require Sisimai::SMTP::Command;
29 544 state $indicators = __PACKAGE__->INDICATORS;
30 544 state $boundaries = ['Content-Type: message/rfc822', 'Content-Type: text/rfc822-headers'];
31 544 state $startingof = {
32 # savemail.c:1040|if (printheader && !putline(" ----- Transcript of session follows -----\n",
33 # savemail.c:1041| mci))
34 # savemail.c:1042| goto writeerr;
35 # savemail.c:1360|if (!putline(
36 # savemail.c:1361| sendbody
37 # savemail.c:1362| ? " ----- Original message follows -----\n"
38 # savemail.c:1363| : " ----- Message header follows -----\n",
39 'message' => [' ----- Transcript of session follows -----'],
40 'error' => ['... while talking to '],
41 };
42  
43 544 ▶ my $emailparts = Sisimai::RFC5322->part($mbody, $boundaries) // return undef; return undef unless $emailparts->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $emailparts->[0] 0 544 544
Condition50%1/2
"Sisimai::RFC5322"->part($mbody, $boundaries) // (return undef)
l!l
5440
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
44 544 my $fieldtable = Sisimai::RFC1894->FIELDTABLE;
45 544 my $dscontents = [__PACKAGE__->DELIVERYSTATUS]; my $v = undef;
46 544 my $permessage = {}; # (Hash) Store values of each Per-Message field
47 544 my $readcursor = 0; # (Integer) Points the current cursor position
48 544 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
49 544 my $thecommand = ''; # (String) SMTP Command name begin with the string '>>>'
50 544 my $esmtpreply = []; # (Array) Reply from remote server on SMTP session
51 544 my $sessionerr = 0; # (Integer) Flag, 1 if it is SMTP session error
52 544 my $anotherset = {}; # (Hash) Another error information
53 544 my $p = '';
54  
55 544 for my $e ( split("\n", $emailparts->[0]) ) {
56 # Read error messages and delivery status lines from the head of the email to the previous
57 # line of the beginning of the original message.
58 15385 ▶ unless( $readcursor ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($readcursor) 6361 9024 15385
59 # Beginning of the bounce message or the message/delivery-status part
60 6361 ▶ $readcursor |= $indicators->{'deliverystatus'} if index($e, $startingof->{'message'}->[0]) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($e, $startingof->{"message"}[0]) == 0 527 5834 6361
61 6361 next;
62 }
63 9024 ▶ next if ($readcursor & $indicators->{'deliverystatus'}) == 0 || $e eq "";
Branch: 1 branch
BranchTrueFalseTotal
if ($readcursor & $indicators->{"deliverystatus"}) == 0 or $e eq "" 1091 7933 9024
Condition66%2/3
($readcursor & $indicators->{"deliverystatus"}) == 0 or $e eq ""
l!l&&r!l&&!r
010917933
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
64  
65 7933 ▶ if( my $f = Sisimai::RFC1894->match($e) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = 'Sisimai::RFC1894'->match($e)) { } 4635 3298 7933
66 # $e matched with any field defined in RFC3464
67 4635 ▶ next unless my $o = Sisimai::RFC1894->field($e);
Branch: 1 branch
BranchTrueFalseTotal
unless my $o = "Sisimai::RFC1894"->field($e) 0 4635 4635
68 4635 $v = $dscontents->[-1];
69  
70 4635 ▶ if( $o->[3] eq 'addr' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($o->[3] eq 'addr') { } 600 4035 4635
elsif ($o->[3] eq 'code') { } 507 3528 4035
71 # Final-Recipient: rfc822; kijitora@example.jp
72 # X-Actual-Recipient: rfc822; kijitora@example.co.jp
73 600 ▶ if( $o->[0] eq 'final-recipient' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o->[0] eq 'final-recipient') { } 537 63 600
74 # Final-Recipient: rfc822; kijitora@example.jp
75 537 ▶ if( $v->{'recipient'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v->{"recipient"}) 10 527 537
76 # There are multiple recipient addresses in the message body.
77 10 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
78 10 $v = $dscontents->[-1];
79 }
80 537 $v->{'recipient'} = $o->[2];
81 537 $recipients++;
82  
83 } else {
84 # X-Actual-Recipient: rfc822; kijitora@example.co.jp
85 63 $v->{'alias'} = $o->[2];
86 }
87 } elsif( $o->[3] eq 'code' ) {
88 # Diagnostic-Code: SMTP; 550 5.1.1 <userunknown@example.jp>... User Unknown
89 507 $v->{'spec'} = $o->[1];
90 507 $v->{'diagnosis'} = $o->[2];
91  
92 } else {
93 # Other DSN fields defined in RFC3464
94 3528 ▶ next unless exists $fieldtable->{ $o->[0] };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$fieldtable{$o->[0]} 0 3528 3528
95 3528 ▶ next if $o->[3] eq "host" && Sisimai::RFC1123->is_internethost($o->[2]) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $o->[3] eq "host" and "Sisimai::RFC1123"->is_internethost($o->[2]) == 0 124 3404 3528
Condition100%3/3
$o->[3] eq "host" and "Sisimai::RFC1123"->is_internethost($o->[2]) == 0
!ll&&!rl&&r
21331271124
MC/DC100%2/2
$o->[3] eq "host" and "Sisimai::RFC1123"->is_internethost($o->[2]) == 0
$o->[3] eq "host" "Sisimai::RFC1123"->is_internethost($o->[2]) == 0
Truth table100%3/3
A and B
A= $o->[3] eq "host"B= "Sisimai::RFC1123"->is_internethost($o->[2]) == 0
ABresult
0X0
100
111
96 3404 $v->{ $fieldtable->{ $o->[0] } } = $o->[2];
97  
98 3404 ▶ next unless $f == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $f == 1 2013 1391 3404
99 1391 $permessage->{ $fieldtable->{ $o->[0] } } = $o->[2];
100 }
101 } else {
102 # The line does not begin with a DSN field defined in RFC3464
103 #
104 # ----- Transcript of session follows -----
105 # ... while talking to mta.example.org.:
106 # >>> DATA
107 # <<< 550 Unknown user recipient@example.jp
108 # 554 5.0.0 Service unavailable
109 3298 ▶ if( substr($e, 0, 1) ne ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($e, 0, 1) ne ' ') { } 3273 25 3298
110 # Other error messages
111 3273 ▶ if( index($e, '>>> ') == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (index($e, '>>> ') == 0) { } 515 2758 3273
elsif (index($e, '<<< ') == 0) { } 937 1821 2758
112 # >>> DATA (Client Command)
113 515 ▶ $thecommand ||= Sisimai::SMTP::Command->find($e);
Condition66%2/3
$thecommand ||= "Sisimai::SMTP::Command"->find($e)
l!l&&r!l&&!r
864290
MC/DC0%0/2
$thecommand ||= "Sisimai::SMTP::Command"->find($e)
$thecommand "Sisimai::SMTP::Command"->find($e)
Truth table66%2/3
A ||= B
A= $thecommandB= "Sisimai::SMTP::Command"->find($e)
ABresult
1X1
011
000
114  
115 } elsif( index($e, '<<< ') == 0 ) {
116 # <<< Response from the SMTP server
117 937 my $cv = substr($e, 4,);
118 937 ▶ push @$esmtpreply, $cv unless grep { $cv eq $_ } @$esmtpreply;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$cv eq $_;} @$esmtpreply 912 25 937
119  
120 } else {
121 # Detect an SMTP session error or a connection error
122 1821 ▶ next if $sessionerr;
Branch: 1 branch
BranchTrueFalseTotal
if $sessionerr 1145 676 1821
123 676 ▶ if( index($e, $startingof->{'error'}->[0]) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($e, $startingof->{"error"}[0]) == 0) 454 222 676
124 # ----- Transcript of session follows -----
125 # ... while talking to mta.example.org.:
126 454 $sessionerr = 1;
127 454 next;
128 }
129  
130 222 ▶ if( index($e, '<') == 0 && Sisimai::String->aligned(\$e, ['@', '>.', ' ']) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($e, '<') == 0 and 'Sisimai::String'->aligned(\$e, ['@', '>.', ' '])) { } 10 212 222
Condition66%2/3
index($e, "<") == 0 and "Sisimai::String"->aligned(\$e, ["\@", ">.", " "])
!ll&&!rl&&r
212010
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
131 # <kijitora@example.co.jp>... Deferred: Name server: example.co.jp.: host name lookup failure
132 10 $anotherset->{'recipient'} = Sisimai::Address->s3s4(substr($e, 0, index($e, '>')));
133 10 $anotherset->{'diagnosis'} = substr($e, index($e, ' ') + 1,);
134  
135 } else {
136 # ----- Transcript of session follows -----
137 # Message could not be delivered for too long
138 # Message will be deleted from queue
139 212 ▶ my $cr = Sisimai::SMTP::Reply->find($e) || '';
Condition100%2/2
'Sisimai::SMTP::Reply'->find($e) || ''
l!l
48164
MC/DC100%1/1
'Sisimai::SMTP::Reply'->find($e) || ''
'Sisimai::SMTP::Reply'->find($e)
Truth table100%2/2
A
A= 'Sisimai::SMTP::Reply'->find($e)
Aresult
11
00
140 212 ▶ my $cs = Sisimai::SMTP::Status->find($e) || '';
Condition100%2/2
'Sisimai::SMTP::Status'->find($e) || ''
l!l
48164
MC/DC100%1/1
'Sisimai::SMTP::Status'->find($e) || ''
'Sisimai::SMTP::Status'->find($e)
Truth table100%2/2
A
A= 'Sisimai::SMTP::Status'->find($e)
Aresult
11
00
141 212 ▶ if( length($cr.$cs) > 7 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $cr . $cs > 7) { } 48 164 212
elsif (index($e, 'Message ') == 0 or index($e, 'Warning: ') == 0) { } 45 119 164
142 # 550 5.1.2 <kijitora@example.org>... Message
143 #
144 # DBI connect('dbname=...')
145 # 554 5.3.0 unknown mailer error 255
146 48 $anotherset->{'status'} = $cs;
147 48 $anotherset->{'diagnosis'} .= ' '.$e;
148  
149 ▶ } elsif( index($e, 'Message ') == 0 || index($e, 'Warning: ') == 0 ) {
Condition100%3/3
index($e, "Message ") == 0 or index($e, "Warning: ") == 0
l!l&&r!l&&!r
405119
MC/DC100%2/2
index($e, "Message ") == 0 or index($e, "Warning: ") == 0
index($e, "Message ") == 0 index($e, "Warning: ") == 0
Truth table100%3/3
A or B
A= index($e, "Message ") == 0B= index($e, "Warning: ") == 0
ABresult
1X1
011
000
150 # Message could not be delivered for too long
151 # Warning: message still undelivered after 4 hours
152 45 $anotherset->{'diagnosis'} .= ' '.$e;
153 }
154 }
155 }
156 } else {
157 # Continued line of the value of Diagnostic-Code field
158 25 ▶ next if index($p, 'Diagnostic-Code:') != 0 || index($e, ' ') != 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($p, "Diagnostic-Code:") != 0 or index($e, " ") != 0 20 5 25
Condition66%2/3
index($p, "Diagnostic-Code:") != 0 or index($e, " ") != 0
l!l&&r!l&&!r
2005
MC/DC50%1/2
index($p, "Diagnostic-Code:") != 0 or index($e, " ") != 0
index($p, "Diagnostic-Code:") != 0 index($e, " ") != 0
Truth table66%2/3
A or B
A= index($p, "Diagnostic-Code:") != 0B= index($e, " ") != 0
ABresult
1X1
011
000
159 5 $v->{'diagnosis'} .= ' '.$e;
160 }
161 }
162 } continue {
163 # Save the current line for the next loop
164 15385 $p = $e;
165 }
166 544 ▶ return undef unless $recipients;
Branch: 1 branch
BranchTrueFalseTotal
unless $recipients 17 527 544
167  
168 527 require Sisimai::Eb;
169 527 for my $e ( @$dscontents ) {
170 # Set default values if each value is empty.
171 537 ▶ $e->{ $_ } ||= $permessage->{ $_ } || '' for keys %$permessage;
Condition60%3/5
$e->{$_} ||= $permessage->{$_} || ""
l!l&&r!l&&!r
1064100
$permessage->{$_} || ''
l!l
100
MC/DC0%0/2
$e->{$_} ||= $permessage->{$_} || ""
$e->{$_} $permessage->{$_}
Truth table66%2/3
A ||= B
A= $e->{$_}B= $permessage->{$_}
ABresult
1X1
011
000
172  
173 537 ▶ if( exists $anotherset->{'diagnosis'} && $anotherset->{'diagnosis'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $anotherset->{"diagnosis"} and $anotherset->{"diagnosis"}) 68 469 537
Condition66%2/3
exists $anotherset->{"diagnosis"} and $anotherset->{"diagnosis"}
!ll&&!rl&&r
469068
MC/DC50%1/2
exists $anotherset->{"diagnosis"} and $anotherset->{"diagnosis"}
exists $anotherset->{"diagnosis"} $anotherset->{"diagnosis"}
Truth table66%2/3
A and B
A= exists $anotherset->{"diagnosis"}B= $anotherset->{"diagnosis"}
ABresult
0X0
100
111
174 # Copy alternative error message
175 68 ▶ $e->{'diagnosis'} = $anotherset->{'diagnosis'} if index($e->{'diagnosis'}, ' ') == 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($e->{"diagnosis"}, " ") == 0 0 68 68
176 68 ▶ $e->{'diagnosis'} = $anotherset->{'diagnosis'} if $e->{'diagnosis'} =~ /\A\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $e->{"diagnosis"} =~ /\A\d+\z/u 15 53 68
177 68 ▶ $e->{'diagnosis'} ||= $anotherset->{'diagnosis'};
Condition66%2/3
$e->{"diagnosis"} ||= $anotherset->{"diagnosis"}
l!l&&r!l&&!r
38300
MC/DC0%0/2
$e->{"diagnosis"} ||= $anotherset->{"diagnosis"}
$e->{"diagnosis"} $anotherset->{"diagnosis"}
Truth table66%2/3
A ||= B
A= $e->{"diagnosis"}B= $anotherset->{"diagnosis"}
ABresult
1X1
011
000
178 }
179  
180 537 while(1) {
181 # Replace or append the error message in "diagnosis" with the ESMTP Reply Code when the
182 # following conditions have matched
183 537 ▶ last if scalar @$esmtpreply == 0 || $recipients != 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$esmtpreply == 0 or $recipients != 1 93 444 537
Condition100%3/3
scalar @$esmtpreply == 0 or $recipients != 1
l!l&&r!l&&!r
7320444
MC/DC100%2/2
scalar @$esmtpreply == 0 or $recipients != 1
scalar @$esmtpreply == 0 $recipients != 1
Truth table100%3/3
A or B
A= scalar @$esmtpreply == 0B= $recipients != 1
ABresult
1X1
011
000
184 444 $e->{'diagnosis'} = sprintf("%s %s", join(' ', @$esmtpreply), $e->{'diagnosis'});
185 444 last;
186 }
187 537 ▶ $e->{'command'} ||= $thecommand || Sisimai::SMTP::Command->find($e->{'diagnosis'}) || '';
Condition87%7/8
$e->{"command"} ||= $thecommand || "Sisimai::SMTP::Command"->find($e->{"diagnosis"}) || ""
l!l&&r!l&&!r
044493
$thecommand || 'Sisimai::SMTP::Command'->find($e->{'diagnosis'}) || ''
l!l
44493
$thecommand or "Sisimai::SMTP::Command"->find($e->{"diagnosis"})
l!l&&r!l&&!r
439593
MC/DC0%0/3
$e->{"command"} ||= $thecommand || "Sisimai::SMTP::Command"->find($e->{"diagnosis"}) || ""
$e->{"command"} $thecommand "Sisimai::SMTP::Command"->find($e->{"diagnosis"})
Truth table0%0/4
A ||= B or C
A= $e->{"command"}B= $thecommandC= "Sisimai::SMTP::Command"->find($e->{"diagnosis"})
ABCresult
1XX1
01X1
0011
0000
188 537 ▶ $e->{'command'} ||= $Sisimai::Eb::CeEHLO if scalar @$esmtpreply;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$esmtpreply 464 73 537
Condition66%2/3
$e->{"command"} ||= $Sisimai::Eb::CeEHLO
l!l&&r!l&&!r
444200
MC/DC0%0/2
$e->{"command"} ||= $Sisimai::Eb::CeEHLO
$e->{"command"} $Sisimai::Eb::CeEHLO
Truth table66%2/3
A ||= B
A= $e->{"command"}B= $Sisimai::Eb::CeEHLO
ABresult
1X1
011
000
189  
190 537 while(1) {
191 # Check alternative status code and override it
192 537 ▶ last unless exists $anotherset->{'status'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $anotherset->{"status"} 489 48 537
193 48 ▶ last unless length $anotherset->{'status'};
Branch: 1 branch
BranchTrueFalseTotal
unless length $anotherset->{"status"} 0 48 48
194 48 ▶ last if Sisimai::SMTP::Status->test($e->{'status'});
Branch: 1 branch
BranchTrueFalseTotal
if "Sisimai::SMTP::Status"->test($e->{"status"}) 48 0 48
195  
196 0 $e->{'status'} = $anotherset->{'status'};
197 0 last;
198 }
199 # @example.jp, no local part
200 # Get email address from the value of Diagnostic-Code field
201 537 ▶ next unless index($e->{'recipient'}, '@') == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless index($e->{"recipient"}, "\@") == 0 532 5 537
202 5 ▶ my $cv = Sisimai::Address->find($e->{'diagnosis'}, 1) || [];
Condition50%1/2
'Sisimai::Address'->find($e->{'diagnosis'}, 1) || []
l!l
50
MC/DC0%0/1
'Sisimai::Address'->find($e->{'diagnosis'}, 1) || []
'Sisimai::Address'->find($e->{'diagnosis'}, 1)
Truth table50%1/2
A
A= 'Sisimai::Address'->find($e->{'diagnosis'}, 1)
Aresult
11
00
203 5 ▶ $e->{'recipient'} = $cv->[0]->{'address'} if scalar @$cv;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$cv 5 0 5
204 }
205 527 return {"ds" => $dscontents, "rfc822" => $emailparts->[1]};
206 }
207  
208 55 1;
209 __END__
« Sisimai/RFC3464.pm ↑ Summary Sisimai/Address.pm »