1package Sisimai::Lhost::MailRu; 2use parent 'Sisimai::Lhost'; 3use feature ':5.10'; 4use strict; 5use warnings; 6 7# Based on Sisimai::Lhost::Exim 8sub description { '@mail.ru: https://mail.ru' } 9sub make { 10 # Detect an error from @mail.ru 11 # @param [Hash] mhead Message headers of a bounce email 12 # @param [String] mbody Message body of a bounce email 13 # @return [Hash] Bounce data list and message/rfc822 part 14 # @return [Undef] failed to parse or the arguments are missing 15 # @since v4.1.4 16 my $class = shift; 17 my $mhead = shift // return undef; 18 my $mbody = shift // return undef; 19 20 # Message-Id: <E1P1YNN-0003AD-Ga@*.mail.ru> 21 return undef unless lc($mhead->{'from'}) =~ /[<]?mailer-daemon[@].*mail[.]ru[>]?/; 22 return undef unless $mhead->{'message-id'} =~ /[.](?:mail[.]ru|smailru[.]net)[>]\z/; 23 return undef unless $mhead->{'subject'} =~ qr{(?: 24 Mail[ ]delivery[ ]failed(:[ ]returning[ ]message[ ]to[ ]sender)? 25 |Warning:[ ]message[ ].+[ ]delayed[ ]+ 26 |Delivery[ ]Status[ ]Notification 27 |Mail[ ]failure 28 |Message[ ]frozen 29 |error[(]s[)][ ]in[ ]forwarding[ ]or[ ]filtering 30 ) 31 }x; 32 33 state $indicators = __PACKAGE__->INDICATORS; 34 state $rebackbone = qr|^------ This is a copy of the message, including all the headers[.] ------|m; 35 state $startingof = { 'message' => ['This message was created automatically by mail delivery software.'] }; 36 state $recommands = [ 37 qr/SMTP error from remote (?:mail server|mailer) after ([A-Za-z]{4})/, 38 qr/SMTP error from remote (?:mail server|mailer) after end of ([A-Za-z]{4})/, 39 ]; 40 state $messagesof = { 41 'expired' => [ 42 'retry timeout exceeded', 43 'No action is required on your part', 44 ], 45 'userunknown' => ['user not found'], 46 'hostunknown' => [ 47 'all host address lookups failed permanently', 48 'all relevant MX records point to non-existent hosts', 49 'Unrouteable address', 50 ], 51 'mailboxfull' => [ 52 'mailbox is full', 53 'error: quota exceed', 54 ], 55 'notaccept' => [ 56 'an MX or SRV record indicated no SMTP service', 57 'no host found for existing SMTP connection', 58 ], 59 'systemerror' => [ 60 'delivery to file forbidden', 61 'delivery to pipe forbidden', 62 'local delivery failed', 63 ], 64 'contenterror'=> ['Too many "Received" headers '], 65 }; 66 67 my $dscontents = [__PACKAGE__->DELIVERYSTATUS]; 68 my $emailsteak = Sisimai::RFC5322->fillet($mbody, $rebackbone); 69 my $readcursor = 0; # (Integer) Points the current cursor position 70 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header 71 my $localhost0 = ''; # (String) Local MTA 72 my $v = undef; 73 74 for my $e ( split("\n", $emailsteak->[0]) ) { 75 # Read error messages and delivery status lines from the head of the email 76 # to the previous line of the beginning of the original message. 77 unless( $readcursor ) { 78 # Beginning of the bounce message or message/delivery-status part 79 $readcursor |= $indicators->{'deliverystatus'} if index($e, $startingof->{'message'}->[0]) == 0; 80 } 81 next unless $readcursor & $indicators->{'deliverystatus'}; 82 next unless length $e; 83 84 # Это письмо создано автоматически 85 # сервером Mail.Ru, # отвечать на него не 86 # нужно. 87 # 88 # К сожалению, Ваше письмо не может 89 # быть# доставлено одному или нескольким 90 # получателям: 91 # 92 # ********************** 93 # 94 # This message was created automatically by mail delivery software. 95 # 96 # A message that you sent could not be delivered to one or more of its 97 # recipients. This is a permanent error. The following address(es) failed: 98 # 99 # kijitora@example.jp 100 # SMTP error from remote mail server after RCPT TO:<kijitora@example.jp>: 101 # host neko.example.jp [192.0.2.222]: 550 5.1.1 <kijitora@example.jp>... User Unknown 102 $v = $dscontents->[-1]; 103 104 if( $e =~ /\A[ \t]+([^ \t]+[@][^ \t]+[.][a-zA-Z]+)\z/ ) { 105 # kijitora@example.jp 106 if( $v->{'recipient'} ) { 107 # There are multiple recipient addresses in the message body. 108 push @$dscontents, __PACKAGE__->DELIVERYSTATUS; 109 $v = $dscontents->[-1]; 110 } 111 $v->{'recipient'} = $1; 112 $recipients++; 113 114 } elsif( scalar @$dscontents == $recipients ) { 115 # Error message 116 next unless length $e; 117 $v->{'diagnosis'} .= $e.' '; 118 119 } else { 120 # Error message when email address above does not include '@' 121 # and domain part. 122 next unless $e =~ /\A[ \t]{4}/; 123 $v->{'alterrors'} .= $e.' '; 124 } 125 } 126 127 unless( $recipients ) { 128 # Fallback for getting recipient addresses 129 if( defined $mhead->{'x-failed-recipients'} ) { 130 # X-Failed-Recipients: kijitora@example.jp 131 my @rcptinhead = split(',', $mhead->{'x-failed-recipients'}); 132 $_ =~ y/ //d for @rcptinhead; 133 $recipients = scalar @rcptinhead; 134 135 for my $e ( @rcptinhead ) { 136 # Insert each recipient address into @$dscontents 137 $dscontents->[-1]->{'recipient'} = $e; 138 next if scalar @$dscontents == $recipients; 139 push @$dscontents, __PACKAGE__->DELIVERYSTATUS; 140 } 141 } 142 } 143 return undef unless $recipients; 144 145 if( scalar @{ $mhead->{'received'} } ) { 146 # Get the name of local MTA 147 # Received: from marutamachi.example.org (c192128.example.net [192.0.2.128]) 148 $localhost0 = $1 if $mhead->{'received'}->[-1] =~ /from[ \t]([^ ]+) /; 149 } 150 151 for my $e ( @$dscontents ) { 152 if( exists $e->{'alterrors'} && $e->{'alterrors'} ) { 153 # Copy alternative error message 154 $e->{'diagnosis'} ||= $e->{'alterrors'}; 155 if( index($e->{'diagnosis'}, '-') == 0 || substr($e->{'diagnosis'}, -2, 2) eq '__' ) { 156 # Override the value of diagnostic code message 157 $e->{'diagnosis'} = $e->{'alterrors'} if $e->{'alterrors'}; 158 } 159 delete $e->{'alterrors'}; 160 } 161 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'}); 162 $e->{'diagnosis'} =~ s/\b__.+\z//; 163 164 unless( $e->{'rhost'} ) { 165 # Get the remote host name 166 # host neko.example.jp [192.0.2.222]: 550 5.1.1 <kijitora@example.jp>... User Unknown 167 $e->{'rhost'} = $1 if $e->{'diagnosis'} =~ /host[ \t]+([^ \t]+)[ \t]\[.+\]:[ \t]/; 168 169 unless( $e->{'rhost'} ) { 170 if( scalar @{ $mhead->{'received'} } ) { 171 # Get localhost and remote host name from Received header. 172 my $r0 = $mhead->{'received'}; 173 $e->{'rhost'} = pop @{ Sisimai::RFC5322->received($r0->[-1]) }; 174 } 175 } 176 } 177 $e->{'lhost'} ||= $localhost0; 178 179 unless( $e->{'command'} ) { 180 # Get the SMTP command name for the session 181 SMTP: for my $r ( @$recommands ) { 182 # Verify each regular expression of SMTP commands 183 next unless $e->{'diagnosis'} =~ $r; 184 $e->{'command'} = uc $1; 185 last; 186 } 187 188 REASON: while(1) { 189 # Detect the reason of bounce 190 if( $e->{'command'} eq 'MAIL' ) { 191 # MAIL | Connected to 192.0.2.135 but sender was rejected. 192 $e->{'reason'} = 'rejected'; 193 194 } elsif( $e->{'command'} eq 'HELO' || $e->{'command'} eq 'EHLO' ) { 195 # HELO | Connected to 192.0.2.135 but my name was rejected. 196 $e->{'reason'} = 'blocked'; 197 198 } else { 199 SESSION: for my $r ( keys %$messagesof ) { 200 # Verify each regular expression of session errors 201 next unless grep { index($e->{'diagnosis'}, $_) > -1 } @{ $messagesof->{ $r } }; 202 $e->{'reason'} = $r; 203 last; 204 } 205 } 206 last; 207 } 208 } 209 $e->{'command'} ||= ''; 210 } 211 return { 'ds' => $dscontents, 'rfc822' => $emailsteak->[1] }; 212} 213 2141; 215__END__ 216 217=encoding utf-8 218 219=head1 NAME 220 221Sisimai::Lhost::MailRu - bounce mail parser class for C<@mail.ru>. 222 223=head1 SYNOPSIS 224 225 use Sisimai::Lhost::MailRu; 226 227=head1 DESCRIPTION 228 229Sisimai::Lhost::MailRu parses a bounce email which created by C<@mail.ru>. 230Methods in the module are called from only Sisimai::Message. 231 232=head1 CLASS METHODS 233 234=head2 C<B<description()>> 235 236C<description()> returns description string of this module. 237 238 print Sisimai::Lhost::MailRu->description; 239 240=head2 C<B<make(I<header data>, I<reference to body string>)>> 241 242C<make()> method parses a bounced email and return results as a array reference. 243See Sisimai::Message for more details. 244 245=head1 AUTHOR 246 247azumakuniyuki 248 249=head1 COPYRIGHT 250 251Copyright (C) 2014-2020 azumakuniyuki, All rights reserved. 252 253=head1 LICENSE 254 255This software is distributed under The BSD 2-Clause License. 256 257=cut 258