File Coverage

lib/Sisimai/Lhost/X3.pm
Criterion Covered Total %
statement 47 49 95.9
branch 20 24 83.3
condition 5 6 83.3
subroutine 6 6 100.0
pod 2 2 100.0
total 80 87 91.9


line stmt bran cond sub pod time code
1             package Sisimai::Lhost::X3;
2 14     14   6492 use parent 'Sisimai::Lhost';
  14         22  
  14         68  
3 14     14   727 use feature ':5.10';
  14         21  
  14         823  
4 14     14   69 use strict;
  14         18  
  14         239  
5 14     14   57 use warnings;
  14         19  
  14         7845  
6              
7 2     2 1 1028 sub description { 'Unknown MTA #3' }
8             sub make {
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 parse or the arguments are missing
14             # @since v4.1.9
15 208     208 1 552 my $class = shift;
16 208   100     472 my $mhead = shift // return undef;
17 207   50     399 my $mbody = shift // return undef;
18              
19 207 100       717 return undef unless index($mhead->{'from'}, 'Mail Delivery System') == 0;
20 26 50       102 return undef unless index($mhead->{'subject'}, 'Delivery status notification') == 0;
21              
22 26         69 state $indicators = __PACKAGE__->INDICATORS;
23 26         50 state $rebackbone = qr|^Content-Type:[ ]message/rfc822|m;
24 26         43 state $startingof = { 'message' => [' This is an automatically generated Delivery Status Notification.'] };
25              
26 26         87 my $dscontents = [__PACKAGE__->DELIVERYSTATUS];
27 26         157 my $emailsteak = Sisimai::RFC5322->fillet($mbody, $rebackbone);
28 26         51 my $readcursor = 0; # (Integer) Points the current cursor position
29 26         50 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
30 26         42 my $v = undef;
31              
32 26         177 for my $e ( split("\n", $emailsteak->[0]) ) {
33             # Read error messages and delivery status lines from the head of the email
34             # to the previous line of the beginning of the original message.
35 415 100       453 unless( $readcursor ) {
36             # Beginning of the bounce message or message/delivery-status part
37 94 100       250 $readcursor |= $indicators->{'deliverystatus'} if index($e, $startingof->{'message'}->[0]) == 0;
38 94         117 next;
39             }
40 321 50       384 next unless $readcursor & $indicators->{'deliverystatus'};
41 321 100       414 next unless length $e;
42              
43             # ============================================================================
44             # This is an automatically generated Delivery Status Notification.
45             #
46             # Delivery to the following recipients failed permanently:
47             #
48             # * kijitora@example.com
49             #
50             #
51             # ============================================================================
52             # Technical details:
53             #
54             # SMTP:RCPT host 192.0.2.8: 553 5.3.0 ... No such user here
55             #
56             #
57             # ============================================================================
58 196         172 $v = $dscontents->[-1];
59              
60 196 100       339 if( $e =~ /\A[ \t]+[*][ \t]([^ ]+[@][^ ]+)\z/ ) {
61             # * kijitora@example.com
62 26 50       77 if( $v->{'recipient'} ) {
63             # There are multiple recipient addresses in the message body.
64 0         0 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
65 0         0 $v = $dscontents->[-1];
66             }
67 26         65 $v->{'recipient'} = $1;
68 26         33 $recipients++;
69              
70             } else {
71             # Detect error message
72 170 100       403 if( $e =~ /\ASMTP:([^ ]+)[ \t](.+)\z/ ) {
    100          
    100          
73             # SMTP:RCPT host 192.0.2.8: 553 5.3.0 ... No such user here
74 11         34 $v->{'command'} = uc $1;
75 11         30 $v->{'diagnosis'} = $2;
76              
77             } elsif( $e =~ /\ARouting: (.+)/ ) {
78             # Routing: Could not find a gateway for kijitora@example.co.jp
79 5         15 $v->{'diagnosis'} = $1;
80              
81             } elsif( $e =~ /\ADiagnostic-Code: smtp; (.+)/ ) {
82             # Diagnostic-Code: smtp; 552 5.2.2 Over quota
83 5         22 $v->{'diagnosis'} = $1;
84             }
85             }
86             }
87 26 50       84 return undef unless $recipients;
88              
89 26         52 for my $e ( @$dscontents ) {
90 26         184 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'});
91 26   100     140 $e->{'status'} = Sisimai::SMTP::Status->find($e->{'diagnosis'}) || '';
92             }
93 26         129 return { 'ds' => $dscontents, 'rfc822' => $emailsteak->[1] };
94             }
95              
96             1;
97             __END__