File Coverage

lib/Sisimai/Lhost/KDDI.pm
Criterion Covered Total %
statement 61 65 93.8
branch 27 36 75.0
condition 9 18 50.0
subroutine 6 6 100.0
pod 2 2 100.0
total 105 127 82.6


line stmt bran cond sub pod time code
1             package Sisimai::Lhost::KDDI;
2 21     21   5146 use parent 'Sisimai::Lhost';
  21         33  
  21         100  
3 21     21   1113 use feature ':5.10';
  21         37  
  21         1231  
4 21     21   100 use strict;
  21         38  
  21         386  
5 21     21   75 use warnings;
  21         38  
  21         16970  
6              
7 2     2 1 914 sub description { 'au by KDDI: https://www.au.kddi.com' }
8             sub make {
9             # Detect an error from au by KDDI
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 245     245 1 703 my $class = shift;
16 245   100     642 my $mhead = shift // return undef;
17 244   50     519 my $mbody = shift // return undef;
18 244         418 my $match = 0;
19              
20             # 'message-id' => qr/[@].+[.]ezweb[.]ne[.]jp[>]\z/,
21 244 100 50     909 $match ||= 1 if $mhead->{'from'} =~ /no-reply[@].+[.]dion[.]ne[.]jp/;
22 244 100 50     788 $match ||= 1 if $mhead->{'reply-to'} && $mhead->{'reply-to'} eq 'no-reply@app.auone-net.jp';
      100        
23 244 50 0     377 $match ||= 1 if grep { rindex($_, 'ezweb.ne.jp (') > -1 } @{ $mhead->{'received'} };
  475         1111  
  244         536  
24 244 50 0     407 $match ||= 1 if grep { rindex($_, '.au.com (') > -1 } @{ $mhead->{'received'} };
  475         990  
  244         493  
25 244 100       602 return undef unless $match;
26              
27 16         37 state $indicators = __PACKAGE__->INDICATORS;
28 16         30 state $rebackbone = qr|^Content-Type:[ ]message/rfc822|m;
29 16         33 state $markingsof = {
30             'message' => qr/\AYour[ ]mail[ ](?:
31             sent[ ]on:?[ ][A-Z][a-z]{2}[,]
32             |attempted[ ]to[ ]be[ ]delivered[ ]on:?[ ][A-Z][a-z]{2}[,]
33             )
34             /x,
35             };
36 16         34 state $messagesof = {
37             'mailboxfull' => ['As their mailbox is full'],
38             'norelaying' => ['Due to the following SMTP relay error'],
39             'hostunknown' => ['As the remote domain doesnt exist'],
40             };
41              
42 16         54 my $dscontents = [__PACKAGE__->DELIVERYSTATUS];
43 16         87 my $emailsteak = Sisimai::RFC5322->fillet($mbody, $rebackbone);
44 16         30 my $readcursor = 0; # (Integer) Points the current cursor position
45 16         24 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
46 16         23 my $v = undef;
47              
48 16         99 for my $e ( split("\n", $emailsteak->[0]) ) {
49             # Read error messages and delivery status lines from the head of the email
50             # to the previous line of the beginning of the original message.
51 138 100       188 unless( $readcursor ) {
52             # Beginning of the bounce message or message/delivery-status part
53 106 100       278 $readcursor |= $indicators->{'deliverystatus'} if $e =~ $markingsof->{'message'};
54             }
55 138 100       207 next unless $readcursor & $indicators->{'deliverystatus'};
56 48 50       67 next unless length $e;
57              
58 48         62 $v = $dscontents->[-1];
59 48 100       182 if( $e =~ /\A[ \t]+Could not be delivered to: [<]([^ ]+[@][^ ]+)[>]/ ) {
    100          
60             # Your mail sent on: Thu, 29 Apr 2010 11:04:47 +0900
61             # Could not be delivered to: <******@**.***.**>
62             # As their mailbox is full.
63 16 50       55 if( $v->{'recipient'} ) {
64             # There are multiple recipient addresses in the message body.
65 0         0 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
66 0         0 $v = $dscontents->[-1];
67             }
68              
69 16         85 my $r = Sisimai::Address->s3s4($1);
70 16 50       65 next unless Sisimai::RFC5322->is_emailaddress($r);
71 16         34 $v->{'recipient'} = $r;
72 16         28 $recipients++;
73              
74             } elsif( $e =~ /Your mail sent on: (.+)\z/ ) {
75             # Your mail sent on: Thu, 29 Apr 2010 11:04:47 +0900
76 16         54 $v->{'date'} = $1;
77              
78             } else {
79             # As their mailbox is full.
80 16 50       117 $v->{'diagnosis'} .= $e.' ' if $e =~ /\A[ \t]+/;
81             }
82             }
83 16 50       50 return undef unless $recipients;
84              
85 16         36 for my $e ( @$dscontents ) {
86 16         66 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'});
87              
88 16 50 33     55 if( defined $mhead->{'x-spasign'} && $mhead->{'x-spasign'} eq 'NG' ) {
89             # Content-Type: text/plain; ..., X-SPASIGN: NG (spamghetti, au by KDDI)
90             # Filtered recipient returns message that include 'X-SPASIGN' header
91 0         0 $e->{'reason'} = 'filtered';
92              
93             } else {
94 16 50       36 if( $e->{'command'} eq 'RCPT' ) {
95             # set "userunknown" when the remote server rejected after RCPT
96             # command.
97 0         0 $e->{'reason'} = 'userunknown';
98              
99             } else {
100             # SMTP command is not RCPT
101 16         51 SESSION: for my $r ( keys %$messagesof ) {
102             # Verify each regular expression of session errors
103 46 100       47 next unless grep { index($e->{'diagnosis'}, $_) > -1 } @{ $messagesof->{ $r } };
  46         138  
  46         80  
104 16         30 $e->{'reason'} = $r;
105 16         32 last;
106             }
107             }
108             }
109             }
110 16         77 return { 'ds' => $dscontents, 'rfc822' => $emailsteak->[1] };
111             }
112              
113             1;
114             __END__