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   6123 use parent 'Sisimai::Lhost';
  22         44  
  22         122  
3 22     22   1371 use feature ':5.10';
  22         37  
  22         1680  
4 22     22   135 use strict;
  22         148  
  22         491  
5 22     22   101 use warnings;
  22         42  
  22         19177  
6              
7 2     2 1 1159 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 1434 my $class = shift;
16 500   100     1358 my $mhead = shift // return undef;
17 499   50     1136 my $mbody = shift // return undef;
18              
19             # X-NAI-Header: Modified by McAfee Email and Web Security Virtual Appliance
20 499 100       1595 return undef unless defined $mhead->{'x-nai-header'};
21 26 50       108 return undef unless index($mhead->{'x-nai-header'}, 'Modified by McAfee') > -1;
22 26 50       86 return undef unless $mhead->{'subject'} eq 'Delivery Status';
23              
24 26         81 state $indicators = __PACKAGE__->INDICATORS;
25 26         54 state $rebackbone = qr|^Content-Type:[ ]message/rfc822|m;
26 26         48 state $startingof = { 'message' => ['--- The following addresses had delivery problems ---'] };
27 26         48 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         447 require Sisimai::RFC1894;
38 26         175 my $fieldtable = Sisimai::RFC1894->FIELDTABLE;
39 26         120 my $dscontents = [__PACKAGE__->DELIVERYSTATUS];
40 26         139 my $emailsteak = Sisimai::RFC5322->fillet($mbody, $rebackbone);
41 26         60 my $readcursor = 0; # (Integer) Points the current cursor position
42 26         69 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
43 26         46 my $diagnostic = ''; # (String) Alternative diagnostic message
44 26         44 my $v = undef;
45 26         49 my $p = '';
46              
47 26         259 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       676 unless( $readcursor ) {
51             # Beginning of the bounce message or message/delivery-status part
52 114 100       329 $readcursor |= $indicators->{'deliverystatus'} if index($e, $startingof->{'message'}->[0]) > -1;
53 114         145 next;
54             }
55 348 50       571 next unless $readcursor & $indicators->{'deliverystatus'};
56 348 100       518 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         288 $v = $dscontents->[-1];
68              
69 218 100       670 if( $e =~ /\A[<]([^ ]+[@][^ ]+)[>][ \t]+[(](.+)[)]\z/ ) {
    100          
70             # (Unknown user kijitora@example.co.jp)
71 26 50       83 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         64 $diagnostic = $2;
78 26         44 $recipients++;
79              
80             } elsif( my $f = Sisimai::RFC1894->match($e) ) {
81             # $e matched with any field defined in RFC3464
82 104         197 my $o = Sisimai::RFC1894->field($e);
83 104 100       255 unless( $o ) {
84             # Fallback code for empty value or invalid formatted value
85             # - Original-Recipient:
86 52 100       306 $v->{'alias'} = Sisimai::Address->s3s4($1) if $e =~ /\AOriginal-Recipient:[ ]*([^ ]+)\z/;
87 52         85 next;
88             }
89 52 50       171 next unless exists $fieldtable->{ $o->[0] };
90 52         195 $v->{ $fieldtable->{ $o->[0] } } = $o->[2];
91              
92             } else {
93             # Continued line of the value of Diagnostic-Code field
94 88 50       222 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         655 $p = $e;
101             }
102 26 50       106 return undef unless $recipients;
103              
104 26         82 for my $e ( @$dscontents ) {
105 26   33     218 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'} || $diagnostic);
106              
107 26         116 SESSION: for my $r ( keys %$refailures ) {
108             # Verify each regular expression of session errors
109 26 100       310 next unless $e->{'diagnosis'} =~ $refailures->{ $r };
110 16         53 $e->{'reason'} = $r;
111 16         50 last;
112             }
113             }
114 26         188 return { 'ds' => $dscontents, 'rfc822' => $emailsteak->[1] };
115             }
116              
117             1;
118             __END__