File Coverage

lib/Sisimai/Lhost/Sendmail.pm
Criterion Covered Total %
statement 106 108 98.1
branch 67 74 90.5
condition 41 53 77.3
subroutine 6 6 100.0
pod 2 2 100.0
total 222 243 91.3


line stmt bran cond sub pod time code
1             package Sisimai::Lhost::Sendmail;
2 54     54   4410 use parent 'Sisimai::Lhost';
  54         110  
  54         473  
3 54     54   5037 use v5.26;
  54         201  
4 54     54   284 use strict;
  54         103  
  54         1782  
5 54     54   357 use warnings;
  54         103  
  54         96308  
6              
7 1     1 1 8 sub description { 'Sendmail Open Source: https://sendmail.org/' }
8             sub inquire {
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 1507     1507 1 4752 my $class = shift;
17 1507   100     4534 my $mhead = shift // return undef;
18 1506   100     4743 my $mbody = shift // return undef;
19              
20 1505 100       6354 return undef if $mhead->{'x-aol-ip'}; # X-AOL-IP is a header defined in AOL
21 1468 100 50     2590 my $match = 0; $match ||= 1 if index($mhead->{'subject'}, 'see transcript for details') > -1;
  1468         7015  
22 1468 100 50     8671 $match ||= 1 if index($mhead->{'subject'}, 'Warning: ') == 0;
23 1468 100       5429 return undef unless $match > 0;
24              
25 542         3097 require Sisimai::RFC1123;
26 542         2593 require Sisimai::SMTP::Reply;
27 542         2646 require Sisimai::SMTP::Status;
28 542         2572 require Sisimai::SMTP::Command;
29 542         1262 state $indicators = __PACKAGE__->INDICATORS;
30 542         845 state $boundaries = ['Content-Type: message/rfc822', 'Content-Type: text/rfc822-headers'];
31 542         972 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 542         3682 my $fieldtable = Sisimai::RFC1894->FIELDTABLE;
44 542         1033 my $permessage = {}; # (Hash) Store values of each Per-Message field
45 542         3016 my $dscontents = [__PACKAGE__->DELIVERYSTATUS]; my $v = undef;
  542         1224  
46 542         3007 my $emailparts = Sisimai::RFC5322->part($mbody, $boundaries);
47 542         976 my $readcursor = 0; # (Integer) Points the current cursor position
48 542         1211 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
49 542         1056 my $thecommand = ''; # (String) SMTP Command name begin with the string '>>>'
50 542         902 my $esmtpreply = []; # (Array) Reply from remote server on SMTP session
51 542         1048 my $sessionerr = 0; # (Integer) Flag, 1 if it is SMTP session error
52 542         858 my $anotherset = {}; # (Hash) Another error information
53 542         1161 my $p = '';
54              
55 542         6581 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 15331 100       25786 unless( $readcursor ) {
59             # Beginning of the bounce message or the message/delivery-status part
60 6339 100       13500 $readcursor |= $indicators->{'deliverystatus'} if index($e, $startingof->{'message'}->[0]) == 0;
61 6339         9137 next;
62             }
63 8992 100 66     34046 next if ($readcursor & $indicators->{'deliverystatus'}) == 0 || $e eq "";
64              
65 7905 100       23621 if( my $f = Sisimai::RFC1894->match($e) ) {
66             # $e matched with any field defined in RFC3464
67 4617 50       12262 next unless my $o = Sisimai::RFC1894->field($e);
68 4617         8425 $v = $dscontents->[-1];
69              
70 4617 100       11517 if( $o->[3] eq 'addr' ) {
    100          
71             # Final-Recipient: rfc822; kijitora@example.jp
72             # X-Actual-Recipient: rfc822; kijitora@example.co.jp
73 598 100       1583 if( $o->[0] eq 'final-recipient' ) {
74             # Final-Recipient: rfc822; kijitora@example.jp
75 535 100       1677 if( $v->{'recipient'} ) {
76             # There are multiple recipient addresses in the message body.
77 10         79 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
78 10         30 $v = $dscontents->[-1];
79             }
80 535         1205 $v->{'recipient'} = $o->[2];
81 535         1463 $recipients++;
82              
83             } else {
84             # X-Actual-Recipient: rfc822; kijitora@example.co.jp
85 63         234 $v->{'alias'} = $o->[2];
86             }
87             } elsif( $o->[3] eq 'code' ) {
88             # Diagnostic-Code: SMTP; 550 5.1.1 ... User Unknown
89 505         1400 $v->{'spec'} = $o->[1];
90 505         1580 $v->{'diagnosis'} = $o->[2];
91              
92             } else {
93             # Other DSN fields defined in RFC3464
94 3514 50       11440 next unless exists $fieldtable->{ $o->[0] };
95 3514 100 100     12504 next if $o->[3] eq "host" && Sisimai::RFC1123->is_internethost($o->[2]) == 0;
96 3392         17221 $v->{ $fieldtable->{ $o->[0] } } = $o->[2];
97              
98 3392 100       9705 next unless $f == 1;
99 1387         5881 $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 3288 100       6902 if( substr($e, 0, 1) ne ' ') {
110             # Other error messages
111 3263 100       10203 if( index($e, '>>> ') == 0 ) {
    100          
112             # >>> DATA (Client Command)
113 513   66     6384 $thecommand ||= Sisimai::SMTP::Command->find($e);
114              
115             } elsif( index($e, '<<< ') == 0 ) {
116             # <<< Response from the SMTP server
117 935         2192 my $cv = substr($e, 4,);
118 935 100       2669 push @$esmtpreply, $cv unless grep { $cv eq $_ } @$esmtpreply;
  943         2368  
119              
120             } else {
121             # Detect an SMTP session error or a connection error
122 1815 100       4279 next if $sessionerr;
123 672 100       2954 if( index($e, $startingof->{'error'}->[0]) == 0 ) {
124             # ----- Transcript of session follows -----
125             # ... while talking to mta.example.org.:
126 453         982 $sessionerr = 1;
127 453         950 next;
128             }
129              
130 219 100 66     739 if( index($e, '<') == 0 && Sisimai::String->aligned(\$e, ['@', '>.', ' ']) ) {
131             # ... Deferred: Name server: example.co.jp.: host name lookup failure
132 10         140 $anotherset->{'recipient'} = Sisimai::Address->s3s4(substr($e, 0, index($e, '>')));
133 10         72 $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 209   100     1120 my $cr = Sisimai::SMTP::Reply->find($e) || '';
140 209   100     1132 my $cs = Sisimai::SMTP::Status->find($e) || '';
141 209 100 100     1314 if( length($cr.$cs) > 7 ) {
    100          
142             # 550 5.1.2 ... Message
143             #
144             # DBI connect('dbname=...')
145             # 554 5.3.0 unknown mailer error 255
146 47         154 $anotherset->{'status'} = $cs;
147 47         242 $anotherset->{'diagnosis'} .= ' '.$e;
148              
149             } elsif( index($e, 'Message ') == 0 || index($e, 'Warning: ') == 0 ) {
150             # Message could not be delivered for too long
151             # Warning: message still undelivered after 4 hours
152 45         218 $anotherset->{'diagnosis'} .= ' '.$e;
153             }
154             }
155             }
156             } else {
157             # Continued line of the value of Diagnostic-Code field
158 25 100 66     206 next if index($p, 'Diagnostic-Code:') != 0 || index($e, ' ') != 0;
159 5         41 $v->{'diagnosis'} .= ' '.Sisimai::String->sweep($e);
160             }
161             }
162             } continue {
163             # Save the current line for the next loop
164 15331         26434 $p = $e;
165             }
166 542 100       4053 return undef unless $recipients;
167              
168 525         1315 for my $e ( @$dscontents ) {
169             # Set default values if each value is empty.
170 535   50     3855 $e->{ $_ } ||= $permessage->{ $_ } || '' for keys %$permessage;
      66        
171              
172 535 50 66     2064 if( exists $anotherset->{'diagnosis'} && $anotherset->{'diagnosis'} ) {
173             # Copy alternative error message
174 67 50       238 $e->{'diagnosis'} = $anotherset->{'diagnosis'} if index($e->{'diagnosis'}, ' ') == 0;
175 67 100       433 $e->{'diagnosis'} = $anotherset->{'diagnosis'} if $e->{'diagnosis'} =~ /\A\d+\z/;
176 67   66     337 $e->{'diagnosis'} ||= $anotherset->{'diagnosis'};
177             }
178              
179 535         1001 while(1) {
180             # Replace or append the error message in "diagnosis" with the ESMTP Reply Code when the
181             # following conditions have matched
182 535 100 100     3008 last if scalar @$esmtpreply == 0 || $recipients != 1;
183 443         2828 $e->{'diagnosis'} = sprintf("%s %s", join(' ', @$esmtpreply), $e->{'diagnosis'});
184 443         860 last;
185             }
186 535         2165 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'});
187 535   100     4806 $e->{'command'} ||= $thecommand || Sisimai::SMTP::Command->find($e->{'diagnosis'}) || '';
      66        
188 535 100 100     2298 $e->{'command'} ||= 'EHLO' if scalar @$esmtpreply;
189              
190 535         852 while(1) {
191             # Check alternative status code and override it
192 535 100       2153 last unless exists $anotherset->{'status'};
193 47 50       145 last unless length $anotherset->{'status'};
194 47 50       226 last if Sisimai::SMTP::Status->test($e->{'status'});
195              
196 0         0 $e->{'status'} = $anotherset->{'status'};
197 0         0 last;
198             }
199             # @example.jp, no local part
200             # Get email address from the value of Diagnostic-Code field
201 535 100       2482 next unless index($e->{'recipient'}, '@') == 0;
202 5   50     56 my $cv = Sisimai::Address->find($e->{'diagnosis'}, 1) || [];
203 5 50       35 $e->{'recipient'} = $cv->[0]->{'address'} if scalar @$cv;
204             }
205 525         5434 return {"ds" => $dscontents, "rfc822" => $emailparts->[1]};
206             }
207              
208             1;
209             __END__