File Coverage

lib/Sisimai/Lhost/Courier.pm
Criterion Covered Total %
statement 75 78 96.1
branch 37 44 84.0
condition 13 26 50.0
subroutine 6 6 100.0
pod 2 2 100.0
total 133 156 85.2


line stmt bran cond sub pod time code
1             package Sisimai::Lhost::Courier;
2 15     15   5353 use parent 'Sisimai::Lhost';
  15         25  
  15         88  
3 15     15   830 use feature ':5.10';
  15         23  
  15         954  
4 15     15   71 use strict;
  15         23  
  15         289  
5 15     15   70 use warnings;
  15         31  
  15         13635  
6              
7 2     2 1 1147 sub description { 'Courier MTA' }
8             sub make {
9             # Detect an error from Courier MTA
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.0.0
15 208     208 1 570 my $class = shift;
16 208   100     503 my $mhead = shift // return undef;
17 207   50     396 my $mbody = shift // return undef;
18 207         231 my $match = 0;
19              
20 207 50 0     639 $match ||= 1 if index($mhead->{'from'}, 'Courier mail server at ') > -1;
21 207 100 50     960 $match ||= 1 if $mhead->{'subject'} =~ /(?:NOTICE: mail delivery status[.]|WARNING: delayed mail[.])/;
22 207 100       410 if( defined $mhead->{'message-id'} ) {
23             # Message-ID:
24 187 100 50     674 $match ||= 1 if $mhead->{'message-id'} =~ /\A[<]courier[.][0-9A-F]+[.]/;
25             }
26 207 100       580 return undef unless $match;
27              
28 21         60 state $indicators = __PACKAGE__->INDICATORS;
29 21         44 state $rebackbone = qr<^Content-Type:[ ](?:message/rfc822|text/rfc822-headers)>m;
30 21         47 state $startingof = {
31             # https://www.courier-mta.org/courierdsn.html
32             # courier/module.dsn/dsn*.txt
33             'message' => ['DELAYS IN DELIVERING YOUR MESSAGE', 'UNDELIVERABLE MAIL'],
34             };
35 21         33 state $messagesof = {
36             # courier/module.esmtp/esmtpclient.c:526| hard_error(del, ctf, "No such domain.");
37             'hostunknown' => ['No such domain.'],
38             # courier/module.esmtp/esmtpclient.c:531| hard_error(del, ctf,
39             # courier/module.esmtp/esmtpclient.c:532| "This domain's DNS violates RFC 1035.");
40             'systemerror' => ["This domain's DNS violates RFC 1035."],
41             # courier/module.esmtp/esmtpclient.c:535| soft_error(del, ctf, "DNS lookup failed.");
42             'networkerror'=> ['DNS lookup failed.'],
43             };
44              
45 21         652 require Sisimai::RFC1894;
46 21         110 my $fieldtable = Sisimai::RFC1894->FIELDTABLE;
47 21         45 my $permessage = {}; # (Hash) Store values of each Per-Message field
48              
49 21         110 my $dscontents = [__PACKAGE__->DELIVERYSTATUS];
50 21         121 my $emailsteak = Sisimai::RFC5322->fillet($mbody, $rebackbone);
51 21         57 my $readcursor = 0; # (Integer) Points the current cursor position
52 21         45 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
53 21         32 my $commandtxt = ''; # (String) SMTP Command name begin with the string '>>>'
54 21         35 my $v = undef;
55 21         36 my $p = '';
56              
57 21         229 for my $e ( split("\n", $emailsteak->[0]) ) {
58             # Read error messages and delivery status lines from the head of the email
59             # to the previous line of the beginning of the original message.
60 788 100       883 unless( $readcursor ) {
61             # Beginning of the bounce message or message/delivery-status part
62 252 100 66     678 if( rindex($e, $startingof->{'message'}->[0]) > -1 ||
63             rindex($e, $startingof->{'message'}->[1]) > -1 ) {
64 21         53 $readcursor |= $indicators->{'deliverystatus'};
65 21         26 next;
66             }
67             }
68 767 100       947 next unless $readcursor & $indicators->{'deliverystatus'};
69 536 100       655 next unless length $e;
70              
71 389 100       587 if( my $f = Sisimai::RFC1894->match($e) ) {
72             # $e matched with any field defined in RFC3464
73 163 50       249 next unless my $o = Sisimai::RFC1894->field($e);
74 163         194 $v = $dscontents->[-1];
75              
76 163 100       316 if( $o->[-1] eq 'addr' ) {
    100          
77             # Final-Recipient: rfc822; kijitora@example.jp
78             # X-Actual-Recipient: rfc822; kijitora@example.co.jp
79 21 50       55 if( $o->[0] eq 'final-recipient' ) {
80             # Final-Recipient: rfc822; kijitora@example.jp
81 21 50       57 if( $v->{'recipient'} ) {
82             # There are multiple recipient addresses in the message body.
83 0         0 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
84 0         0 $v = $dscontents->[-1];
85             }
86 21         34 $v->{'recipient'} = $o->[2];
87 21         49 $recipients++;
88              
89             } else {
90             # X-Actual-Recipient: rfc822; kijitora@example.co.jp
91 0         0 $v->{'alias'} = $o->[2];
92             }
93             } elsif( $o->[-1] eq 'code' ) {
94             # Diagnostic-Code: SMTP; 550 5.1.1 ... User Unknown
95 21         39 $v->{'spec'} = $o->[1];
96 21         47 $v->{'diagnosis'} = $o->[2];
97              
98             } else {
99             # Other DSN fields defined in RFC3464
100 121 50       206 next unless exists $fieldtable->{ $o->[0] };
101 121         205 $v->{ $fieldtable->{ $o->[0] } } = $o->[2];
102              
103 121 100       229 next unless $f == 1;
104 63         149 $permessage->{ $fieldtable->{ $o->[0] } } = $o->[2];
105             }
106             } else {
107             # The line does not begin with a DSN field defined in RFC3464
108             #
109             # This is a delivery status notification from marutamachi.example.org,
110             # running the Courier mail server, version 0.65.2.
111             #
112             # The original message was received on Sat, 11 Dec 2010 12:19:57 +0900
113             # from [127.0.0.1] (c10920.example.com [192.0.2.20])
114             #
115             # ---------------------------------------------------------------------------
116             #
117             # UNDELIVERABLE MAIL
118             #
119             # Your message to the following recipients cannot be delivered:
120             #
121             # :
122             # mx.example.co.jp [74.207.247.95]:
123             # >>> RCPT TO:
124             # <<< 550 5.1.1 ... User Unknown
125             #
126             # ---------------------------------------------------------------------------
127 226 100       398 if( $e =~ /\A[>]{3}[ ]+([A-Z]{4})[ ]?/ ) {
128             # >>> DATA
129 16   33     94 $commandtxt ||= $1;
130              
131             } else {
132             # Continued line of the value of Diagnostic-Code field
133 210 100       318 next unless index($p, 'Diagnostic-Code:') == 0;
134 5 50       33 next unless $e =~ /\A[ \t]+(.+)\z/;
135 5         17 $v->{'diagnosis'} .= ' '.$1;
136             }
137             }
138             } continue {
139             # Save the current line for the next loop
140 788         887 $p = $e;
141             }
142 21 50       93 return undef unless $recipients;
143              
144 21         52 for my $e ( @$dscontents ) {
145             # Set default values if each value is empty.
146 21   0     157 $e->{ $_ } ||= $permessage->{ $_ } || '' for keys %$permessage;
      33        
147 21         176 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'});
148              
149 21         74 for my $r ( keys %$messagesof ) {
150             # Verify each regular expression of session errors
151 57 100       63 next unless grep { index($e->{'diagnosis'}, $_) > -1 } @{ $messagesof->{ $r } };
  57         175  
  57         89  
152 5         11 $e->{'reason'} = $r;
153 5         10 last;
154             }
155 21   100     150 $e->{'command'} ||= $commandtxt || '';
      66        
156             }
157 21         192 return { 'ds' => $dscontents, 'rfc822' => $emailsteak->[1] };
158             }
159              
160             1;
161             __END__