File Coverage

lib/Sisimai/Lhost/EZweb.pm
Criterion Covered Total %
statement 87 92 94.5
branch 48 64 75.0
condition 16 22 72.7
subroutine 6 6 100.0
pod 2 2 100.0
total 159 186 85.4


line stmt bran cond sub pod time code
1             package Sisimai::Lhost::EZweb;
2 14     14   7775 use parent 'Sisimai::Lhost';
  14         28  
  14         74  
3 14     14   890 use feature ':5.10';
  14         27  
  14         964  
4 14     14   77 use strict;
  14         26  
  14         287  
5 14     14   67 use warnings;
  14         34  
  14         20799  
6              
7 2     2 1 1216 sub description { 'au EZweb: http://www.au.kddi.com/mobile/' }
8             sub make {
9             # Detect an error from EZweb
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.0.0
15 222     222 1 755 my $class = shift;
16 222   100     675 my $mhead = shift // return undef;
17 221   50     588 my $mbody = shift // return undef;
18 221         308 my $match = 0;
19              
20             # Pre-process email headers of NON-STANDARD bounce message au by EZweb, as
21             # known as ezweb.ne.jp.
22             # Subject: Mail System Error - Returned Mail
23             # From:
24             # Received: from ezweb.ne.jp (wmflb12na02.ezweb.ne.jp [222.15.69.197])
25             # Received: from nmomta.auone-net.jp ([aaa.bbb.ccc.ddd]) by ...
26             #
27 221 100       819 $match++ if rindex($mhead->{'from'}, 'Postmaster@ezweb.ne.jp') > -1;
28 221 50       725 $match++ if rindex($mhead->{'from'}, 'Postmaster@au.com') > -1;
29 221 100       692 $match++ if $mhead->{'subject'} eq 'Mail System Error - Returned Mail';
30 221 100       391 $match++ if grep { rindex($_, 'ezweb.ne.jp (EZweb Mail) with') > -1 } @{ $mhead->{'received'} };
  326         1196  
  221         605  
31 221 50       369 $match++ if grep { rindex($_, '.au.com (') > -1 } @{ $mhead->{'received'} };
  326         988  
  221         603  
32 221 100       630 if( defined $mhead->{'message-id'} ) {
33 201 100       799 $match++ if substr($mhead->{'message-id'}, -13, 13) eq '.ezweb.ne.jp>';
34 201 50       731 $match++ if substr($mhead->{'message-id'}, -8, 8) eq '.au.com>';
35             }
36 221 100       678 return undef if $match < 2;
37              
38 40         100 state $indicators = __PACKAGE__->INDICATORS;
39 40         85 state $rebackbone = qr<^(?:[-]{50}|Content-Type:[ ]*message/rfc822)>m;
40 40         263 my $markingsof = {
41             'message' => qr{\A(?:
42             The[ ]user[(]s[)][ ]
43             |Your[ ]message[ ]
44             |Each[ ]of[ ]the[ ]following
45             |[<][^ ]+[@][^ ]+[>]\z
46             )
47             }x,
48             'boundary' => qr/\A__SISIMAI_PSEUDO_BOUNDARY__\z/,
49             };
50 40         121 state $refailures = {
51             #'notaccept' => [qr/The following recipients did not receive this message:/],
52             'mailboxfull' => [qr/The user[(]s[)] account is temporarily over quota/],
53             'suspend' => [
54             # http://www.naruhodo-au.kddi.com/qa3429203.html
55             # The recipient may be unpaid user...?
56             qr/The user[(]s[)] account is disabled[.]/,
57             qr/The user[(]s[)] account is temporarily limited[.]/,
58             ],
59             'expired' => [
60             # Your message was not delivered within 0 days and 1 hours.
61             # Remote host is not responding.
62             qr/Your message was not delivered within /,
63             ],
64             'onhold' => [qr/Each of the following recipients was rejected by a remote mail server/],
65             };
66              
67 40         542 require Sisimai::RFC1894;
68 40         228 my $fieldtable = Sisimai::RFC1894->FIELDTABLE;
69 40         157 my $dscontents = [__PACKAGE__->DELIVERYSTATUS];
70 40         218 my $emailsteak = Sisimai::RFC5322->fillet($mbody, $rebackbone);
71 40         99 my $readcursor = 0; # (Integer) Points the current cursor position
72 40         72 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
73 40         71 my $v = undef;
74              
75 40 50       121 if( $mhead->{'content-type'} ) {
76             # Get the boundary string and set regular expression for matching with
77             # the boundary string.
78 40         171 my $b0 = Sisimai::MIME->boundary($mhead->{'content-type'}, 1);
79 40 100       733 $markingsof->{'boundary'} = qr/\A\Q$b0\E\z/ if $b0; # Convert to regular expression
80             }
81 40         115 my @rxmessages; push @rxmessages, @{ $refailures->{ $_ } } for keys %$refailures;
  40         174  
  160         338  
82              
83 40         307 for my $e ( split("\n", $emailsteak->[0]) ) {
84             # Read error messages and delivery status lines from the head of the email
85             # to the previous line of the beginning of the original message.
86 555 100       844 unless( $readcursor ) {
87             # Beginning of the bounce message or message/delivery-status part
88 293 100       991 $readcursor |= $indicators->{'deliverystatus'} if $e =~ $markingsof->{'message'};
89             }
90 555 100       1009 next unless $readcursor & $indicators->{'deliverystatus'};
91 302 100       473 next unless length $e;
92              
93             # The user(s) account is disabled.
94             #
95             # <***@ezweb.ne.jp>: 550 user unknown (in reply to RCPT TO command)
96             #
97             # -- OR --
98             # Each of the following recipients was rejected by a remote
99             # mail server.
100             #
101             # Recipient: <******@ezweb.ne.jp>
102             # >>> RCPT TO:<******@ezweb.ne.jp>
103             # <<< 550 <******@ezweb.ne.jp>: User unknown
104 214         298 $v = $dscontents->[-1];
105              
106 214 100 100     1430 if( $e =~ /\A[<]([^ ]+[@][^ ]+)[>]\z/ ||
    100 100        
107             $e =~ /\A[<]([^ ]+[@][^ ]+)[>]:?(.*)\z/ ||
108             $e =~ /\A[ \t]+Recipient: [<]([^ ]+[@][^ ]+)[>]/ ) {
109              
110 40 50       106 if( $v->{'recipient'} ) {
111             # There are multiple recipient addresses in the message body.
112 0         0 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
113 0         0 $v = $dscontents->[-1];
114             }
115              
116 40         279 my $r = Sisimai::Address->s3s4($1);
117 40         105 $v->{'recipient'} = $r;
118 40         78 $recipients++;
119              
120             } elsif( my $f = Sisimai::RFC1894->match($e) ) {
121             # $e matched with any field defined in RFC3464
122 45 50       86 next unless my $o = Sisimai::RFC1894->field($e);
123 45 50       111 next unless exists $fieldtable->{ $o->[0] };
124 45         131 $v->{ $fieldtable->{ $o->[0] } } = $o->[2];
125              
126             } else {
127             # The line does not begin with a DSN field defined in RFC3464
128 129 50       412 next if Sisimai::String->is_8bit(\$e);
129 129 100       414 if( $e =~ /\A[ \t]+[>]{3}[ \t]+([A-Z]{4})/ ) {
130             # >>> RCPT TO:<******@ezweb.ne.jp>
131 15         67 $v->{'command'} = $1;
132              
133             } else {
134             # Check error message
135 114 100       178 if( grep { $e =~ $_ } @rxmessages ) {
  570         1589  
136             # Check with regular expressions of each error
137 25         116 $v->{'diagnosis'} .= ' '.$e;
138             } else {
139             # >>> 550
140 89         392 $v->{'alterrors'} .= ' '.$e;
141             }
142             }
143             } # End of error message part
144             }
145 40 50       193 return undef unless $recipients;
146              
147 40         97 for my $e ( @$dscontents ) {
148 40 50 66     210 if( exists $e->{'alterrors'} && $e->{'alterrors'} ) {
149             # Copy alternative error message
150 34   66     135 $e->{'diagnosis'} ||= $e->{'alterrors'};
151 34 50 33     204 if( index($e->{'diagnosis'}, '-') == 0 || substr($e->{'diagnosis'}, -2, 2) eq '__' ) {
152             # Override the value of diagnostic code message
153 0 0       0 $e->{'diagnosis'} = $e->{'alterrors'} if $e->{'alterrors'};
154             }
155 34         73 delete $e->{'alterrors'};
156             }
157 40         175 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'});
158              
159 40 100 66     207 if( defined $mhead->{'x-spasign'} && $mhead->{'x-spasign'} eq 'NG' ) {
160             # Content-Type: text/plain; ..., X-SPASIGN: NG (spamghetti, au by EZweb)
161             # Filtered recipient returns message that include 'X-SPASIGN' header
162 6         15 $e->{'reason'} = 'filtered';
163              
164             } else {
165 34 100       98 if( $e->{'command'} eq 'RCPT' ) {
166             # set "userunknown" when the remote server rejected after RCPT
167             # command.
168 10         28 $e->{'reason'} = 'userunknown';
169              
170             } else {
171             # SMTP command is not RCPT
172 24         89 SESSION: for my $r ( keys %$refailures ) {
173             # Verify each regular expression of session errors
174 52         80 PATTERN: for my $rr ( @{ $refailures->{ $r } } ) {
  52         99  
175             # Check each regular expression
176 56 100       262 next(PATTERN) unless $e->{'diagnosis'} =~ $rr;
177 24         48 $e->{'reason'} = $r;
178 24         65 last(SESSION);
179             }
180             }
181             }
182             }
183 40 50       163 next if $e->{'reason'};
184 0 0       0 next if $e->{'recipient'} =~ /[@](?:ezweb[.]ne[.]jp|au[.]com)\z/;
185 0         0 $e->{'reason'} = 'userunknown';
186             }
187 40         372 return { 'ds' => $dscontents, 'rfc822' => $emailsteak->[1] };
188             }
189              
190             1;
191             __END__