File Coverage

lib/Sisimai/Lhost/X3.pm
Criterion Covered Total %
statement 46 48 95.8
branch 20 22 90.9
condition 10 12 83.3
subroutine 6 6 100.0
pod 2 2 100.0
total 84 90 93.3


line stmt bran cond sub pod time code
1             package Sisimai::Lhost::X3;
2 34     34   5042 use parent 'Sisimai::Lhost';
  34         114  
  34         229  
3 34     34   2761 use v5.26;
  34         140  
4 34     34   175 use strict;
  34         91  
  34         943  
5 34     34   169 use warnings;
  34         62  
  34         28340  
6              
7 1     1 1 5 sub description { 'Unknown MTA #3' }
8             sub inquire {
9             # Detect an error from Unknown MTA #3
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.9
15 883     883 1 3295 my $class = shift;
16 883   100     2524 my $mhead = shift // return undef;
17 882   100     2248 my $mbody = shift // return undef;
18              
19 881 100       4328 return undef unless index($mhead->{'from'}, 'Mail Delivery System') == 0;
20 45 100       264 return undef unless index($mhead->{'subject'}, 'Delivery status notification') == 0;
21              
22 26         125 require Sisimai::SMTP::Command;
23 26         89 state $indicators = __PACKAGE__->INDICATORS;
24 26         52 state $boundaries = ['Content-Type: message/rfc822'];
25 26         74 state $startingof = {'message' => [' This is an automatically generated Delivery Status Notification.']};
26              
27 26         185 my $dscontents = [__PACKAGE__->DELIVERYSTATUS]; my $v = undef;
  26         65  
28 26         138 my $emailparts = Sisimai::RFC5322->part($mbody, $boundaries);
29 26         83 my $readcursor = 0; # (Integer) Points the current cursor position
30 26         45 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
31              
32 26         370 for my $e ( split("\n", $emailparts->[0]) ) {
33             # Read error messages and delivery status lines from the head of the email to the previous
34             # line of the beginning of the original message.
35 472 100       819 unless( $readcursor ) {
36             # Beginning of the bounce message or message/delivery-status part
37 94 100       302 $readcursor |= $indicators->{'deliverystatus'} if index($e, $startingof->{'message'}->[0]) == 0;
38 94         142 next;
39             }
40 378 100 66     1224 next if ($readcursor & $indicators->{'deliverystatus'}) == 0 || $e eq "";
41              
42             # ============================================================================
43             # This is an automatically generated Delivery Status Notification.
44             #
45             # Delivery to the following recipients failed permanently:
46             #
47             # * kijitora@example.com
48             #
49             #
50             # ============================================================================
51             # Technical details:
52             #
53             # SMTP:RCPT host 192.0.2.8: 553 5.3.0 ... No such user here
54             #
55             #
56             # ============================================================================
57 191         251 $v = $dscontents->[-1];
58              
59 191 100 66     538 if( index($e, ' * ') > -1 && index($e, '@') > 1 ) {
60             # * kijitora@example.com
61 26 50       86 if( $v->{'recipient'} ) {
62             # There are multiple recipient addresses in the message body.
63 0         0 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
64 0         0 $v = $dscontents->[-1];
65             }
66 26         117 $v->{'recipient'} = substr($e, index($e, ' * ') + 3,);
67 26         59 $recipients++;
68              
69             } else {
70             # Detect error message
71 165 100       598 if( index($e, 'SMTP:') == 0 ) {
    100          
    100          
72             # SMTP:RCPT host 192.0.2.8: 553 5.3.0 ... No such user here
73 11         128 $v->{'command'} = Sisimai::SMTP::Command->find($e);
74 11         31 $v->{'diagnosis'} = $e;
75              
76             } elsif( index($e, 'Routing: ') == 0 ) {
77             # Routing: Could not find a gateway for kijitora@example.co.jp
78 5         17 $v->{'diagnosis'} = substr($e, 9,);
79              
80             } elsif( index($e, 'Diagnostic-Code: smtp; ') == 0 ) {
81             # Diagnostic-Code: smtp; 552 5.2.2 Over quota
82 5         26 $v->{'diagnosis'} = substr($e, index($e, ';') + 2,);
83             }
84             }
85             }
86 26 50       132 return undef unless $recipients;
87              
88 26         57 for my $e ( @$dscontents ) {
89 26         237 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'});
90 26   100     217 $e->{'status'} = Sisimai::SMTP::Status->find($e->{'diagnosis'}) || '';
91             }
92 26         205 return {"ds" => $dscontents, "rfc822" => $emailparts->[1]};
93             }
94              
95             1;
96             __END__