Mail/Qmail/Filter/Graylist.pm untested

filter: Statementstmt 0.0% 0 / 48 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 81 CC 6 SCAR 32.6
CC
6
Cov
0%
filter
42.0
BEGIN
2.0
BEGIN
2.0
CRAP 26.0
1 use 5.014;
2 use warnings;
3  
4 package Mail::Qmail::Filter::Graylist;
5  
6 our $VERSION = '0.12';
7  
8 use Mo qw(coerce default required);
9 extends 'Mail::Qmail::Filter';
10  
11 has defer_text => 'You have been graylisted. Please try again later.';
12 has dir => required => 1;
13 has min_secs => 300;
14  
15 sub filter {
16 my $self = shift;
17 my $message = $self->message;
18 my $mail_from = $message->from;
19  
20 require Path::Tiny and Path::Tiny->import('path') unless defined &path;
21  
22 my ( $sender_localpart, $sender_domain ) =
23 length $mail_from
24 ? split /\@/, lc $mail_from, 2
25 : ('_none') x 2;
26 my $remote_ip = $message->remote_ip;
27  
28 my $passed = 0;
29 my @to = $message->to;
30 Rcpt: for ( $message->to ) {
31 my ( $rcpt_localpart, $rcpt_domain ) = split /\@/, lc, 2;
32 my $graylist_file = path( $self->dir, map y/0-9A-Za-z._-/_/cr,
33 $rcpt_domain, $rcpt_localpart, $sender_domain, $sender_localpart );
34 if ( $graylist_file->exists ) {
35 {
36 my $fh = $graylist_file->openr;
37 local $_;
38 while (<$fh>) {
39 /^\Q$remote_ip\E / or next;
40 chomp;
41 $self->debug( 'known' . ( @to > 1 && " for $_" ) );
42 ++$passed;
43 next Rcpt;
44 }
45 }
46 if ( $^T - $graylist_file->stat->mtime >= $self->min_secs ) {
47 $graylist_file->append(
48 "$remote_ip " . $message->remote_host . "\n" );
49 ++$passed;
50 $self->debug( 'allowed' . ( @to > 1 && " for $_" ) );
51 next Rcpt;
52 }
53 else {
54 $self->debug( 'too recent' . ( @to > 1 && " for $_" ) );
55 }
56 }
57 else {
58 $graylist_file->parent->mkdir;
59 $graylist_file->openw;
60 }
61 }
62 return if $passed == @to;
63 $self->defer( $self->defer_text );
64 }
65  
66 1;
67  
68 __END__