lib/Mail/Decency/Core/Excludes.pm

filter: Statementstmt 21.3% 16 / 75 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 37.5% 3 / 8 Podpod 100% 1 / 1 totaltotal 14.8% 20 / 135 CC 29 SCAR 45.9
CC
29
Cov
13%
__ANON__
210.0
do_exclude
42.0
_get_exclude_from_file
42.0
CRAP 99.0
« lib/Mail/Decency/Policy/Honeypot.pm ↑ Summary lib/Mail/Decency/ContentFilter/Core/Cmd.pm »
1 package Mail::Decency::Core::Excludes;
2  
3 20 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 20 ▶use version 0.74; our $VERSION = qv( "v0.1.4" );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 20 ▶use feature qw/ switch /;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Mail::Decency::Core::Meta::Excludes
12  
13 =head1 DESCRIPTION
14  
15 Excludes module handling per recipient/sender domain/address.
16  
17 Those exlusions can be either defined in the configuration and/or a plain text file and/or the a database.
18  
19 =head1 CONFIG
20  
21 ---
22
23 exclusions:
24
25 modules:
26
27 DNSBL:
28 sender_domain:
29 - sender.tld
30 - somedomain.tld
31 recipient_domain:
32 - recipient.tld
33 - anotherdomain.tld
34 sender_address:
35 - some@sender.tld
36 recipient_address:
37 - bla@recipient.tld
38
39 file: /etc/decency/exclusions.txt
40
41 database: 1
42
43  
44 =head2 EXCLUSION PLAIN TEXT FILE
45  
46 like this:
47  
48 sender_domain:DNSBL:sender.tld
49 sender_domain:DNSBL:somedomain.tld
50 recipient_domain:GeoWeight:recipient.tld
51 recipient_domain:GeoWeight:anotherdomain.tld
52 sender_address:SPF:some@sender.tld
53 recipient_address:SPF:bla@recipient.tld
54  
55 =head2 DATABASE
56  
57 =head1 CLASS ATTRIBUTES
58  
59 =head2 exclude_sender_domain : HashRef[Bool]
60  
61 =cut
62  
63 20 has exclusions => ( is => 'rw', isa => 'HashRef[Bool]', predicate => 'enable_exclusions' );
64  
65 =head2 enable_file : Str
66  
67 =cut
68  
69 20 has exclusion_file => ( is => 'rw', isa => 'Str', predicate => 'enable_exclusion_file' );
70  
71 =head2 enable_database : Bool
72  
73 =cut
74  
75 20 has enable_database => ( is => 'rw', isa => 'Bool', default => 0 );
76  
77 =head2 exclusion_methods : ArrayRef[Str]
78  
79 =cut
80  
81 20 has exclusion_methods => ( is => 'rw', isa => 'ArrayRef[Str]', predicate => 'has_exclusions' );
82  
83 =head1 METHODS
84  
85 =head2 after init
86  
87 =cut
88  
89 0 ▶after init => sub {
Subroutine __ANON__: not called CC=14 SCAR=53.5
90 0 my ( $self ) = @_;
91
92 0 ▶ return unless defined $self->config->{ exclusions };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->config->{"exclusions"} 0 0 0
93 0 my @exclusion_methods = ();
94
95 # having exclusions in configuration
96 0 ▶ if ( defined( my $m_ref = $self->config->{ exclusions }->{ modules } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $m_ref = $self->config->{"exclusions"}{"modules"})) 0 0 0
97 0 $self->exclusions( {} );
98 0 ▶ while ( my ( $module, $t_ref ) = each %$m_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$m_ref) 0 0 0
99 0 ▶ while ( my ( $type, $values_ref ) = each %$t_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$t_ref) 0 0 0
100 0 $self->exclusions->{ "$type:$module:$_" } = 1 for @$values_ref;
101 }
102 }
103 0 push @exclusion_methods, '_get_exclude_from_config';
104 }
105
106 # enable database ..
107 0 ▶ if ( $self->config->{ exclusions }->{ database } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->{"exclusions"}{"database"}) 0 0 0
108 0 ▶ $self->{ schema_definition } ||= {};
Condition0%0/2
$self->{"schema_definition"} ||= {}
l!l
00
MC/DC0%0/1
$self->{"schema_definition"} ||= {}
$self->{"schema_definition"}
Truth table0%0/2
A
A= $self->{"schema_definition"}
Aresult
11
00
109 $self->{ schema_definition }->{ exclusions } = {
110 0 $self->name => {
111 module => [ varchar => 32 ],
112 type => [ varchar => 20 ],
113 value => [ varchar => 255 ],
114 -unique => [ qw/ module type value / ]
115 },
116 };
117 0 $self->enable_database( 1 );
118 0 push @exclusion_methods, '_get_exclude_from_database';
119 }
120
121 # having a plaintext file ..
122 0 ▶ if ( defined( my $file = $self->config->{ exclusions }->{ file } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $file = $self->config->{"exclusions"}{"file"})) 0 0 0
123 0 ▶ $file = $self->config_dir. "/$file"
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file or $file =~ m[^/] 0 0 0
Condition0%0/3
-f $file or $file =~ m[^/]
l!l&&r!l&&!r
000
MC/DC0%0/2
-f $file or $file =~ m[^/]
-f $file $file =~ m[^/]
Truth table0%0/3
A or B
A= -f $fileB= $file =~ m[^/]
ABresult
1X1
011
000
124 if ! -f $file && $file !~ /^\//;
125 0 ▶ die "Exclusion file '$file' does not exist or not accessable\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
126 unless -f $file;
127 0 ▶ die "Exclusion file '$file' not readable\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -r $file 0 0 0
128 unless -r $file;
129 0 ▶ open my $fh, '<', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "Error opening exclusion file '${file}': $@" 0 0 0
130 or die "Error opening exclusion file '$file': $@";
131 0 close $fh;
132 0 $self->exclusion_file( $file );
133 0 push @exclusion_methods, '_get_exclude_from_file';
134 }
135
136 0 $self->exclusion_methods( \@exclusion_methods );
137
138 20 };
139  
140 =head2 do_exclude
141  
142 Returns bool wheter the current mail (session) shall overstep the current module
143  
144 =cut
145  
146 0 ▶sub do_exclude {
Subroutine do_exclude: not called CC=6 SCAR=37.4
Pod documented
147 0 my ( $self, $module ) = @_;
148
149 0 ▶ return unless $self->has_exclusions;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_exclusions 0 0 0
150
151 0 my $session = $self->session_data;
152
153 0 my @check = (
154 [
155 "recipient_address:$module:". lc( $session->to ),
156 [ "recipient_address", "$module", lc( $session->to ) ]
157 ],
158 [
159 "recipient_domain:$module:". lc( $session->to_domain ),
160 [ "recipient_domain", "$module", lc( $session->to_domain ) ]
161 ],
162 [
163 "sender_address:$module:". lc( $session->from ),
164 [ "sender_address", "$module", lc( $session->from ) ]
165 ],
166 [
167 "sender_domain:$module:". lc( $session->from_domain ),
168 [ "sender_domain", "$module", lc( $session->from_domain ) ]
169 ],
170 );
171
172 0 foreach my $check( @check ) {
173 0 ▶ return 1 if $self->cache->get( $check );
Branch: 1 branch
BranchTrueFalseTotal
if $self->cache->get($check) 0 0 0
174 }
175
176 0 foreach my $check( @{ $self->exclusion_methods } ) {
177 0 my ( $ok, $cache ) = $self->$check( \@check );
178 0 ▶ if ( $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 0 0 0
179 0 $self->cache->set( $cache => 1 );
180 0 return 1;
181 }
182 }
183 }
184  
185 =head2 _get_exclude_from_config
186  
187 =cut
188  
189 0 ▶sub _get_exclude_from_config {
Subroutine _get_exclude_from_config: not called CC=3 SCAR=24.8
190 0 my ( $self, $check_ref ) = @_;
191
192 0 foreach my $check( @$check_ref ) {
193 0 ▶ if ( defined $self->exclusions->{ $check->[0] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->exclusions->{$check->[0]}) 0 0 0
194 0 return ( 1, $check->[0] );
195 }
196 }
197
198 0 return;
199 }
200  
201 =head2 _get_exclude_from_file
202  
203 =cut
204  
205 0 ▶sub _get_exclude_from_file {
Subroutine _get_exclude_from_file: not called CC=6 SCAR=37.4
206 0 my ( $self, $check_ref ) = @_;
207
208 0 my ( $ok, $cache_name );
209 0 eval {
210 0 ▶ open my $fh, '<', $self->exclusion_file or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $self->exclusion_file or die $! 0 0 0
211
212 CHECK_LINE:
213 0 ▶ while ( my $l = <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $l = readline $fh)) 0 0 0
214 0 chomp $l;
215 0 foreach my $check( @$check_ref ) {
216 0 ▶ if ( $check->[0] eq $l ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check->[0] eq $l) 0 0 0
217 0 $ok++;
218 0 $cache_name = $check->[0];
219 0 last CHECK_LINE;
220 }
221 }
222 }
223
224 0 close $fh;
225 };
226 0 ▶ $self->logger->error( "Error reading from exclusion file: $@" ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
227
228 0 return ( $ok, $cache_name );
229 }
230  
231 =head2 _get_exclude_from_database
232  
233 =cut
234  
235 0 ▶sub _get_exclude_from_database {
Subroutine _get_exclude_from_database: not called CC=4 SCAR=30.0
236 0 my ( $self, $check_ref ) = @_;
237
238 0 foreach my $check( @$check_ref ) {
239 0 my $db_ref = $self->database->get( exclusions => $self->name => {
240 type => $check->[1]->[0],
241 module => $check->[1]->[1],
242 value => $check->[1]->[2],
243 } );
244 0 ▶ return ( 1, $check->[0] ) if $db_ref && $db_ref->{ value } eq $check->[1]->[2];
Branch: 1 branch
BranchTrueFalseTotal
if $db_ref and $db_ref->{"value"} eq $check->[1][2] 0 0 0
Condition0%0/3
$db_ref and $db_ref->{"value"} eq $check->[1][2]
!ll&&!rl&&r
000
MC/DC0%0/2
$db_ref and $db_ref->{"value"} eq $check->[1][2]
$db_ref $db_ref->{"value"} eq $check->[1][2]
Truth table0%0/3
A and B
A= $db_refB= $db_ref->{"value"} eq $check->[1][2]
ABresult
0X0
100
111
245 }
246
247 0 return;
248 }
249  
250 =head1 AUTHOR
251  
252 Ulrich Kautz <uk@fortrabbit.de>
253  
254 =head1 COPYRIGHT
255  
256 Copyright (c) 2010 the L</AUTHOR> as listed above
257  
258 =head1 LICENCSE
259  
260 This library is free software and may be distributed under the same terms as perl itself.
261  
262 =cut
263  
264  
265 20 1;
« lib/Mail/Decency/Policy/Honeypot.pm ↑ Summary lib/Mail/Decency/ContentFilter/Core/Cmd.pm »