File Coverage

lib/Sisimai/Lhost/MessageLabs.pm
Criterion Covered Total %
statement 65 69 94.2
branch 29 38 76.3
condition 5 12 41.6
subroutine 6 6 100.0
pod 2 2 100.0
total 107 127 84.2


line stmt bran cond sub pod time code
1             package Sisimai::Lhost::MessageLabs;
2 16     16   5788 use parent 'Sisimai::Lhost';
  16         70  
  16         105  
3 16     16   893 use feature ':5.10';
  16         28  
  16         962  
4 16     16   79 use strict;
  16         25  
  16         257  
5 16     16   58 use warnings;
  16         22  
  16         12257  
6              
7 2     2 1 1188 sub description { 'Symantec.cloud http://www.messagelabs.com' }
8             sub make {
9             # Detect an error from MessageLabs.com
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.10
15 219     219 1 591 my $class = shift;
16 219   100     551 my $mhead = shift // return undef;
17 218   50     498 my $mbody = shift // return undef;
18              
19             # X-Msg-Ref: server-11.tower-143.messagelabs.com!1419367175!36473369!1
20             # X-Originating-IP: [10.245.230.38]
21             # X-StarScan-Received:
22             # X-StarScan-Version: 6.12.5; banners=-,-,-
23             # X-VirusChecked: Checked
24 218 100       705 return undef unless defined $mhead->{'x-msg-ref'};
25 16 50       56 return undef unless rindex($mhead->{'from'}, 'MAILER-DAEMON@messagelabs.com') > -1;
26 16 50       55 return undef unless index($mhead->{'subject'}, 'Mail Delivery Failure') == 0;
27              
28 16         49 state $indicators = __PACKAGE__->INDICATORS;
29 16         32 state $rebackbone = qr|^Content-Type:[ ]text/rfc822-headers|m;
30 16         28 state $startingof = { 'message' => ['Content-Type: message/delivery-status'] };
31 16         39 state $refailures = {
32             'userunknown' => qr/(?:542 .+ Rejected|No such user)/,
33             'securityerror' => qr/Please turn on SMTP Authentication in your mail client/,
34             };
35              
36 16         466 require Sisimai::RFC1894;
37 16         85 my $fieldtable = Sisimai::RFC1894->FIELDTABLE;
38 16         29 my $permessage = {}; # (Hash) Store values of each Per-Message field
39              
40 16         70 my $dscontents = [__PACKAGE__->DELIVERYSTATUS];
41 16         88 my $emailsteak = Sisimai::RFC5322->fillet($mbody, $rebackbone);
42 16         29 my $readcursor = 0; # (Integer) Points the current cursor position
43 16         28 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
44 16         22 my $v = undef;
45 16         31 my $p = '';
46              
47 16         157 for my $e ( split("\n", $emailsteak->[0]) ) {
48             # Read error messages and delivery status lines from the head of the email
49             # to the previous line of the beginning of the original message.
50 461 100       528 unless( $readcursor ) {
51             # Beginning of the bounce message or message/delivery-status part
52 316 100       518 $readcursor |= $indicators->{'deliverystatus'} if index($e, $startingof->{'message'}->[0]) == 0;
53 316         267 next;
54             }
55 145 50       209 next unless $readcursor & $indicators->{'deliverystatus'};
56 145 100       193 next unless length $e;
57              
58 129 100       229 if( my $f = Sisimai::RFC1894->match($e) ) {
59             # $e matched with any field defined in RFC3464
60 117 50       204 next unless my $o = Sisimai::RFC1894->field($e);
61 117         143 $v = $dscontents->[-1];
62              
63 117 100       225 if( $o->[-1] eq 'addr' ) {
    100          
64             # Final-Recipient: rfc822; kijitora@example.jp
65             # X-Actual-Recipient: rfc822; kijitora@example.co.jp
66 16 50       68 if( $o->[0] eq 'final-recipient' ) {
67             # Final-Recipient: rfc822; kijitora@example.jp
68 16 50       47 if( $v->{'recipient'} ) {
69             # There are multiple recipient addresses in the message body.
70 0         0 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
71 0         0 $v = $dscontents->[-1];
72             }
73 16         38 $v->{'recipient'} = $o->[2];
74 16         32 $recipients++;
75              
76             } else {
77             # X-Actual-Recipient: rfc822; kijitora@example.co.jp
78 0         0 $v->{'alias'} = $o->[2];
79             }
80             } elsif( $o->[-1] eq 'code' ) {
81             # Diagnostic-Code: SMTP; 550 5.1.1 ... User Unknown
82 16         46 $v->{'spec'} = $o->[1];
83 16         42 $v->{'diagnosis'} = $o->[2];
84              
85             } else {
86             # Other DSN fields defined in RFC3464
87 85 50       143 next unless exists $fieldtable->{ $o->[0] };
88 85         150 $v->{ $fieldtable->{ $o->[0] } } = $o->[2];
89              
90 85 100       153 next unless $f == 1;
91 32         81 $permessage->{ $fieldtable->{ $o->[0] } } = $o->[2];
92             }
93             } else {
94             # Continued line of the value of Diagnostic-Code field
95 12 100       58 next unless index($p, 'Diagnostic-Code:') == 0;
96 6 50       26 next unless $e =~ /\A[ \t]+(.+)\z/;
97 0         0 $v->{'diagnosis'} .= ' '.$1;
98             } # End of message/delivery-status
99             } continue {
100             # Save the current line for the next loop
101 461         523 $p = $e;
102             }
103 16 50       72 return undef unless $recipients;
104              
105 16         36 for my $e ( @$dscontents ) {
106             # Set default values if each value is empty.
107 16   33     78 $e->{'lhost'} ||= $permessage->{'rhost'};
108 16   0     90 $e->{ $_ } ||= $permessage->{ $_ } || '' for keys %$permessage;
      33        
109 16         144 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'});
110              
111 16         72 SESSION: for my $r ( keys %$refailures ) {
112             # Verify each regular expression of session errors
113 22 100       164 next unless $e->{'diagnosis'} =~ $refailures->{ $r };
114 16         33 $e->{'reason'} = $r;
115 16         31 last;
116             }
117             }
118 16         107 return { 'ds' => $dscontents, 'rfc822' => $emailsteak->[1] };
119             }
120              
121             1;
122             __END__