Mail/Abuse/Processor/Table.pm

filter: Statementstmt 93.7% 75 / 80 Branchbran 59.0% 13 / 22 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 12 / 12 Podpod 100% 1 / 1 totaltotal 82.4% 103 / 125 CC 18 SCAR 20.8
CC
18
Cov
83%
process
13.7
_parse_table
9.4
BEGIN
1.0
CRAP 8.0
« Mail/Abuse/Incident/MyNetWatchman.pm ↑ Summary Mail/Abuse/Reader/Stdin.pm »
1 package Mail::Abuse::Processor::Table;
2  
3 2 require 5.005_62;
4  
5 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use PerlIO::gzip;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use NetAddr::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Tie::NetAddr::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use base 'Mail::Abuse::Processor';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # The code below should be in a single line
16  
17 2 our $VERSION = do { my @r = (q$Revision: 1.2 $ =~ /\d+/g); sprintf " %d."."%03d" x $#r, @r };
18  
19 2 our %Table = ();
20  
21 =pod
22  
23 =head1 NAME
24  
25 Mail::Abuse::Processor::Table - Match incidents to users using a static table
26  
27 =head1 SYNOPSIS
28  
29 use Mail::Abuse::Processor::Table;
30  
31 use Mail::Abuse::Report;
32 my $p = new Mail::Abuse::Processor::Table;
33 my $report = new Mail::Abuse::Report (processors => [ $p ]);
34  
35 # ... other pieces of code that configure the report ...
36  
37 =head1 DESCRIPTION
38  
39 This class matches incidents to
40  
41 =over
42  
43 =item B<debug table>
44  
45 If set to a true value, causes this module to emit debugging info
46 using C<warn()>.
47  
48 =cut
49  
50 2 ▶use constant DEBUG => 'debug table';
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 =pod
53  
54 =item B<table location>
55  
56 The path of a file where the information table is to be found. The
57 file consists on columns separated by whitespace and should have the
58 following format:
59  
60 IP-range var=value;var=value;...
61 IP-range var=value;var=value;...
62 IP-range var1.var2=value;var=value;...
63  
64 B<IP-range> should be an IP subnet in any format that can be
65 understood by C<NetAddr::IP>. Tipically, this should be CIDR location,
66 for readability. The following example:
67  
68 10.0.0.0 foo=bar;baz=camel;fumble.foo=pivot
69  
70 Would yield the following structure as result when a match occurs:
71  
72 { foo => 'bar', baz = 'camel', fumble => { foo = 'pivot' }}
73  
74 These values should not be changed, as currently they are references
75 to the actual data read.
76  
77 On the last column, a number of variables and its values can be
78 specified. Multiple tuples can be separated by a ';' character. The
79 dot in the name can be used in place of the C<-E<gt>> operator, to
80 easily create hashrefs. These hashrefs are stored in the
81 C<Mail::Abuse::Incident> object that is passed to the C<process()>
82 method.
83  
84 Comments are delimited by a '#' character, which causes the text up to
85 the end of line to be ignored.
86  
87 =cut
88  
89 2 ▶use constant LOCATION => 'table location';
Subroutine BEGIN: called CC=1 SCAR=0.0
90  
91 =pod
92  
93 =back
94  
95 The following functions are implemented.
96  
97 =over
98  
99 =item C<process($report)>
100  
101 Takes a C<Mail::Abuse::Report> object as an argument and, for each
102 C<Mail::Abuse::Incident> collected, perform a lookup in the given
103 table, attempting to match it by IP address.
104  
105 If a match is found, all the supplied hashrefs are introduced in the
106 C<Mail::Abuse::Incident> under the key C<table>.
107  
108 =cut
109 ;
110  
111 sub _parse_table ($)
112 1 ▶{
Subroutine _parse_table: called CC=9 SCAR=22.4
113 1 my $loc = shift;
114  
115 1 ▶ return if %Table; # do nothing if this is populated
Branch: 1 branch
BranchTrueFalseTotal
if %Mail::Abuse::Processor::Table::Table 0 1 1
116  
117 1 my $fh = new IO::File;
118  
119 1 ▶ unless ($fh->open($loc, '<:gzip(autopop)'))
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh->open($loc, "<:gzip(autopop)")) 0 1 1
120 {
121 0 die "M::A::P::Table: Failed to open table $loc: $!\n";
122 }
123  
124 1 ▶ while (my $line = <$fh>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 1 1 2
125 {
126 1 chomp $line;
127 1 $line =~ s/^\s+//;
128 1 $line =~ s/#.*$//; # Get rid of comments and ignore blanks
129 1 ▶ next if $line =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 0 1 1
130  
131 1 my ($spec, $data) = split /\s+/, $line, 2;
132
133 1 ▶ next unless $spec = new NetAddr::IP $spec;
Branch: 1 branch
BranchTrueFalseTotal
unless $spec = "NetAddr::IP"->new($spec) 0 1 1
134  
135 1 my $hash = {};
136  
137 1 for my $tuple (split /;/, $data)
138 {
139 4 my $where = $hash;
140 4 my ($var, $val) = split /=/, $tuple, 2;
141 4 my @keys = split /\./, $var;
142 4 my $last = pop @keys;
143 4 for my $item (@keys)
144 {
145 5 ▶ $where->{$item} = {} unless $where->{$item};
Branch: 1 branch
BranchTrueFalseTotal
unless $where->{$item} 5 0 5
146 5 $where = $where->{$item};
147 }
148 4 $where->{$last} = $val;
149 }
150 1 $Table{$spec} = $hash;
151 }
152  
153 1 $fh->close;
154 }
155  
156 sub process
157 1 ▶{
Subroutine process: called CC=10 SCAR=26.2
Pod documented
158 1 my $self = shift;
159 1 my $rep = shift;
160  
161 1 ▶ unless ($rep->config or ref $rep->config ne 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
unless ($rep->config or ref $rep->config ne "HASH") 0 1 1
Condition33%1/3
$rep->config or ref $rep->config ne "HASH"
l!l&&r!l&&!r
100
MC/DC0%0/2
$rep->config or ref $rep->config ne "HASH"
$rep->config ref $rep->config ne "HASH"
Truth table33%1/3
A or B
A= $rep->configB= ref $rep->config ne "HASH"
ABresult
1X1
011
000
162 {
163 0 warn "Invalid or no config";
164 0 return;
165 }
166  
167 1 my $loc = $rep->config->{&LOCATION};
168 1 my $debug = $rep->config->{&DEBUG};
169 1 my $fh;
170  
171 1 ▶ unless ($loc and -f $loc) # Bail out if given garbage detail path
Branch: 1 branch
BranchTrueFalseTotal
unless ($loc and -f $loc) 0 1 1
Condition33%1/3
$loc and -f $loc
!ll&&!rl&&r
001
MC/DC0%0/2
$loc and -f $loc
$loc -f $loc
Truth table33%1/3
A and B
A= $locB= -f $loc
ABresult
0X0
100
111
172 {
173 0 warn "M::A::P::Table: ", &LOCATION,
174 " does not point to a valid file\n";
175 0 return;
176 }
177  
178 1 _parse_table $loc;
179  
180 1 ▶ return if @{$rep->incidents} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @{$rep->incidents;} == 0 0 1 1
181  
182 1 for my $i (@{$rep->incidents})
183 {
184 5 ▶ if (my $result = $Table{$i->ip})
Branch: 1 branch
BranchTrueFalseTotal
if (my $result = $Mail::Abuse::Processor::Table::Table{$i->ip}) 3 2 5
185 { # Match!
186 3 ▶ $i->table({}) unless $i->table;
Branch: 1 branch
BranchTrueFalseTotal
unless $i->table 3 0 3
187 3 $i->table->{$_} = $result->{$_} for keys %$result;
188 }
189 }
190 1 return 1;
191 }
192  
193 2 tie %Table, 'Tie::NetAddr::IP';
194  
195 __END__
« Mail/Abuse/Incident/MyNetWatchman.pm ↑ Summary Mail/Abuse/Reader/Stdin.pm »