File Coverage

lib/Sisimai/Lhost/X6.pm
Criterion Covered Total %
statement 50 53 94.3
branch 18 22 81.8
condition 9 10 90.0
subroutine 6 6 100.0
pod 2 2 100.0
total 85 93 91.4


line stmt bran cond sub pod time code
1             package Sisimai::Lhost::X6;
2 35     35   3352 use parent 'Sisimai::Lhost';
  35         54  
  35         200  
3 35     35   2176 use v5.26;
  35         102  
4 35     35   138 use strict;
  35         83  
  35         779  
5 35     35   112 use warnings;
  35         75  
  35         17710  
6              
7 1     1 1 3 sub description { 'Unknown MTA #6' }
8             sub inquire {
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 decode or the arguments are missing
14             # @since v4.25.6
15 866     866 1 2607 my $class = shift;
16 866   100     1751 my $mhead = shift // return undef;
17 865   100     1916 my $mbody = shift // return undef;
18              
19 864 100       2018 return undef unless $mhead->{'subject'};
20 846 100       2292 return undef if index($mhead->{'subject'}, 'There was an error sending your mail') != 0;
21              
22 11         44 state $indicators = __PACKAGE__->INDICATORS;
23 11         26 state $boundaries = ['The attachment contains the original mail headers'];
24 11         17 state $startingof = {'message' => ['We had trouble delivering your message. Full details follow:']};
25              
26 11         56 my $dscontents = [__PACKAGE__->DELIVERYSTATUS]; my $v = undef;
  11         16  
27 11         49 my $emailparts = Sisimai::RFC5322->part($mbody, $boundaries);
28 11         18 my $readcursor = 0; # (Integer) Points the current cursor position
29 11         13 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
30              
31 11         58 for my $e ( split("\n", $emailparts->[0]) ) {
32             # Read error messages and delivery status lines from the head of the email to the previous
33             # line of the beginning of the original message.
34 110 100       158 unless( $readcursor ) {
35             # Beginning of the bounce message or message/delivery-status part
36 33 100       85 $readcursor |= $indicators->{'deliverystatus'} if index($e, $startingof->{'message'}->[0]) == 0;
37 33         32 next;
38             }
39 77 100 66     153 next if ($readcursor & $indicators->{'deliverystatus'}) == 0 || $e eq "";
40              
41             # We had trouble delivering your message. Full details follow:
42             #
43             # Subject: 'Nyaan'
44             # Date: 'Thu, 29 Apr 2012 23:34:45 +0000'
45             #
46             # 1 error(s):
47             #
48             # SMTP Server rejected recipient
49             # (Error following RCPT command). It responded as follows: [550 5.1.1 User unknown]
50 44         36 $v = $dscontents->[-1];
51 44         46 my $p1 = index($e, 'The following recipients returned permanent errors: ');
52 44         36 my $p2 = index($e, 'SMTP Server <');
53 44 100 100     93 if( $p1 == 0 || $p2 == 0 ) {
54             # SMTP Server rejected recipient
55             # The following recipients returned permanent errors: neko@example.jp.
56 11 50       21 if( $v->{'recipient'} ) {
57             # There are multiple recipient addresses in the message body.
58 0         0 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
59 0         0 $v = $dscontents->[-1];
60             }
61 11 100       41 if( $p1 == 0 ) {
    50          
62             # The following recipients returned permanent errors: neko@example.jp.
63 6         11 $p1 = index($e, 'errors: ');
64 6         9 $p2 = index($e, ' ', $p1 + 8);
65 6         44 $v->{'recipient'} = Sisimai::Address->s3s4(substr($e, $p1 + 8, $p2 - $p1 - 8));
66              
67             } elsif( $p2 == 0 ) {
68             # SMTP Server rejected recipient
69 5         6 $p1 = rindex($e, '<');
70 5         7 $p2 = rindex($e, '>');
71 5         48 $v->{'recipient'} = Sisimai::Address->s3s4(substr($e, $p1, $p2 - $p1));
72              
73             } else {
74 0         0 next;
75             }
76 11         25 $v->{'diagnosis'} = $e;
77 11         17 $recipients++;
78             }
79             }
80 11 50       31 return undef unless $recipients;
81              
82 11         32 require Sisimai::SMTP::Command;
83 11         16 for my $e ( @$dscontents ) {
84             # Get the last SMTP command from the error message
85 11 50       80 if( my $cv = Sisimai::SMTP::Command->find($e->{'diagnosis'}) ) {
86             # ...(Error following RCPT command).
87 11         43 $e->{'command'} = $cv;
88             }
89             }
90 11         51 return {"ds" => $dscontents, "rfc822" => $emailparts->[1]};
91             }
92              
93             1;
94             __END__