File Coverage

lib/Sisimai/Lhost/Facebook.pm
Criterion Covered Total %
statement 70 75 93.3
branch 30 42 71.4
condition 4 7 57.1
subroutine 6 6 100.0
pod 2 2 100.0
total 112 132 84.8


line stmt bran cond sub pod time code
1             package Sisimai::Lhost::Facebook;
2 14     14   5651 use parent 'Sisimai::Lhost';
  14         24  
  14         143  
3 14     14   755 use feature ':5.10';
  14         20  
  14         863  
4 14     14   64 use strict;
  14         27  
  14         278  
5 14     14   64 use warnings;
  14         27  
  14         11695  
6              
7 2     2 1 1144 sub description { 'Facebook: https://www.facebook.com' }
8             sub make {
9             # Detect an error from Facebook
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.0.0
15 193     193 1 632 my $class = shift;
16 193   100     505 my $mhead = shift // return undef;
17 192   50     462 my $mbody = shift // return undef;
18              
19 192 100       658 return undef unless $mhead->{'from'} eq 'Facebook ';
20 11 50       34 return undef unless $mhead->{'subject'} eq 'Sorry, your message could not be delivered';
21              
22 11         41 state $indicators = __PACKAGE__->INDICATORS;
23 11         27 state $rebackbone = qr|^Content-Disposition:[ ]inline|m;
24 11         25 state $startingof = { 'message' => ['This message was created automatically by Facebook.'] };
25 11         61 state $errorcodes = {
26             # http://postmaster.facebook.com/response_codes
27             # NOT TESTD EXCEPT RCP-P2
28             'userunknown' => [
29             'RCP-P1', # The attempted recipient address does not exist.
30             'INT-P1', # The attempted recipient address does not exist.
31             'INT-P3', # The attempted recpient group address does not exist.
32             'INT-P4', # The attempted recipient address does not exist.
33             ],
34             'filtered' => [
35             'RCP-P2', # The attempted recipient's preferences prevent messages from being delivered.
36             'RCP-P3', # The attempted recipient's privacy settings blocked the delivery.
37             ],
38             'mesgtoobig' => [
39             'MSG-P1', # The message exceeds Facebook's maximum allowed size.
40             'INT-P2', # The message exceeds Facebook's maximum allowed size.
41             ],
42             'contenterror' => [
43             'MSG-P2', # The message contains an attachment type that Facebook does not accept.
44             'MSG-P3', # The message contains multiple instances of a header field that can only be present once. Please see RFC 5322, section 3.6 for more information
45             'POL-P6', # The message contains a url that has been blocked by Facebook.
46             'POL-P7', # The message does not comply with Facebook's abuse policies and will not be accepted.
47             ],
48             'securityerror' => [
49             'POL-P1', # Your mail server's IP Address is listed on the Spamhaus PBL.
50             'POL-P2', # Facebook will no longer accept mail from your mail server's IP Address.
51             'POL-P5', # The message contains a virus.
52             'POL-P7', # The message does not comply with Facebook's Domain Authentication requirements.
53             ],
54             'notaccept' => [
55             'POL-P3', # Facebook is not accepting messages from your mail server. This will persist for 4 to 8 hours.
56             'POL-P4', # Facebook is not accepting messages from your mail server. This will persist for 24 to 48 hours.
57             'POL-T1', # Facebook is not accepting messages from your mail server, but they may be retried later. This will persist for 1 to 2 hours.
58             'POL-T2', # Facebook is not accepting messages from your mail server, but they may be retried later. This will persist for 4 to 8 hours.
59             'POL-T3', # Facebook is not accepting messages from your mail server, but they may be retried later. This will persist for 24 to 48 hours.
60             ],
61             'rejected' => [
62             'DNS-P1', # Your SMTP MAIL FROM domain does not exist.
63             'DNS-P2', # Your SMTP MAIL FROM domain does not have an MX record.
64             'DNS-T1', # Your SMTP MAIL FROM domain exists but does not currently resolve.
65             'DNS-P3', # Your mail server does not have a reverse DNS record.
66             'DNS-T2', # You mail server's reverse DNS record does not currently resolve.
67             ],
68             'systemerror' => [
69             'CON-T1', # Facebook's mail server currently has too many connections open to allow another one.
70             ],
71             'toomanyconn' => [
72             'CON-T3', # Your mail server has opened too many new connections to Facebook's mail servers in a short period of time.
73             ],
74             'suspend' => [
75             'RCP-T4', # The attempted recipient address is currently deactivated. The user may or may not reactivate it.
76             ],
77             'undefined' => [
78             'RCP-T1', # The attempted recipient address is not currently available due to an internal system issue. This is a temporary condition.
79             'MSG-T1', # The number of recipients on the message exceeds Facebook's allowed maximum.
80             'CON-T2', # Your mail server currently has too many connections open to Facebook's mail servers.
81             'CON-T4', # Your mail server has exceeded the maximum number of recipients for its current connection.
82             ],
83             };
84              
85 11         588 require Sisimai::RFC1894;
86 11         46 my $fieldtable = Sisimai::RFC1894->FIELDTABLE;
87 11         24 my $permessage = {}; # (Hash) Store values of each Per-Message field
88              
89 11         43 my $dscontents = [__PACKAGE__->DELIVERYSTATUS];
90 11         68 my $emailsteak = Sisimai::RFC5322->fillet($mbody, $rebackbone);
91 11         30 my $readcursor = 0; # (Integer) Points the current cursor position
92 11         18 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
93 11         17 my $fbresponse = ''; # (String) Response code from Facebook
94 11         17 my $v = undef;
95 11         19 my $p = '';
96              
97 11         73 for my $e ( split("\n", $emailsteak->[0]) ) {
98             # Read error messages and delivery status lines from the head of the email
99             # to the previous line of the beginning of the original message.
100 192 100       231 unless( $readcursor ) {
101             # Beginning of the bounce message or message/delivery-status part
102 22 100       89 $readcursor |= $indicators->{'deliverystatus'} if index($e, $startingof->{'message'}->[0]) == 0;
103 22         34 next;
104             }
105 170 50       255 next unless $readcursor & $indicators->{'deliverystatus'};
106 170 100       201 next unless length $e;
107              
108 126 100       191 if( my $f = Sisimai::RFC1894->match($e) ) {
109             # $e matched with any field defined in RFC3464
110 77 50       134 next unless my $o = Sisimai::RFC1894->field($e);
111 77         97 $v = $dscontents->[-1];
112              
113 77 100       140 if( $o->[-1] eq 'addr' ) {
    100          
114             # Final-Recipient: rfc822; kijitora@example.jp
115             # X-Actual-Recipient: rfc822; kijitora@example.co.jp
116 11 50       34 if( $o->[0] eq 'final-recipient' ) {
117             # Final-Recipient: rfc822; kijitora@example.jp
118 11 50       42 if( $v->{'recipient'} ) {
119             # There are multiple recipient addresses in the message body.
120 0         0 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
121 0         0 $v = $dscontents->[-1];
122             }
123 11         21 $v->{'recipient'} = $o->[2];
124 11         22 $recipients++;
125              
126             } else {
127             # X-Actual-Recipient: rfc822; kijitora@example.co.jp
128 0         0 $v->{'alias'} = $o->[2];
129             }
130             } elsif( $o->[-1] eq 'code' ) {
131             # Diagnostic-Code: SMTP; 550 5.1.1 ... User Unknown
132 11         31 $v->{'spec'} = $o->[1];
133 11         26 $v->{'diagnosis'} = $o->[2];
134              
135             } else {
136             # Other DSN fields defined in RFC3464
137 55 50       99 next unless exists $fieldtable->{ $o->[0] };
138 55         113 $v->{ $fieldtable->{ $o->[0] } } = $o->[2];
139              
140 55 100       101 next unless $f == 1;
141 22         57 $permessage->{ $fieldtable->{ $o->[0] } } = $o->[2];
142             }
143             } else {
144             # Continued line of the value of Diagnostic-Code field
145 49 100       92 next unless index($p, 'Diagnostic-Code:') == 0;
146 5 50       29 next unless $e =~ /\A[ \t]+(.+)\z/;
147 5         18 $v->{'diagnosis'} .= ' '.$1;
148             }
149             } continue {
150             # Save the current line for the next loop
151 192         231 $p = $e;
152             }
153 11 50       41 return undef unless $recipients;
154              
155 11         28 for my $e ( @$dscontents ) {
156 11   33     52 $e->{'lhost'} ||= $permessage->{'lhost'};
157 11         79 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'});
158              
159 11 50       70 if( $e->{'diagnosis'} =~ /\b([A-Z]{3})[-]([A-Z])(\d)\b/ ) {
160             # Diagnostic-Code: smtp; 550 5.1.1 RCP-P2
161 11         77 $fbresponse = sprintf("%s-%s%d", $1, $2, $3);
162             }
163              
164 11         52 SESSION: for my $r ( keys %$errorcodes ) {
165             # Verify each regular expression of session errors
166 75         69 PATTERN: for my $rr ( @{ $errorcodes->{ $r } } ) {
  75         116  
167             # Check each regular expression
168 223 100       311 next(PATTERN) unless $fbresponse eq $rr;
169 11         23 $e->{'reason'} = $r;
170 11         19 last(SESSION);
171             }
172             }
173 11 50       35 next if $e->{'reason'};
174              
175             # http://postmaster.facebook.com/response_codes
176             # Facebook System Resource Issues
177             # These codes indicate a temporary issue internal to Facebook's
178             # system. Administrators observing these issues are not required to
179             # take any action to correct them.
180             #
181             # * INT-Tx
182             #
183             # https://groups.google.com/forum/#!topic/cdmix/eXfi4ddgYLQ
184             # This block has not been tested because we have no email sample
185             # including "INT-T?" error code.
186 0 0       0 next unless $fbresponse =~ /\AINT-T\d+\z/;
187 0         0 $e->{'reason'} = 'systemerror';
188             }
189 11         78 return { 'ds' => $dscontents, 'rfc822' => $emailsteak->[1] };
190             }
191              
192             1;
193             __END__