File Coverage

lib/Sisimai/Lhost/AmazonWorkMail.pm
Criterion Covered Total %
statement 63 69 91.3
branch 33 42 78.5
condition 9 22 40.9
subroutine 6 6 100.0
pod 2 2 100.0
total 113 141 80.1


line stmt bran cond sub pod time code
1             package Sisimai::Lhost::AmazonWorkMail;
2 26     26   5317 use parent 'Sisimai::Lhost';
  26         46  
  26         164  
3 26     26   1392 use feature ':5.10';
  26         45  
  26         1629  
4 26     26   139 use strict;
  26         50  
  26         514  
5 26     26   126 use warnings;
  26         50  
  26         19407  
6              
7             # https://aws.amazon.com/workmail/
8 2     2 1 1001 sub description { 'Amazon WorkMail: https://aws.amazon.com/workmail/' }
9             sub make {
10             # Detect an error from Amazon WorkMail
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 425     425 1 993 my $class = shift;
17 425   100     1008 my $mhead = shift // return undef;
18 424   50     944 my $mbody = shift // return undef;
19 424         535 my $match = 0;
20 424   100     2305 my $xmail = $mhead->{'x-original-mailer'} || $mhead->{'x-mailer'} || '';
21              
22             # X-Mailer: Amazon WorkMail
23             # X-Original-Mailer: Amazon WorkMail
24             # X-Ses-Outgoing: 2016.01.14-54.240.27.159
25 424 100       931 $match++ if $mhead->{'x-ses-outgoing'};
26 424 100       780 if( $xmail ) {
27             # X-Mailer: Amazon WorkMail
28             # X-Original-Mailer: Amazon WorkMail
29 63 100       199 $match++ if $xmail eq 'Amazon WorkMail';
30             }
31 424 100       1163 return undef if $match < 2;
32              
33 36         81 state $indicators = __PACKAGE__->INDICATORS;
34 36         69 state $rebackbone = qr|^content-type:[ ]message/rfc822|m;
35 36         59 state $startingof = { 'message' => ['Technical report:'] };
36              
37 36         763 require Sisimai::RFC1894;
38 36         192 my $fieldtable = Sisimai::RFC1894->FIELDTABLE;
39 36         77 my $permessage = {}; # (Hash) Store values of each Per-Message field
40              
41 36         101 my $dscontents = [__PACKAGE__->DELIVERYSTATUS];
42 36         238 my $emailsteak = Sisimai::RFC5322->fillet($mbody, $rebackbone);
43 36         87 my $readcursor = 0; # (Integer) Points the current cursor position
44 36         63 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
45 36         59 my $v = undef;
46              
47 36         621 for my $e ( split("\n", $emailsteak->[0]) ) {
48             # Read error messages and delivery status lines from the head of the email
49             # to the previous line of the beginning of the original message.
50 1527 100       1737 unless( $readcursor ) {
51             # Beginning of the bounce message or message/delivery-status part
52 252 100       509 $readcursor |= $indicators->{'deliverystatus'} if index($e, $startingof->{'message'}->[0]) == 0;
53 252         263 next;
54             }
55 1275 50       1593 next unless $readcursor & $indicators->{'deliverystatus'};
56 1275 100       1597 next unless length $e;
57              
58 1059 100       1435 if( my $f = Sisimai::RFC1894->match($e) ) {
59             # $e matched with any field defined in RFC3464
60 180 50       313 next unless my $o = Sisimai::RFC1894->field($e);
61 180         237 $v = $dscontents->[-1];
62              
63 180 100       362 if( $o->[-1] eq 'addr' ) {
    100          
64             # Final-Recipient: rfc822; kijitora@example.jp
65             # X-Actual-Recipient: rfc822; kijitora@example.co.jp
66 36 50       111 if( $o->[0] eq 'final-recipient' ) {
67             # Final-Recipient: rfc822; kijitora@example.jp
68 36 50       118 if( $v->{'recipient'} ) {
69             # There are multiple recipient addresses in the message body.
70 0         0 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
71 0         0 $v = $dscontents->[-1];
72             }
73 36         89 $v->{'recipient'} = $o->[2];
74 36         90 $recipients++;
75              
76             } else {
77             # X-Actual-Recipient: rfc822; kijitora@example.co.jp
78 0         0 $v->{'alias'} = $o->[2];
79             }
80             } elsif( $o->[-1] eq 'code' ) {
81             # Diagnostic-Code: SMTP; 550 5.1.1 ... User Unknown
82 36         96 $v->{'spec'} = $o->[1];
83 36         85 $v->{'diagnosis'} = $o->[2];
84              
85             } else {
86             # Other DSN fields defined in RFC3464
87 108 50       221 next unless exists $fieldtable->{ $o->[0] };
88 108         203 $v->{ $fieldtable->{ $o->[0] } } = $o->[2];
89              
90 108 100       266 next unless $f == 1;
91 36         131 $permessage->{ $fieldtable->{ $o->[0] } } = $o->[2];
92             }
93             }
94              
95             #
96             #
97             #
98             #
99 987 100       1575 last if index($e, '') == 0;
100             }
101 36 50       198 return undef unless $recipients;
102              
103 36         83 for my $e ( @$dscontents ) {
104             # Set default values if each value is empty.
105 36   33     218 $e->{'lhost'} ||= $permessage->{'rhost'};
106 36   0     198 $e->{ $_ } ||= $permessage->{ $_ } || '' for keys %$permessage;
      33        
107 36         281 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'});
108              
109 36 50       149 if( $e->{'status'} =~ /\A[45][.][01][.]0\z/ ) {
110             # Get other D.S.N. value from the error message
111             # 5.1.0 - Unknown address error 550-'5.7.1 ...
112 0         0 my $errormessage = $e->{'diagnosis'};
113 0 0       0 $errormessage = $1 if $e->{'diagnosis'} =~ /["'](\d[.]\d[.]\d.+)['"]/;
114 0   0     0 $e->{'status'} = Sisimai::SMTP::Status->find($errormessage) || $e->{'status'};
115             }
116              
117             # 554 4.4.7 Message expired: unable to deliver in 840 minutes.
118             # <421 4.4.2 Connection timed out>
119 36 100       182 $e->{'replycode'} = $1 if $e->{'diagnosis'} =~ /[<]([245]\d\d)[ ].+[>]/;
120 36   50     279 $e->{'reason'} ||= Sisimai::SMTP::Status->name($e->{'status'}) || '';
      33        
121             }
122 36         293 return { 'ds' => $dscontents, 'rfc822' => $emailsteak->[1] };
123             }
124              
125             1;
126             __END__