| 1 | package Mail::SpamAssassin::RedisAddrList; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | # ABSTRACT: redis address list for spamassassin auto-whitelist | ||||||||||||||||||||
| 7 | 1 | our $VERSION = '1.003'; # VERSION | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 1 | ▶ | use Mail::SpamAssassin::PersistentAddrList; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 1 | ▶ | use Mail::SpamAssassin::Util qw(untaint_var); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 1 | ▶ | use Mail::SpamAssassin::Logger; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 1 | ▶ | use Redis; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 1 | our @ISA = qw(Mail::SpamAssassin::PersistentAddrList); | |||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | ########################################################################### | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | sub new { | ||||||||||||||||||||
| 20 | 0 | ▶ | my $class = shift; | ||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 21 | 0 | ▶ | $class = ref($class) || $class; | ||||||||||||||||||
|
Condition0%0/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table0%0/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||
| 22 | 0 | my $self = $class->SUPER::new(@_); | |||||||||||||||||||
| 23 | 0 | $self->{class} = $class; | |||||||||||||||||||
| 24 | 0 | bless ($self, $class); | |||||||||||||||||||
| 25 | 0 | $self; | |||||||||||||||||||
| 26 | } | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | ########################################################################### | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | sub new_checker { | ||||||||||||||||||||
| 31 | 0 | ▶ | my ($factory, $main) = @_; | ||||||||||||||||||
|
Subroutine
new_checker: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||
| 32 | 0 | my $class = $factory->{class}; | |||||||||||||||||||
| 33 | 0 | my $conf = $main->{conf}; | |||||||||||||||||||
| 34 | 0 | ▶ | my $prefix = $conf->{auto_whitelist_redis_prefix} || 'awl_'; | ||||||||||||||||||
|
Condition0%0/2
$conf->{'auto_whitelist_redis_prefix'} || 'awl_'
MC/DC0%0/1
$conf->{'auto_whitelist_redis_prefix'} || 'awl_'
$conf->{'auto_whitelist_redis_prefix'}
Truth table0%0/2
A
A= $conf->{'auto_whitelist_redis_prefix'}
| |||||||||||||||||||||
| 35 | 0 | my $redis_server = $conf->{auto_whitelist_redis_server}; | |||||||||||||||||||
| 36 | 0 | my $password = $conf->{auto_whitelist_redis_password}; | |||||||||||||||||||
| 37 | 0 | my $database = $conf->{auto_whitelist_redis_database}; | |||||||||||||||||||
| 38 | 0 | ▶ | my $expire = $conf->{auto_whitelist_redis_expire} || 0; | ||||||||||||||||||
|
Condition0%0/2
$conf->{'auto_whitelist_redis_expire'} || 0
MC/DC0%0/1
$conf->{'auto_whitelist_redis_expire'} || 0
$conf->{'auto_whitelist_redis_expire'}
Truth table0%0/2
A
A= $conf->{'auto_whitelist_redis_expire'}
| |||||||||||||||||||||
| 39 | 0 | my $debug = $conf->{auto_whitelist_redis_debug}; | |||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 0 | untaint_var( \$redis_server ); | |||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 0 | my $self = { | |||||||||||||||||||
| 44 | 'main' => $main, | ||||||||||||||||||||
| 45 | 'prefix' => $prefix, | ||||||||||||||||||||
| 46 | 'expire' => $expire, | ||||||||||||||||||||
| 47 | }; | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 0 | Mail::SpamAssassin::Plugin::info('initializing connection to redis server...'); | |||||||||||||||||||
| 50 | 0 | eval { | |||||||||||||||||||
| 51 | 0 | ▶ | $self->{'redis'} = Redis->new( | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 52 | server => defined $redis_server ? | ||||||||||||||||||||
| 53 | $redis_server : '127.0.0.1:6379', | ||||||||||||||||||||
| 54 | debug => $debug, | ||||||||||||||||||||
| 55 | defined $password ? ( password => $password ) : (), | ||||||||||||||||||||
| 56 | ); | ||||||||||||||||||||
| 57 | }; | ||||||||||||||||||||
| 58 | 0 | ▶ | if( $@ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | 0 | die('could not connect to redis: '.$@); | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 0 | ▶ | if( $database ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 63 | 0 | Mail::SpamAssassin::Plugin::info("selecting redis database $database..."); | |||||||||||||||||||
| 64 | 0 | $self->{'redis'}->select($database); | |||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 0 | bless ($self, $class); | |||||||||||||||||||
| 68 | 0 | return $self; | |||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | ########################################################################### | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | sub finish { | ||||||||||||||||||||
| 74 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
finish: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 0 | $self->{'redis'}->quit; | |||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | ########################################################################### | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | sub get_addr_entry { | ||||||||||||||||||||
| 82 | 0 | ▶ | my ($self, $addr, $signedby) = @_; | ||||||||||||||||||
|
Subroutine
get_addr_entry: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 0 | my $entry = { | |||||||||||||||||||
| 85 | addr => $addr, | ||||||||||||||||||||
| 86 | }; | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | my ( $count, $score ) = $self->{'redis'}->mget( | ||||||||||||||||||||
| 89 | $self->{'prefix'}.$addr.'_count', | ||||||||||||||||||||
| 90 | 0 | $self->{'prefix'}.$addr.'_score', | |||||||||||||||||||
| 91 | ); | ||||||||||||||||||||
| 92 | 0 | ▶ | $entry->{count} = defined $count ? $count : 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | 0 | ▶ | $entry->{totscore} = defined $score ? $score / 1000 : 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 0 | dbg("auto-whitelist: redis-based $addr scores ".$entry->{count}.'/'.$entry->{totscore}); | |||||||||||||||||||
| 96 | 0 | return $entry; | |||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | sub _update_addr_expire { | ||||||||||||||||||||
| 100 | 0 | ▶ | my ($self, $addr) = @_; | ||||||||||||||||||
|
Subroutine
_update_addr_expire: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 101 | 0 | my $expire = $self->{'expire'}; | |||||||||||||||||||
| 102 | 0 | ▶ | return unless $expire; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 0 | ▶ | $self->{'redis'}->expire($self->{'prefix'}.$addr.'_count', $expire, sub {}); | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 105 | 0 | ▶ | $self->{'redis'}->expire($self->{'prefix'}.$addr.'_score', $expire, sub {}); | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 0 | return; | |||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | sub add_score { | ||||||||||||||||||||
| 111 | 0 | ▶ | my($self, $entry, $score) = @_; | ||||||||||||||||||
|
Subroutine
add_score: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 0 | ▶ | $entry->{count} ||= 0; | ||||||||||||||||||
|
Condition0%0/2
$entry->{"count"} ||= 0
MC/DC0%0/1
$entry->{"count"} ||= 0
$entry->{"count"}
Truth table0%0/2
A
A= $entry->{"count"}
| |||||||||||||||||||||
| 114 | 0 | ▶ | $entry->{addr} ||= ''; | ||||||||||||||||||
|
Condition0%0/2
$entry->{"addr"} ||= ""
MC/DC0%0/1
$entry->{"addr"} ||= ""
$entry->{"addr"}
Truth table0%0/2
A
A= $entry->{"addr"}
| |||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 0 | $entry->{count}++; | |||||||||||||||||||
| 117 | 0 | $entry->{totscore} += $score; | |||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | 0 | dbg("auto-whitelist: add_score: new count: ".$entry->{count}.", new totscore: ".$entry->{totscore}); | |||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 0 | $self->{'redis'}->incr( $self->{'prefix'}.$entry->{'addr'}.'_count' ); | |||||||||||||||||||
| 122 | 0 | $self->{'redis'}->incrby( $self->{'prefix'}.$entry->{'addr'}.'_score', int($score * 1000) ); | |||||||||||||||||||
| 123 | 0 | $self->_update_addr_expire($entry->{addr}); | |||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 0 | return $entry; | |||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | sub remove_entry { | ||||||||||||||||||||
| 129 | 0 | ▶ | my ($self, $entry) = @_; | ||||||||||||||||||
|
Subroutine
remove_entry: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 0 | my $addr = $entry->{addr}; | |||||||||||||||||||
| 132 | $self->{'redis'}->del( | ||||||||||||||||||||
| 133 | $self->{'prefix'}.$addr.'_count', | ||||||||||||||||||||
| 134 | 0 | $self->{'prefix'}.$addr.'_score' ); | |||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | 0 | ▶ | if ( my ($mailaddr) = ($addr) =~ /^(.*)\|ip=none$/) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 137 | # it doesn't have an IP attached. | ||||||||||||||||||||
| 138 | # try to delete any per-IP entries for this addr as well. | ||||||||||||||||||||
| 139 | # could be slow... | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | # make sure wildcard characters are escaped | ||||||||||||||||||||
| 142 | 0 | $mailaddr =~ s/([\*\[\]\?])/\\$1/g; | |||||||||||||||||||
| 143 | 0 | my @keys = $self->{'redis'}->keys($self->{'prefix'}.$mailaddr.'|ip=*'); | |||||||||||||||||||
| 144 | 0 | ▶ | $self->{'redis'}->del( @keys ) if @keys; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 145 | } | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | 0 | return; | |||||||||||||||||||
| 148 | } | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 1 | 1; | |||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | __END__ | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | =pod | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | =encoding UTF-8 | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | =head1 NAME | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | Mail::SpamAssassin::RedisAddrList - redis address list for spamassassin auto-whitelist | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | =head1 VERSION | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | version 1.003 | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | =head1 AUTHOR | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | Markus Benning <ich@markusbenning.de> | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | This software is Copyright (c) 2015 by Markus Benning. | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | This is free software, licensed under: | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | The Apache License, Version 2.0, January 2004 | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | =cut | ||||||||||||||||||||