File Coverage

lib/Sisimai/Lhost/IMailServer.pm
Criterion Covered Total %
statement 52 54 96.3
branch 17 20 85.0
condition 20 22 90.9
subroutine 6 6 100.0
pod 2 2 100.0
total 97 104 93.2


line stmt bran cond sub pod time code
1             package Sisimai::Lhost::IMailServer;
2 35     35   5509 use parent 'Sisimai::Lhost';
  35         88  
  35         263  
3 35     35   3389 use v5.26;
  35         130  
4 35     35   170 use strict;
  35         69  
  35         1130  
5 35     35   197 use warnings;
  35         75  
  35         28690  
6              
7 1     1 1 4 sub description { 'Progress iMail Server: https://community.progress.com/s/products/imailserver' }
8             sub inquire {
9             # Detect an error from Progress iMail Server
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 decode or the arguments are missing
14             # @since v4.1.1
15 893     893 1 3144 my $class = shift;
16 893   100     2890 my $mhead = shift // return undef;
17 892   100     2520 my $mbody = shift // return undef;
18              
19             # X-Mailer:
20 891 100 50     1683 my $match = 0; $match ||= 1 if index($mhead->{'subject'}, 'Undeliverable Mail ') == 0;
  891         3442  
21 891 100 100     4105 $match ||= 1 if defined $mhead->{'x-mailer'} && index($mhead->{'x-mailer'}, '
      100        
22 891 100       3663 return undef unless $match;
23              
24 26         48 state $boundaries = ['Original message follows.'];
25 26         62 state $startingof = {'error' => ['Body of message generated response:']};
26 26         77 state $messagesof = {
27             'hostunknown' => ['Unknown host'],
28             'userunknown' => ['Unknown user', 'Invalid final delivery userid'],
29             'mailboxfull' => ['User mailbox exceeds allowed size'],
30             'virusdetected' => ['Requested action not taken: virus detected'],
31             'spamdetected' => ['Blacklisted URL in message'],
32             'expired' => ['Delivery failed '],
33             };
34              
35 26         150 my $dscontents = [__PACKAGE__->DELIVERYSTATUS]; my $v = undef;
  26         56  
36 26         170 my $emailparts = Sisimai::RFC5322->part($mbody, $boundaries);
37 26         67 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
38              
39 26         149 for my $e ( split("\n", $emailparts->[0]) ) {
40             # Read error messages and delivery status lines from the head of the email to the previous
41             # line of the beginning of the original message.
42              
43             # Unknown user: kijitora@example.com
44             #
45             # Original message follows.
46 96         119 $v = $dscontents->[-1];
47              
48 96         128 my $p0 = index($e, ': ');
49 96 100 100     454 if( ($p0 > 8 && Sisimai::String->aligned(\$e, [': ', '@'])) || index($e, 'undeliverable ') == 0 ) {
      100        
50             # Unknown user: kijitora@example.com
51             # undeliverable to kijitora@example.com
52 26 50       78 if( $v->{'recipient'} ) {
53             # There are multiple recipient addresses in the message body.
54 0         0 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
55 0         0 $v = $dscontents->[-1];
56             }
57 26         53 $v->{'diagnosis'} = $e;
58 26         182 $v->{'recipient'} = Sisimai::Address->s3s4($e);
59 26         76 $recipients++;
60              
61             } else {
62             # Other error message text
63 70   100     115 $v->{'alterrors'} //= '';
64 70 100       136 $v->{'alterrors'} .= ' '.$e if $v->{'alterrors'};
65 70 100       148 $v->{'alterrors'} = $e if index($e, $startingof->{'error'}->[0]) > -1;
66             }
67             }
68 26 50       87 return undef unless $recipients;
69              
70 26         154 require Sisimai::SMTP::Command;
71 26         80 for my $e ( @$dscontents ) {
72 26 50 66     116 if( exists $e->{'alterrors'} && $e->{'alterrors'} ) {
73             # Copy alternative error message
74 5         17 $e->{'diagnosis'} = $e->{'alterrors'}.' '.$e->{'diagnosis'};
75 5         18 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'});
76 5         11 delete $e->{'alterrors'};
77             }
78 26         98 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'});
79 26         236 $e->{'command'} = Sisimai::SMTP::Command->find($e->{'diagnosis'});
80              
81 26         119 SESSION: for my $r ( keys %$messagesof ) {
82             # Verify each regular expression of session errors
83 72 100       156 next unless grep { index($e->{'diagnosis'}, $_) > -1 } $messagesof->{ $r }->@*;
  85         252  
84 26         75 $e->{'reason'} = $r;
85 26         60 last;
86             }
87             }
88 26         150 return {"ds" => $dscontents, "rfc822" => $emailparts->[1]};
89             }
90              
91             1;
92             __END__