line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Email::Filter::Rules; |
2
|
2
|
|
|
2
|
|
325824
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
66
|
|
3
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
65
|
|
4
|
2
|
|
|
2
|
|
10
|
use constant RULE_SPLIT_LIMIT => 3; |
|
2
|
|
|
|
|
19
|
|
|
2
|
|
|
|
|
1331
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = 1.2; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub new { |
9
|
7
|
|
|
7
|
1
|
2663
|
my ( $class, %args ) = @_; |
10
|
7
|
100
|
|
|
|
25
|
return unless $args{rules}; |
11
|
6
|
|
|
|
|
13
|
my @rules = (); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# added the \n check because I got a warning if a did a -e test on |
14
|
|
|
|
|
|
|
# a scalar containing newlines |
15
|
6
|
100
|
100
|
|
|
170
|
if ( $args{rules} !~ /\n/s && -e $args{rules} ) { |
|
|
100
|
|
|
|
|
|
16
|
1
|
50
|
|
|
|
42
|
open my $fh, '<', $args{rules} or die "Can't open file $args{rules}: $!"; |
17
|
1
|
|
|
|
|
59
|
@rules = <$fh>; |
18
|
1
|
|
|
|
|
14
|
close $fh; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
elsif ( ref $args{rules} eq 'ARRAY' ) { |
21
|
4
|
|
|
|
|
15
|
@rules = @{ $args{rules} }; |
|
4
|
|
|
|
|
12
|
|
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
else { |
24
|
1
|
|
|
|
|
7
|
@rules = split /\n/, $args{rules}; |
25
|
|
|
|
|
|
|
} |
26
|
6
|
100
|
|
|
|
20
|
return unless scalar @rules; |
27
|
|
|
|
|
|
|
|
28
|
5
|
|
|
|
|
7
|
my @rule_data = (); |
29
|
|
|
|
|
|
|
|
30
|
5
|
|
|
|
|
13
|
for my $line (@rules) { |
31
|
51
|
|
|
|
|
62
|
chomp($line); |
32
|
51
|
100
|
|
|
|
107
|
next if $line =~ /^#/; |
33
|
48
|
|
|
|
|
112
|
my ( $folder, $methods, $substring ) = split( /\s+/, $line, RULE_SPLIT_LIMIT ); |
34
|
48
|
100
|
100
|
|
|
234
|
next unless $methods && $folder && $substring; |
|
|
|
100
|
|
|
|
|
35
|
28
|
|
|
|
|
52
|
my $escaped_substring = quotemeta($substring); |
36
|
28
|
|
|
|
|
63
|
my @methods = split( /\:/, $methods ); |
37
|
28
|
|
|
|
|
131
|
push @rule_data, { |
38
|
|
|
|
|
|
|
methods => \@methods, |
39
|
|
|
|
|
|
|
substring => $escaped_substring, |
40
|
|
|
|
|
|
|
folder => $folder, |
41
|
|
|
|
|
|
|
}; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
5
|
|
|
|
|
23
|
my $obj = { |
45
|
|
|
|
|
|
|
rule_data => \@rule_data, |
46
|
|
|
|
|
|
|
debug => $args{debug}, |
47
|
|
|
|
|
|
|
}; |
48
|
|
|
|
|
|
|
|
49
|
5
|
|
|
|
|
31
|
return bless $obj, $class; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub apply_rules { |
53
|
5
|
|
|
5
|
1
|
5005
|
my ( $self, $email ) = @_; |
54
|
5
|
|
|
|
|
8
|
for my $rule ( @{ $self->{rule_data} } ) { |
|
5
|
|
|
|
|
22
|
|
55
|
9
|
|
|
|
|
12
|
for my $method ( @{ $rule->{methods} } ) { |
|
9
|
|
|
|
|
21
|
|
56
|
9
|
|
|
|
|
14
|
my $substring = $rule->{substring}; |
57
|
9
|
100
|
|
|
|
58
|
next unless $email->can($method); |
58
|
7
|
|
|
|
|
26
|
my $testing = $email->$method; |
59
|
7
|
100
|
|
|
|
231
|
next unless $testing; |
60
|
5
|
100
|
|
|
|
178
|
warn "testing $method \"$testing\" with $substring\n" |
61
|
|
|
|
|
|
|
if $self->{debug}; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# going to keep the 'i' now, may pass this in somehow |
64
|
5
|
100
|
|
|
|
141
|
return $rule->{folder} if $testing =~ /$substring/is; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
} |
67
|
2
|
|
|
|
|
13
|
return; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
1; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
__END__ |