| 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.001'; # 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=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||
| 32 | 0 | my $class = $factory->{class}; | |||||||||||||||||||
| 33 | 0 | my $conf = $main->{conf}; | |||||||||||||||||||
| 34 | 0 | my $prefix = $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 $debug = $conf->{auto_whitelist_redis_debug}; | |||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 0 | untaint_var( \$redis_server ); | |||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 0 | ▶ | my $self = { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 43 | 'main' => $main, | ||||||||||||||||||||
| 44 | 'prefix' => defined $prefix ? $prefix : 'awl_', | ||||||||||||||||||||
| 45 | }; | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 0 | Mail::SpamAssassin::Plugin::info('initializing connection to redis server...'); | |||||||||||||||||||
| 48 | 0 | eval { | |||||||||||||||||||
| 49 | 0 | ▶ | $self->{'redis'} = Redis->new( | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 50 | server => defined $redis_server ? | ||||||||||||||||||||
| 51 | $redis_server : '127.0.0.1:6379', | ||||||||||||||||||||
| 52 | debug => $debug, | ||||||||||||||||||||
| 53 | defined $password ? ( password => $password ) : (), | ||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||
| 55 | }; | ||||||||||||||||||||
| 56 | 0 | ▶ | if( $@ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | 0 | die('could not connect to redis: '.$@); | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 0 | ▶ | if( $database ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 61 | 0 | Mail::SpamAssassin::Plugin::info("selecting redis database $database..."); | |||||||||||||||||||
| 62 | 0 | $self->{'redis'}->select($database); | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 0 | bless ($self, $class); | |||||||||||||||||||
| 66 | 0 | return $self; | |||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | ########################################################################### | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | sub finish { | ||||||||||||||||||||
| 72 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
finish: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 0 | $self->{'redis'}->quit; | |||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | ########################################################################### | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | sub get_addr_entry { | ||||||||||||||||||||
| 80 | 0 | ▶ | my ($self, $addr, $signedby) = @_; | ||||||||||||||||||
|
Subroutine
get_addr_entry: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 0 | my $entry = { | |||||||||||||||||||
| 83 | addr => $addr, | ||||||||||||||||||||
| 84 | }; | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | my ( $count, $score ) = $self->{'redis'}->mget( | ||||||||||||||||||||
| 87 | $self->{'prefix'}.$addr.'_count', | ||||||||||||||||||||
| 88 | 0 | $self->{'prefix'}.$addr.'_score', | |||||||||||||||||||
| 89 | ); | ||||||||||||||||||||
| 90 | 0 | ▶ | $entry->{count} = defined $count ? $count : 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 91 | 0 | ▶ | $entry->{totscore} = defined $score ? $score / 1000 : 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 0 | dbg("auto-whitelist: redis-based $addr scores ".$entry->{count}.'/'.$entry->{totscore}); | |||||||||||||||||||
| 94 | 0 | return $entry; | |||||||||||||||||||
| 95 | } | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | sub add_score { | ||||||||||||||||||||
| 98 | 0 | ▶ | my($self, $entry, $score) = @_; | ||||||||||||||||||
|
Subroutine
add_score: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 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"}
| |||||||||||||||||||||
| 101 | 0 | ▶ | $entry->{addr} ||= ''; | ||||||||||||||||||
|
Condition0%0/2
$entry->{"addr"} ||= ""
MC/DC0%0/1
$entry->{"addr"} ||= ""
$entry->{"addr"}
Truth table0%0/2
A
A= $entry->{"addr"}
| |||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 0 | $entry->{count}++; | |||||||||||||||||||
| 104 | 0 | $entry->{totscore} += $score; | |||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 0 | dbg("auto-whitelist: add_score: new count: ".$entry->{count}.", new totscore: ".$entry->{totscore}); | |||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 0 | $self->{'redis'}->incr( $self->{'prefix'}.$entry->{'addr'}.'_count' ); | |||||||||||||||||||
| 109 | 0 | $self->{'redis'}->incrby( $self->{'prefix'}.$entry->{'addr'}.'_score', int($score * 1000) ); | |||||||||||||||||||
| 110 | 0 | return $entry; | |||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | sub remove_entry { | ||||||||||||||||||||
| 114 | 0 | ▶ | my ($self, $entry) = @_; | ||||||||||||||||||
|
Subroutine
remove_entry: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 0 | my $addr = $entry->{addr}; | |||||||||||||||||||
| 117 | $self->{'redis'}->del( | ||||||||||||||||||||
| 118 | $self->{'prefix'}.$addr.'_count', | ||||||||||||||||||||
| 119 | 0 | $self->{'prefix'}.$addr.'_score' ); | |||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 0 | ▶ | if ( my $mailaddr = ($addr) =~ /^(.*)\|ip=none$/) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 122 | # it doesn't have an IP attached. | ||||||||||||||||||||
| 123 | # try to delete any per-IP entries for this addr as well. | ||||||||||||||||||||
| 124 | # could be slow... | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | 0 | $mailaddr =~ s/\*//g; | |||||||||||||||||||
| 127 | 0 | my @keys = $self->{'redis'}->keys($self->{'prefix'}.$mailaddr.'*'); | |||||||||||||||||||
| 128 | 0 | $self->{'redis'}->del( @keys ); | |||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 0 | return; | |||||||||||||||||||
| 132 | } | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | 1 | 1; | |||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | __END__ | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | =pod | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | =encoding UTF-8 | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | =head1 NAME | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | Mail::SpamAssassin::RedisAddrList - redis address list for spamassassin auto-whitelist | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | =head1 VERSION | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | version 1.001 | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | =head1 AUTHOR | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | Markus Benning <ich@markusbenning.de> | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | This software is Copyright (c) 2015 by Markus Benning. | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | This is free software, licensed under: | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | The Apache License, Version 2.0, January 2004 | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | =cut | ||||||||||||||||||||