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   5998 use parent 'Sisimai::Lhost';
  23         56  
  23         131  
3 23     23   1477 use feature ':5.10';
  23         45  
  23         1572  
4 23     23   136 use strict;
  23         50  
  23         436  
5 23     23   121 use warnings;
  23         41  
  23         32777  
6              
7 2     2 1 1167 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 873 my $class = shift;
16 283   100     998 my $mhead = shift // return undef;
17 282   50     723 my $mbody = shift // return undef;
18 282         458 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     944 $match ||= 1 if defined $mhead->{'x-ms-embedded-report'};
24 282         552 EXCHANGE_OR_NOT: while(1) {
25             # Check the value of X-Mailer header
26 282 100       730 last if $match;
27              
28 260 100       775 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         89 my $tryto = ['Internet Mail Service (', 'Microsoft Exchange Server Internet Mail Connector'];
32 32   50     118 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       103 last if $match;
35             }
36              
37 255 100       782 if( defined $mhead->{'x-mimeole'} ) {
38             # X-MimeOLE: Produced By Microsoft Exchange V6.5
39 5 50 0     22 $match ||= 1 if index($mhead->{'x-mimeole'}, 'Produced By Microsoft Exchange') == 0;
40 5 50       13 last if $match;
41             }
42              
43 255 100       431 last unless scalar @{ $mhead->{'received'} };
  255         946  
44 215         353 for my $e ( @{ $mhead->{'received'} } ) {
  215         623  
45             # Received: by ***.**.** with Internet Mail Service (5.5.2657.72)
46 497 100       1367 next unless rindex($e, ' with Internet Mail Service (') > -1;
47 5         11 $match = 1;
48 5         14 last(EXCHANGE_OR_NOT);
49             }
50 210         363 last;
51             }
52 282 100       949 return undef unless $match;
53              
54 32         92 state $indicators = __PACKAGE__->INDICATORS;
55 32         86 state $rebackbone = qr|^Content-Type:[ ]message/rfc822|m;
56 32         63 state $startingof = {
57             'message' => ['Your message'],
58             'error' => ['did not reach the following recipient(s):'],
59             };
60 32         96 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         137 my $dscontents = [__PACKAGE__->DELIVERYSTATUS];
89 32         204 my $emailsteak = Sisimai::RFC5322->fillet($mbody, $rebackbone);
90 32         61 my $readcursor = 0; # (Integer) Points the current cursor position
91 32         64 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
92 32         58 my $statuspart = 0; # (Integer) Flag, 1 = have got delivery status part.
93 32         49 my $connvalues = 0; # (Integer) Flag, 1 if all the value of $connheader have been set
94 32         145 my $connheader = {
95             'to' => '', # The value of "To"
96             'date' => '', # The value of "Date"
97             'subject' => '', # The value of "Subject"
98             };
99 32         70 my $v = undef;
100              
101 32         256 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       736 unless( $readcursor ) {
105             # Beginning of the bounce message or message/delivery-status part
106 87 100       275 $readcursor |= $indicators->{'deliverystatus'} if index($e, $startingof->{'message'}->[0]) == 0;
107 87         136 next;
108             }
109 392 50       633 next unless $readcursor & $indicators->{'deliverystatus'};
110 392 50       547 next if $statuspart;
111              
112 392 100       761 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         362 $v = $dscontents->[-1];
126              
127 264 100 66     1092 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       103 if( $v->{'recipient'} ) {
132             # There are multiple recipient addresses in the message body.
133 5         24 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
134 5         16 $v = $dscontents->[-1];
135             }
136 37         97 $v->{'recipient'} = $1;
137 37         101 $v->{'msexch'} = 0;
138 37         64 $recipients++;
139              
140             } elsif( $e =~ /\A[ \t]+(MSEXCH:.+)\z/ ) {
141             # MSEXCH:IMS:KIJITORA CAT:EXAMPLE:EXCHANGE 0 (000C05A6) Unknown Recipient
142 37         143 $v->{'diagnosis'} .= $1;
143              
144             } else {
145 190 50       373 next if $v->{'msexch'};
146 190 100       384 if( index($v->{'diagnosis'}, 'MSEXCH:') == 0 ) {
147             # Continued from MEEXCH in the previous line
148 17         46 $v->{'msexch'} = 1;
149 17         48 $v->{'diagnosis'} .= ' '.$e;
150 17         49 $statuspart = 1;
151              
152             } else {
153             # Error message in the body part
154 173         415 $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     819 if( $e =~ /\A[ \t]+To:[ \t]+(.+)\z/ ) {
    100          
    100          
165             # To: shironeko@example.jp
166 32 50       98 next if $connheader->{'to'};
167 32         118 $connheader->{'to'} = $1;
168 32         58 $connvalues++;
169              
170             } elsif( $e =~ /\A[ \t]+Subject:[ \t]+(.+)\z/ ) {
171             # Subject: ...
172 32 50       107 next if length $connheader->{'subject'};
173 32         82 $connheader->{'subject'} = $1;
174 32         62 $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       122 next if $connheader->{'date'};
181 32         82 $connheader->{'date'} = $1;
182 32         54 $connvalues++;
183             }
184             } # End of error message part
185             }
186 32 50       137 return undef unless $recipients;
187              
188 32         95 for my $e ( @$dscontents ) {
189 37         259 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'});
190 37         110 delete $e->{'msexch'};
191              
192 37 100       292 if( $e->{'diagnosis'} =~ /\AMSEXCH:.+[ \t]*[(]([0-9A-F]{8})[)][ \t]*(.*)\z/ ) {
193             # MSEXCH:IMS:KIJITORA CAT:EXAMPLE:EXCHANGE 0 (000C05A6) Unknown Recipient
194 32         86 my $capturedcode = $1;
195 32         71 my $errormessage = $2;
196              
197 32         142 for my $r ( keys %$errorcodes ) {
198             # Find captured code from the error code table
199 162 100       185 next unless grep { $capturedcode eq $_ } @{ $errorcodes->{ $r } };
  260         557  
  162         271  
200 32         78 $e->{'reason'} = $r;
201 32   50     173 $e->{'status'} = Sisimai::SMTP::Status->code($r) || '';
202 32         58 last;
203             }
204 32         79 $e->{'diagnosis'} = $errormessage;
205             }
206              
207             # Could not detect the reason from the value of "diagnosis", copy alternative error message
208 37 100       123 next if $e->{'reason'};
209 5 50       19 next unless exists $e->{'alterrors'};
210 5 50       20 next unless length $e->{'alterrors'};
211 5         19 $e->{'diagnosis'} = $e->{'alterrors'}.' '.$e->{'diagnosis'};
212 5         20 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'});
213 5         15 delete $e->{'alterrors'};
214             }
215              
216 32 100       100 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         45 $emailsteak->[1] .= sprintf("Date: %s\n", $connheader->{'date'});
220 12         43 $emailsteak->[1] .= sprintf("Subject: %s\n", $connheader->{'subject'});
221             }
222 32         215 return { 'ds' => $dscontents, 'rfc822' => $emailsteak->[1] };
223             }
224              
225             1;
226             __END__