File Coverage

lib/Sisimai/Lhost/X1.pm
Criterion Covered Total %
statement 47 50 94.0
branch 20 22 90.9
condition 7 9 77.7
subroutine 6 6 100.0
pod 2 2 100.0
total 82 89 92.1


line stmt bran cond sub pod time code
1             package Sisimai::Lhost::X1;
2 34     34   4553 use parent 'Sisimai::Lhost';
  34         75  
  34         264  
3 34     34   3738 use v5.26;
  34         130  
4 34     34   362 use strict;
  34         70  
  34         1044  
5 34     34   168 use warnings;
  34         135  
  34         34931  
6              
7 1     1 1 5 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 878     878 1 4525 my $class = shift;
16 878   100     2849 my $mhead = shift // return undef;
17 877   100     2411 my $mbody = shift // return undef;
18              
19 876         1460 my $proceedsto = 0;
20 876 100       3320 $proceedsto = 1 if index($mhead->{'subject'}, 'Returned Mail: ') == 0;
21 876 100       3158 $proceedsto = 1 if index($mhead->{'subject'}, 'Mail Delivery Failure') == 0;
22 876 100       5683 $proceedsto = 1 if Sisimai::String->aligned(\$mhead->{'from'}, ['"Mail Deliver', 'System" ']);
23 876 100       3500 return undef unless $proceedsto;
24              
25 50         185 state $indicators = __PACKAGE__->INDICATORS;
26 50         103 state $boundaries = ['Content-Type: message/rfc822', 'Received: from '];
27 50         103 state $markingsof = {'message' => ['The original message was received at ']};
28              
29 50         307 my $dscontents = [__PACKAGE__->DELIVERYSTATUS]; my $v = undef;
  50         118  
30 50         243 my $emailparts = Sisimai::RFC5322->part($mbody, $boundaries);
31 50         103 my $readcursor = 0; # (Integer) Points the current cursor position
32 50         83 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
33 50         95 my $datestring = ''; # (String) Date string
34              
35 50         550 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       1678 unless( $readcursor ) {
39             # Beginning of the bounce message or message/delivery-status part
40 894 100       1776 $readcursor |= $indicators->{'deliverystatus'} if index($e, $markingsof->{'message'}->[0]) == 0;
41 894         1161 next;
42             }
43 148 100 66     683 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         124 $v = $dscontents->[-1];
52              
53 79 100       340 if( Sisimai::String->aligned(\$e, ['@', ' [', ']']) ) {
    50          
54             # kijitora@example.co.jp [User unknown]
55 21 50       87 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         57 my $p1 = index($e, ' ');
61 21         54 my $p2 = index($e, ']');
62 21         85 $v->{'recipient'} = substr($e, 0, $p1);
63 21         80 $v->{'diagnosis'} = substr($e, $p1 + 2, $p2 - $p1 - 2);
64 21         67 $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       491 return undef unless $recipients;
72              
73 21         46 for my $e ( @$dscontents ) {
74 21         134 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'});
75 21   50     148 $e->{'date'} = $datestring || '';
76             }
77 21         148 return {"ds" => $dscontents, "rfc822" => $emailparts->[1]};
78             }
79              
80             1;
81             __END__