line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# This file is part of App-Milter-Limit-Plugin-BerkeleyDB |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# This software is copyright (c) 2010 by Michael Schout. |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# This is free software; you can redistribute it and/or modify it under |
7
|
|
|
|
|
|
|
# the same terms as the Perl 5 programming language system itself. |
8
|
|
|
|
|
|
|
# |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
package App::Milter::Limit::Plugin::BerkeleyDB; |
11
|
|
|
|
|
|
|
$App::Milter::Limit::Plugin::BerkeleyDB::VERSION = '0.52'; |
12
|
|
|
|
|
|
|
# ABSTRACT: BerkeleyDB driver for App::Milter::Limit |
13
|
|
|
|
|
|
|
|
14
|
2
|
|
|
2
|
|
37128
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
47
|
|
15
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
48
|
|
16
|
2
|
|
|
2
|
|
9
|
use base qw(App::Milter::Limit::Plugin Class::Accessor); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
523
|
|
17
|
|
|
|
|
|
|
|
18
|
2
|
|
|
2
|
|
4643
|
use App::Milter::Limit::Log; |
|
2
|
|
|
|
|
14593
|
|
|
2
|
|
|
|
|
100
|
|
19
|
2
|
|
|
2
|
|
205
|
use BerkeleyDB qw(DB_CREATE DB_INIT_MPOOL DB_INIT_CDB); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors(qw(_db)); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub init { |
24
|
|
|
|
|
|
|
my $self = shift; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
$self->init_defaults; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
App::Milter::Limit::Util::make_path($self->config_get('driver', 'home')); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# db/env creation deferred until child_init |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub init_defaults { |
34
|
|
|
|
|
|
|
my $self = shift; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
$self->config_defaults('driver', |
37
|
|
|
|
|
|
|
home => $self->config_get('global', 'state_dir'), |
38
|
|
|
|
|
|
|
file => 'bdb-stats.db', |
39
|
|
|
|
|
|
|
); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# open BerkeleyDB handles in child_init handler. |
43
|
|
|
|
|
|
|
sub child_init { |
44
|
|
|
|
|
|
|
my $self = shift; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
my $conf = App::Milter::Limit::Config->section('driver'); |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
my $env = BerkeleyDB::Env->new( |
49
|
|
|
|
|
|
|
-Home => $$conf{home}, |
50
|
|
|
|
|
|
|
-Flags => DB_CREATE | DB_INIT_MPOOL | DB_INIT_CDB) |
51
|
|
|
|
|
|
|
or die "failed to open BerkeleyDB env: $!"; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
my $db = BerkeleyDB::Hash->new( |
54
|
|
|
|
|
|
|
-Filename => $$conf{file}, |
55
|
|
|
|
|
|
|
-Env => $env, |
56
|
|
|
|
|
|
|
-Flags => DB_CREATE) or die "failed to open BerkeleyDB: $!"; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
$self->_db($db); |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
debug("BerkeleyDB connection initialized"); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub query { |
64
|
|
|
|
|
|
|
my ($self, $from) = @_; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
my $conf = App::Milter::Limit::Config->global; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
my $db = $self->_db; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
my $val; |
71
|
|
|
|
|
|
|
$db->db_get($from, $val); |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
unless (defined $val) { |
74
|
|
|
|
|
|
|
# initialize new record for sender |
75
|
|
|
|
|
|
|
$val = join ':', time, 0; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
my ($start, $count) = split ':', $val; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
# reset counter if it is expired |
81
|
|
|
|
|
|
|
if ($start < time - $$conf{expire}) { |
82
|
|
|
|
|
|
|
$count = 0; |
83
|
|
|
|
|
|
|
$start = time; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# update database for this sender. |
87
|
|
|
|
|
|
|
$val = join ':', $start, ++$count; |
88
|
|
|
|
|
|
|
$db->db_put($from, $val); |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
return $count; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
1; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
__END__ |