Mail/Abuse/Processor/ArchiveDBI.pm

filter: Statementstmt 83.6% 123 / 147 Branchbran 47.8% 22 / 46 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 16 / 16 Podpod 100% 1 / 1 totaltotal 77.0% 168 / 218 CC 34 SCAR 29.2
CC
34
Cov
75%
process
35.5
_value
17.9
_decode_columns
4.0
CRAP 18.5
« Template/Plugin/Abuse.pm ↑ Summary Mail/Abuse/Incident/Normalize.pm »
1 package Mail::Abuse::Processor::ArchiveDBI;
2  
3 2 require 5.005_62;
4  
5 2 ▶use DBI;
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  
9 2 ▶use base 'Mail::Abuse::Processor';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # The code below should be in a single line
12  
13 2 our $VERSION = do { my @r = (q$Revision: 1.6 $ =~ /\d+/g); sprintf " %d."."%03d" x $#r, @r };
14  
15 2 ▶use constant REPORT_DSN => 'archive dsn';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use constant REPORT_USER => 'archive user';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use constant REPORT_PASSWORD => 'archive password';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use constant REPORT_COLS => 'archive reports columns';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use constant INCIDENT_COLS => 'archive incident columns';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use constant INCIDENT_FK => 'archive incident foreign key';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use constant DEBUG => 'debug archive';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 2 my $sql_ireport = ';'; # Report insertion SQL
24 2 my $sql_iincident = ';'; # Incident insertion SQL
25  
26 2 my $dbh; # Our database handler
27 my $sth_ireport; # Report insertion statement
28 2 my $sth_iincident; # Incident insertion statement
29  
30 2 my $rep_cols;
31 2 my $inc_cols;
32 2 my $inc_fks;
33  
34 2 my @cols_rep = ();
35 2 my @cols_inc = ();
36  
37 =pod
38  
39 =head1 NAME
40  
41 Mail::Abuse::Processor::ArchiveDBI - Assign a score to an abuse report
42  
43 =head1 SYNOPSIS
44  
45 use Mail::Abuse::Processor::ArchiveDBI;
46  
47 use Mail::Abuse::Report;
48 my $p = new Mail::Abuse::Processor::ArchiveDBI;
49 my $report = new Mail::Abuse::Report (processors => [ $p ]);
50  
51 # ... other pieces of code that configure the report ...
52  
53 =head1 DESCRIPTION
54  
55 This class allows for the storage of index information from a
56 C<Mail::Abuse::Report> object and the C<Mail::Abuse::Incident> objects
57 it contains. The storage is done into any backend properly supported
58 by DBI.
59  
60 B<This module can only be used once in the pipeline.> The DBI handle
61 used is kept in a package-local variable. This is not a problem,
62 usually. The database connection is disconnected upon object
63 destruction.
64  
65 The following configuration entries control the behavior of this
66 module:
67  
68 =over
69  
70 =item B<archive dsn>
71  
72 Specifies the DSN to be used to connect to the DBI datasource. See
73 L<DBI> for information about its format for using different backends.
74  
75 =item B<archive user>
76  
77 The username required for connection to the DBI datasource.
78  
79 =item B<archive password>
80  
81 The password required for connection to the DBI datasource.
82  
83 =item B<archive reports columns>
84  
85 Define which data elements from a C<Mail::Abuse::Report> object will
86 be stored as the columns of each row in the database. The elements are
87 specified as E<lt>columnE<gt>:E<lt>methodE<gt>, where B<column> is
88 the database column name used to denote said element and B<method> is
89 the accessor in the C<Mail::Abuse::Report> object.
90  
91 Multiple elemnts referring to the same column can be specified, so as
92 to provide alternative means of accessing the data. The methods are
93 accessed as depicted for the option -m for L<abuso>.
94  
95 Usually, you will want "store_file" (created by
96 L<Mail::Abuse::Processor::Store>) to be used as the primary key, and
97 "score" as additional data, assuming that you use
98 C<::Processor::Store> and C<::Processor::Score> in your local
99 configuration.
100  
101 There are special "pseudo-elements" that can be also used on the
102 right-hand side. Those are:
103  
104 =over
105  
106 =item C<$num>
107  
108 The number of incidents left within this C<Mail::Abuse::Report>
109 object.
110  
111 =item C<$time>
112  
113 The current value of the C<time()> function.
114  
115 =item C<$ENV{...}>
116  
117 The current value of the corresponding environment variable, which may
118 also be undef.
119  
120 =back
121  
122 =item B<archive incident columns>
123  
124 Define the data elements from each C<Mail::Abuse::Incident> that will
125 be stored in the DBI backend. This follows the same conventions and
126 syntax as B<archive report columns>.
127  
128 The same "pseudo-elements" are supported, however the value of C<$num>
129 is the current index of the incident within the abuse report.
130  
131 The typical value for this would include the elements "ip", "time" and
132 "type". The primary key for this table, typically will be "store_file"
133 and "$num".
134  
135 =item B<archive incidents foreign key>
136  
137 Since there is a one-to-many relationship between the report table and
138 the incident table, this configuration directive allows for the
139 specification of the columns that are used as the primary key of the
140 reports table.
141  
142 Normally, you will want this to be "store_file", which serves as a
143 unique identifier for the report.
144  
145 =item B<debug archive>
146  
147 When set to a true value, debug information will be issued using
148 C<warn()>.
149  
150 =back
151  
152 The following functions are implemented.
153  
154 =over
155  
156 =item C<process($report)>
157  
158 Takes a C<Mail::Abuse::Report> object as an argument and performs the
159 processing action required.
160  
161 =cut
162  
163 1 ▶sub DESTROY { $dbh and $dbh->disconnect(); }
Subroutine DESTROY: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$dbh and $dbh->disconnect 1 0 1
164  
165 sub _decode_columns ($$)
166 2 ▶{
Subroutine _decode_columns: called CC=4 SCAR=13.9
167 2 my $rep = shift;
168 2 my $key = shift;
169  
170 2 my %cols = ();
171  
172 2 for my $spec (split(/[,\s]+/, $rep->config->{$key}))
173 {
174 9 my ($col, $element) = split(/:/, $spec, 2);
175 9 ▶ my $r_l = $cols{$col} || [];
Condition100%2/2
$cols{$col} || []
l!l
18
MC/DC100%1/1
$cols{$col} || []
$cols{$col}
Truth table100%2/2
A
A= $cols{$col}
Aresult
11
00
176 9 ▶ push @$r_l, $element unless grep { $_ eq $element } @$r_l;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $element;} @$r_l 9 0 9
177 9 $cols{$col} = $r_l;
178 }
179  
180 2 return \%cols;
181 }
182  
183 # Obtain a value from a report or incident, in short circuit
184 # (ie, the first element to match, wins)
185 sub _value ($$$)
186 16 ▶{
Subroutine _value: called CC=13 SCAR=28.9
187 16 my $obj = shift;
188 16 my $r_spec = shift;
189 16 my $r_dollars = shift;
190  
191 # warn "# _value from $obj with ", join(', ', @$r_spec), "\n";
192  
193 16 my $ret = undef;
194 16 for my $spec (@$r_spec)
195 {
196 17 ▶ if ($spec =~ /^\$(\w+)$/)
Branch: 2 branches
BranchTrueFalseTotal
if ($spec =~ /^\$(\w+)$/) { } 5 12 17
elsif ($spec =~ /^\$ENV{([^}]+)}$/) { } 0 12 12
197 {
198 5 ▶ return $r_dollars->{$1} if exists $r_dollars->{$1};
Branch: 1 branch
BranchTrueFalseTotal
if exists $r_dollars->{$1} 5 0 5
199 }
200 elsif ($spec =~ m/^\$ENV{([^}]+)}$/)
201 {
202 0 return $ENV{$1};
203 }
204  
205 12 my @things = split /\./, $spec;
206
207 12 my $r = $obj;
208 12 my @own = @things;
209  
210 12 ▶ while (defined $r and my $c = shift @own)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $r and my $c = shift @own) 12 11 23
Condition66%2/3
defined $r and my $c = shift @own
!ll&&!rl&&r
01112
MC/DC50%1/2
defined $r and my $c = shift @own
defined $r my $c = shift @own
Truth table66%2/3
A and B
A= defined $rB= my $c = shift @own
ABresult
0X0
100
111
211 {
212 12 ▶ if ($c =~ /^\d+$/)
Branch: 2 branches
BranchTrueFalseTotal
if ($c =~ /^\d+$/) { } 0 12 12
elsif ($c) { } 12 0 12
213 {
214 0 ▶ unless (ref $r eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $r eq "ARRAY") 0 0 0
215 {
216 0 warn "ArchiveDBI: Invalid type for $spec\n";
217 0 undef $r;
218 0 last;
219 }
220 0 $r = $r->[$c];
221 }
222 elsif ($c)
223 {
224 12 ▶ if (ref $r eq 'HASH')
Branch: 2 branches
BranchTrueFalseTotal
if (ref $r eq 'HASH') { } 0 12 12
elsif (eval { do { defined $r->$c } }) { } 11 1 12
225 {
226 0 $r = $r->{$c};
227 }
228 12 elsif (eval { defined $r->$c })
229 {
230 2 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
231 11 $r = $r->$c;
232 }
233 else
234 {
235 # warn "ArchiveDBI: Invalid type for $spec\n";
236 1 undef $r;
237 1 last;
238 }
239 }
240 }
241 12 ▶ return $r if defined $r;
Branch: 1 branch
BranchTrueFalseTotal
if defined $r 11 1 12
242 }
243 0 return;
244 }
245  
246 sub process
247 1 ▶{
Subroutine process: called CC=18 SCAR=35.7
Pod documented
248 1 my $self = shift;
249 1 my $rep = shift;
250  
251 # Init the database connection if not already done
252 1 ▶ unless ($dbh)
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbh) 1 0 1
253 {
254 1 my $DSN = $rep->config->{&REPORT_DSN};
255 1 my $LOGIN = $rep->config->{&REPORT_USER};
256 1 my $PASSWORD = $rep->config->{&REPORT_PASSWORD};
257  
258 # Database connection using the configured parameters
259 1 $dbh = DBI->connect($DSN, $LOGIN, $PASSWORD,
260 {
261 AutoCommit => 1,
262 RaiseError => 0,
263 PrintError => 1,
264 },
265 );
266
267 # Obtain the columns and elements we will be storing into the
268 # database for each report.
269  
270 1 $rep_cols = _decode_columns $rep, REPORT_COLS;
271 1 $inc_cols = _decode_columns $rep, INCIDENT_COLS;
272 1 $inc_fks = [ split(/[,\s]+/, $rep->config->{&INCIDENT_FK}) ];
273  
274 1 ▶ if ($rep->config->{&DEBUG})
Branch: 1 branch
BranchTrueFalseTotal
if ($rep->config->{&DEBUG}) 0 1 1
275 {
276 0 warn "# Report columns:\n";
277 0 ▶ while (my ($c, $r_e) = each %$rep_cols)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$rep_cols) 0 0 0
278 {
279 0 warn "# $c -> [ ", join(',', @$r_e), " ]\n";
280 }
281 0 warn "# Incident FKs:\n";
282 0 warn "# ", join(', ', @$inc_fks), "\n";
283 0 warn "# Incident columns:\n";
284 0 ▶ while (my ($c, $r_e) = each %$inc_cols)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$inc_cols) 0 0 0
285 {
286 0 warn "# $c -> [ ", join(',', @$r_e), " ]\n";
287 }
288 }
289  
290 # Create the SQL statements that reflect this configuration.
291  
292 1 @cols_inc = (@$inc_fks, sort keys %$inc_cols);
293 1 @cols_rep = (sort keys %$rep_cols);
294
295 1 ▶ if (@cols_inc)
Branch: 1 branch
BranchTrueFalseTotal
if (@cols_inc) 1 0 1
296 {
297 1 $sql_iincident = 'INSERT INTO Incidents (';
298 1 $sql_iincident .= join(', ', @cols_inc);
299 1 $sql_iincident .= ') VALUES (';
300 1 $sql_iincident .= join ', ', split //,
301 '?' x @cols_inc;
302 1 $sql_iincident .= ')';
303 }
304
305 1 ▶ if (@cols_rep)
Branch: 1 branch
BranchTrueFalseTotal
if (@cols_rep) 1 0 1
306 {
307 1 $sql_ireport = 'INSERT INTO Reports (';
308 1 $sql_ireport .= join(', ', @cols_rep);
309 1 $sql_ireport .= ') VALUES (';
310 1 $sql_ireport .= join ', ', split //,
311 '?' x @cols_rep;
312 1 $sql_ireport .= ')';
313 }
314  
315 1 ▶ if ($rep->config->{&DEBUG})
Branch: 1 branch
BranchTrueFalseTotal
if ($rep->config->{&DEBUG}) 0 1 1
316 {
317 0 warn "# Report SQL statement:\n";
318 0 warn "# I: $sql_ireport\n";
319 0 warn "# Incident SQL statement:\n";
320 0 warn "# I: $sql_iincident\n";
321 }
322  
323 # Prepare the SQL statements that insert the requested
324 # information into the database
325  
326 1 $sth_ireport = $dbh->prepare($sql_ireport);
327 1 $sth_iincident = $dbh->prepare($sql_iincident);
328 }
329  
330 # Find out the information about this report that we will send to the
331 # database
332  
333 1 my %rep_values = (); # Hash where the values for each key
334 # will be stored...
335  
336 $rep_values{$_} = _value $rep,
337 $rep_cols->{$_},
338 { time => time,
339 1 num => scalar @{$rep->incidents}} for @cols_rep;
340  
341 1 ▶ if ($rep->config->{&DEBUG})
Branch: 1 branch
BranchTrueFalseTotal
if ($rep->config->{&DEBUG}) 0 1 1
342 {
343 0 warn "# Values for this report:\n";
344 warn "# $_ = " .
345 (defined($rep_values{$_}) ? $rep_values{$_} : 'UNDEF') . "\n"
346 0 ▶ for @cols_rep;
Branch: 1 branch
BranchTrueFalseTotal
defined $rep_values{$_} ? : 0 0 0
347 }
348  
349 # Perform the insertion of the report information to the database
350  
351 1 $sth_ireport->execute(map { $rep_values{$_} } @cols_rep);
352  
353 # Iterate over the incidents to process each one in turn
354 1 my $num = 0;
355  
356 1 for my $i (@{$rep->incidents})
357 {
358 # Insert the incident information in the database
359 3 my %inc_values = ();
360  
361 3 $inc_values{$_} = $rep_values{$_} for @$inc_fks;
362 $inc_values{$_} = _value $i, $inc_cols->{$_},
363 3 { time => time, num => $num } for keys %$inc_cols;
364  
365 3 ▶ if ($rep->config->{&DEBUG})
Branch: 1 branch
BranchTrueFalseTotal
if ($rep->config->{&DEBUG}) 0 3 3
366 {
367 0 warn "# Values for incident $num:\n";
368 warn "# $_ = " .
369 (defined($inc_values{$_}) ? $inc_values{$_} : 'UNDEF') . "\n"
370 0 ▶ for @cols_inc;
Branch: 1 branch
BranchTrueFalseTotal
defined $inc_values{$_} ? : 0 0 0
371 }
372  
373 3 $sth_iincident->execute(map { $inc_values{$_} } @cols_inc);
374 3 $num ++;
375 }
376  
377 }
378  
379 2 "All your base are belong to us";
380  
381 __END__
« Template/Plugin/Abuse.pm ↑ Summary Mail/Abuse/Incident/Normalize.pm »