Mail/Abuse/Report.pm

filter: Statementstmt 97.0% 98 / 101 Branchbran 62.0% 31 / 50 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 14 / 14 Podpod 75.0% 3 / 4 totaltotal 84.0% 148 / 176 CC 32 SCAR 22.2
CC
32
Cov
84%
next
18.4
load_config
7.4
new
5.6
CRAP 9.3
« Mail/Abuse/Incident/SpamCop.pm ↑ Summary Mail/Abuse/Processor/TableDBI.pm »
1 package Mail::Abuse::Report;
2  
3 23 require 5.005_62;
4  
5 23 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 23 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 # use Config::Auto;
10 23 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 # The code below should be in a single line
12  
13 23 our $VERSION = do { my @r = (q$Revision: 1.9 $ =~ /\d+/g); sprintf " %d."."%03d" x $#r, @r };
14  
15 # Keys that we keep after a flush
16 23 our @Keep = qw/
17 text
18 debug
19 config
20 reader
21 filters
22 parsers
23 processors
24 /;
25  
26 =pod
27  
28 =head1 NAME
29  
30 Mail::Abuse::Report - Process an abuse report
31  
32 =head1 SYNOPSIS
33  
34 use Mail::Abuse::Report;
35  
36 my $r = new Mail::Abuse::Report
37 (
38 -text => \$report_text,
39 -reader => $reader,
40 -filters => [ $f1, $f2, ... ],
41 -parsers => [ $i1, $i2, ... ],
42 -processors => [ $p1, $p2, ... ],
43 -debug => 1,
44 -config => $config_file,
45 );
46  
47 =head1 DESCRIPTION
48  
49 This class encapsulates an abuse report and provides methods to
50 automate tasks such as extracting individual incidents from it, filtering the incidents, etc.
51  
52 Each of the methods are described in detail, below:
53  
54 =over
55  
56 =item C<-E<gt>new(%args)>
57  
58 Creates a new C<Mail::Abuse::Report> object. It accepts the following
59 arguments:
60  
61 =over
62  
63 =item C<-text>
64  
65 Specifies the text that will be used to fill the report with. This is
66 incompatible with C<-reader>, so choose one and stick to it. Defaults
67 to C<undef>.
68  
69 =item C<-reader>
70  
71 Specifies the object (tipically a member of C<Mail::Abuse::Reader>)
72 that will be used to fetch the text of the next report. This is
73 incompatible with C<-text>, so pick one and stick to it. Defaults to
74 C<undef>.
75  
76 =item C<-filters>
77  
78 A reference to a list of objects that can filter incidents. Normally,
79 objects based on C<Mail::Abuse::Filter>. Defaults to no filters.
80  
81 =item C<-parsers>
82  
83 A reference to a list of objects that can parse incidents out of the
84 report text. Normally, objects based on
85 C<Mail::Abuse::Incident>. Defaults to no parsers.
86  
87 =item C<-processors>
88  
89 A reference to a list of objects that can process the incidents on the
90 report, normally objects based on the C<Mail::Abuse::Processor>
91 class. Defaults to no processors.
92  
93 =item C<-debug>
94  
95 A true value causes diagnostic messages to be sent via C<warn()>.
96  
97 =item C<-config>
98  
99 Specifies the name of the config file to fetch configuration items
100 from. Can be left unspecified.
101  
102 =back
103  
104 =cut
105  
106 sub new
107 42 ▶{
Subroutine new: called CC=5 SCAR=17.2
Pod documented
108 42 my $type = shift;
109 42 ▶ my $class = ref($type) || $type || 'Mail::Abuse::Report';
Condition40%2/5
ref $type || $type || 'Mail::Abuse::Report'
l!l
420
ref $type or $type
l!l&&r!l&&!r
0420
MC/DC0%0/2
ref $type || $type || 'Mail::Abuse::Report'
ref $type $type
Truth table33%1/3
A or B
A= ref $typeB= $type
ABresult
1X1
011
000
110  
111 42 ▶ croak "Invalid call to Mail::Abuse::Report::new"
Branch: 1 branch
BranchTrueFalseTotal
unless $class 0 42 42
112 unless $class;
113  
114 my %self = validate_with
115 (
116 params => \@_,
117 ignore_case => 1,
118 strip_leading => '-',
119 spec =>
120 {
121 text =>
122 {
123 type => SCALARREF,
124 default => undef,
125 },
126
127 reader =>
128 {
129 type => OBJECT,
130 can => [ qw(read) ],
131 default => undef,
132 },
133  
134 filters =>
135 {
136 type => ARRAYREF,
137 default => [],
138 },
139  
140 parsers =>
141 {
142 type => ARRAYREF,
143 default => [],
144 },
145  
146 processors =>
147 {
148 type => ARRAYREF,
149 default => [],
150 },
151  
152 debug =>
153 {
154 type => SCALAR,
155 default => 0,
156 },
157  
158 config =>
159 {
160 type => SCALAR,
161 default => undef,
162 callbacks =>
163 {
164 'config file must be readable' => sub
165 42 ▶ {
Subroutine __ANON__: called CC=2 SCAR=7.6
166 42 ▶ defined $_[0] and -f $_[0];
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and -f $_[0] 42 0 42
167 },
168 },
169 },
170 },
171 42 );
172  
173 41 my $self = \%self;
174  
175 41 bless $self, $class;
176  
177 41 ▶ $self->load_config or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->load_config or return 41 0 41
178  
179 41 $self->incidents([]);
180  
181 41 return $self;
182  
183 }
184  
185 sub load_config
186 41 ▶{
Subroutine load_config: called CC=7 SCAR=20.1
Pod undocumented
187 41 my $self = shift;
188 41 my $config = $self->config;
189 41 ▶ warn "M::A::Report: reading config" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 41 41
190 # eval { $self->config(Config::Auto::parse($config, format => 'colon')) };
191 41 $self->config({});
192  
193 41 my $fh = new IO::File $config;
194
195 41 ▶ unless ($fh)
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 41 41
196 {
197 0 warn "M::A::Report: Failed to open $config: $!\n";
198 0 return;
199 }
200  
201 41 ▶ while (<$fh>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 142 41 183
202 {
203 142 chomp;
204 142 s/\#.*$//g;
205 142 ▶ next unless /^([^:]+):\s*(.*)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^([^:]+):\s*(.*)$/ 60 82 142
206 82 $self->config->{lc $1} = $2;
207 }
208
209 41 $fh->close;
210  
211 41 ▶ warn "Config read: $@\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 41 41
212 41 ▶ return if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 41 41
213 41 return $self;
214 }
215  
216 =pod
217  
218 =item C<-E<gt>next()>
219  
220 When the object has a reader, fetches the next report text, parses it
221 with the incidents and filters the incidents and processes them with the
222 supplied processors.
223  
224 Returns the report object if succesful or false otherwise.
225  
226 If no reader has been supplied to the report object, the same text
227 will be analyzed over and over again.
228  
229 =cut
230  
231 sub next
232 74 ▶{
Subroutine next: called CC=16 SCAR=29.1
Pod documented
233 74 my $self = shift;
234  
235 74 $self->flush;
236
237 74 ▶ if ($self->reader)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->reader) 68 6 74
238 {
239 68 ▶ warn "Reading from reader object\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 68 68
240 68 ▶ return unless $self->reader->read($self);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->reader->read($self) 0 68 68
241 }
242  
243 74 ▶ return unless $self->text;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->text 0 74 74
244  
245 74 for my $i (@{$self->parsers})
246 {
247 80 ▶ warn "Parsing with parser $i\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 80 80
248 80 my @incidents = $i->parse($self);
249 80 ▶ warn scalar @incidents, " incidents found\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 80 80
250 80 ▶ next unless @incidents;
Branch: 1 branch
BranchTrueFalseTotal
unless @incidents 24 56 80
251  
252 0 ▶ warn "init incidents: ", join(',', map { ref $_ } @incidents), "\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 56 56
253 if $self->debug;
254
255 56 for my $f (@{$self->filters})
256 {
257 21 ▶ warn "Filtering with filter $f\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 21 21
258 21 @incidents = grep { $f->criteria($self, $_) } @incidents;
259 21 ▶ warn scalar @incidents, " incidents left\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 21 21
260 }
261
262 56 ▶ warn scalar @incidents, " final incidents left\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 56 56
263 56 push @{$self->incidents}, @incidents;
264 }
265  
266 # At this point, the incidents are
267 # properly registered within the
268 # object, so we can safely process
269 # them
270  
271 74 for my $p (@{$self->processors})
272 {
273 43 ▶ warn "Processing with $p\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 43 43
274 43 $p->process($self);
275 }
276  
277 74 return $self;
278 }
279  
280 =pod
281  
282 =item C<-E<gt>flush()>
283  
284 Discards all non-essential information from the object. It is meant to
285 be called before reusing the object to process a new report.
286  
287 This method is automatically called by C<-E<gt>next>.
288  
289 =cut
290  
291 sub flush
292 76 ▶{
Subroutine flush: called CC=5 SCAR=16.2
Pod documented
293 76 my $self = shift;
294  
295 76 ▶ warn "M::A::Report->flush" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 76 76
296  
297 76 for my $k (keys %$self)
298 {
299 617 ▶ next if grep { $k eq $_ } @Keep;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$k eq $_;} @Mail::Abuse::Report::Keep 476 141 617
300 141 ▶ warn "flush key $k\n" if $self->debug;;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 141 141
301 141 delete $self->{$k};
302 }
303  
304 76 $self->incidents([]);
305  
306 76 return $self;
307 }
308  
309 =pod
310  
311 =back
312  
313 Also, a number of acccessor methods are defined as follows:
314  
315 =over
316  
317 =item C<-E<gt>filters>
318  
319 When invoked without arguments, returns a reference to the list of
320 filters (C<Mail::Abuse::Filter> objects) attached to this abuse
321 report. Calling the accessor with a list of objects will replace
322 whatever was the prior list.
323  
324 The list can be manipulated, affecting the object directly.
325  
326 =item C<-E<gt>incidents>
327  
328 When invoked without arguments, returns a reference to the list of
329 incidents extracted from this report (C<Mail::Abuse::Incident>
330 objects). Calling the accessor with a reference to a list of objects
331 will replace whatever was the prior list.
332  
333 The list can be manipulated, affecting the object directly.
334  
335 =item C<-E<gt>parsers>
336  
337 When invoked without arguments, returns a reference to the list of
338 parsers that are used in this report (C<Mail::Abuse::Incident>
339 objects). Calling the accessor with a reference to a list of objects
340 will replace whatever was the prior list.
341  
342 The list can be manipulated, affecting the object directly.
343  
344 =item C<-E<gt>processors>
345  
346 When invoked without arguments, returns a reference to the list of
347 processors attached to this report (C<Mail::Abuse::Processor>
348 objects). Calling the accessor with a reference to a list of objects
349 will replace whatever was the prior list.
350  
351 The list can be manipulated, affecting the object directly.
352  
353 =item C<-E<gt>text>
354  
355 Accepts a reference to a scalar containing the text of the report.
356  
357 Returns a reference to the text of the report.
358  
359 If the text is altered, you should call C<-E<gt>flush()> to avoid
360 insanity.
361  
362 =item C<-E<gt>reader>
363  
364 If passed a reader object, it will replace the one used for
365 initialization. Otherwise, will return the reader object passed to
366 C<-E<gt>new>.
367  
368 =item C<-E<gt>config>
369  
370 Returns a reference to a hash containing the configuration information
371 read-in by this report. It can be replaced by simply supplying a new
372 reference to a configuration hash.
373  
374 =item C<-E<gt>debug>
375  
376 Returns the debug level of the object. When set to true, debug
377 information is issued through C<warn>.
378  
379 =cut
380  
381 sub AUTOLOAD
382 165 ▶{
Subroutine AUTOLOAD: called CC=1 SCAR=0.0
383 23 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
384 23 ▶ use vars qw($AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
385 165 my $method = $AUTOLOAD;
386 165 $method =~ s/^.*:://;
387 *$method = sub
388 2938 ▶ {
Subroutine __ANON__: called CC=2 SCAR=6.9
389 2938 my $self = shift;
390 2938 my $ret = $self->{$method};
391 2938 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 403 2535 2938
392 {
393 403 $ret = $self->{$method};
394 403 $self->{$method} = shift;
395 }
396 2938 return $ret;
397 165 };
398 165 goto \&$method;
399 }
400  
401 23 1;
402  
403 __END__
« Mail/Abuse/Incident/SpamCop.pm ↑ Summary Mail/Abuse/Processor/TableDBI.pm »