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   6031 use parent 'Sisimai::Lhost';
  14         31  
  14         78  
3 14     14   885 use feature ':5.10';
  14         29  
  14         1031  
4 14     14   86 use strict;
  14         35  
  14         268  
5 14     14   254 use warnings;
  14         31  
  14         14389  
6              
7 2     2 1 1242 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 762 my $class = shift;
16 207   100     495 my $mhead = shift // return undef;
17 206   50     493 my $mbody = shift // return undef;
18 206         305 my $match = 0;
19              
20             # X-Mailer:
21 206 100 50     917 $match ||= 1 if $mhead->{'subject'} =~ /\AUndeliverable Mail[ ]*\z/;
22 206 100 50     914 $match ||= 1 if defined $mhead->{'x-mailer'} && index($mhead->{'x-mailer'}, '
      100        
23 206 100       650 return undef unless $match;
24              
25 25         72 state $rebackbone = qr|^Original[ ]message[ ]follows[.]|m;
26 25         52 state $startingof = { 'error' => ['Body of message generated response:'] };
27 25         70 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         83 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         131 my $dscontents = [__PACKAGE__->DELIVERYSTATUS];
44 25         184 my $emailsteak = Sisimai::RFC5322->fillet($mbody, $rebackbone);
45 25         112 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
46 25         80 my $v = undef;
47              
48 25         172 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         122 $v = $dscontents->[-1];
56              
57 81 100       395 if( $e =~ /\A([^ ]+)[ ](.+)[:][ \t]*([^ ]+[@][^ ]+)/ ) {
    100          
58             # Unknown user: kijitora@example.com
59 21 50       77 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         134 $v->{'diagnosis'} = $1.' '.$2;
65 21         59 $v->{'recipient'} = $3;
66 21         57 $recipients++;
67              
68             } elsif( $e =~ /\Aundeliverable[ ]+to[ ]+(.+)\z/ ) {
69             # undeliverable to kijitora@example.com
70 4 50       30 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         24 $v->{'recipient'} = Sisimai::Address->s3s4($1);
76 4         17 $recipients++;
77              
78             } else {
79             # Other error message text
80 56   100     125 $v->{'alterrors'} //= '';
81 56 100       126 $v->{'alterrors'} .= ' '.$e if $v->{'alterrors'};
82 56 100       155 $v->{'alterrors'} = $e if index($e, $startingof->{'error'}->[0]) > -1;
83             }
84             }
85 25 50       92 return undef unless $recipients;
86              
87 25         113 for my $e ( @$dscontents ) {
88 25 50 66     94 if( exists $e->{'alterrors'} && $e->{'alterrors'} ) {
89             # Copy alternative error message
90 4         17 $e->{'diagnosis'} = $e->{'alterrors'}.' '.$e->{'diagnosis'};
91 4         29 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'});
92 4         22 delete $e->{'alterrors'};
93             }
94 25         147 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'});
95              
96 25         220 COMMAND: for my $r ( keys %$recommands ) {
97             # Detect SMTP command from the message
98 125 50       584 next unless $e->{'diagnosis'} =~ $recommands->{ $r };
99 0         0 $e->{'command'} = uc $r;
100 0         0 last;
101             }
102              
103 25         105 SESSION: for my $r ( keys %$refailures ) {
104             # Verify each regular expression of session errors
105 109 100       538 next unless $e->{'diagnosis'} =~ $refailures->{ $r };
106 21         51 $e->{'reason'} = $r;
107 21         74 last;
108             }
109             }
110 25         170 return { 'ds' => $dscontents, 'rfc822' => $emailsteak->[1] };
111             }
112              
113             1;
114             __END__