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   5349 use parent 'Sisimai::Lhost';
  14         21  
  14         83  
3 14     14   766 use feature ':5.10';
  14         28  
  14         866  
4 14     14   66 use strict;
  14         23  
  14         233  
5 14     14   57 use warnings;
  14         20  
  14         11897  
6              
7 2     2 1 1013 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 606 my $class = shift;
16 207   100     478 my $mhead = shift // return undef;
17 206   50     458 my $mbody = shift // return undef;
18 206         446 my $match = 0;
19              
20             # X-Mailer:
21 206 100 50     754 $match ||= 1 if $mhead->{'subject'} =~ /\AUndeliverable Mail[ ]*\z/;
22 206 100 50     809 $match ||= 1 if defined $mhead->{'x-mailer'} && index($mhead->{'x-mailer'}, '
      100        
23 206 100       505 return undef unless $match;
24              
25 25         43 state $rebackbone = qr|^Original[ ]message[ ]follows[.]|m;
26 25         44 state $startingof = { 'error' => ['Body of message generated response:'] };
27 25         62 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         75 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             'securityerror' => qr/\ARequested action not taken: virus detected/,
39             'undefined' => qr/\Aundeliverable to/,
40             'expired' => qr/\ADelivery failed \d+ attempts/,
41             };
42              
43 25         104 my $dscontents = [__PACKAGE__->DELIVERYSTATUS];
44 25         134 my $emailsteak = Sisimai::RFC5322->fillet($mbody, $rebackbone);
45 25         64 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
46 25         38 my $v = undef;
47              
48 25         110 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         103 $v = $dscontents->[-1];
56              
57 81 100       280 if( $e =~ /\A([^ ]+)[ ](.+)[:][ \t]*([^ ]+[@][^ ]+)/ ) {
    100          
58             # Unknown user: kijitora@example.com
59 21 50       74 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         90 $v->{'diagnosis'} = $1.' '.$2;
65 21         48 $v->{'recipient'} = $3;
66 21         41 $recipients++;
67              
68             } elsif( $e =~ /\Aundeliverable[ ]+to[ ]+(.+)\z/ ) {
69             # undeliverable to kijitora@example.com
70 4 50       12 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         17 $v->{'recipient'} = Sisimai::Address->s3s4($1);
76 4         10 $recipients++;
77              
78             } else {
79             # Other error message text
80 56   100     98 $v->{'alterrors'} //= '';
81 56 100       91 $v->{'alterrors'} .= ' '.$e if $v->{'alterrors'};
82 56 100       121 $v->{'alterrors'} = $e if index($e, $startingof->{'error'}->[0]) > -1;
83             }
84             }
85 25 50       70 return undef unless $recipients;
86              
87 25         61 for my $e ( @$dscontents ) {
88 25 50 66     111 if( exists $e->{'alterrors'} && $e->{'alterrors'} ) {
89             # Copy alternative error message
90 4         17 $e->{'diagnosis'} = $e->{'alterrors'}.' '.$e->{'diagnosis'};
91 4         23 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'});
92 4         9 delete $e->{'alterrors'};
93             }
94 25         152 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'});
95              
96 25         105 COMMAND: for my $r ( keys %$recommands ) {
97             # Detect SMTP command from the message
98 125 50       470 next unless $e->{'diagnosis'} =~ $recommands->{ $r };
99 0         0 $e->{'command'} = uc $r;
100 0         0 last;
101             }
102              
103 25         88 SESSION: for my $r ( keys %$refailures ) {
104             # Verify each regular expression of session errors
105 75 100       303 next unless $e->{'diagnosis'} =~ $refailures->{ $r };
106 21         40 $e->{'reason'} = $r;
107 21         44 last;
108             }
109             }
110 25         124 return { 'ds' => $dscontents, 'rfc822' => $emailsteak->[1] };
111             }
112              
113             1;
114             __END__