File Coverage

lib/Sisimai/Lhost/GMX.pm
Criterion Covered Total %
statement 58 58 100.0
branch 26 30 86.6
condition 11 16 68.7
subroutine 6 6 100.0
pod 2 2 100.0
total 103 112 91.9


line stmt bran cond sub pod time code
1             package Sisimai::Lhost::GMX;
2 21     21   5397 use parent 'Sisimai::Lhost';
  21         35  
  21         112  
3 21     21   1140 use feature ':5.10';
  21         32  
  21         1340  
4 21     21   98 use strict;
  21         29  
  21         394  
5 21     21   89 use warnings;
  21         32  
  21         14980  
6              
7 2     2 1 1050 sub description { 'GMX: https://www.gmx.net' }
8             sub make {
9             # Detect an error from GMX and mail.com
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.4
15 267     267 1 708 my $class = shift;
16 267   100     623 my $mhead = shift // return undef;
17 266   50     549 my $mbody = shift // return undef;
18              
19             # Envelope-To:
20             # X-GMX-Antispam: 0 (Mail was not recognized as spam); Detail=V3;
21             # X-GMX-Antivirus: 0 (no virus found)
22             # X-UI-Out-Filterresults: unknown:0;
23 266 100       931 return undef unless defined $mhead->{'x-gmx-antispam'};
24              
25 21         61 state $indicators = __PACKAGE__->INDICATORS;
26 21         38 state $rebackbone = qr|^---[ ]The[ ]header[ ]of[ ]the[ ]original[ ]message[ ]is[ ]following[.][ ]---|m;
27 21         36 state $startingof = { 'message' => ['This message was created automatically by mail delivery software'] };
28 21         40 state $messagesof = { 'expired' => ['delivery retry timeout exceeded'] };
29              
30 21         94 my $dscontents = [__PACKAGE__->DELIVERYSTATUS];
31 21         147 my $emailsteak = Sisimai::RFC5322->fillet($mbody, $rebackbone);
32 21         34 my $readcursor = 0; # (Integer) Points the current cursor position
33 21         34 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
34 21         35 my $v = undef;
35              
36 21         130 for my $e ( split("\n", $emailsteak->[0]) ) {
37             # Read error messages and delivery status lines from the head of the email
38             # to the previous line of the beginning of the original message.
39 230 100       281 unless( $readcursor ) {
40             # Beginning of the bounce message or message/delivery-status part
41 21 50       168 $readcursor |= $indicators->{'deliverystatus'} if index($e, $startingof->{'message'}->[0]) == 0;
42 21         36 next;
43             }
44 209 50       269 next unless $readcursor & $indicators->{'deliverystatus'};
45 209 100       243 next unless length $e;
46              
47             # This message was created automatically by mail delivery software.
48             #
49             # A message that you sent could not be delivered to one or more of
50             # its recipients. This is a permanent error. The following address
51             # failed:
52             #
53             # "shironeko@example.jp":
54             # SMTP error from remote server after RCPT command:
55             # host: mx.example.jp
56             # 5.1.1 ... User Unknown
57 162         151 $v = $dscontents->[-1];
58              
59 162 100 100     649 if( $e =~ /\A["]([^ ]+[@][^ ]+)["]:\z/ || $e =~ /\A[<]([^ ]+[@][^ ]+)[>]\z/ ) {
    100          
    100          
60             # "shironeko@example.jp":
61             # ---- OR ----
62             #
63             #
64             # Reason:
65             # delivery retry timeout exceeded
66 26 100       60 if( $v->{'recipient'} ) {
67             # There are multiple recipient addresses in the message body.
68 5         18 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
69 5         11 $v = $dscontents->[-1];
70             }
71 26         68 $v->{'recipient'} = $1;
72 26         40 $recipients++;
73              
74             } elsif( $e =~ /\ASMTP error .+ ([A-Z]{4}) command:\z/ ) {
75             # SMTP error from remote server after RCPT command:
76 21         54 $v->{'command'} = $1;
77              
78             } elsif( $e =~ /\Ahost:[ \t]*(.+)\z/ ) {
79             # host: mx.example.jp
80 21         39 $v->{'rhost'} = $1;
81              
82             } else {
83             # Get error message
84 94 100 66     492 if( $e =~ /\b[45][.]\d[.]\d\b/ || $e =~ /[<][^ ]+[@][^ ]+[>]/ || $e =~ /\b[45]\d{2}\b/ ) {
      66        
85 21   33     72 $v->{'diagnosis'} ||= $e;
86              
87             } else {
88 73 50       114 next if $e eq '';
89 73 100       185 if( $e eq 'Reason:' ) {
    100          
90             # Reason:
91             # delivery retry timeout exceeded
92 5         11 $v->{'diagnosis'} = $e;
93              
94             } elsif( $v->{'diagnosis'} eq 'Reason:' ) {
95 5         12 $v->{'diagnosis'} = $e;
96             }
97             }
98             }
99             }
100 21 50       72 return undef unless $recipients;
101              
102 21         54 for my $e ( @$dscontents ) {
103 26         50 $e->{'diagnosis'} =~ y/\n/ /;
104 26         140 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'});
105              
106 26         87 SESSION: for my $r ( keys %$messagesof ) {
107             # Verify each regular expression of session errors
108 26 100       43 next unless grep { index($e->{'diagnosis'}, $_) > -1 } @{ $messagesof->{ $r } };
  26         132  
  26         63  
109 5         13 $e->{'reason'} = $r;
110 5         11 last;
111             }
112             }
113 21         88 return { 'ds' => $dscontents, 'rfc822' => $emailsteak->[1] };
114             }
115              
116             1;
117             __END__