File Coverage

lib/Sisimai/Lhost/McAfee.pm
Criterion Covered Total %
statement 60 64 93.7
branch 25 34 73.5
condition 4 7 57.1
subroutine 6 6 100.0
pod 2 2 100.0
total 97 113 85.8


line stmt bran cond sub pod time code
1             package Sisimai::Lhost::McAfee;
2 22     22   6077 use parent 'Sisimai::Lhost';
  22         52  
  22         131  
3 22     22   1377 use feature ':5.10';
  22         48  
  22         1519  
4 22     22   124 use strict;
  22         42  
  22         474  
5 22     22   94 use warnings;
  22         57  
  22         20183  
6              
7 2     2 1 1273 sub description { 'McAfee Email Appliance' }
8             sub make {
9             # Detect an error from McAfee
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 500     500 1 1244 my $class = shift;
16 500   100     1105 my $mhead = shift // return undef;
17 499   50     950 my $mbody = shift // return undef;
18              
19             # X-NAI-Header: Modified by McAfee Email and Web Security Virtual Appliance
20 499 100       1513 return undef unless defined $mhead->{'x-nai-header'};
21 26 50       193 return undef unless index($mhead->{'x-nai-header'}, 'Modified by McAfee') > -1;
22 26 50       104 return undef unless $mhead->{'subject'} eq 'Delivery Status';
23              
24 26         72 state $indicators = __PACKAGE__->INDICATORS;
25 26         52 state $rebackbone = qr|^Content-Type:[ ]message/rfc822|m;
26 26         46 state $startingof = { 'message' => ['--- The following addresses had delivery problems ---'] };
27 26         45 state $refailures = {
28             'userunknown' => qr{(?:
29             [ ]User[ ][(].+[@].+[)][ ]unknown[.][ ]
30             |550[ ]Unknown[ ]user[ ][^ ]+[@][^ ]+
31             |550[ ][<].+?[@].+?[>][.]+[ ]User[ ]not[ ]exist
32             |No[ ]such[ ]user
33             )
34             }x,
35             };
36              
37 26         484 require Sisimai::RFC1894;
38 26         141 my $fieldtable = Sisimai::RFC1894->FIELDTABLE;
39 26         148 my $dscontents = [__PACKAGE__->DELIVERYSTATUS];
40 26         204 my $emailsteak = Sisimai::RFC5322->fillet($mbody, $rebackbone);
41 26         130 my $readcursor = 0; # (Integer) Points the current cursor position
42 26         55 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
43 26         50 my $diagnostic = ''; # (String) Alternative diagnostic message
44 26         41 my $v = undef;
45 26         47 my $p = '';
46              
47 26         241 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 462 100       701 unless( $readcursor ) {
51             # Beginning of the bounce message or message/delivery-status part
52 114 100       357 $readcursor |= $indicators->{'deliverystatus'} if index($e, $startingof->{'message'}->[0]) > -1;
53 114         130 next;
54             }
55 348 50       633 next unless $readcursor & $indicators->{'deliverystatus'};
56 348 100       538 next unless length $e;
57              
58             # Content-Type: text/plain; name="deliveryproblems.txt"
59             #
60             # --- The following addresses had delivery problems ---
61             #
62             # (User unknown user@example.com)
63             #
64             # --------------Boundary-00=_00000000000000000000
65             # Content-Type: message/delivery-status; name="deliverystatus.txt"
66             #
67 218         238 $v = $dscontents->[-1];
68              
69 218 100       776 if( $e =~ /\A[<]([^ ]+[@][^ ]+)[>][ \t]+[(](.+)[)]\z/ ) {
    100          
70             # (Unknown user kijitora@example.co.jp)
71 26 50       84 if( $v->{'recipient'} ) {
72             # There are multiple recipient addresses in the message body.
73 0         0 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
74 0         0 $v = $dscontents->[-1];
75             }
76 26         92 $v->{'recipient'} = $1;
77 26         55 $diagnostic = $2;
78 26         48 $recipients++;
79              
80             } elsif( my $f = Sisimai::RFC1894->match($e) ) {
81             # $e matched with any field defined in RFC3464
82 104         275 my $o = Sisimai::RFC1894->field($e);
83 104 100       342 unless( $o ) {
84             # Fallback code for empty value or invalid formatted value
85             # - Original-Recipient:
86 52 100       447 $v->{'alias'} = Sisimai::Address->s3s4($1) if $e =~ /\AOriginal-Recipient:[ ]*([^ ]+)\z/;
87 52         113 next;
88             }
89 52 50       169 next unless exists $fieldtable->{ $o->[0] };
90 52         153 $v->{ $fieldtable->{ $o->[0] } } = $o->[2];
91              
92             } else {
93             # Continued line of the value of Diagnostic-Code field
94 88 50       286 next unless index($p, 'Diagnostic-Code:') == 0;
95 0 0       0 next unless $e =~ /\A[ \t]+(.+)\z/;
96 0         0 $v->{'diagnosis'} .= ' '.$1;
97             } # End of error message part
98             } continue {
99             # Save the current line for the next loop
100 462         691 $p = $e;
101             }
102 26 50       151 return undef unless $recipients;
103              
104 26         85 for my $e ( @$dscontents ) {
105 26   33     206 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'} || $diagnostic);
106              
107 26         205 SESSION: for my $r ( keys %$refailures ) {
108             # Verify each regular expression of session errors
109 26 100       342 next unless $e->{'diagnosis'} =~ $refailures->{ $r };
110 16         41 $e->{'reason'} = $r;
111 16         42 last;
112             }
113             }
114 26         189 return { 'ds' => $dscontents, 'rfc822' => $emailsteak->[1] };
115             }
116              
117             1;
118             __END__