File Coverage

lib/Sisimai/Lhost/X1.pm
Criterion Covered Total %
statement 45 48 93.7
branch 20 22 90.9
condition 7 9 77.7
subroutine 6 6 100.0
pod 2 2 100.0
total 80 87 91.9


line stmt bran cond sub pod time code
1             package Sisimai::Lhost::X1;
2 35     35   3521 use parent 'Sisimai::Lhost';
  35         50  
  35         221  
3 35     35   2289 use v5.26;
  35         96  
4 35     35   127 use strict;
  35         42  
  35         736  
5 35     35   103 use warnings;
  35         41  
  35         17073  
6              
7 1     1 1 2 sub description { 'Unknown MTA #1' }
8             sub inquire {
9             # Detect an error from Unknown MTA #1
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.3
15 876     876 1 2490 my $class = shift;
16 876   100     2019 my $mhead = shift // return undef;
17 875   100     1571 my $mbody = shift // return undef;
18              
19 874         898 my $proceedsto = 0;
20 874 100       1869 $proceedsto = 1 if index($mhead->{'subject'}, 'Returned Mail: ') == 0;
21 874 100       1923 $proceedsto = 1 if index($mhead->{'subject'}, 'Mail Delivery Failure') == 0;
22 874 100       3566 $proceedsto = 1 if Sisimai::String->aligned(\$mhead->{'from'}, ['"Mail Deliver', 'System" ']);
23 874 100       2460 return undef unless $proceedsto;
24              
25 50         112 state $indicators = __PACKAGE__->INDICATORS;
26 50         66 state $boundaries = ['Content-Type: message/rfc822', 'Received: from '];
27 50         68 state $markingsof = {'message' => ['The original message was received at ']};
28              
29 50         188 my $dscontents = [__PACKAGE__->DELIVERYSTATUS]; my $v = undef;
  50         73  
30 50         188 my $emailparts = Sisimai::RFC5322->part($mbody, $boundaries);
31 50         69 my $readcursor = 0; # (Integer) Points the current cursor position
32 50         57 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
33 50         78 my $datestring = ''; # (String) Date string
34              
35 50         344 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 1042 100       1076 unless( $readcursor ) {
39             # Beginning of the bounce message or message/delivery-status part
40 894 100       1130 $readcursor |= $indicators->{'deliverystatus'} if index($e, $markingsof->{'message'}->[0]) == 0;
41 894         751 next;
42             }
43 148 100 66     357 next if ($readcursor & $indicators->{'deliverystatus'}) == 0 || $e eq "";
44              
45             # The original message was received at Thu, 29 Apr 2010 23:34:45 +0900 (JST)
46             # from shironeko@example.jp
47             #
48             # ---The following addresses had delivery errors---
49             #
50             # kijitora@example.co.jp [User unknown]
51 79         80 $v = $dscontents->[-1];
52              
53 79 100       175 if( Sisimai::String->aligned(\$e, ['@', ' [', ']']) ) {
    50          
54             # kijitora@example.co.jp [User unknown]
55 21 50       49 if( $v->{'recipient'} ) {
56             # There are multiple recipient addresses in the message body.
57 0         0 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
58 0         0 $v = $dscontents->[-1];
59             }
60 21         41 my $p1 = index($e, ' ');
61 21         24 my $p2 = index($e, ']');
62 21         42 $v->{'recipient'} = substr($e, 0, $p1);
63 21         49 $v->{'diagnosis'} = substr($e, $p1 + 2, $p2 - $p1 - 2);
64 21         37 $recipients++;
65              
66             } elsif( index($e, $markingsof->{'message'}->[0]) == 0 ) {
67             # The original message was received at Thu, 29 Apr 2010 23:34:45 +0900 (JST)
68 0         0 $datestring = substr($e, length $markingsof->{'message'}->[0],);
69             }
70             }
71 50 100       251 return undef unless $recipients;
72              
73 21   50     107 $_->{'date'} = $datestring || '' for @$dscontents;
74 21         105 return {"ds" => $dscontents, "rfc822" => $emailparts->[1]};
75             }
76              
77             1;
78             __END__