| 1 | package Sisimai::RFC3834; | ||||||||||||||||||||||||||||
| 2 | 8 | ▶ | use v5.26; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | # http://tools.ietf.org/html/rfc3834 | ||||||||||||||||||||||||||||
| 7 | 1 | ▶ | sub description { 'Detector for auto replied message' } | ||||||||||||||||||||||||||
|
Subroutine
description: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 8 | 95 | ▶ | sub inquire { | ||||||||||||||||||||||||||
|
Subroutine
inquire: called CC=33 SCAR=35.1
Pod
documented
| |||||||||||||||||||||||||||||
| 9 | # Detect auto reply message as RFC3834 | ||||||||||||||||||||||||||||
| 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 decode or the arguments are missing | ||||||||||||||||||||||||||||
| 14 | # @since v4.1.28 | ||||||||||||||||||||||||||||
| 15 | 95 | my $class = shift; | |||||||||||||||||||||||||||
| 16 | 95 | ▶ | my $mhead = shift // return undef; return undef unless keys %$mhead; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
shift() // (return undef)
MC/DC100%1/1
shift() // (return undef)
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 17 | 92 | ▶ | my $mbody = shift // return undef; return undef unless ref $mbody eq 'SCALAR'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
shift() // (return undef)
MC/DC0%0/1
shift() // (return undef)
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 18 | 91 | my $lower = {}; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 91 | my $markingsof = {'boundary' => '__SISIMAI_PSEUDO_BOUNDARY__'}; | |||||||||||||||||||||||||||
| 21 | 91 | my $lowerlabel = ['from', 'to', 'subject', 'auto-submitted', 'precedence', 'x-apple-action']; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 91 | for my $e ( @$lowerlabel ) { | |||||||||||||||||||||||||||
| 24 | # Set lower-cased value of each header related to auto-response | ||||||||||||||||||||||||||||
| 25 | 546 | ▶ | next unless exists $mhead->{ $e }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 285 | $lower->{ $e } = lc $mhead->{ $e }; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 91 | state $donotparse = { | |||||||||||||||||||||||||||
| 30 | 'from' => ['root@', 'postmaster@', 'mailer-daemon@'], | ||||||||||||||||||||||||||||
| 31 | 'to' => ['root@'], | ||||||||||||||||||||||||||||
| 32 | 'subject' => [ | ||||||||||||||||||||||||||||
| 33 | 'security information for', # sudo(1) | ||||||||||||||||||||||||||||
| 34 | 'mail failure -', # Exim | ||||||||||||||||||||||||||||
| 35 | ], | ||||||||||||||||||||||||||||
| 36 | }; | ||||||||||||||||||||||||||||
| 37 | 91 | state $autoreply0 = { | |||||||||||||||||||||||||||
| 38 | # http://www.iana.org/assignments/auto-submitted-keywords/auto-submitted-keywords.xhtml | ||||||||||||||||||||||||||||
| 39 | 'auto-submitted' => ['auto-generated', 'auto-replied', 'auto-notified'], | ||||||||||||||||||||||||||||
| 40 | 'precedence' => ['auto_reply'], | ||||||||||||||||||||||||||||
| 41 | 'subject' => ['auto:', 'auto response:', 'automatic reply:', 'out of office:', 'out of the office:'], | ||||||||||||||||||||||||||||
| 42 | 'x-apple-action' => ['vacation'], | ||||||||||||||||||||||||||||
| 43 | }; | ||||||||||||||||||||||||||||
| 44 | 91 | state $subjectset = qr{\A(?> | |||||||||||||||||||||||||||
| 45 | (?:.+?)?re: | ||||||||||||||||||||||||||||
| 46 | |auto(?:[ ]response): | ||||||||||||||||||||||||||||
| 47 | |automatic[ ]reply: | ||||||||||||||||||||||||||||
| 48 | |out[ ]of[ ]office: | ||||||||||||||||||||||||||||
| 49 | ) | ||||||||||||||||||||||||||||
| 50 | [ ]*(.+)\z | ||||||||||||||||||||||||||||
| 51 | }x; | ||||||||||||||||||||||||||||
| 52 | 91 | state $suspending = [ | |||||||||||||||||||||||||||
| 53 | ["this email inbox", " is no longer in use."], | ||||||||||||||||||||||||||||
| 54 | ]; | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 91 | my $leave = 0; DETECT_EXCLUSION_MESSAGE: for my $e ( keys %$donotparse ) { | |||||||||||||||||||||||||||
| 57 | # Exclude message from root@ | ||||||||||||||||||||||||||||
| 58 | 268 | ▶ | next unless exists $lower->{ $e }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 250 | ▶ | next unless grep { index($lower->{ $e }, $_) > -1 } $donotparse->{ $e }->@*; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 19 | $leave = 1; | |||||||||||||||||||||||||||
| 61 | 19 | last; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | 91 | ▶ | return undef if $leave; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 72 | my $match = 0; DETECT_AUTO_REPLY_MESSAGE0: for my $e ( keys %$autoreply0 ) { | |||||||||||||||||||||||||||
| 66 | # RFC3834 Auto-Submitted and other headers | ||||||||||||||||||||||||||||
| 67 | 259 | ▶ | next unless exists $lower->{ $e }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 93 | ▶ | next unless grep { index($lower->{ $e }, $_) == 0 } $autoreply0->{ $e }->@*; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 31 | $match++; | |||||||||||||||||||||||||||
| 71 | 31 | last; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | 72 | ▶ | return undef unless $match; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 31 | require Sisimai::Eb; | |||||||||||||||||||||||||||
| 76 | 31 | require Sisimai::Lhost; | |||||||||||||||||||||||||||
| 77 | 31 | my $dscontents = [Sisimai::Lhost->DELIVERYSTATUS]; my $v = $dscontents->[-1]; | |||||||||||||||||||||||||||
| 78 | 31 | my $recipients = 0; # (Integer) The number of 'Final-Recipient' header | |||||||||||||||||||||||||||
| 79 | 31 | my $maxmsgline = 5; # (Integer) Max message length(lines) | |||||||||||||||||||||||||||
| 80 | 31 | my $haveloaded = 0; # (Integer) The number of lines loaded from message body | |||||||||||||||||||||||||||
| 81 | 31 | my $blanklines = 0; # (Integer) Counter for countinuous blank lines | |||||||||||||||||||||||||||
| 82 | 31 | my $countuntil = 1; # (Integer) Maximun value of blank lines in the body part | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | RECIPIENT_ADDRESS: { | ||||||||||||||||||||||||||||
| 85 | # Try to get the address of the recipient | ||||||||||||||||||||||||||||
| 86 | 31 | for my $e ('reply-to', 'from', 'return-path') { | |||||||||||||||||||||||||||
| 87 | # Get the recipient address | ||||||||||||||||||||||||||||
| 88 | 57 | ▶ | next unless exists $mhead->{ $e }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 31 | $v->{'recipient'} = $mhead->{ $e }; | |||||||||||||||||||||||||||
| 91 | 31 | last; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 31 | ▶ | if( $v->{'recipient'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | # Clean-up the recipient address | ||||||||||||||||||||||||||||
| 96 | 31 | $v->{'recipient'} = Sisimai::Address->s3s4($v->{'recipient'}); | |||||||||||||||||||||||||||
| 97 | 31 | $recipients++; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 31 | ▶ | return undef unless $recipients; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 31 | ▶ | if( $mhead->{'content-type'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | # Get the boundary string and set regular expression for matching with the boundary string. | ||||||||||||||||||||||||||||
| 104 | 25 | my $q = Sisimai::RFC2045->boundary($mhead->{'content-type'}, 0); | |||||||||||||||||||||||||||
| 105 | 25 | ▶ | $markingsof->{'boundary'} = $q if $q; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | MESSAGE_BODY: { | ||||||||||||||||||||||||||||
| 109 | # Get vacation message | ||||||||||||||||||||||||||||
| 110 | 31 | for my $e ( split("\n", $$mbody) ) { | |||||||||||||||||||||||||||
| 111 | # Read the first 5 lines except a blank line | ||||||||||||||||||||||||||||
| 112 | 73 | ▶ | $countuntil += 1 if index($e, $markingsof->{'boundary'}) > -1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 73 | ▶ | unless( length $e ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | # Check a blank line | ||||||||||||||||||||||||||||
| 116 | 21 | ▶ | last if ++$blanklines > $countuntil; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 16 | next; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | 52 | ▶ | next if rindex($e, ' ') < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 37 | ▶ | next if index($e, 'Content-Type') == 0 || index($e, 'Content-Transfer') == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
index($e, "Content-Type") == 0 or index($e, "Content-Transfer") == 0
MC/DC0%0/2
index($e, "Content-Type") == 0 or index($e, "Content-Transfer") == 0
index($e, "Content-Type") == 0
index($e, "Content-Transfer") == 0
Truth table33%1/3
A or B
A= index($e, "Content-Type") == 0B= index($e, "Content-Transfer") == 0
| |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 37 | $v->{'diagnosis'} .= $e.' '; | |||||||||||||||||||||||||||
| 123 | 37 | $haveloaded++; | |||||||||||||||||||||||||||
| 124 | 37 | ▶ | last if $haveloaded >= $maxmsgline; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | 31 | ▶ | $v->{'diagnosis'} ||= $mhead->{'subject'}; | ||||||||||||||||||||||||||
|
Condition66%2/3
$v->{"diagnosis"} ||= $mhead->{"subject"}
MC/DC0%0/2
$v->{"diagnosis"} ||= $mhead->{"subject"}
$v->{"diagnosis"}
$mhead->{"subject"}
Truth table66%2/3
A ||= B
A= $v->{"diagnosis"}B= $mhead->{"subject"}
| |||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | 31 | $v->{'reason'} = $Sisimai::Eb::ReAWAY; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 31 | my $cv = lc $v->{'diagnosis'}; for my $e ( @$suspending ) { | |||||||||||||||||||||||||||
| 131 | # Check that the auto-replied message indicates the "Suspend" reason or not. | ||||||||||||||||||||||||||||
| 132 | 31 | ▶ | next unless Sisimai::String->aligned(\$cv, $e); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 5 | $v->{'reason'} = $Sisimai::Eb::ReQUIT; last; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 31 | $v->{'date'} = $mhead->{'date'}; | |||||||||||||||||||||||||||
| 137 | 31 | $v->{'status'} = ''; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | # Get the Subject header from the original message | ||||||||||||||||||||||||||||
| 140 | 31 | ▶ | my $rfc822part = $lower->{'subject'} =~ $subjectset ? 'Subject: '.$1."\n" : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 31 | return {"ds" => $dscontents, "rfc822" => $rfc822part}; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 8 | 1; | |||||||||||||||||||||||||||
| 145 | __END__ | ||||||||||||||||||||||||||||