File Coverage

lib/Sisimai/Lhost/IMailServer.pm
Criterion Covered Total %
statement 50 52 96.1
branch 17 20 85.0
condition 20 22 90.9
subroutine 6 6 100.0
pod 2 2 100.0
total 95 102 93.1


line stmt bran cond sub pod time code
1             package Sisimai::Lhost::IMailServer;
2 36     36   3329 use parent 'Sisimai::Lhost';
  36         52  
  36         233  
3 36     36   2286 use v5.26;
  36         106  
4 36     36   128 use strict;
  36         52  
  36         895  
5 36     36   209 use warnings;
  36         72  
  36         20684  
6              
7 1     1 1 2 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 891     891 1 2773 my $class = shift;
16 891   100     1913 my $mhead = shift // return undef;
17 890   100     1961 my $mbody = shift // return undef;
18              
19             # X-Mailer:
20 889 100 50     1137 my $match = 0; $match ||= 1 if index($mhead->{'subject'}, 'Undeliverable Mail ') == 0;
  889         2240  
21 889 100 100     2649 $match ||= 1 if defined $mhead->{'x-mailer'} && index($mhead->{'x-mailer'}, '
      100        
22 889 100       2016 return undef unless $match;
23              
24 26         33 state $boundaries = ['Original message follows.'];
25 26         40 state $startingof = {'error' => ['Body of message generated response:']};
26 26         37 state $messagesof = {
27             'userunknown' => ['Unknown user', 'Invalid final delivery userid'],
28             'expired' => ['Delivery failed '],
29             };
30              
31 26         88 my $dscontents = [__PACKAGE__->DELIVERYSTATUS]; my $v = undef;
  26         37  
32 26         113 my $emailparts = Sisimai::RFC5322->part($mbody, $boundaries);
33 26         35 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
34              
35 26         81 for my $e ( split("\n", $emailparts->[0]) ) {
36             # Read error messages and delivery status lines from the head of the email to the previous
37             # line of the beginning of the original message.
38              
39             # Unknown user: kijitora@example.com
40             #
41             # Original message follows.
42 96         100 $v = $dscontents->[-1];
43              
44 96         111 my $p0 = index($e, ': ');
45 96 100 100     334 if( ($p0 > 8 && Sisimai::String->aligned(\$e, [': ', '@'])) || index($e, 'undeliverable ') == 0 ) {
      100        
46             # Unknown user: kijitora@example.com
47             # undeliverable to kijitora@example.com
48 26 50       77 if( $v->{'recipient'} ) {
49             # There are multiple recipient addresses in the message body.
50 0         0 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
51 0         0 $v = $dscontents->[-1];
52             }
53 26         39 $v->{'diagnosis'} = $e;
54 26         104 $v->{'recipient'} = Sisimai::Address->s3s4($e);
55 26         74 $recipients++;
56              
57             } else {
58             # Other error message text
59 70   100     100 $v->{'alterrors'} //= '';
60 70 100       94 $v->{'alterrors'} .= ' '.$e if $v->{'alterrors'};
61 70 100       107 $v->{'alterrors'} = $e if index($e, $startingof->{'error'}->[0]) > -1;
62             }
63             }
64 26 50       66 return undef unless $recipients;
65              
66 26         105 require Sisimai::SMTP::Command;
67 26         37 for my $e ( @$dscontents ) {
68 26 50 66     64 if( exists $e->{'alterrors'} && $e->{'alterrors'} ) {
69             # Copy alternative error message
70 5         13 $e->{'diagnosis'} = $e->{'alterrors'}.' '.$e->{'diagnosis'};
71 5         10 delete $e->{'alterrors'};
72             }
73 26         154 $e->{'command'} = Sisimai::SMTP::Command->find($e->{'diagnosis'});
74              
75 26         61 SESSION: for my $r ( keys %$messagesof ) {
76             # Verify each regular expression of session errors
77 46 100       65 next unless grep { index($e->{'diagnosis'}, $_) > -1 } $messagesof->{ $r }->@*;
  67         135  
78 16         23 $e->{'reason'} = $r;
79 16         30 last;
80             }
81             }
82 26         123 return {"ds" => $dscontents, "rfc822" => $emailparts->[1]};
83             }
84              
85             1;
86             __END__