File Coverage

lib/Sisimai/Lhost/X6.pm
Criterion Covered Total %
statement 43 45 95.5
branch 13 18 72.2
condition 5 7 71.4
subroutine 6 6 100.0
pod 2 2 100.0
total 69 78 88.4


line stmt bran cond sub pod time code
1             package Sisimai::Lhost::X6;
2 14     14   6005 use parent 'Sisimai::Lhost';
  14         30  
  14         81  
3 14     14   865 use feature ':5.10';
  14         25  
  14         947  
4 14     14   88 use strict;
  14         26  
  14         315  
5 14     14   66 use warnings;
  14         26  
  14         8709  
6              
7 2     2 1 1319 sub description { 'Unknown MTA #6' }
8             sub make {
9             # Detect an error from Unknown MTA #6
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.25.6
15 193     193 1 658 my $class = shift;
16 193   100     578 my $mhead = shift // return undef;
17 192   50     510 my $mbody = shift // return undef;
18 192 100       665 return undef unless index($mhead->{'subject'}, 'There was an error sending your mail') == 0;
19              
20 11         41 state $indicators = __PACKAGE__->INDICATORS;
21 11         28 state $rebackbone = qr/^The attachment contains the original mail headers.+$/m;
22 11         38 state $markingsof = { 'message' => qr/\A\d+[ ]*error[(]s[)]:/ };
23              
24 11         50 my $dscontents = [__PACKAGE__->DELIVERYSTATUS];
25 11         102 my $emailsteak = Sisimai::RFC5322->fillet($mbody, $rebackbone);
26 11         21 my $readcursor = 0; # (Integer) Points the current cursor position
27 11         27 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
28 11         20 my $v = undef;
29              
30 11         77 for my $e ( split("\n", $emailsteak->[0]) ) {
31             # Read error messages and delivery status lines from the head of the email
32             # to the previous line of the beginning of the original message.
33 110 100       168 unless( $readcursor ) {
34             # Beginning of the bounce message or message/delivery-status part
35 88 100       262 $readcursor |= $indicators->{'deliverystatus'} if $e =~ $markingsof->{'message'};
36 88         115 next;
37             }
38 22 50       59 next unless $readcursor & $indicators->{'deliverystatus'};
39 22 100       52 next unless length $e;
40              
41             # 1 error(s):
42             #
43             # SMTP Server rejected recipient
44             # (Error following RCPT command). It responded as follows: [550 5.1.1 User unknown]
45 11         32 $v = $dscontents->[-1];
46 11 50 66     96 if( $e =~ /<([^ @]+[@][^ @]+)>/ || $e =~ /errors:[ ]*([^ ]+[@][^ ]+)/ ) {
47             # SMTP Server rejected recipient
48             # The following recipients returned permanent errors: neko@example.jp.
49 11 50       36 if( $v->{'recipient'} ) {
50             # There are multiple recipient addresses in the message body.
51 0         0 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
52 0         0 $v = $dscontents->[-1];
53             }
54 11         82 $v->{'recipient'} = Sisimai::Address->s3s4($1);
55 11         29 $v->{'diagnosis'} = $e;
56 11         23 $recipients++;
57             }
58             }
59 11 50       48 return undef unless $recipients;
60              
61 11         35 for my $e ( @$dscontents ) {
62             # Get the last SMTP command from the error message
63 11 50       110 if( $e->{'diagnosis'} =~ /\b(HELO|EHLO|MAIL|RCPT|DATA)\b/ ) {
64             # ...(Error following RCPT command).
65 11         34 $e->{'command'} = $1;
66             }
67 11         104 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'});
68             }
69 11         75 return { 'ds' => $dscontents, 'rfc822' => $emailsteak->[1] };
70             }
71              
72             1;
73             __END__