File Coverage

lib/Sisimai/Lhost/IMailServer.pm
Criterion Covered Total %
statement 55 61 90.1
branch 21 26 80.7
condition 12 16 75.0
subroutine 6 6 100.0
pod 2 2 100.0
total 96 111 86.4


line stmt bran cond sub pod time code
1             package Sisimai::Lhost::IMailServer;
2 14     14   6061 use parent 'Sisimai::Lhost';
  14         29  
  14         78  
3 14     14   850 use feature ':5.10';
  14         24  
  14         954  
4 14     14   83 use strict;
  14         34  
  14         266  
5 14     14   59 use warnings;
  14         40  
  14         14127  
6              
7 2     2 1 1157 sub description { 'IPSWITCH IMail Server' }
8             sub make {
9             # Detect an error from IMailServer
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.1
15 207     207 1 730 my $class = shift;
16 207   100     625 my $mhead = shift // return undef;
17 206   50     575 my $mbody = shift // return undef;
18 206         591 my $match = 0;
19              
20             # X-Mailer:
21 206 100 50     887 $match ||= 1 if $mhead->{'subject'} =~ /\AUndeliverable Mail[ ]*\z/;
22 206 100 50     1014 $match ||= 1 if defined $mhead->{'x-mailer'} && index($mhead->{'x-mailer'}, '
      100        
23 206 100       678 return undef unless $match;
24              
25 25         84 state $rebackbone = qr|^Original[ ]message[ ]follows[.]|m;
26 25         57 state $startingof = { 'error' => ['Body of message generated response:'] };
27 25         89 state $recommands = {
28             'conn' => qr/(?:SMTP connection failed,|Unexpected connection response from server:)/,
29             'ehlo' => qr|Unexpected response to EHLO/HELO:|,
30             'mail' => qr|Server response to MAIL FROM:|,
31             'rcpt' => qr|Additional RCPT TO generated following response:|,
32             'data' => qr|DATA command generated response:|,
33             };
34 25         63 state $refailures = {
35             'hostunknown' => qr/Unknown host/,
36             'userunknown' => qr/\A(?:Unknown user|Invalid final delivery userid)/,
37             'mailboxfull' => qr/\AUser mailbox exceeds allowed size/,
38             'virusdetected' => qr/\ARequested action not taken: virus detected/,
39             'undefined' => qr/\Aundeliverable to/,
40             'expired' => qr/\ADelivery failed \d+ attempts/,
41             };
42              
43 25         121 my $dscontents = [__PACKAGE__->DELIVERYSTATUS];
44 25         150 my $emailsteak = Sisimai::RFC5322->fillet($mbody, $rebackbone);
45 25         61 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
46 25         194 my $v = undef;
47              
48 25         166 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              
52             # Unknown user: kijitora@example.com
53             #
54             # Original message follows.
55 81         114 $v = $dscontents->[-1];
56              
57 81 100       346 if( $e =~ /\A([^ ]+)[ ](.+)[:][ \t]*([^ ]+[@][^ ]+)/ ) {
    100          
58             # Unknown user: kijitora@example.com
59 21 50       78 if( $v->{'recipient'} ) {
60             # There are multiple recipient addresses in the message body.
61 0         0 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
62 0         0 $v = $dscontents->[-1];
63             }
64 21         102 $v->{'diagnosis'} = $1.' '.$2;
65 21         87 $v->{'recipient'} = $3;
66 21         53 $recipients++;
67              
68             } elsif( $e =~ /\Aundeliverable[ ]+to[ ]+(.+)\z/ ) {
69             # undeliverable to kijitora@example.com
70 4 50       14 if( $v->{'recipient'} ) {
71             # There are multiple recipient addresses in the message body.
72 0         0 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
73 0         0 $v = $dscontents->[-1];
74             }
75 4         25 $v->{'recipient'} = Sisimai::Address->s3s4($1);
76 4         11 $recipients++;
77              
78             } else {
79             # Other error message text
80 56   100     122 $v->{'alterrors'} //= '';
81 56 100       111 $v->{'alterrors'} .= ' '.$e if $v->{'alterrors'};
82 56 100       142 $v->{'alterrors'} = $e if index($e, $startingof->{'error'}->[0]) > -1;
83             }
84             }
85 25 50       100 return undef unless $recipients;
86              
87 25         73 for my $e ( @$dscontents ) {
88 25 50 66     96 if( exists $e->{'alterrors'} && $e->{'alterrors'} ) {
89             # Copy alternative error message
90 4         17 $e->{'diagnosis'} = $e->{'alterrors'}.' '.$e->{'diagnosis'};
91 4         30 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'});
92 4         13 delete $e->{'alterrors'};
93             }
94 25         150 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'});
95              
96 25         141 COMMAND: for my $r ( keys %$recommands ) {
97             # Detect SMTP command from the message
98 125 50       557 next unless $e->{'diagnosis'} =~ $recommands->{ $r };
99 0         0 $e->{'command'} = uc $r;
100 0         0 last;
101             }
102              
103 25         120 SESSION: for my $r ( keys %$refailures ) {
104             # Verify each regular expression of session errors
105 109 100       453 next unless $e->{'diagnosis'} =~ $refailures->{ $r };
106 21         54 $e->{'reason'} = $r;
107 21         56 last;
108             }
109             }
110 25         135 return { 'ds' => $dscontents, 'rfc822' => $emailsteak->[1] };
111             }
112              
113             1;
114             __END__