File Coverage

lib/Sisimai/Lhost/Exchange2003.pm
Criterion Covered Total %
statement 102 102 100.0
branch 55 66 83.3
condition 24 35 68.5
subroutine 6 6 100.0
pod 2 2 100.0
total 189 211 89.5


line stmt bran cond sub pod time code
1             package Sisimai::Lhost::Exchange2003;
2 43     43   2865 use parent 'Sisimai::Lhost';
  43         67  
  43         215  
3 43     43   2864 use v5.26;
  43         114  
4 43     43   166 use strict;
  43         76  
  43         1020  
5 43     43   162 use warnings;
  43         54  
  43         46337  
6              
7 1     1 1 2 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 950     950 1 2822 my $class = shift;
16 950   100     1813 my $mhead = shift // return undef;
17 949   100     1732 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 948 100 50     1075 my $match = 0; $match ||= 1 if defined $mhead->{'x-ms-embedded-report'};
  948         2168  
23 948         1115 EXCHANGE_OR_NOT: while(1) {
24             # Check the value of X-Mailer header
25 948 100       1761 last if $match;
26              
27 926 100       1890 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         188 my $tryto = ['Internet Mail Service (', 'Microsoft Exchange Server Internet Mail Connector'];
31 82   50     260 my $value = $mhead->{'x-mailer'} || '';
32 82 100 50     413 $match ||= 1 if index($value, $tryto->[0]) == 0 || index($value, $tryto->[1]) == 0;
      66        
33 82 100       179 last if $match;
34             }
35              
36 921 100       1809 if( defined $mhead->{'x-mimeole'} ) {
37             # X-MimeOLE: Produced By Microsoft Exchange V6.5
38 5 50 0     14 $match ||= 1 if index($mhead->{'x-mimeole'}, 'Produced By Microsoft Exchange') == 0;
39 5 50       13 last if $match;
40             }
41              
42 921 100       2099 last unless scalar $mhead->{'received'}->@*;
43 843         1735 for my $e ( $mhead->{'received'}->@* ) {
44             # Received: by ***.**.** with Internet Mail Service (5.5.2657.72)
45 1784 100       3881 next unless rindex($e, ' with Internet Mail Service (') > -1;
46 5         10 $match = 1;
47 5         9 last(EXCHANGE_OR_NOT);
48             }
49 838         1281 last;
50             }
51 948 100       2213 return undef unless $match;
52              
53 32         50 state $indicators = __PACKAGE__->INDICATORS;
54 32         37 state $boundaries = ['Content-Type: message/rfc822'];
55 32         44 state $startingof = {
56             'message' => ['Your message'],
57             'error' => ['did not reach the following recipient(s):'],
58             };
59 32         62 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         97 my $dscontents = [__PACKAGE__->DELIVERYSTATUS]; my $v = undef;
  32         38  
88 32         114 my $emailparts = Sisimai::RFC5322->part($mbody, $boundaries);
89 32         37 my $readcursor = 0; # (Integer) Points the current cursor position
90 32         37 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
91 32         48 my $statuspart = 0; # (Integer) Flag, 1 = have got delivery status part.
92 32         25 my $connvalues = 0; # (Integer) Flag, 1 if all the value of $connheader have been set
93 32         100 my $connheader = {
94             'to' => '', # The value of "To"
95             'date' => '', # The value of "Date"
96             'subject' => '', # The value of "Subject"
97             };
98              
99 32         198 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       623 unless( $readcursor ) {
103             # Beginning of the bounce message or message/delivery-status part
104 82 100       168 $readcursor |= $indicators->{'deliverystatus'} if index($e, $startingof->{'message'}->[0]) == 0;
105 82         83 next;
106             }
107 392 50 33     843 next if ($readcursor & $indicators->{'deliverystatus'}) == 0 || $statuspart == 1;
108              
109 392 100       461 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         232 $v = $dscontents->[-1];
123              
124 264 100 100     583 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       66 if( $v->{'recipient'} ) {
128             # There are multiple recipient addresses in the message body.
129 5         16 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
130 5         15 $v = $dscontents->[-1];
131             }
132              
133 37 50       61 my $p1 = index(lc $e, 'smtp='); $p1 = $p1 == -1 ? 0 : $p1 + 5;
  37         63  
134 37         51 my $p2 = index($e, ' on ') + 1;
135 37         202 $v->{'recipient'} = Sisimai::Address->s3s4(substr($e, $p1, $p2));
136 37         71 $v->{'msexch'} = 0;
137 37         67 $recipients++;
138              
139             } elsif( index($e, ' ') == 0 && index($e, 'MSEXCH:') > 0 ) {
140             # MSEXCH:IMS:KIJITORA CAT:EXAMPLE:EXCHANGE 0 (000C05A6) Unknown Recipient
141 37         100 $v->{'diagnosis'} .= substr($e, index($e, 'MSEXCH:'),);
142              
143             } else {
144 190 50       257 next if $v->{'msexch'};
145 190 100       265 if( index($v->{'diagnosis'}, 'MSEXCH:') == 0 ) {
146             # Continued from MEEXCH in the previous line
147 17         24 $v->{'msexch'} = 1;
148 17         33 $v->{'diagnosis'} .= ' '.$e;
149 17         32 $statuspart = 1;
150              
151             } else {
152             # Error message in the body part
153 173         390 $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     537 if( index($e, ' To: ') == 0 || index($e, ' To: ') == 0 ) {
    100 100        
    100 100        
164             # To: shironeko@example.jp
165 32 50       107 next if $connheader->{'to'};
166 32         71 $connheader->{'to'} = substr($e, rindex($e, ' ') + 1,);
167 32         37 $connvalues++;
168              
169             } elsif( index($e, ' Subject: ') == 0 || index($e, ' Subject: ') == 0 ) {
170             # Subject: ...
171 32 50       54 next if length $connheader->{'subject'};
172 32         65 $connheader->{'subject'} = substr($e, rindex($e, ' ') + 1,);
173 32         37 $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       56 next if $connheader->{'date'};
179 32         59 $connheader->{'date'} = substr($e, index($e, ':') + 2,);
180 32         34 $connvalues++;
181             }
182             } # End of error message part
183             }
184 32 50       111 return undef unless $recipients;
185              
186 32         41 for my $e ( @$dscontents ) {
187 37         49 delete $e->{'msexch'};
188              
189 37 50       71 if( index($e->{'diagnosis'}, 'MSEXCH:') == 0 ) {
190             # MSEXCH:IMS:KIJITORA CAT:EXAMPLE:EXCHANGE 0 (000C05A6) Unknown Recipient
191 37         81 my $capturedcode = substr($e->{'diagnosis'}, index($e->{'diagnosis'}, '(') + 1, 8);
192 37         56 my $errormessage = substr($e->{'diagnosis'}, index($e->{'diagnosis'}, ')') + 1, );
193              
194 37         122 for my $r ( keys %$errorcodes ) {
195             # Find captured code from the error code table
196 181 100       202 next unless grep { $capturedcode eq $_ } $errorcodes->{ $r }->@*;
  311         423  
197 32         41 $e->{'reason'} = $r;
198 32   50     183 $e->{'status'} = Sisimai::SMTP::Status->code($r) || '';
199 32         40 last;
200             }
201 37         71 $e->{'diagnosis'} = $errormessage;
202             }
203              
204             # Could not detect the reason from the value of "diagnosis", copy alternative error message
205 37 100       72 next if $e->{'reason'};
206 5 50 33     23 next unless exists $e->{'alterrors'} && length $e->{'alterrors'};
207              
208 5         14 $e->{'diagnosis'} = $e->{'alterrors'}.' '.$e->{'diagnosis'};
209 5         9 delete $e->{'alterrors'};
210             }
211              
212 32 100       60 unless( length $emailparts->[1] ) {
213             # When original message does not included in the bounce message
214 12         34 $emailparts->[1] .= sprintf("From: %s\n", $connheader->{'to'});
215 12         25 $emailparts->[1] .= sprintf("Date: %s\n", $connheader->{'date'});
216 12         29 $emailparts->[1] .= sprintf("Subject: %s\n", $connheader->{'subject'});
217             }
218 32         166 return {"ds" => $dscontents, "rfc822" => $emailparts->[1]};
219             }
220              
221             1;
222             __END__