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   8187 use parent 'Sisimai::Lhost';
  17         363  
  17         107  
3 17     17   1085 use feature ':5.10';
  17         37  
  17         1184  
4 17     17   94 use strict;
  17         29  
  17         346  
5 17     17   79 use warnings;
  17         38  
  17         430  
6 17     17   503 use Sisimai::String;
  17         35  
  17         466  
7 17     17   99 use Encode;
  17         40  
  17         1863  
8 17     17   113 use Encode::Guess; Encode::Guess->add_suspects(@{ Sisimai::String->encodenames });
  17         42  
  17         229  
9              
10 2     2 1 1214 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 734 my $class = shift;
19 220   100     684 my $mhead = shift // return undef;
20 219   50     1888 my $mbody = shift // return undef;
21 219 100       955 return undef unless $mhead->{'subject'} =~ /\ADELIVERY(?:[ ]|_)FAILURE:/;
22              
23 17         82 state $indicators = __PACKAGE__->INDICATORS;
24 17         44 state $rebackbone = qr|^Content-Type:[ ]message/rfc822|m;
25 17         33 state $startingof = { 'message' => ['Your message'] };
26 17         42 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         819 require Sisimai::RFC1894;
38 17         126 my $fieldtable = Sisimai::RFC1894->FIELDTABLE;
39 17         50 my $permessage = {}; # (Hash) Store values of each Per-Message field
40 17         82 my $dscontents = [__PACKAGE__->DELIVERYSTATUS];
41 17         99 my $emailsteak = Sisimai::RFC5322->fillet($mbody, $rebackbone);
42 17         35 my $readcursor = 0; # (Integer) Points the current cursor position
43 17         28 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
44 17         102 my $subjecttxt = ''; # (String) The value of Subject:
45 17         29 my $v = undef;
46 17         29 my $p = '';
47              
48 17         144 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       108 $readcursor |= $indicators->{'deliverystatus'} if index($e, $startingof->{'message'}->[0]) == 0;
54 22         39 next;
55             }
56 275 50       441 next unless $readcursor & $indicators->{'deliverystatus'};
57 275 100       399 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         215 $v = $dscontents->[-1];
72 168 100       501 if( $e eq 'was not delivered to:' ) {
    100          
    100          
73             # was not delivered to:
74 17 50       59 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     92 $v->{'recipient'} ||= $e;
80 17         24 $recipients++;
81              
82             } elsif( $e =~ /\A[ ][ ]([^ ]+[@][^ ]+)\z/ ) {
83             # Continued from the line "was not delivered to:"
84             # kijitora@example.net
85 17         117 $v->{'recipient'} = Sisimai::Address->s3s4($1);
86              
87             } elsif( $e eq 'because:' ) {
88             # because:
89 17         52 $v->{'diagnosis'} = $e;
90              
91             } else {
92 117 100 66     562 if( exists $v->{'diagnosis'} && $v->{'diagnosis'} eq 'because:' ) {
    100          
    100          
93             # Error message, continued from the line "because:"
94 17         53 $v->{'diagnosis'} = $e;
95              
96             } elsif( $e =~ /\A[ ][ ]Subject: (.+)\z/ ) {
97             # Subject: Nyaa
98 17         64 $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       133 next unless my $o = Sisimai::RFC1894->field($e);
103 61 100       152 next if $o->[-1] eq 'addr';
104              
105 50 100       102 if( $o->[-1] eq 'code' ) {
106             # Diagnostic-Code: SMTP; 550 5.1.1 ... User Unknown
107 11   33     66 $v->{'spec'} ||= $o->[1];
108 11   33     41 $v->{'diagnosis'} ||= $o->[2];
109              
110             } else {
111             # Other DSN fields defined in RFC3464
112 39 50       92 next unless exists $fieldtable->{ $o->[0] };
113 39         86 $v->{ $fieldtable->{ $o->[0] } } = $o->[2];
114              
115 39 100       101 next unless $f == 1;
116 11         43 $permessage->{ $fieldtable->{ $o->[0] } } = $o->[2];
117             }
118             }
119             }
120             }
121 17 50       73 return undef unless $recipients;
122              
123 17         59 for my $e ( @$dscontents ) {
124             # Check the utf8 flag and fix
125 17         29 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       58 last unless length $e->{'diagnosis'};
129 17 100       565 last unless Sisimai::String->is_8bit(\$e->{'diagnosis'});
130              
131 5         22 my $cv = $e->{'diagnosis'};
132 5         316 my $ce = Encode::Guess->guess($cv);
133 5 50       1339 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         74 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'});
141 17         60 $e->{'recipient'} = Sisimai::Address->s3s4($e->{'recipient'});
142 17   66     110 $e->{'lhost'} ||= $permessage->{'rhost'};
143 17   0     87 $e->{ $_ } ||= $permessage->{ $_ } || '' for keys %$permessage;
      33        
144              
145 17         74 for my $r ( keys %$messagesof ) {
146             # Check each regular expression of Domino error messages
147 31 100       61 next unless grep { index($e->{'diagnosis'}, $_) > -1 } @{ $messagesof->{ $r } };
  82         277  
  31         80  
148 11         37 $e->{'reason'} = $r;
149 11   50     69 $e->{'status'} ||= Sisimai::SMTP::Status->code($r, 0) || '';
      66        
150 11         26 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       114 $emailsteak->[1] .= sprintf("Subject: %s\n", $subjecttxt) unless $emailsteak->[1] =~ /^Subject:/m;
157              
158 17         134 return { 'ds' => $dscontents, 'rfc822' => $emailsteak->[1] };
159             }
160              
161             1;
162             __END__