File Coverage

lib/Sisimai/Lhost/DragonFly.pm
Criterion Covered Total %
statement 45 47 95.7
branch 16 20 80.0
condition 9 12 75.0
subroutine 6 6 100.0
pod 2 2 100.0
total 78 87 89.6


line stmt bran cond sub pod time code
1             package Sisimai::Lhost::DragonFly;
2 41     41   3260 use parent 'Sisimai::Lhost';
  41         66  
  41         223  
3 41     41   2684 use v5.26;
  41         112  
4 41     41   201 use strict;
  41         52  
  41         784  
5 41     41   143 use warnings;
  41         73  
  41         20485  
6              
7 1     1 1 3 sub description { 'DragonFly: https://www.dragonflybsd.org/handbook/mta/' }
8             sub inquire {
9             # Detect an error from DMA: DragonFly Mail Agent
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 v5.0.4
15 1068     1068 1 2775 my $class = shift;
16 1068   100     2107 my $mhead = shift // return undef;
17 1067   100     2459 my $mbody = shift // return undef;
18              
19 1066 100       3299 return undef unless index($mhead->{'subject'}, 'Mail delivery failed') > -1;
20 198 100       422 return undef unless grep { rindex($_, ' (DragonFly Mail Agent') > -1 } $mhead->{'received'}->@*;
  227         668  
21              
22 150         197 state $indicators = __PACKAGE__->INDICATORS;
23 150         178 state $boundaries = ['Original message follows.', 'Message headers follow'];
24 150         195 state $startingof = {
25             # https://github.com/corecode/dma/blob/ffad280aa40c242aa9a2cb9ca5b1b6e8efedd17e/mail.c#L84
26             'message' => ['This is the DragonFly Mail Agent '],
27             };
28              
29 150         443 my $dscontents = [__PACKAGE__->DELIVERYSTATUS]; my $v = undef;
  150         225  
30 150         584 my $emailparts = Sisimai::RFC5322->part($mbody, $boundaries);
31 150         216 my $readcursor = 0; # (Integer) Points the current cursor position
32 150         190 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
33              
34 150         495 require Sisimai::Address;
35 150         411 require Sisimai::SMTP::Command;
36              
37 150         664 for my $e ( split("\n", $emailparts->[0]) ) {
38             # Read error messages and delivery status lines from the head of the email to the previous
39             # line of the beginning of the original message.
40 905 100       1302 unless( $readcursor ) {
41             # Beginning of the bounce message or message/delivery-status part
42 150 50       546 $readcursor |= $indicators->{'deliverystatus'} if index($e, $startingof->{'message'}->[0]) == 0;
43 150         220 next;
44             }
45 755 100 66     2031 next if ($readcursor & $indicators->{'deliverystatus'}) == 0 || $e eq "";
46              
47             # This is the DragonFly Mail Agent v0.13 at df.example.jp.
48             #
49             # There was an error delivering your mail to .
50             #
51             # email.example.jp [192.0.2.25] did not like our RCPT TO:
52             # 552 5.2.2 : Recipient address rejected: Mailbox full
53             #
54             # Original message follows.
55 455         567 $v = $dscontents->[-1];
56              
57 455 100       813 if( index($e, 'There was an error delivering your mail to <') == 0 ) {
58             # email.example.jp [192.0.2.25] did not like our RCPT TO:
59             # 552 5.2.2 : Recipient address rejected: Mailbox full
60 150 50       297 if( $v->{'recipient'} ) {
61             # There are multiple recipient addresses in the message body.
62 0         0 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
63 0         0 $v = $dscontents->[-1];
64             }
65 150         991 $v->{'recipient'} = Sisimai::Address->s3s4(substr($e, index($e, '<'), -1));
66 150         293 $recipients++;
67              
68             } else {
69             # Pick the error message
70 305         609 $v->{'diagnosis'} .= $e;
71              
72             # Pick the remote hostname, and the SMTP command
73             # net.c:500| snprintf(errmsg, sizeof(errmsg), "%s [%s] did not like our %s:\n%s",
74 305 100 66     968 next if index($e, ' did not like our ') < 0 || length $v->{'rhost'} > 0;
75              
76 135         399 my $p = [split(' ', $e, 3)];
77 135 50       362 $v->{'rhost'} = index($p->[0], '.') > 1 ? $p->[0] : $p->[1];
78 135   50     949 $v->{'command'} = Sisimai::SMTP::Command->find($e) || '';
79             }
80             }
81 150 50       365 return undef unless $recipients;
82 150         664 return {"ds" => $dscontents, "rfc822" => $emailparts->[1]};
83             }
84              
85             1;
86             __END__