File Coverage

lib/Sisimai/Lhost/X3.pm
Criterion Covered Total %
statement 43 45 95.5
branch 20 22 90.9
condition 8 10 80.0
subroutine 6 6 100.0
pod 2 2 100.0
total 79 85 92.9


line stmt bran cond sub pod time code
1             package Sisimai::Lhost::X3;
2 35     35   3273 use parent 'Sisimai::Lhost';
  35         54  
  35         182  
3 35     35   2300 use v5.26;
  35         97  
4 35     35   288 use strict;
  35         90  
  35         784  
5 35     35   118 use warnings;
  35         46  
  35         16603  
6              
7 1     1 1 2 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 881     881 1 2649 my $class = shift;
16 881   100     1827 my $mhead = shift // return undef;
17 880   100     1487 my $mbody = shift // return undef;
18              
19 879 100       2757 return undef unless index($mhead->{'from'}, 'Mail Delivery System') == 0;
20 45 100       131 return undef unless index($mhead->{'subject'}, 'Delivery status notification') == 0;
21              
22 26         61 require Sisimai::SMTP::Command;
23 26         69 state $indicators = __PACKAGE__->INDICATORS;
24 26         29 state $boundaries = ['Content-Type: message/rfc822'];
25 26         43 state $startingof = {'message' => [' This is an automatically generated Delivery Status Notification.']};
26              
27 26         107 my $dscontents = [__PACKAGE__->DELIVERYSTATUS]; my $v = undef;
  26         33  
28 26         88 my $emailparts = Sisimai::RFC5322->part($mbody, $boundaries);
29 26         32 my $readcursor = 0; # (Integer) Points the current cursor position
30 26         27 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
31              
32 26         156 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       523 unless( $readcursor ) {
36             # Beginning of the bounce message or message/delivery-status part
37 94 100       197 $readcursor |= $indicators->{'deliverystatus'} if index($e, $startingof->{'message'}->[0]) == 0;
38 94         83 next;
39             }
40 378 100 66     699 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         152 $v = $dscontents->[-1];
58              
59 191 100 66     314 if( index($e, ' * ') > -1 && index($e, '@') > 1 ) {
60             # * kijitora@example.com
61 26 50       45 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         59 $v->{'recipient'} = substr($e, index($e, ' * ') + 3,);
67 26         27 $recipients++;
68              
69             } else {
70             # Detect error message
71 165 100       327 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         82 $v->{'command'} = Sisimai::SMTP::Command->find($e);
74 11         19 $v->{'diagnosis'} = $e;
75              
76             } elsif( index($e, 'Routing: ') == 0 ) {
77             # Routing: Could not find a gateway for kijitora@example.co.jp
78 5         11 $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         17 $v->{'diagnosis'} = substr($e, index($e, ';') + 2,);
83             }
84             }
85             }
86 26 50       76 return undef unless $recipients;
87 26         97 return {"ds" => $dscontents, "rfc822" => $emailparts->[1]};
88             }
89              
90             1;
91             __END__