File Coverage

lib/Sisimai/Lhost/MailFoundry.pm
Criterion Covered Total %
statement 45 47 95.7
branch 17 22 77.2
condition 9 10 90.0
subroutine 6 6 100.0
pod 2 2 100.0
total 79 87 90.8


line stmt bran cond sub pod time code
1             package Sisimai::Lhost::MailFoundry;
2 35     35   4215 use parent 'Sisimai::Lhost';
  35         77  
  35         247  
3 35     35   3008 use v5.26;
  35         126  
4 35     35   222 use strict;
  35         79  
  35         971  
5 35     35   192 use warnings;
  35         103  
  35         24945  
6              
7 1     1 1 5 sub description { 'MailFoundry: https://www.barracuda.com/' }
8             sub inquire {
9             # Detect an error from MailFoundry
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 878     878 1 3719 my $class = shift;
16 878   100     2982 my $mhead = shift // return undef;
17 877   100     2472 my $mbody = shift // return undef;
18              
19 876 100       3552 return undef unless $mhead->{'subject'} eq 'Message delivery has failed';
20 11 50       28 return undef unless grep { rindex($_, '(MAILFOUNDRY) id') > -1 } $mhead->{'received'}->@*;
  22         60  
21              
22 11         38 state $indicators = __PACKAGE__->INDICATORS;
23 11         17 state $boundaries = ['Content-Type: message/rfc822'];
24 11         27 state $startingof = {
25             'message' => ['Unable to deliver message to:'],
26             'error' => ['Delivery failed for the following reason:'],
27             };
28              
29 11         51 my $dscontents = [__PACKAGE__->DELIVERYSTATUS]; my $v = undef;
  11         16  
30 11         57 my $emailparts = Sisimai::RFC5322->part($mbody, $boundaries);
31 11         34 my $readcursor = 0; # (Integer) Points the current cursor position
32 11         13 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
33              
34 11         55 for my $e ( split("\n", $emailparts->[0]) ) {
35             # Read error messages and delivery status lines from the head of the email to the previous
36             # line of the beginning of the original message.
37 77 100       111 unless( $readcursor ) {
38             # Beginning of the bounce message or message/delivery-status part
39 33 100       86 $readcursor |= $indicators->{'deliverystatus'} if index($e, $startingof->{'message'}->[0]) == 0;
40             }
41 77 100 100     208 next if ($readcursor & $indicators->{'deliverystatus'}) == 0 || $e eq "";
42              
43             # Unable to deliver message to:
44             # Delivery failed for the following reason:
45             # Server mx22.example.org[192.0.2.222] failed with: 550 No such user here
46             #
47             # This has been a permanent failure. No further delivery attempts will be made.
48 44         70 $v = $dscontents->[-1];
49              
50 44 100 66     134 if( index($e, 'Unable to deliver message to: <') == 0 && index($e, '@') > 1 ) {
51             # Unable to deliver message to:
52 11 50       50 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 11         87 $v->{'recipient'} = Sisimai::Address->s3s4(substr($e, index($e, '<'), ));
58 11         35 $recipients++;
59              
60             } else {
61             # Delivery failed for the following reason:
62 33 100       63 if( $e eq $startingof->{'error'}->[0] ) { $v->{'diagnosis'} = $e; next }
  11         20  
  11         31  
63              
64 22 50       52 next unless $v->{'diagnosis'};
65 22 50       42 next if index($e, '-') == 0;
66 22         51 $v->{'diagnosis'} .= ' '.$e;
67             }
68             }
69 11 50       31 return undef unless $recipients;
70              
71 11         21 for my $e ( @$dscontents ) {
72             # Set default values if each value is empty.
73 11         91 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'});
74             }
75 11         48 return {"ds" => $dscontents, "rfc822" => $emailparts->[1]};
76             }
77              
78             1;
79             __END__