File Coverage

lib/Sisimai/Lhost/ReceivingSES.pm
Criterion Covered Total %
statement 71 75 94.6
branch 30 38 78.9
condition 11 20 55.0
subroutine 6 6 100.0
pod 2 2 100.0
total 120 141 85.1


line stmt bran cond sub pod time code
1             package Sisimai::Lhost::ReceivingSES;
2 17     17   5151 use parent 'Sisimai::Lhost';
  17         30  
  17         84  
3 17     17   903 use feature ':5.10';
  17         31  
  17         992  
4 17     17   81 use strict;
  17         30  
  17         305  
5 17     17   63 use warnings;
  17         26  
  17         14255  
6              
7             # https://aws.amazon.com/ses/
8 2     2 1 945 sub description { 'Amazon SES(Receiving): https://aws.amazon.com/ses/' };
9             sub make {
10             # Detect an error from Amazon SES/Receiving
11             # @param [Hash] mhead Message headers of a bounce email
12             # @param [String] mbody Message body of a bounce email
13             # @return [Hash] Bounce data list and message/rfc822 part
14             # @return [Undef] failed to parse or the arguments are missing
15             # @since v4.1.29
16 325     325 1 801 my $class = shift;
17 325   100     697 my $mhead = shift // return undef;
18 324   50     1001 my $mbody = shift // return undef;
19              
20             # X-SES-Outgoing: 2015.10.01-54.240.27.7
21             # Feedback-ID: 1.us-west-2.HX6/J9OVlHTadQhEu1+wdF9DBj6n6Pa9sW5Y/0pSOi8=:AmazonSES
22 324 100       1812 return undef unless $mhead->{'x-ses-outgoing'};
23              
24 47         108 state $indicators = __PACKAGE__->INDICATORS;
25 47         102 state $rebackbone = qr|^content-type:[ ]text/rfc822-headers|m;
26 47         76 state $startingof = { 'message' => ['This message could not be delivered.'] };
27 47         106 state $messagesof = {
28             # The followings are error messages in Rule sets/*/Actions/Template
29             'filtered' => ['Mailbox does not exist'],
30             'mesgtoobig' => ['Message too large'],
31             'mailboxfull' => ['Mailbox full'],
32             'contenterror' => ['Message content rejected'],
33             };
34              
35 47         146 require Sisimai::RFC1894;
36 47         131 my $fieldtable = Sisimai::RFC1894->FIELDTABLE;
37 47         111 my $permessage = {}; # (Hash) Store values of each Per-Message field
38              
39 47         164 my $dscontents = [__PACKAGE__->DELIVERYSTATUS];
40 47         188 my $emailsteak = Sisimai::RFC5322->fillet($mbody, $rebackbone);
41 47         104 my $readcursor = 0; # (Integer) Points the current cursor position
42 47         72 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
43 47         67 my $v = undef;
44 47         67 my $p = '';
45              
46 47         694 for my $e ( split("\n", $emailsteak->[0]) ) {
47             # Read error messages and delivery status lines from the head of the email
48             # to the previous line of the beginning of the original message.
49 2134 100       2405 unless( $readcursor ) {
50             # Beginning of the bounce message or message/delivery-status part
51 910 100       1462 $readcursor |= $indicators->{'deliverystatus'} if index($e, $startingof->{'message'}->[0]) == 0;
52 910         784 next;
53             }
54 1224 50       1605 next unless $readcursor & $indicators->{'deliverystatus'};
55 1224 100       1388 next unless length $e;
56              
57 1080 100       1526 if( my $f = Sisimai::RFC1894->match($e) ) {
58             # $e matched with any field defined in RFC3464
59 252 50       465 next unless my $o = Sisimai::RFC1894->field($e);
60 252         316 $v = $dscontents->[-1];
61              
62 252 100       477 if( $o->[-1] eq 'addr' ) {
    100          
63             # Final-Recipient: rfc822; kijitora@example.jp
64             # X-Actual-Recipient: rfc822; kijitora@example.co.jp
65 72 100       197 if( $o->[0] eq 'final-recipient' ) {
66             # Final-Recipient: rfc822; kijitora@example.jp
67 36 50       92 if( $v->{'recipient'} ) {
68             # There are multiple recipient addresses in the message body.
69 0         0 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
70 0         0 $v = $dscontents->[-1];
71             }
72 36         80 $v->{'recipient'} = $o->[2];
73 36         79 $recipients++;
74              
75             } else {
76             # X-Actual-Recipient: rfc822; kijitora@example.co.jp
77 36         113 $v->{'alias'} = $o->[2];
78             }
79             } elsif( $o->[-1] eq 'code' ) {
80             # Diagnostic-Code: SMTP; 550 5.1.1 ... User Unknown
81 36         109 $v->{'spec'} = $o->[1];
82 36         87 $v->{'diagnosis'} = $o->[2];
83              
84             } else {
85             # Other DSN fields defined in RFC3464
86 144 50       274 next unless exists $fieldtable->{ $o->[0] };
87 144         240 $v->{ $fieldtable->{ $o->[0] } } = $o->[2];
88              
89 144 100       271 next unless $f == 1;
90 72         201 $permessage->{ $fieldtable->{ $o->[0] } } = $o->[2];
91             }
92             } else {
93             # Continued line of the value of Diagnostic-Code field
94 828 50       1254 next unless index($p, 'Diagnostic-Code:') == 0;
95 0 0       0 next unless $e =~ /\A[ \t]+(.+)\z/;
96 0         0 $v->{'diagnosis'} .= ' '.$1;
97             }
98             } continue {
99             # Save the current line for the next loop
100 2134         2340 $p = $e;
101             }
102 47 100       321 return undef unless $recipients;
103              
104 36         100 for my $e ( @$dscontents ) {
105             # Set default values if each value is empty.
106 36   33     177 $e->{'lhost'} ||= $permessage->{'rhost'};
107 36   0     245 $e->{ $_ } ||= $permessage->{ $_ } || '' for keys %$permessage;
      33        
108 36         106 $e->{'diagnosis'} =~ y/\n/ /;
109 36         270 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'});
110              
111 36 100       194 if( $e->{'status'} =~ /\A[45][.][01][.]0\z/ ) {
112             # Get other D.S.N. value from the error message
113             # 5.1.0 - Unknown address error 550-'5.7.1 ...
114 5         16 my $errormessage = $e->{'diagnosis'};
115 5 50       21 $errormessage = $1 if $e->{'diagnosis'} =~ /["'](\d[.]\d[.]\d.+)['"]/;
116 5   33     28 $e->{'status'} = Sisimai::SMTP::Status->find($errormessage) || $e->{'status'};
117             }
118              
119 36         153 SESSION: for my $r ( keys %$messagesof ) {
120             # Verify each regular expression of session errors
121 111 100       122 next unless grep { index($e->{'diagnosis'}, $_) > -1 } @{ $messagesof->{ $r } };
  111         342  
  111         170  
122 26         62 $e->{'reason'} = $r;
123 26         43 last;
124             }
125 36   100     161 $e->{'reason'} ||= Sisimai::SMTP::Status->name($e->{'status'}) || '';
      100        
126             }
127 36         275 return { 'ds' => $dscontents, 'rfc822' => $emailsteak->[1] };
128             }
129              
130             1;
131             __END__