File Coverage

lib/Sisimai/Lhost/Exchange2003.pm
Criterion Covered Total %
statement 107 107 100.0
branch 57 68 83.8
condition 14 23 60.8
subroutine 6 6 100.0
pod 2 2 100.0
total 186 206 90.2


line stmt bran cond sub pod time code
1             package Sisimai::Lhost::Exchange2003;
2 23     23   5989 use parent 'Sisimai::Lhost';
  23         48  
  23         124  
3 23     23   1468 use feature ':5.10';
  23         43  
  23         1582  
4 23     23   124 use strict;
  23         66  
  23         446  
5 23     23   106 use warnings;
  23         59  
  23         33734  
6              
7 2     2 1 1170 sub description { 'Microsoft Exchange Server 2003' }
8             sub make {
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 parse or the arguments are missing
14             # @since v4.1.1
15 283     283 1 824 my $class = shift;
16 283   100     744 my $mhead = shift // return undef;
17 282   50     696 my $mbody = shift // return undef;
18 282         414 my $match = 0;
19              
20             # X-MS-TNEF-Correlator: <00000000000000000000000000000000000000@example.com>
21             # X-Mailer: Internet Mail Service (5.5.1960.3)
22             # X-MS-Embedded-Report:
23 282 100 50     889 $match ||= 1 if defined $mhead->{'x-ms-embedded-report'};
24 282         425 EXCHANGE_OR_NOT: while(1) {
25             # Check the value of X-Mailer header
26 282 100       603 last if $match;
27              
28 260 100       666 if( defined $mhead->{'x-mailer'} ) {
29             # X-Mailer: Microsoft Exchange Server Internet Mail Connector Version 4.0.994.63
30             # X-Mailer: Internet Mail Service (5.5.2232.9)
31 32         166 my $tryto = ['Internet Mail Service (', 'Microsoft Exchange Server Internet Mail Connector'];
32 32   50     134 my $value = $mhead->{'x-mailer'} || '';
33 32 100 50     273 $match ||= 1 if index($value, $tryto->[0]) == 0 || index($value, $tryto->[1]) == 0;
      66        
34 32 100       134 last if $match;
35             }
36              
37 255 100       722 if( defined $mhead->{'x-mimeole'} ) {
38             # X-MimeOLE: Produced By Microsoft Exchange V6.5
39 5 50 0     18 $match ||= 1 if index($mhead->{'x-mimeole'}, 'Produced By Microsoft Exchange') == 0;
40 5 50       13 last if $match;
41             }
42              
43 255 100       409 last unless scalar @{ $mhead->{'received'} };
  255         739  
44 215         367 for my $e ( @{ $mhead->{'received'} } ) {
  215         634  
45             # Received: by ***.**.** with Internet Mail Service (5.5.2657.72)
46 497 100       1354 next unless rindex($e, ' with Internet Mail Service (') > -1;
47 5         14 $match = 1;
48 5         26 last(EXCHANGE_OR_NOT);
49             }
50 210         445 last;
51             }
52 282 100       897 return undef unless $match;
53              
54 32         94 state $indicators = __PACKAGE__->INDICATORS;
55 32         65 state $rebackbone = qr|^Content-Type:[ ]message/rfc822|m;
56 32         68 state $startingof = {
57             'message' => ['Your message'],
58             'error' => ['did not reach the following recipient(s):'],
59             };
60 32         71 state $errorcodes = {
61             'onhold' => [
62             '000B099C', # Host Unknown, Message exceeds size limit, ...
63             '000B09AA', # Unable to relay for, Message exceeds size limit,...
64             '000B09B6', # Error messages by remote MTA
65             ],
66             'userunknown' => [
67             '000C05A6', # Unknown Recipient,
68             ],
69             'systemerror' => [
70             '00010256', # Too many recipients.
71             '000D06B5', # No proxy for recipient (non-smtp mail?)
72             ],
73             'networkerror' => [
74             '00120270', # Too Many Hops
75             ],
76             'contenterror' => [
77             '00050311', # Conversion to Internet format failed
78             '000502CC', # Conversion to Internet format failed
79             ],
80             'securityerror' => [
81             '000B0981', # 502 Server does not support AUTH
82             ],
83             'filtered' => [
84             '000C0595', # Ambiguous Recipient
85             ],
86             };
87              
88 32         130 my $dscontents = [__PACKAGE__->DELIVERYSTATUS];
89 32         212 my $emailsteak = Sisimai::RFC5322->fillet($mbody, $rebackbone);
90 32         132 my $readcursor = 0; # (Integer) Points the current cursor position
91 32         86 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
92 32         54 my $statuspart = 0; # (Integer) Flag, 1 = have got delivery status part.
93 32         59 my $connvalues = 0; # (Integer) Flag, 1 if all the value of $connheader have been set
94 32         137 my $connheader = {
95             'to' => '', # The value of "To"
96             'date' => '', # The value of "Date"
97             'subject' => '', # The value of "Subject"
98             };
99 32         60 my $v = undef;
100              
101 32         306 for my $e ( split("\n", $emailsteak->[0]) ) {
102             # Read error messages and delivery status lines from the head of the email
103             # to the previous line of the beginning of the original message.
104 479 100       737 unless( $readcursor ) {
105             # Beginning of the bounce message or message/delivery-status part
106 87 100       324 $readcursor |= $indicators->{'deliverystatus'} if index($e, $startingof->{'message'}->[0]) == 0;
107 87         126 next;
108             }
109 392 50       662 next unless $readcursor & $indicators->{'deliverystatus'};
110 392 50       625 next if $statuspart;
111              
112 392 100       670 if( $connvalues == scalar(keys %$connheader) ) {
113             # did not reach the following recipient(s):
114             #
115             # kijitora@example.co.jp on Thu, 29 Apr 2007 16:51:51 -0500
116             # The recipient name is not recognized
117             # The MTS-ID of the original message is: c=jp;a= ;p=neko
118             # ;l=EXCHANGE000000000000000000
119             # MSEXCH:IMS:KIJITORA CAT:EXAMPLE:EXCHANGE 0 (000C05A6) Unknown Recipient
120             # mikeneko@example.co.jp on Thu, 29 Apr 2007 16:51:51 -0500
121             # The recipient name is not recognized
122             # The MTS-ID of the original message is: c=jp;a= ;p=neko
123             # ;l=EXCHANGE000000000000000000
124             # MSEXCH:IMS:KIJITORA CAT:EXAMPLE:EXCHANGE 0 (000C05A6) Unknown Recipient
125 264         319 $v = $dscontents->[-1];
126              
127 264 100 66     1319 if( $e =~ /\A[ \t]*([^ ]+[@][^ ]+) on[ \t]*.*\z/ ||
    100          
128             $e =~ /\A[ \t]*.+(?:SMTP|smtp)=([^ ]+[@][^ ]+) on[ \t]*.*\z/ ) {
129             # kijitora@example.co.jp on Thu, 29 Apr 2007 16:51:51 -0500
130             # kijitora@example.com on 4/29/99 9:19:59 AM
131 37 100       152 if( $v->{'recipient'} ) {
132             # There are multiple recipient addresses in the message body.
133 5         55 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
134 5         19 $v = $dscontents->[-1];
135             }
136 37         94 $v->{'recipient'} = $1;
137 37         85 $v->{'msexch'} = 0;
138 37         65 $recipients++;
139              
140             } elsif( $e =~ /\A[ \t]+(MSEXCH:.+)\z/ ) {
141             # MSEXCH:IMS:KIJITORA CAT:EXAMPLE:EXCHANGE 0 (000C05A6) Unknown Recipient
142 37         151 $v->{'diagnosis'} .= $1;
143              
144             } else {
145 190 50       332 next if $v->{'msexch'};
146 190 100       339 if( index($v->{'diagnosis'}, 'MSEXCH:') == 0 ) {
147             # Continued from MEEXCH in the previous line
148 17         73 $v->{'msexch'} = 1;
149 17         63 $v->{'diagnosis'} .= ' '.$e;
150 17         57 $statuspart = 1;
151              
152             } else {
153             # Error message in the body part
154 173         420 $v->{'alterrors'} .= ' '.$e;
155             }
156             }
157             } else {
158             # Your message
159             #
160             # To: shironeko@example.jp
161             # Subject: ...
162             # Sent: Thu, 29 Apr 2010 18:14:35 +0000
163             #
164 128 100 100     901 if( $e =~ /\A[ \t]+To:[ \t]+(.+)\z/ ) {
    100          
    100          
165             # To: shironeko@example.jp
166 32 50       125 next if $connheader->{'to'};
167 32         131 $connheader->{'to'} = $1;
168 32         67 $connvalues++;
169              
170             } elsif( $e =~ /\A[ \t]+Subject:[ \t]+(.+)\z/ ) {
171             # Subject: ...
172 32 50       140 next if length $connheader->{'subject'};
173 32         79 $connheader->{'subject'} = $1;
174 32         58 $connvalues++;
175              
176             } elsif( $e =~ m|\A[ \t]+Sent:[ \t]+([A-Z][a-z]{2},.+[-+]\d{4})\z| ||
177             $e =~ m|\A[ \t]+Sent:[ \t]+(\d+[/]\d+[/]\d+[ \t]+\d+:\d+:\d+[ \t].+)|) {
178             # Sent: Thu, 29 Apr 2010 18:14:35 +0000
179             # Sent: 4/29/99 9:19:59 AM
180 32 50       128 next if $connheader->{'date'};
181 32         85 $connheader->{'date'} = $1;
182 32         57 $connvalues++;
183             }
184             } # End of error message part
185             }
186 32 50       149 return undef unless $recipients;
187              
188 32         84 for my $e ( @$dscontents ) {
189 37         267 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'});
190 37         169 delete $e->{'msexch'};
191              
192 37 100       331 if( $e->{'diagnosis'} =~ /\AMSEXCH:.+[ \t]*[(]([0-9A-F]{8})[)][ \t]*(.*)\z/ ) {
193             # MSEXCH:IMS:KIJITORA CAT:EXAMPLE:EXCHANGE 0 (000C05A6) Unknown Recipient
194 32         73 my $capturedcode = $1;
195 32         65 my $errormessage = $2;
196              
197 32         149 for my $r ( keys %$errorcodes ) {
198             # Find captured code from the error code table
199 200 100       227 next unless grep { $capturedcode eq $_ } @{ $errorcodes->{ $r } };
  328         712  
  200         403  
200 32         149 $e->{'reason'} = $r;
201 32   50     198 $e->{'status'} = Sisimai::SMTP::Status->code($r) || '';
202 32         64 last;
203             }
204 32         118 $e->{'diagnosis'} = $errormessage;
205             }
206              
207             # Could not detect the reason from the value of "diagnosis", copy alternative error message
208 37 100       121 next if $e->{'reason'};
209 5 50       17 next unless exists $e->{'alterrors'};
210 5 50       19 next unless length $e->{'alterrors'};
211 5         18 $e->{'diagnosis'} = $e->{'alterrors'}.' '.$e->{'diagnosis'};
212 5         17 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'});
213 5         26 delete $e->{'alterrors'};
214             }
215              
216 32 100       118 unless( length $emailsteak->[1] ) {
217             # When original message does not included in the bounce message
218 12         68 $emailsteak->[1] .= sprintf("From: %s\n", $connheader->{'to'});
219 12         47 $emailsteak->[1] .= sprintf("Date: %s\n", $connheader->{'date'});
220 12         43 $emailsteak->[1] .= sprintf("Subject: %s\n", $connheader->{'subject'});
221             }
222 32         237 return { 'ds' => $dscontents, 'rfc822' => $emailsteak->[1] };
223             }
224              
225             1;
226             __END__