File Coverage

lib/Sisimai/Lhost/Exchange2003.pm
Criterion Covered Total %
statement 104 104 100.0
branch 55 66 83.3
condition 24 35 68.5
subroutine 6 6 100.0
pod 2 2 100.0
total 191 213 89.6


line stmt bran cond sub pod time code
1             package Sisimai::Lhost::Exchange2003;
2 42     42   3962 use parent 'Sisimai::Lhost';
  42         87  
  42         314  
3 42     42   3549 use v5.26;
  42         144  
4 42     42   221 use strict;
  42         75  
  42         1180  
5 42     42   191 use warnings;
  42         90  
  42         65039  
6              
7 1     1 1 4 sub description { 'Microsoft Exchange Server 2003: https://www.microsoft.com/microsoft-365/exchange/email' }
8             sub inquire {
9             # Detect an error from Microsoft Exchange Server 2003
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.1
15 952     952 1 3348 my $class = shift;
16 952   100     2819 my $mhead = shift // return undef;
17 951   100     3230 my $mbody = shift // return undef;
18              
19             # X-MS-TNEF-Correlator: <00000000000000000000000000000000000000@example.com>
20             # X-Mailer: Internet Mail Service (5.5.1960.3)
21             # X-MS-Embedded-Report:
22 950 100 50     1649 my $match = 0; $match ||= 1 if defined $mhead->{'x-ms-embedded-report'};
  950         3500  
23 950         2184 EXCHANGE_OR_NOT: while(1) {
24             # Check the value of X-Mailer header
25 950 100       3062 last if $match;
26              
27 928 100       3288 if( defined $mhead->{'x-mailer'} ) {
28             # X-Mailer: Microsoft Exchange Server Internet Mail Connector Version 4.0.994.63
29             # X-Mailer: Internet Mail Service (5.5.2232.9)
30 82         286 my $tryto = ['Internet Mail Service (', 'Microsoft Exchange Server Internet Mail Connector'];
31 82   50     339 my $value = $mhead->{'x-mailer'} || '';
32 82 100 50     567 $match ||= 1 if index($value, $tryto->[0]) == 0 || index($value, $tryto->[1]) == 0;
      66        
33 82 100       311 last if $match;
34             }
35              
36 923 100       2857 if( defined $mhead->{'x-mimeole'} ) {
37             # X-MimeOLE: Produced By Microsoft Exchange V6.5
38 5 50 0     25 $match ||= 1 if index($mhead->{'x-mimeole'}, 'Produced By Microsoft Exchange') == 0;
39 5 50       17 last if $match;
40             }
41              
42 923 100       3087 last unless scalar $mhead->{'received'}->@*;
43 846         2574 for my $e ( $mhead->{'received'}->@* ) {
44             # Received: by ***.**.** with Internet Mail Service (5.5.2657.72)
45 1797 100       6629 next unless rindex($e, ' with Internet Mail Service (') > -1;
46 5         12 $match = 1;
47 5         17 last(EXCHANGE_OR_NOT);
48             }
49 841         1471 last;
50             }
51 950 100       3663 return undef unless $match;
52              
53 32         82 state $indicators = __PACKAGE__->INDICATORS;
54 32         78 state $boundaries = ['Content-Type: message/rfc822'];
55 32         75 state $startingof = {
56             'message' => ['Your message'],
57             'error' => ['did not reach the following recipient(s):'],
58             };
59 32         86 state $errorcodes = {
60             'onhold' => [
61             '000B099C', # Host Unknown, Message exceeds size limit, ...
62             '000B09AA', # Unable to relay for, Message exceeds size limit,...
63             '000B09B6', # Error messages by remote MTA
64             ],
65             'userunknown' => [
66             '000C05A6', # Unknown Recipient,
67             ],
68             'systemerror' => [
69             '00010256', # Too many recipients.
70             '000D06B5', # No proxy for recipient (non-smtp mail?)
71             ],
72             'networkerror' => [
73             '00120270', # Too Many Hops
74             ],
75             'contenterror' => [
76             '00050311', # Conversion to Internet format failed
77             '000502CC', # Conversion to Internet format failed
78             ],
79             'securityerror' => [
80             '000B0981', # 502 Server does not support AUTH
81             ],
82             'filtered' => [
83             '000C0595', # Ambiguous Recipient
84             ],
85             };
86              
87 32         212 my $dscontents = [__PACKAGE__->DELIVERYSTATUS]; my $v = undef;
  32         73  
88 32         172 my $emailparts = Sisimai::RFC5322->part($mbody, $boundaries);
89 32         66 my $readcursor = 0; # (Integer) Points the current cursor position
90 32         53 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
91 32         60 my $statuspart = 0; # (Integer) Flag, 1 = have got delivery status part.
92 32         54 my $connvalues = 0; # (Integer) Flag, 1 if all the value of $connheader have been set
93 32         170 my $connheader = {
94             'to' => '', # The value of "To"
95             'date' => '', # The value of "Date"
96             'subject' => '', # The value of "Subject"
97             };
98              
99 32         340 for my $e ( split("\n", $emailparts->[0]) ) {
100             # Read error messages and delivery status lines from the head of the email to the previous
101             # line of the beginning of the original message.
102 474 100       961 unless( $readcursor ) {
103             # Beginning of the bounce message or message/delivery-status part
104 82 100       286 $readcursor |= $indicators->{'deliverystatus'} if index($e, $startingof->{'message'}->[0]) == 0;
105 82         187 next;
106             }
107 392 50 33     1406 next if ($readcursor & $indicators->{'deliverystatus'}) == 0 || $statuspart == 1;
108              
109 392 100       831 if( $connvalues == scalar(keys %$connheader) ) {
110             # did not reach the following recipient(s):
111             #
112             # kijitora@example.co.jp on Thu, 29 Apr 2007 16:51:51 -0500
113             # The recipient name is not recognized
114             # The MTS-ID of the original message is: c=jp;a= ;p=neko
115             # ;l=EXCHANGE000000000000000000
116             # MSEXCH:IMS:KIJITORA CAT:EXAMPLE:EXCHANGE 0 (000C05A6) Unknown Recipient
117             # mikeneko@example.co.jp on Thu, 29 Apr 2007 16:51:51 -0500
118             # The recipient name is not recognized
119             # The MTS-ID of the original message is: c=jp;a= ;p=neko
120             # ;l=EXCHANGE000000000000000000
121             # MSEXCH:IMS:KIJITORA CAT:EXAMPLE:EXCHANGE 0 (000C05A6) Unknown Recipient
122 264         483 $v = $dscontents->[-1];
123              
124 264 100 100     1069 if( Sisimai::String->aligned(\$e, ['@', ' on ']) ) {
    100          
125             # kijitora@example.co.jp on Thu, 29 Apr 2007 16:51:51 -0500
126             # kijitora@example.com on 4/29/99 9:19:59 AM
127 37 100       113 if( $v->{'recipient'} ) {
128             # There are multiple recipient addresses in the message body.
129 5         27 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
130 5         14 $v = $dscontents->[-1];
131             }
132              
133 37 50       134 my $p1 = index(lc $e, 'smtp='); $p1 = $p1 == -1 ? 0 : $p1 + 5;
  37         98  
134 37         95 my $p2 = index($e, ' on ') + 1;
135 37         328 $v->{'recipient'} = Sisimai::Address->s3s4(substr($e, $p1, $p2));
136 37         142 $v->{'msexch'} = 0;
137 37         113 $recipients++;
138              
139             } elsif( index($e, ' ') == 0 && index($e, 'MSEXCH:') > 0 ) {
140             # MSEXCH:IMS:KIJITORA CAT:EXAMPLE:EXCHANGE 0 (000C05A6) Unknown Recipient
141 37         177 $v->{'diagnosis'} .= substr($e, index($e, 'MSEXCH:'),);
142              
143             } else {
144 190 50       417 next if $v->{'msexch'};
145 190 100       395 if( index($v->{'diagnosis'}, 'MSEXCH:') == 0 ) {
146             # Continued from MEEXCH in the previous line
147 17         34 $v->{'msexch'} = 1;
148 17         59 $v->{'diagnosis'} .= ' '.$e;
149 17         76 $statuspart = 1;
150              
151             } else {
152             # Error message in the body part
153 173         594 $v->{'alterrors'} .= ' '.$e;
154             }
155             }
156             } else {
157             # Your message
158             #
159             # To: shironeko@example.jp
160             # Subject: ...
161             # Sent: Thu, 29 Apr 2010 18:14:35 +0000
162             #
163 128 100 100     1054 if( index($e, ' To: ') == 0 || index($e, ' To: ') == 0 ) {
    100 100        
    100 100        
164             # To: shironeko@example.jp
165 32 50       100 next if $connheader->{'to'};
166 32         164 $connheader->{'to'} = substr($e, rindex($e, ' ') + 1,);
167 32         65 $connvalues++;
168              
169             } elsif( index($e, ' Subject: ') == 0 || index($e, ' Subject: ') == 0 ) {
170             # Subject: ...
171 32 50       110 next if length $connheader->{'subject'};
172 32         109 $connheader->{'subject'} = substr($e, rindex($e, ' ') + 1,);
173 32         64 $connvalues++;
174              
175             } elsif( index($e, ' Sent: ') == 0 || index($e, ' Sent: ') == 0 ) {
176             # Sent: Thu, 29 Apr 2010 18:14:35 +0000
177             # Sent: 4/29/99 9:19:59 AM
178 32 50       100 next if $connheader->{'date'};
179 32         106 $connheader->{'date'} = substr($e, index($e, ':') + 2,);
180 32         55 $connvalues++;
181             }
182             } # End of error message part
183             }
184 32 50       161 return undef unless $recipients;
185              
186 32         86 for my $e ( @$dscontents ) {
187 37         176 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'});
188 37         110 delete $e->{'msexch'};
189              
190 37 50       128 if( index($e->{'diagnosis'}, 'MSEXCH:') == 0 ) {
191             # MSEXCH:IMS:KIJITORA CAT:EXAMPLE:EXCHANGE 0 (000C05A6) Unknown Recipient
192 37         156 my $capturedcode = substr($e->{'diagnosis'}, index($e->{'diagnosis'}, '(') + 1, 8);
193 37         104 my $errormessage = substr($e->{'diagnosis'}, index($e->{'diagnosis'}, ')') + 1, );
194              
195 37         219 for my $r ( keys %$errorcodes ) {
196             # Find captured code from the error code table
197 175 100       369 next unless grep { $capturedcode eq $_ } $errorcodes->{ $r }->@*;
  295         748  
198 32         83 $e->{'reason'} = $r;
199 32   50     350 $e->{'status'} = Sisimai::SMTP::Status->code($r) || '';
200 32         71 last;
201             }
202 37         124 $e->{'diagnosis'} = $errormessage;
203             }
204              
205             # Could not detect the reason from the value of "diagnosis", copy alternative error message
206 37 100       129 next if $e->{'reason'};
207 5 50 33     36 next unless exists $e->{'alterrors'} && length $e->{'alterrors'};
208              
209 5         20 $e->{'diagnosis'} = $e->{'alterrors'}.' '.$e->{'diagnosis'};
210 5         20 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'});
211 5         15 delete $e->{'alterrors'};
212             }
213              
214 32 100       121 unless( length $emailparts->[1] ) {
215             # When original message does not included in the bounce message
216 12         52 $emailparts->[1] .= sprintf("From: %s\n", $connheader->{'to'});
217 12         73 $emailparts->[1] .= sprintf("Date: %s\n", $connheader->{'date'});
218 12         33 $emailparts->[1] .= sprintf("Subject: %s\n", $connheader->{'subject'});
219             }
220 32         305 return {"ds" => $dscontents, "rfc822" => $emailparts->[1]};
221             }
222              
223             1;
224             __END__