Mail/Abuse/Processor/Store.pm

filter: Statementstmt 86.9% 93 / 107 Branchbran 59.5% 25 / 42 Conditioncond 65.0% 13 / 20 MC/DCmcdc 50.0% 6 / 12 Subroutinesub 100% 17 / 17 Podpod 100% 1 / 1 totaltotal 77.8% 155 / 199 CC 30 SCAR 36.9
CC
30
Cov
78%
process
60.8
BEGIN
1.0
BEGIN
1.0
CRAP 40.0
« Mail/Abuse/Processor/Radius.pm ↑ Summary Mail/Abuse/Filter/Time.pm »
1 package Mail::Abuse::Processor::Store;
2  
3 3 require 5.005_62;
4  
5 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use PerlIO::gzip;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Storable qw/nstore_fd/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Digest::MD5 qw/md5_hex/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 3 ▶use base 'Mail::Abuse::Processor';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 3 ▶use constant ROOT => 'store root path';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use constant EMPTY => 'store empty path';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use constant MODE => 'store mode';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use constant DEBUG => 'debug store';
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 # The code below should be in a single line
26  
27 3 our $VERSION = do { my @r = (q$Revision: 1.12 $ =~ /\d+/g); sprintf " %d."."%03d" x $#r, @r };
28  
29 =pod
30  
31 =head1 NAME
32  
33 Mail::Abuse::Processor::Store - Process a Mail::Abuse::Report
34  
35 =head1 SYNOPSIS
36  
37 use Mail::Abuse::Processor::Store;
38  
39 use Mail::Abuse::Report;
40 my $p = new Mail::Abuse::Processor::Store;
41 my $report = new Mail::Abuse::Report (processors => [ $p ]);
42  
43 # ... other pieces of code that configure the report ...
44  
45 =head1 DESCRIPTION
46  
47 This class stores a processed report in a file hierarchy that is
48 composed using the smallest acceptable timestamp from the list of
49 incidents in a report.
50  
51 If no incidents are found within a report, a special name is built
52 based on the report text.
53  
54 The place where the files are created can be controlled with entries
55 in the configuration file. Currently, the following directives are
56 understood.
57  
58 =over
59  
60 =item B<store root path>
61  
62 Points to the root of the tree where reports are to be
63 stored. Defaults to the current directory.
64  
65 =item B<store empty path>
66  
67 The name of the leaf where reports with no incidents are stored. This
68 is a subdir of B<store root path>. It defaults to the very creative
69 name, "empty".
70  
71 =item B<store mode>
72  
73 The mode in which to store abuse reports. The following modes are
74 supported.
75  
76 =over
77  
78 =item B<serialized>
79  
80 This is the default, and uses L<Storable> to serialize the in-memory
81 C<Mail::Abuse::Report> object and store it in a flat file.
82  
83 =item B<serialized-gz>
84  
85 Just like B<serialized>, but the resulting file is compressed with the
86 equivalent of L<gzip>, using L<PerlIO::gzip>.
87  
88 =item B<plain>
89  
90 Store only the report text, as a flat file.
91  
92 =item B<plain-gz>
93  
94 Just like B<plain> but the resulting file is compressed on the fly
95 using L<PerlIO::gzip>.
96  
97 =back
98  
99 =item B<debug store>
100  
101 If set to a true value, causes this module to emit debugging
102 information using C<warn()>.
103  
104 =back
105  
106 The following functions are implemented.
107  
108 =over
109  
110 =item C<process($report)>
111  
112 Takes a C<Mail::Abuse::Report> object as an argument and performs the
113 processing action required.
114  
115 The processed C<$report> will have the filename where the report was
116 stored, added in C<store_file>.
117  
118 =cut
119  
120 sub process
121 10 ▶{
Subroutine process: called CC=30 SCAR=41.1
Pod documented
122 10 my $self = shift;
123 10 my $rep = shift;
124  
125 10 my $earliest = $rep->incidents->[0];
126  
127 10 my $path;
128 my $file;
129
130 10 ▶ unless ($self->root)
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->root) 10 0 10
131 {
132 10 ▶ $self->root($rep->config->{&ROOT} || File::Spec->curdir());
Condition33%1/3
$rep->config->{&ROOT} || 'File::Spec'->curdir
l!l&&r!l&&!r
1000
MC/DC0%0/2
$rep->config->{&ROOT} || 'File::Spec'->curdir
$rep->config->{&ROOT} 'File::Spec'->curdir
Truth table33%1/3
A || B
A= $rep->config->{&ROOT}B= 'File::Spec'->curdir
ABresult
1X1
011
000
133 warn "Store: Root set to '" . $self->root . "'\n"
134 10 ▶ if $rep->config->{&DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $rep->config->{&DEBUG} 0 10 10
135 }
136  
137 10 ▶ unless ($self->empty)
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->empty) 10 0 10
138 {
139 10 ▶ $self->empty($rep->config->{&EMPTY} || 'empty');
Condition50%1/2
$rep->config->{&EMPTY} || 'empty'
l!l
010
MC/DC0%0/1
$rep->config->{&EMPTY} || 'empty'
$rep->config->{&EMPTY}
Truth table50%1/2
A
A= $rep->config->{&EMPTY}
Aresult
11
00
140 warn "Store: Empty placeholder set to '" . $self->empty . "'\n"
141 10 ▶ if $rep->config->{&DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $rep->config->{&DEBUG} 0 10 10
142 }
143  
144 10 ▶ if ($earliest)
Branch: 1 branch
BranchTrueFalseTotal
if ($earliest) 5 5 10
145 {
146 5 for my $i (@{$rep->incidents})
147 {
148 15 ▶ if ($i->time and $earliest->time and $i->time < $earliest->time)
Branch: 2 branches
BranchTrueFalseTotal
if ($i->time and $earliest->time and $i->time < $earliest->time) { } 4 11 15
elsif (not defined $earliest->time) { } 0 11 11
Condition50%3/6
$i->time and $earliest->time and $i->time < $earliest->time
!ll&&!rl&&r
0114
$i->time and $earliest->time
!ll&&!rl&&r
0015
MC/DC33%1/3
$i->time and $earliest->time and $i->time < $earliest->time
$i->time $earliest->time $i->time < $earliest->time
Truth table50%2/4
A and B and C
A= $i->timeB= $earliest->timeC= $i->time < $earliest->time
ABCresult
0XX0
10X0
1100
1111
149 {
150 4 $earliest = $i;
151 }
152 elsif (! defined $earliest->time)
153 {
154 0 $earliest = $i;
155 }
156 }
157  
158 5 ▶ if (defined $earliest->time)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $earliest->time) { } 5 0 5
159 {
160 # Build the pathname...
161 5 $path = File::Spec->catdir
162 (
163 $self->root, split
164 (/:/, strftime("%Y:%m:%d",
165 (localtime($earliest->time))[0..5])
166 ),
167 );
168
169 5 $file = File::Spec->catfile($path, md5_hex($ {$rep->text}));
170 }
171 else
172 {
173 warn "Store: No incident has time!\n"
174 0 ▶ if $rep->config->{&DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $rep->config->{&DEBUG} 0 0 0
175 }
176 }
177  
178 10 ▶ unless ($file and $path)
Branch: 1 branch
BranchTrueFalseTotal
unless ($file and $path) 5 5 10
Condition66%2/3
$file and $path
!ll&&!rl&&r
505
MC/DC50%1/2
$file and $path
$file $path
Truth table66%2/3
A and B
A= $fileB= $path
ABresult
0X0
100
111
179 {
180 5 $path = File::Spec->catdir
181 (
182 $self->root,
183 $self->empty,
184 );
185  
186 5 $file = File::Spec->catfile($path, md5_hex($ {$rep->text}));
187 }
188
189 warn "Store: File name set to $file\n"
190 10 ▶ if $rep->config->{&DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $rep->config->{&DEBUG} 0 10 10
191  
192 10 eval { mkpath [ $path ] }; # Create the target path...
193  
194 10 ▶ if ($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 10 10
195 {
196 0 warn "Store: Failed to create dir $path: $!\n";
197 0 return;
198 }
199  
200 10 ▶ if (-f $file)
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file) 0 10 10
201 {
202 0 warn "Store: $file already exists. Not overriden\n";
203 0 return 1;
204 }
205  
206 10 $rep->store_file($file);
207  
208 10 my $fh = new IO::File;
209  
210 10 ▶ if (exists $rep->config->{&MODE}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rep->config->{&MODE} and $rep->config->{&MODE} =~ /-gz$/) { } 4 6 10
Condition100%3/3
exists $rep->config->{&MODE} and $rep->config->{&MODE} =~ /-gz$/
!ll&&!rl&&r
244
MC/DC100%2/2
exists $rep->config->{&MODE} and $rep->config->{&MODE} =~ /-gz$/
exists $rep->config->{&MODE} $rep->config->{&MODE} =~ /-gz$/
Truth table100%3/3
A and B
A= exists $rep->config->{&MODE}B= $rep->config->{&MODE} =~ /-gz$/
ABresult
0X0
100
111
211 and $rep->config->{&MODE} =~ m/-gz$/)
212 {
213 4 ▶ unless ($fh->open($file, ">:gzip"))
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh->open($file, ">:gzip")) 0 4 4
214 {
215 0 warn "Store: Failed to gz-open $file: $!\n";
216 0 return;
217 }
218 }
219 else
220 {
221 6 ▶ unless ($fh->open($file, ">"))
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh->open($file, ">")) 0 6 6
222 {
223 0 warn "Store: Failed to open $file: $!\n";
224 0 return;
225 }
226 }
227  
228 10 ▶ if (not exists $rep->config->{&MODE}
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $rep->config->{&MODE} or $rep->config->{&MODE} =~ /^serialized/) { } 6 4 10
elsif ($rep->config->{&MODE} =~ /^plain/) { } 4 0 4
Condition100%3/3
not exists $rep->config->{&MODE} or $rep->config->{&MODE} =~ /^serialized/
l!l&&r!l&&!r
244
MC/DC100%2/2
not exists $rep->config->{&MODE} or $rep->config->{&MODE} =~ /^serialized/
not exists $rep->config->{&MODE} $rep->config->{&MODE} =~ /^serialized/
Truth table100%3/3
not(A) or B
A= not exists $rep->config->{&MODE}B= $rep->config->{&MODE} =~ /^serialized/
ABresult
1X1
011
000
229 or $rep->config->{&MODE} =~ m/^serialized/)
230 {
231 eval
232 6 {
233 6 ▶ nstore_fd($rep, $fh)
Branch: 1 branch
BranchTrueFalseTotal
nstore_fd($rep, $fh) or warn "Storable::nstore_fd failed with $!\n" 6 0 6
234 || warn "Storable::nstore_fd failed with $!\n";
235 };
236
237 6 ▶ if ($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 6 6
238 {
239 0 warn "Store: Failed to nstore: $@\n";
240 0 return;
241 }
242  
243 }
244 elsif ($rep->config->{&MODE} =~ m/^plain/)
245 {
246 4 print $fh ${$rep->text};
247 }
248  
249 10 ▶ unless ($fh->close)
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh->close) 0 10 10
250 {
251 0 warn "Failed to close $file: $!\n";
252 0 return;
253 }
254
255 10 return 1;
256 }
257  
258 __END__
« Mail/Abuse/Processor/Radius.pm ↑ Summary Mail/Abuse/Filter/Time.pm »