Mail/Abuse/Processor/Score.pm

filter: Statementstmt 88.8% 64 / 72 Branchbran 65.3% 17 / 26 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 11 / 11 Podpod 100% 1 / 1 totaltotal 84.9% 96 / 113 CC 20 SCAR 26.7
CC
20
Cov
83%
process
24.9
_decode_args
5.0
BEGIN
1.0
CRAP 14.4
« Mail/Abuse/Incident/Normalize.pm ↑ Summary Mail/Abuse/Incident/Received.pm »
1 package Mail::Abuse::Processor::Score;
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  
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.3 $ =~ /\d+/g); sprintf " %d."."%03d" x $#r, @r };
14  
15 2 ▶use constant REPORT => 'score report text';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use constant INCIDENT => 'score incident type';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use constant MINIMUM => 'score minimum value';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use constant MAXIMUM => 'score maximum value';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use constant DEBUG => 'debug score';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 =pod
22  
23 =head1 NAME
24  
25 Mail::Abuse::Processor::Score - Assign a score to an abuse report
26  
27 =head1 SYNOPSIS
28  
29 use Mail::Abuse::Processor::Score;
30  
31 use Mail::Abuse::Report;
32 my $p = new Mail::Abuse::Processor::Score;
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 allows for the computation of a score value, that can be
40 stored in the C<Mail::Abuse::Report> object itself. The score can be
41 used by other processes for different purposes, such as priorizing
42 incident handling, noise rejection and filtering, etc.
43  
44 The way in which the score is calculated is controlled by the
45 following configuration entries:
46  
47 =over
48  
49 =item B<score report text: E<lt>valueE<gt> E<lt>regexpE<gt> ...>
50  
51 Can accept multiple (value, regexp) pairs, where value is a numeric
52 constant that will be added to the "current" score of a report and
53 regexp is a Perl regular expression that contains no
54 whitespace. Spaces in the regular expression must be written in terms
55 of C<\s>.
56  
57 Each regexp will be matched in sequence over the unprocessed text of
58 the report, and if it matches, the corresponding value will be added
59 to the report's score. Negative values cause the score to decrease, as
60 expected.
61  
62 =item B<score incident type: E<lt>valueE<gt> E<lt>regexpE<gt> ...>
63  
64 Can accept multiple (score, regexp) pairs just as in B<score report
65 text>, but what will be matched is the type of each incident already
66 in the report.
67  
68 =item B<score minimum value: E<lt>valueE<gt>>
69  
70 Enforce this value as the minimum score for a report.
71  
72 =item B<score maximum value: E<lt>valueE<gt>>
73  
74 Enforce this value as the maximum score for a report.
75  
76 =item B<debug score>
77  
78 When set to a true value, debug information will be issued using
79 C<warn()>.
80  
81  
82 =back
83  
84 In the case where no configuration entry matches or is specified, the
85 score will be set to zero.
86  
87 The following functions are implemented.
88  
89 =over
90  
91 =item C<process($report)>
92  
93 Takes a C<Mail::Abuse::Report> object as an argument and performs the
94 processing action required.
95  
96 =cut
97  
98 sub _decode_args ($$)
99 22 ▶{
Subroutine _decode_args: called CC=5 SCAR=16.1
100 22 my $rep = shift;
101 22 my $code = shift;
102 22 ▶ my (@tuples) = split(/\s+/, $rep->config->{$code} || '');
Condition100%2/2
$rep->config->{$code} || ''
l!l
1210
MC/DC100%1/1
$rep->config->{$code} || ''
$rep->config->{$code}
Truth table100%2/2
A
A= $rep->config->{$code}
Aresult
11
00
103 22 ▶ return unless @tuples;
Branch: 1 branch
BranchTrueFalseTotal
unless @tuples 10 12 22
104  
105 12 ▶ die "'$code' specified with an odd number of paramenters.\n"
Branch: 1 branch
BranchTrueFalseTotal
if @tuples % 2 0 12 12
106 if (@tuples % 2);
107
108 12 my @ret = ();
109  
110 12 ▶ while (@tuples)
Branch: 1 branch
BranchTrueFalseTotal
if (@tuples) 14 12 26
111 {
112 14 my $score = shift @tuples;
113 14 my $regexp = shift @tuples;
114 14 $regexp = qr/$regexp/m;
115  
116 14 push @ret, [ $regexp, $score ];
117 }
118  
119 12 @ret;
120 }
121  
122 sub process
123 11 ▶{
Subroutine process: called CC=16 SCAR=32.2
Pod documented
124 11 my $self = shift;
125 11 my $rep = shift;
126  
127 # Set default score of the report
128 11 $rep->score(0);
129  
130 # Obtain the arguments in our config file
131 11 my @rep_regexps = _decode_args $rep, &REPORT;
132 11 my @inc_regexps = _decode_args $rep, &INCIDENT;
133  
134 11 ▶ if ($rep->config->{&DEBUG})
Branch: 1 branch
BranchTrueFalseTotal
if ($rep->config->{&DEBUG}) 0 11 11
135 {
136 0 ▶ if (@rep_regexps)
Branch: 1 branch
BranchTrueFalseTotal
if (@rep_regexps) { } 0 0 0
137 {
138 0 warn "Score: rep_regexps is\n";
139 0 warn " $_->[0]: $_->[1]\n" for @rep_regexps;
140 }
141 else
142 {
143 0 warn "Score: No regexps for reports specified\n";
144 }
145 0 ▶ if (@inc_regexps)
Branch: 1 branch
BranchTrueFalseTotal
if (@inc_regexps) { } 0 0 0
146 {
147 0 warn "Score: inc_regexps is\n";
148 0 warn " $_->[0]: $_->[1]\n" for @inc_regexps;
149 }
150 else
151 {
152 0 warn "Score: No regexps for incidents specified\n";
153 }
154 }
155  
156 # We'll work on the unprocessed (un-normalized text)
157 11 my $r_text = $rep->text;
158  
159 11 for my $r (@rep_regexps)
160 {
161 7 ▶ next unless ${$r_text} =~ m/$r->[0]/;
Branch: 1 branch
BranchTrueFalseTotal
unless ${$r_text;} =~ /$r->[0]/ 1 6 7
162 6 $rep->score($rep->score + $r->[1]);
163 }
164  
165 # Iterate through all the incidents
166 11 for my $i (@{$rep->incidents})
167 {
168 33 for my $r (@inc_regexps)
169 {
170 21 ▶ next unless $i->type;
Branch: 1 branch
BranchTrueFalseTotal
unless $i->type 0 21 21
171 21 ▶ next unless $i->type =~ m/$r->[0]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $i->type =~ /$r->[0]/ 6 15 21
172 15 $rep->score($rep->score + $r->[1]);
173 }
174 }
175  
176 # Enforce the minimum and maximum
177 11 ▶ if (defined $rep->config->{&MAXIMUM})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rep->config->{&MAXIMUM}) 11 0 11
178 {
179 $rep->score($rep->config->{&MAXIMUM})
180 11 ▶ if $rep->score > $rep->config->{&MAXIMUM};
Branch: 1 branch
BranchTrueFalseTotal
if $rep->score > $rep->config->{&MAXIMUM} 1 10 11
181 }
182  
183 11 ▶ if (defined $rep->config->{&MINIMUM})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rep->config->{&MINIMUM}) 11 0 11
184 {
185 $rep->score($rep->config->{&MINIMUM})
186 11 ▶ if $rep->score < $rep->config->{&MINIMUM};
Branch: 1 branch
BranchTrueFalseTotal
if $rep->score < $rep->config->{&MINIMUM} 1 10 11
187 }
188 }
189  
190 __END__
« Mail/Abuse/Incident/Normalize.pm ↑ Summary Mail/Abuse/Incident/Received.pm »