line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
################################################################# |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# Hook::Filter::RulePool - A pool of filter rules |
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
# $Id: RulePool.pm,v 1.3 2007/05/23 08:26:15 erwan_lemonnier Exp $ |
6
|
|
|
|
|
|
|
# |
7
|
|
|
|
|
|
|
# 070516 erwan Started |
8
|
|
|
|
|
|
|
# 070522 erwan flush_rules returns self |
9
|
|
|
|
|
|
|
# |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
package Hook::Filter::RulePool; |
12
|
|
|
|
|
|
|
|
13
|
12
|
|
|
12
|
|
68
|
use strict; |
|
12
|
|
|
|
|
22
|
|
|
12
|
|
|
|
|
427
|
|
14
|
12
|
|
|
12
|
|
63
|
use warnings; |
|
12
|
|
|
|
|
23
|
|
|
12
|
|
|
|
|
343
|
|
15
|
12
|
|
|
12
|
|
65
|
use Carp qw(croak); |
|
12
|
|
|
|
|
23
|
|
|
12
|
|
|
|
|
596
|
|
16
|
12
|
|
|
12
|
|
70
|
use Data::Dumper; |
|
12
|
|
|
|
|
21
|
|
|
12
|
|
|
|
|
1480
|
|
17
|
12
|
|
|
12
|
|
10897
|
use Hook::Filter::Rule; |
|
12
|
|
|
|
|
25
|
|
|
12
|
|
|
|
|
1002
|
|
18
|
|
|
|
|
|
|
|
19
|
12
|
|
|
12
|
|
72
|
use base qw(Exporter); |
|
12
|
|
|
|
|
23
|
|
|
12
|
|
|
|
|
7961
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
our @EXPORT = (); |
22
|
|
|
|
|
|
|
our @EXPORT_OK = ('get_rule_pool'); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# the filter rules |
26
|
|
|
|
|
|
|
my @rules; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
#--------------------------------------------------------------- |
29
|
|
|
|
|
|
|
# |
30
|
|
|
|
|
|
|
# A singleton pattern with lazy initialization and embedded constructor |
31
|
|
|
|
|
|
|
# |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
my $pool; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub get_rule_pool { |
36
|
27
|
100
|
|
27
|
1
|
2910
|
if (!defined $pool) { |
37
|
12
|
|
|
|
|
47
|
$pool = bless({},__PACKAGE__); |
38
|
|
|
|
|
|
|
} |
39
|
27
|
|
|
|
|
85
|
return $pool; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# make sure no one calls the constructor |
43
|
|
|
|
|
|
|
sub new { |
44
|
1
|
|
|
1
|
1
|
283
|
croak "use get_pool() instead of new()"; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
#---------------------------------------------------------------- |
48
|
|
|
|
|
|
|
# |
49
|
|
|
|
|
|
|
# add_rule - add a rule to the pool |
50
|
|
|
|
|
|
|
# |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub add_rule { |
53
|
29
|
|
|
29
|
1
|
2090
|
my ($self,$obj) = @_; |
54
|
|
|
|
|
|
|
|
55
|
29
|
100
|
100
|
|
|
356
|
if (!defined $obj || (ref $obj ne "Hook::Filter::Rule" && ref \$obj ne "SCALAR") || scalar @_ != 2) { |
|
|
|
66
|
|
|
|
|
|
|
|
100
|
|
|
|
|
56
|
3
|
|
|
|
|
5
|
shift @_; |
57
|
3
|
|
|
|
|
14
|
croak "invalid parameters: Hook::Filter::RulePool->add_rule expects an instance of Hook::Filter::Rule or a rule string, and not [".Dumper(@_)."]"; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
26
|
100
|
|
|
|
94
|
if (ref \$obj eq "SCALAR") { |
61
|
|
|
|
|
|
|
# $obj is just a string containing a rule in text form |
62
|
19
|
|
|
|
|
115
|
my $rule = new Hook::Filter::Rule($obj); |
63
|
|
|
|
|
|
|
|
64
|
19
|
|
|
|
|
157
|
my ($pkg,$line) = (caller(0))[0,2]; |
65
|
19
|
|
50
|
|
|
126
|
my $fnc = (caller(1))[3] || "main"; |
66
|
19
|
|
|
|
|
126
|
$rule->source("added by ".$pkg."::".$fnc.", l.$line"); |
67
|
|
|
|
|
|
|
|
68
|
19
|
|
|
|
|
43
|
push @rules, $rule; |
69
|
|
|
|
|
|
|
} else { |
70
|
|
|
|
|
|
|
# $obj is an instance of Hook::Filter::Rule |
71
|
7
|
|
|
|
|
14
|
push @rules, $obj; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
26
|
|
|
|
|
85
|
return $self; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
#---------------------------------------------------------------- |
78
|
|
|
|
|
|
|
# |
79
|
|
|
|
|
|
|
# flush_rules - remove all rules |
80
|
|
|
|
|
|
|
# |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub flush_rules { |
83
|
11
|
|
|
11
|
1
|
3511
|
@rules = (); |
84
|
11
|
|
|
|
|
38
|
return $_[0]; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
#---------------------------------------------------------------- |
88
|
|
|
|
|
|
|
# |
89
|
|
|
|
|
|
|
# get_rules - return all registered rules |
90
|
|
|
|
|
|
|
# |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub get_rules { |
93
|
7
|
|
|
7
|
1
|
496
|
return @rules; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
#---------------------------------------------------------------- |
97
|
|
|
|
|
|
|
# |
98
|
|
|
|
|
|
|
# eval_rules - eval all rules and return true if one is true or none is registered (fail safe) |
99
|
|
|
|
|
|
|
# |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub eval_rules { |
102
|
57
|
|
|
57
|
1
|
3267
|
my $self = shift; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
# if no rules are registered, default to true (allow call) |
105
|
57
|
100
|
|
|
|
165
|
return 1 if (!@rules); |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
# evaluate all rules, until one is found to be true or all are found to be false |
108
|
50
|
|
|
|
|
108
|
foreach my $rule (@rules) { |
109
|
80
|
100
|
|
|
|
235
|
return 1 if ($rule->eval()); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
22
|
|
|
|
|
89
|
return 0; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
1; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
__END__ |