1package Sisimai::Lhost::Facebook; 2use parent 'Sisimai::Lhost'; 3use feature ':5.10'; 4use strict; 5use warnings; 6 7sub description { 'Facebook: https://www.facebook.com' } 8sub 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 my $class = shift; 16 my $mhead = shift // return undef; 17 my $mbody = shift // return undef; 18 19 return undef unless $mhead->{'from'} eq 'Facebook <mailer-daemon@mx.facebook.com>'; 20 return undef unless $mhead->{'subject'} eq 'Sorry, your message could not be delivered'; 21 22 state $indicators = __PACKAGE__->INDICATORS; 23 state $rebackbone = qr|^Content-Disposition:[ ]inline|m; 24 state $startingof = { 'message' => ['This message was created automatically by Facebook.'] }; 25 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 require Sisimai::RFC1894; 86 my $fieldtable = Sisimai::RFC1894->FIELDTABLE; 87 my $permessage = {}; # (Hash) Store values of each Per-Message field 88 89 my $dscontents = [__PACKAGE__->DELIVERYSTATUS]; 90 my $emailsteak = Sisimai::RFC5322->fillet($mbody, $rebackbone); 91 my $readcursor = 0; # (Integer) Points the current cursor position 92 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header 93 my $fbresponse = ''; # (String) Response code from Facebook 94 my $v = undef; 95 my $p = ''; 96 97 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 unless( $readcursor ) { 101 # Beginning of the bounce message or message/delivery-status part 102 $readcursor |= $indicators->{'deliverystatus'} if index($e, $startingof->{'message'}->[0]) == 0; 103 next; 104 } 105 next unless $readcursor & $indicators->{'deliverystatus'}; 106 next unless length $e; 107 108 if( my $f = Sisimai::RFC1894->match($e) ) { 109 # $e matched with any field defined in RFC3464 110 next unless my $o = Sisimai::RFC1894->field($e); 111 $v = $dscontents->[-1]; 112 113 if( $o->[-1] eq 'addr' ) { 114 # Final-Recipient: rfc822; kijitora@example.jp 115 # X-Actual-Recipient: rfc822; kijitora@example.co.jp 116 if( $o->[0] eq 'final-recipient' ) { 117 # Final-Recipient: rfc822; kijitora@example.jp 118 if( $v->{'recipient'} ) { 119 # There are multiple recipient addresses in the message body. 120 push @$dscontents, __PACKAGE__->DELIVERYSTATUS; 121 $v = $dscontents->[-1]; 122 } 123 $v->{'recipient'} = $o->[2]; 124 $recipients++; 125 126 } else { 127 # X-Actual-Recipient: rfc822; kijitora@example.co.jp 128 $v->{'alias'} = $o->[2]; 129 } 130 } elsif( $o->[-1] eq 'code' ) { 131 # Diagnostic-Code: SMTP; 550 5.1.1 <userunknown@example.jp>... User Unknown 132 $v->{'spec'} = $o->[1]; 133 $v->{'diagnosis'} = $o->[2]; 134 135 } else { 136 # Other DSN fields defined in RFC3464 137 next unless exists $fieldtable->{ $o->[0] }; 138 $v->{ $fieldtable->{ $o->[0] } } = $o->[2]; 139 140 next unless $f == 1; 141 $permessage->{ $fieldtable->{ $o->[0] } } = $o->[2]; 142 } 143 } else { 144 # Continued line of the value of Diagnostic-Code field 145 next unless index($p, 'Diagnostic-Code:') == 0; 146 next unless $e =~ /\A[ \t]+(.+)\z/; 147 $v->{'diagnosis'} .= ' '.$1; 148 } 149 } continue { 150 # Save the current line for the next loop 151 $p = $e; 152 } 153 return undef unless $recipients; 154 155 for my $e ( @$dscontents ) { 156 $e->{'lhost'} ||= $permessage->{'lhost'}; 157 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'}); 158 159 if( $e->{'diagnosis'} =~ /\b([A-Z]{3})[-]([A-Z])(\d)\b/ ) { 160 # Diagnostic-Code: smtp; 550 5.1.1 RCP-P2 161 $fbresponse = sprintf("%s-%s%d", $1, $2, $3); 162 } 163 164 SESSION: for my $r ( keys %$errorcodes ) { 165 # Verify each regular expression of session errors 166 PATTERN: for my $rr ( @{ $errorcodes->{ $r } } ) { 167 # Check each regular expression 168 next(PATTERN) unless $fbresponse eq $rr; 169 $e->{'reason'} = $r; 170 last(SESSION); 171 } 172 } 173 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 next unless $fbresponse =~ /\AINT-T\d+\z/; 187 $e->{'reason'} = 'systemerror'; 188 } 189 return { 'ds' => $dscontents, 'rfc822' => $emailsteak->[1] }; 190} 191 1921; 193__END__ 194 195=encoding utf-8 196 197=head1 NAME 198 199Sisimai::Lhost::Facebook - bounce mail parser class for C<Facebook>. 200 201=head1 SYNOPSIS 202 203 use Sisimai::Lhost::Facebook; 204 205=head1 DESCRIPTION 206 207Sisimai::Lhost::Facebook parses a bounce email which created by C<Facebook>. 208Methods in the module are called from only Sisimai::Message. 209 210=head1 CLASS METHODS 211 212=head2 C<B<description()>> 213 214C<description()> returns description string of this module. 215 216 print Sisimai::Lhost::Facebook->description; 217 218=head2 C<B<make(I<header data>, I<reference to body string>)>> 219 220C<make()> method parses a bounced email and return results as a array reference. 221See Sisimai::Message for more details. 222 223=head1 AUTHOR 224 225azumakuniyuki 226 227=head1 COPYRIGHT 228 229Copyright (C) 2014-2020 azumakuniyuki, All rights reserved. 230 231=head1 LICENSE 232 233This software is distributed under The BSD 2-Clause License. 234 235=cut 236