File Coverage

lib/Sisimai/Lhost/Domino.pm
Criterion Covered Total %
statement 86 91 94.5
branch 39 46 84.7
condition 14 29 48.2
subroutine 9 9 100.0
pod 2 2 100.0
total 150 177 84.7


line stmt bran cond sub pod time code
1             package Sisimai::Lhost::Domino;
2 17     17   8036 use parent 'Sisimai::Lhost';
  17         335  
  17         94  
3 17     17   1050 use feature ':5.10';
  17         32  
  17         1231  
4 17     17   91 use strict;
  17         42  
  17         420  
5 17     17   75 use warnings;
  17         25  
  17         458  
6 17     17   467 use Sisimai::String;
  17         30  
  17         486  
7 17     17   92 use Encode;
  17         38  
  17         1820  
8 17     17   1095 use Encode::Guess; Encode::Guess->add_suspects(@{ Sisimai::String->encodenames });
  17         40  
  17         215  
9              
10 2     2 1 1320 sub description { 'IBM Domino Server' }
11             sub make {
12             # Detect an error from IBM Domino
13             # @param [Hash] mhead Message headers of a bounce email
14             # @param [String] mbody Message body of a bounce email
15             # @return [Hash] Bounce data list and message/rfc822 part
16             # @return [Undef] failed to parse or the arguments are missing
17             # @since v4.0.2
18 220     220 1 806 my $class = shift;
19 220   100     570 my $mhead = shift // return undef;
20 219   50     530 my $mbody = shift // return undef;
21 219 100       1039 return undef unless $mhead->{'subject'} =~ /\ADELIVERY(?:[ ]|_)FAILURE:/;
22              
23 17         86 state $indicators = __PACKAGE__->INDICATORS;
24 17         63 state $rebackbone = qr|^Content-Type:[ ]message/rfc822|m;
25 17         35 state $startingof = { 'message' => ['Your message'] };
26 17         67 state $messagesof = {
27             'userunknown' => [
28             'not listed in Domino Directory',
29             'not listed in public Name & Address Book',
30             "non répertorié dans l'annuaire Domino",
31             'Domino ディレクトリには見つかりません',
32             ],
33             'filtered' => ['Cannot route mail to user'],
34             'systemerror' => ['Several matches found in Domino Directory'],
35             };
36              
37 17         820 require Sisimai::RFC1894;
38 17         90 my $fieldtable = Sisimai::RFC1894->FIELDTABLE;
39 17         62 my $permessage = {}; # (Hash) Store values of each Per-Message field
40 17         79 my $dscontents = [__PACKAGE__->DELIVERYSTATUS];
41 17         101 my $emailsteak = Sisimai::RFC5322->fillet($mbody, $rebackbone);
42 17         69 my $readcursor = 0; # (Integer) Points the current cursor position
43 17         47 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
44 17         32 my $subjecttxt = ''; # (String) The value of Subject:
45 17         30 my $v = undef;
46 17         27 my $p = '';
47              
48 17         142 for my $e ( split("\n", $emailsteak->[0]) ) {
49             # Read error messages and delivery status lines from the head of the email
50             # to the previous line of the beginning of the original message.
51 297 100       433 unless( $readcursor ) {
52             # Beginning of the bounce message or message/delivery-status part
53 22 100       121 $readcursor |= $indicators->{'deliverystatus'} if index($e, $startingof->{'message'}->[0]) == 0;
54 22         54 next;
55             }
56 275 50       436 next unless $readcursor & $indicators->{'deliverystatus'};
57 275 100       416 next unless length $e;
58              
59             # Your message
60             #
61             # Subject: Test Bounce
62             #
63             # was not delivered to:
64             #
65             # kijitora@example.net
66             #
67             # because:
68             #
69             # User some.name (kijitora@example.net) not listed in Domino Directory
70             #
71 168         207 $v = $dscontents->[-1];
72 168 100       556 if( $e eq 'was not delivered to:' ) {
    100          
    100          
73             # was not delivered to:
74 17 50       71 if( $v->{'recipient'} ) {
75             # There are multiple recipient addresses in the message body.
76 0         0 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
77 0         0 $v = $dscontents->[-1];
78             }
79 17   33     95 $v->{'recipient'} ||= $e;
80 17         50 $recipients++;
81              
82             } elsif( $e =~ /\A[ ][ ]([^ ]+[@][^ ]+)\z/ ) {
83             # Continued from the line "was not delivered to:"
84             # kijitora@example.net
85 17         125 $v->{'recipient'} = Sisimai::Address->s3s4($1);
86              
87             } elsif( $e eq 'because:' ) {
88             # because:
89 17         49 $v->{'diagnosis'} = $e;
90              
91             } else {
92 117 100 66     590 if( exists $v->{'diagnosis'} && $v->{'diagnosis'} eq 'because:' ) {
    100          
    100          
93             # Error message, continued from the line "because:"
94 17         76 $v->{'diagnosis'} = $e;
95              
96             } elsif( $e =~ /\A[ ][ ]Subject: (.+)\z/ ) {
97             # Subject: Nyaa
98 17         65 $subjecttxt = $1;
99              
100             } elsif( my $f = Sisimai::RFC1894->match($e) ) {
101             # There are some fields defined in RFC3464, try to match
102 61 50       132 next unless my $o = Sisimai::RFC1894->field($e);
103 61 100       132 next if $o->[-1] eq 'addr';
104              
105 50 100       115 if( $o->[-1] eq 'code' ) {
106             # Diagnostic-Code: SMTP; 550 5.1.1 ... User Unknown
107 11   33     89 $v->{'spec'} ||= $o->[1];
108 11   33     48 $v->{'diagnosis'} ||= $o->[2];
109              
110             } else {
111             # Other DSN fields defined in RFC3464
112 39 50       95 next unless exists $fieldtable->{ $o->[0] };
113 39         84 $v->{ $fieldtable->{ $o->[0] } } = $o->[2];
114              
115 39 100       100 next unless $f == 1;
116 11         48 $permessage->{ $fieldtable->{ $o->[0] } } = $o->[2];
117             }
118             }
119             }
120             }
121 17 50       101 return undef unless $recipients;
122              
123 17         72 for my $e ( @$dscontents ) {
124             # Check the utf8 flag and fix
125 17         30 UTF8FLAG: while(1) {
126             # Delete the utf8 flag because there are a string including some characters which have
127             # utf8 flag but utf8::is_utf8 returns false
128 17 50       52 last unless length $e->{'diagnosis'};
129 17 100       107 last unless Sisimai::String->is_8bit(\$e->{'diagnosis'});
130              
131 5         33 my $cv = $e->{'diagnosis'};
132 5         31 my $ce = Encode::Guess->guess($cv);
133 5 50       1393 last unless ref $ce;
134              
135 0         0 $cv = Encode::encode_utf8($cv);
136 0         0 $e->{'diagnosis'} = $cv;
137 0         0 last;
138             }
139              
140 17         94 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'});
141 17         109 $e->{'recipient'} = Sisimai::Address->s3s4($e->{'recipient'});
142 17   66     146 $e->{'lhost'} ||= $permessage->{'rhost'};
143 17   0     94 $e->{ $_ } ||= $permessage->{ $_ } || '' for keys %$permessage;
      33        
144              
145 17         72 for my $r ( keys %$messagesof ) {
146             # Check each regular expression of Domino error messages
147 33 100       47 next unless grep { index($e->{'diagnosis'}, $_) > -1 } @{ $messagesof->{ $r } };
  84         297  
  33         85  
148 11         31 $e->{'reason'} = $r;
149 11   50     69 $e->{'status'} ||= Sisimai::SMTP::Status->code($r, 0) || '';
      66        
150 11         419 last;
151             }
152             }
153              
154             # Set the value of $subjecttxt as a Subject if there is no original
155             # message in the bounce mail.
156 17 100       158 $emailsteak->[1] .= sprintf("Subject: %s\n", $subjecttxt) unless $emailsteak->[1] =~ /^Subject:/m;
157              
158 17         423 return { 'ds' => $dscontents, 'rfc822' => $emailsteak->[1] };
159             }
160              
161             1;
162             __END__