Mail/Abuse/Filter/Time.pm

filter: Statementstmt 92.4% 49 / 53 Branchbran 62.5% 20 / 32 Conditioncond 47.6% 10 / 21 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 70.9% 88 / 124 CC 25 SCAR 36.0
CC
25
Cov
75%
criteria
43.7
BEGIN
1.0
BEGIN
1.0
CRAP 36.6
« Mail/Abuse/Processor/Store.pm ↑ Summary Mail/Abuse/Processor/Explain.pm »
1 package Mail::Abuse::Filter::Time;
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 Date::Manip;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use base 'Mail::Abuse::Filter';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # The code below should be in a single line
14  
15 2 our $VERSION = do { my @r = (q$Revision: 1.9 $ =~ /\d+/g); sprintf " %d."."%03d" x $#r, @r };
16  
17 =pod
18  
19 =head1 NAME
20  
21 Mail::Abuse::Filter::Time - Filter incidents according to how old they are
22  
23 =head1 SYNOPSIS
24  
25 use Mail::Abuse::Filter::Time;
26 my $f = new Mail::Abuse::Filter::Time;
27  
28 $report->filter([$f]);
29  
30 =head1 DESCRIPTION
31  
32 Removes those events from a C<Mail::Abuse::Report> that are older than
33 a given threshold, which can be specified in the configuration file
34 for the abuse report.
35  
36 The following configuration keys are recognized:
37  
38 =over
39  
40 =item B<filter before>
41  
42 Incidents older than the specified time will be removed from the
43 report and not considered. The time can be specified as a specific
44 date or as a time delta according to the specifications in
45 L<Date::Manip>.
46  
47 If this is not specified, the default is to ignore incidents that
48 happened more than 96 hours in the past (ie, "96 hours ago").
49  
50 =item B<filter after>
51  
52 Incidents newer than the specified time will be removed from the
53 report and not considered. Normally, this time specification should be
54 specified as a relative date (ie, "in 48 hours" which is the
55 default). This is useful to discard events that occur in the future.
56  
57 =item B<filter local timezone>
58  
59 If specified, assume this timezone for the conversion of the
60 dates. Defaults to UTC.
61  
62 =item B<debug time filter>
63  
64 When set to a true value, causes this module to emit debugging
65 messages via C<warn()>. Of course, defaults to a false value.
66  
67 =back
68  
69 The following methods are implemented in this class.
70  
71 =over
72  
73 =item C<criteria($report, $incident)>
74  
75 This function receives a C<Mail::Abuse::Report> and a
76 C<Mail::Abuse::Incident> object. It returns a true value if the
77 incident should be handled or false otherwise. This function will be
78 generally called by the C<Mail::Abuse::Report> object when requested
79 to filter its events.
80  
81 =cut
82  
83 sub criteria
84 10 ▶{
Subroutine criteria: called CC=25 SCAR=37.8
Pod documented
85 10 my $self = shift;
86 10 my $rep = shift;
87 10 my $inc = shift;
88  
89 # warn "criteria self: ", ref $self, "\n";
90 # warn "criteria rep: ", ref $rep, "\n";
91 # warn "criteria inc: ", ref $inc, "\n";
92  
93 10 ▶ unless ($self->before)
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->before) 2 8 10
94 {
95 2 my $date_before;
96 my $date_after;
97  
98 2 ▶ Date_Init("TZ=" . ($rep->config->{'filter local timezone'} || 'UTC'));
Condition50%1/2
$rep->config->{'filter local timezone'} || 'UTC'
l!l
02
MC/DC0%0/1
$rep->config->{'filter local timezone'} || 'UTC'
$rep->config->{'filter local timezone'}
Truth table50%1/2
A
A= $rep->config->{'filter local timezone'}
Aresult
11
00
99  
100 eval
101 2 {
102 2 ▶ if (ref $rep->config->{'filter before'} eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rep->config->{'filter before'} eq 'ARRAY') { } 0 2 2
103 {
104 $date_before =
105 ParseDate(join(' ',
106 0 @{$rep->config->{'filter before'}}));
107 }
108 else {
109 $date_before =
110 2 ▶ ParseDate($rep->config->{'filter before'}
Condition50%1/2
$rep->config->{'filter before'} || '96 hours ago'
l!l
20
MC/DC0%0/1
$rep->config->{'filter before'} || '96 hours ago'
$rep->config->{'filter before'}
Truth table50%1/2
A
A= $rep->config->{'filter before'}
Aresult
11
00
111 || "96 hours ago");
112 }
113  
114 2 ▶ if (ref $rep->config->{'filter after'} eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rep->config->{'filter after'} eq 'ARRAY') { } 0 2 2
115 {
116 $date_after =
117 ParseDate(join(' ',
118 0 @{$rep->config->{'filter after'}}));
119 }
120 else {
121 $date_after =
122 2 ▶ ParseDate($rep->config->{'filter after'}
Condition50%1/2
$rep->config->{'filter after'} || 'in 48 hours'
l!l
20
MC/DC0%0/1
$rep->config->{'filter after'} || 'in 48 hours'
$rep->config->{'filter after'}
Truth table50%1/2
A
A= $rep->config->{'filter after'}
Aresult
11
00
123 || "in 48 hours");
124 }
125 };
126  
127 2 ▶ warn "Parsing said: $@" if $@ and $rep->config->{'debug time filter'};
Branch: 1 branch
BranchTrueFalseTotal
if $@ and $rep->config->{"debug time filter"} 0 2 2
Condition33%1/3
$@ and $rep->config->{"debug time filter"}
!ll&&!rl&&r
200
MC/DC0%0/2
$@ and $rep->config->{"debug time filter"}
$@ $rep->config->{"debug time filter"}
Truth table33%1/3
A and B
A= $@B= $rep->config->{"debug time filter"}
ABresult
0X0
100
111
128 2 ▶ die "Filter::Time: Cannot parse 'filter before' date\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $date_before 0 2 2
129 unless $date_before;
130 2 ▶ die "Filter::Time: Cannot parse 'filter after' date\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $date_after 0 2 2
131 unless $date_after;
132 2 $self->before(UnixDate($date_before, '%s'));
133 2 $self->after(UnixDate($date_after, '%s'));
134 2 ▶ die "Filter::Time: Times before the epoch are not supported"
Branch: 1 branch
BranchTrueFalseTotal
if $self->before < 0 0 2 2
135 if $self->before < 0;
136
137 warn "Filter::Time: Removing incidents older than ", $self->before,
138 2 ▶ "\n" if $rep->config->{'debug time filter'};
Branch: 1 branch
BranchTrueFalseTotal
if $rep->config->{"debug time filter"} 0 2 2
139 warn "Filter::Time: Removing incidents newer than ", $self->after,
140 2 ▶ "\n" if $rep->config->{'debug time filter'};
Branch: 1 branch
BranchTrueFalseTotal
if $rep->config->{"debug time filter"} 0 2 2
141 }
142  
143 10 ▶ if ($inc->time and $self->before and $inc->time < $self->before)
Branch: 2 branches
BranchTrueFalseTotal
if ($inc->time and $self->before and $inc->time < $self->before) { } 3 7 10
elsif ($inc->time and $self->after and $inc->time > $self->after) { } 4 3 7
Condition50%3/6
$inc->time and $self->before and $inc->time < $self->before
!ll&&!rl&&r
073
$inc->time and $self->before
!ll&&!rl&&r
0010
MC/DC33%1/3
$inc->time and $self->before and $inc->time < $self->before
$inc->time $self->before $inc->time < $self->before
Truth table50%2/4
A and B and C
A= $inc->timeB= $self->beforeC= $inc->time < $self->before
ABCresult
0XX0
10X0
1100
1111
144 {
145 warn "Filter::Time - discard before ", $inc->time, "\n"
146 3 ▶ if $rep->config->{'debug time filter'};
Branch: 1 branch
BranchTrueFalseTotal
if $rep->config->{"debug time filter"} 0 3 3
147 3 ▶ $rep->filtered(0) unless $rep->filtered;
Branch: 1 branch
BranchTrueFalseTotal
unless $rep->filtered 2 1 3
148 3 $rep->filtered($rep->filtered + 1);
149 3 return;
150 }
151 ▶ elsif ($inc->time and $self->after and $inc->time > $self->after)
Condition50%3/6
$inc->time and $self->after and $inc->time > $self->after
!ll&&!rl&&r
034
$inc->time and $self->after
!ll&&!rl&&r
007
MC/DC33%1/3
$inc->time and $self->after and $inc->time > $self->after
$inc->time $self->after $inc->time > $self->after
Truth table50%2/4
A and B and C
A= $inc->timeB= $self->afterC= $inc->time > $self->after
ABCresult
0XX0
10X0
1100
1111
152 {
153 warn "Filter::Time - discard after ", $inc->time, "\n"
154 4 ▶ if $rep->config->{'debug time filter'};
Branch: 1 branch
BranchTrueFalseTotal
if $rep->config->{"debug time filter"} 0 4 4
155 4 ▶ $rep->filtered(0) unless $rep->filtered;
Branch: 1 branch
BranchTrueFalseTotal
unless $rep->filtered 0 4 4
156 4 $rep->filtered($rep->filtered + 1);
157 4 return;
158 }
159  
160 warn "Filter::Time - accept ", $inc->time, "\n"
161 3 ▶ if $rep->config->{'debug time filter'};
Branch: 1 branch
BranchTrueFalseTotal
if $rep->config->{"debug time filter"} 0 3 3
162 3 return $inc;
163 }
164  
165 __END__
« Mail/Abuse/Processor/Store.pm ↑ Summary Mail/Abuse/Processor/Explain.pm »