lib/Mail/Decency/Core/SessionItem/ContentFilter.pm

filter: Statementstmt 22.6% 33 / 146 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 34 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 42.8% 6 / 14 Podpod 100% 7 / 7 totaltotal 16.5% 46 / 278 CC 48 SCAR 52.2
CC
48
Cov
14%
_init_file
420.0
retreive_policy_scoring
182.0
update_from_policy_cache
30.0
CRAP 185.5
« lib/Mail/Decency/Core/Server.pm ↑ Summary lib/Mail/Decency/ContentFilter/DKIM.pm »
1 package Mail::Decency::Core::SessionItem::ContentFilter;
2  
3 9 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 extends qw/
5 Mail::Decency::Core::SessionItem
6 /;
7  
8 9 ▶use version 0.74; our $VERSION = qv( "v0.1.4" );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 9 ▶use MIME::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 Mail::Decency::Core::SessionItem::ContentFilter
18  
19 =head1 DESCRIPTION
20  
21 The id attribute is the current QUEUE ID
22  
23 =head1 CLASS ATTRIBUTES
24  
25 =head2 file
26  
27 The file (in the spool folder, absolute path)
28  
29 =cut
30  
31 9 has file => ( is => 'ro', isa => "Str", required => 1, trigger => \&_init_file );
32  
33 =head2 store
34  
35 YAML file containing the current info
36  
37 =cut
38  
39 9 has store => ( is => 'rw', isa => "Str" );
40  
41 =head2 file_size
42  
43 Size of the current file (id)
44  
45 =cut
46  
47 9 has file_size => ( is => 'rw', isa => "Int", default => 0 );
48  
49 =head2 virus
50  
51 String containg info (name) of the virus
52  
53 =cut
54  
55 9 has virus => ( is => 'rw', isa => "Str" );
56  
57 =head2 next_id
58  
59 If set, we now of the next queue id
60  
61 =cut
62  
63 9 has next_id => ( is => 'rw', isa => "Str" );
64  
65 =head2 prev_id
66  
67 If set, we now of the previous queue id
68  
69 =cut
70  
71 9 has prev_id => ( is => 'rw', isa => "Str" );
72  
73 =head2 mime_output_dir
74  
75 The directory where mime files are to be output (from content filter)
76  
77 =cut
78  
79 9 has mime_output_dir => ( is => 'rw', isa => "Str", required => 1 );
80  
81 =head2 mime
82  
83 Is a MIME::Entity object representing the current mail
84  
85 =cut
86  
87 9 has mime => ( is => 'rw', isa => "MIME::Entity" );
88  
89 =head2 mime_filer
90  
91 The filer used for cleanup
92  
93 =cut
94  
95 9 has mime_filer => ( is => 'rw', isa => "MIME::Parser::FileUnder" );
96  
97 =head2 mime_fh
98  
99 File handle for mime file
100  
101 =cut
102  
103 9 has mime_fh => ( is => 'rw', isa => "IO::File" );
104  
105 =head2 verify_key
106  
107 Instance of L<Crypt::OpenSSL::RSA> representing the forward sign key
108  
109 =cut
110  
111 9 has verify_key => ( is => 'rw', isa => 'Crypt::OpenSSL::RSA', predicate => 'can_verify' );
112  
113  
114 =head2 verify_ttl
115  
116 TTL for validity of signatures in seconds
117  
118 =cut
119  
120 9 has verify_ttl => ( is => 'rw', isa => 'Int', predicate => 'has_verify_ttl' );
121  
122  
123 =head2 update_store
124  
125 Write store YAML file
126  
127 =cut
128  
129 0 ▶sub update_store {
Subroutine update_store: not called CC=4 SCAR=30.0
Pod documented
130 0 my ( $self ) = @_;
131 0 ▶ open my $fh, '>', $self->store
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $self->store or die 'Cannot open store file ' . $self->store . " for write: $!" 0 0 0
132 or die "Cannot open store file ". $self->store. " for write: $!";
133 0 my %create = ();
134 0 ▶ $create{ from } = $self->from if $self->from;
Branch: 1 branch
BranchTrueFalseTotal
if $self->from 0 0 0
135 0 ▶ $create{ to } = $self->to if $self->to;
Branch: 1 branch
BranchTrueFalseTotal
if $self->to 0 0 0
136 0 print $fh YAML::Dump( {
137 file => $self->file,
138 size => $self->file_size,
139 %create
140 } );
141 0 close $fh;
142 }
143  
144  
145 =head2 update_from_policy_cache
146  
147 Update session from cached policy session
148  
149 =cut
150  
151 0 ▶sub update_from_policy_cache {
Subroutine update_from_policy_cache: not called CC=5 SCAR=34.0
Pod documented
152 0 my ( $self, $hash_ref ) = @_;
153
154 # update spam score
155 $self->spam_score( $self->spam_score + $hash_ref->{ spam_score } )
156 0 ▶ if $hash_ref->{ spam_score };
Branch: 1 branch
BranchTrueFalseTotal
if $hash_ref->{"spam_score"} 0 0 0
157
158 # update spam details
159 0 push @{ $self->spam_details }, @{ $hash_ref->{ spam_details } }
160 0 ▶ if $hash_ref->{ spam_details };
Branch: 1 branch
BranchTrueFalseTotal
if $hash_ref->{"spam_details"} 0 0 0
161
162 # update spam details
163 0 ▶ if ( $hash_ref->{ flags } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash_ref->{"flags"}) 0 0 0
164 0 $self->set_flag( $_ ) for keys %{ $hash_ref->{ flags } };
165 }
166
167 0 return;
168 }
169  
170  
171 =head2 update_from_cache
172  
173 Update session from cached session
174  
175 =cut
176  
177 0 ▶sub update_from_cache {
Subroutine update_from_cache: not called CC=5 SCAR=34.0
Pod documented
178 0 my ( $self, $hash_ref ) = @_;
179
180 0 $self->update_from_policy_cache( $hash_ref );
181
182 $self->virus( join( "; ", $self->virus, $hash_ref->{ virus } ) )
183 0 ▶ if $hash_ref->{ virus };
Branch: 1 branch
BranchTrueFalseTotal
if $hash_ref->{"virus"} 0 0 0
184
185 0 foreach my $id( qw/ next_id prev_id / ) {
186 $self->$id( $hash_ref->{ $id } )
187 0 ▶ if ! $self->$id && $hash_ref->{ $id };
Branch: 1 branch
BranchTrueFalseTotal
if not $self->$id and $hash_ref->{$id} 0 0 0
Condition0%0/3
not $self->$id and $hash_ref->{$id}
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->$id and $hash_ref->{$id}
not $self->$id $hash_ref->{$id}
Truth table0%0/3
not(A) and B
A= not $self->$idB= $hash_ref->{$id}
ABresult
0X0
100
111
188 }
189
190 0 return;
191 }
192  
193  
194 =head2 write_mime
195  
196 Update the file ($self->file) from mime .. should be performed after
197 mime manipulations
198  
199 =cut
200  
201 0 ▶sub write_mime {
Subroutine write_mime: not called CC=2 SCAR=17.9
Pod documented
202 0 my ( $self ) = @_;
203
204 # get mime object
205 0 my $mime = $self->mime;
206
207 # resync file size
208 0 $mime->sync_headers( Length => 'COMPUTE' );
209
210 # store backup fore failure recovery
211 0 my $tmp_name = $self->file. ".$$.". time();
212 0 rename( $self->file, $tmp_name );
213
214 # write back to file
215 0 eval {
216 0 unlink( $self->file );
217 0 open my $fh, '>', $self->file;
218 0 $mime->print( $fh );
219 0 close $fh;
220 };
221
222 # restore backup on error
223 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
224 0 rename( $tmp_name, $self->file );
225 0 return 0;
226 }
227 else {
228 0 unlink( $tmp_name );
229 }
230
231 0 return 1;
232 }
233  
234  
235 =head2 for_cache
236  
237 returns data formatted for cache
238  
239 =cut
240  
241 0 ▶sub for_cache {
Subroutine for_cache: not called CC=1 SCAR=6.9
Pod documented
242 0 my ( $self ) = @_;
243
244 return {
245 0 spam_score => $self->spam_score,
246 spam_details => $self->spam_details,
247 virus => $self->virus,
248 queue_id => $self->id,
249 next_id => $self->next_id,
250 prev_id => $self->prev_id
251 };
252 }
253  
254  
255 =head2 cleanup
256  
257 Called at the end of the session.. removes all temp files and the mail file
258  
259 =cut
260  
261 0 ▶sub cleanup {
Subroutine cleanup: not called CC=5 SCAR=34.0
Pod documented
262 0 my ( $self ) = @_;
263
264 # close mime handle
265 0 eval { $self->mime_fh->close }; # do silent, don't care
266
267 # clear mime
268 0 $self->mime_filer->purge;
269
270 # remove store file
271 0 ▶ unlink $self->store
Branch: 1 branch
BranchTrueFalseTotal
if $self->store and -f $self->store 0 0 0
Condition0%0/3
$self->store and -f $self->store
!ll&&!rl&&r
000
MC/DC0%0/2
$self->store and -f $self->store
$self->store -f $self->store
Truth table0%0/3
A and B
A= $self->storeB= -f $self->store
ABresult
0X0
100
111
272 if $self->store && -f $self->store;
273
274 # remove store file
275 0 ▶ unlink $self->file
Branch: 1 branch
BranchTrueFalseTotal
if $self->file and -f $self->file 0 0 0
Condition0%0/3
$self->file and -f $self->file
!ll&&!rl&&r
000
MC/DC0%0/2
$self->file and -f $self->file
$self->file -f $self->file
Truth table0%0/3
A and B
A= $self->fileB= -f $self->file
ABresult
0X0
100
111
276 if $self->file && -f $self->file;
277
278 0 $self->unset;
279
280 0 return ;
281 }
282  
283  
284 =head2 retreive_policy_scoring
285  
286 =cut
287  
288 0 ▶sub retreive_policy_scoring {
Subroutine retreive_policy_scoring: not called CC=13 SCAR=52.0
Pod documented
289 0 my ( $self, $accept_scoring ) = @_;
290
291 # having decency instance (from policy) ?
292 my @instance = map {
293 0 chomp;
294 0 my ( $instance, $signature, $weight, $timestamp, $flags, @info ) = split( /\|/, $_ );
295 0 [ $instance, $signature, $weight, $timestamp, $flags, @info ];
296 } $self->mime->head->get( 'X-Decency-Instance' );
297
298 # remember wheter cleanup is required
299 0 my $cleanup_instance = scalar @instance > 0;
300
301 # using signed forwarded info ? (bother only if scoring from external is accepted!)
302 0 ▶ if ( @instance && $accept_scoring && $self->can_verify ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@instance and $accept_scoring and $self->can_verify) 0 0 0
Condition0%0/6
@instance and $accept_scoring and $self->can_verify
!ll&&!rl&&r
000
@instance and $accept_scoring
!ll&&!rl&&r
000
MC/DC0%0/3
@instance and $accept_scoring and $self->can_verify
@instance $accept_scoring $self->can_verify
Truth table0%0/4
A and B and C
A= @instanceB= $accept_scoringC= $self->can_verify
ABCresult
0XX0
10X0
1100
1111
303
304 # get all valid instances
305 @instance = grep {
306 0 my ( $instance, $signature, $weight, $timestamp, $flags, @info ) = @$_;
307
308 # verify instance
309 0 my $ok = $self->verify_key->verify(
310 join( "|", $signature, $weight, $timestamp, $flags, @info ),
311 pack( "H*", $signature )
312 );
313
314 # valid ?
315 0 ▶ $ok && $timestamp <= time() && ( ! $self->has_verify_ttl || $timestamp + $self->verify_ttl >= time() );
Condition0%0/9
$ok && $timestamp <= time && (!$self->has_verify_ttl || $timestamp + $self->verify_ttl >= time)
!ll&&!rl&&r
000
$ok and $timestamp <= time
!ll&&!rl&&r
000
not $self->has_verify_ttl or $timestamp + $self->verify_ttl >= time
l!l&&r!l&&!r
000
MC/DC0%0/4
$ok && $timestamp <= time && (!$self->has_verify_ttl || $timestamp + $self->verify_ttl >= time)
$ok $timestamp <= time not $self->has_verify_ttl $timestamp + $self->verify_ttl >= time
Truth table0%0/5
A and B && not(C) or D
A= $okB= $timestamp <= timeC= not $self->has_verify_ttlD= $timestamp + $self->verify_ttl >= time
ABCDresult
0XXX0
10XX0
11000
111X1
11011
316 } @instance;
317 }
318
319 # having any instances ?
320 0 ▶ if ( @instance ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@instance) 0 0 0
321
322 # handle first instance
323 # this is the LATEST instance.. contains the FINAL score
324 0 my $first_ref = shift @instance;
325 0 my ( $instance, $keyword, $weight, $timestamp, $flags, @info ) = @$first_ref;
326
327 # try read from cache
328 # if policy and content filter use the same cache, this will hit!
329 0 my $cached = $self->cache->get( "POLICY-$instance" );
330 0 ▶ if ( $cached ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cached) { } 0 0 0
elsif ($accept_scoring) { } 0 0 0
331
332 # remove policy finally from cache..
333 # there are no policy filters behind the content filter ..
334 0 $self->cache->remove( "POLICY-$instance" );
335
336 # add spam score, details
337 0 $self->update_from_policy_cache( $cached );
338 }
339
340 # not from cache
341 # if policy server accepts scorings in the first place ..
342 elsif ( $accept_scoring ) {
343
344 # init for update ..
345 # only the first weight will be used, because it is the last
346 # policy weight and therfore the cumulated policy weight
347 $cached= {
348 spam_score => $weight,
349 spam_details => \@info,
350 0 flags => { map { ( $_ => 1 ) } split( /\s*,\s*/, $flags ) }
351 };
352
353 # get flags and info from older instances
354 0 foreach my $older_instance( @instance ) {
355 0 ( undef, undef, undef, undef, my $add_flags, my @add_info )
356 = split( /\|/, $instance );
357 0 push @{ $cached->{ spam_details } }, @add_info;
358 0 $cached->{ flags }->{ $_ } = 1 for split( /\s*,\s*/, $add_flags );
359 }
360
361 # add spam score, details
362 0 $self->update_from_policy_cache( $cached );
363 }
364 }
365
366 # cleanup instances ?
367 0 ▶ if ( $cleanup_instance ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cleanup_instance) 0 0 0
368 0 $self->mime->head->delete( 'X-Decency-Instance' );
369 0 $self->write_mime();
370 }
371 }
372  
373 =pod
374  
375 PRIVATE METHODS
376  
377 =pod
378  
379 _init_file
380  
381 Triggerd on file set
382  
383 =cut
384  
385 0 ▶sub _init_file {
Subroutine _init_file: not called CC=20 SCAR=60.4
386 0 my ( $self ) = @_;
387
388 0 ▶ die "Cannot access file '". $self->file. "'" unless -f $self->file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $self->file 0 0 0
389 0 $self->file_size( -s $self->file );
390
391 # store
392 0 $self->store( $self->file. '.info' );
393 0 my $has_store = 0;
394 0 ▶ if ( -f $self->store ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $self->store) 0 0 0
395 0 $has_store++;
396 0 my $ref;
397 0 eval {
398 0 $ref = YAML::LoadFile( $self->store );
399 };
400 0 ▶ die "Error loading YAML file ". $self->store. ": $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
401 0 ▶ die "YAML file ". $self->store. " mal formatted, should be HASH, is '". ref( $ref ). "'"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ref eq "HASH" 0 0 0
402 unless ref( $ref ) eq 'HASH';
403
404 0 foreach my $attr( qw/ from to / ) {
405 0 ▶ $self->$attr( $ref->{ $attr } ) unless $self->$attr;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->$attr 0 0 0
406 }
407 }
408
409 # setup mime
410 0 my $parser = MIME::Parser->new;
411 0 $parser->output_under( $self->mime_output_dir );
412 0 $parser->decode_headers( 1 );
413
414 # read from file and create
415 0 ▶ my $orig_fh = IO::File->new( $self->file, 'r' )
Branch: 1 branch
BranchTrueFalseTotal
my $orig_fh = "IO::File"->new($self->file, "r") or die 'Cannot open ' . $self->file . " for read\n" 0 0 0
416 or die "Cannot open ". $self->file. " for read\n";
417
418 0 eval {
419 0 my $mime = $parser->parse( $orig_fh );
420 0 $self->mime( $mime );
421 0 $self->mime_filer( $parser->filer );
422 0 $self->mime_fh( $orig_fh );
423 };
424 0 ▶ die "Error parsing MIME: $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
425
426 # extract relevant headers ..
427 0 ▶ unless ( $self->to ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->to) 0 0 0
428 0 ▶ my $to = "". ( $self->mime->head->get( 'Delivered-To' ) || $self->mime->head->get( 'To' ) || "" );
Condition0%0/5
$self->mime->head->get('Delivered-To') || $self->mime->head->get('To') || ''
l!l
00
$self->mime->head->get("Delivered-To") or $self->mime->head->get("To")
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->mime->head->get('Delivered-To') || $self->mime->head->get('To') || ''
$self->mime->head->get("Delivered-To") $self->mime->head->get("To")
Truth table0%0/3
A or B
A= $self->mime->head->get("Delivered-To")B= $self->mime->head->get("To")
ABresult
1X1
011
000
429 0 ▶ if ( $to ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to) 0 0 0
430 0 ▶ if ( $to =~ /<([^>]+)>/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to =~ /<([^>]+)>/) { } 0 0 0
431 0 $self->to( $1 );
432 }
433 else {
434 0 $self->to( $to );
435 }
436 }
437 }
438
439 # extact from..
440 0 ▶ unless ( $self->from ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->from) 0 0 0
441 0 ▶ my $from = "". ( $self->mime->head->get( 'Return-Path' ) || $self->mime->head->get( 'From' ) || "" );
Condition0%0/5
$self->mime->head->get('Return-Path') || $self->mime->head->get('From') || ''
l!l
00
$self->mime->head->get("Return-Path") or $self->mime->head->get("From")
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->mime->head->get('Return-Path') || $self->mime->head->get('From') || ''
$self->mime->head->get("Return-Path") $self->mime->head->get("From")
Truth table0%0/3
A or B
A= $self->mime->head->get("Return-Path")B= $self->mime->head->get("From")
ABresult
1X1
011
000
442 0 ▶ if ( $from ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from) 0 0 0
443 0 ▶ if ( $from =~ /<([^>]+)>/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from =~ /<([^>]+)>/) { } 0 0 0
444 0 $self->from( $1 );
445 }
446 else {
447 0 $self->from( $from );
448 }
449 }
450 }
451
452 # write relevant info to store file
453 0 ▶ $self->update_store() unless $has_store;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_store 0 0 0
454 }
455  
456 =head1 AUTHOR
457  
458 Ulrich Kautz <uk@fortrabbit.de>
459  
460 =head1 COPYRIGHT
461  
462 Copyright (c) 2010 the L</AUTHOR> as listed above
463  
464 =head1 LICENCSE
465  
466 This library is free software and may be distributed under the same terms as perl itself.
467  
468 =cut
469  
470 9 1;
« lib/Mail/Decency/Core/Server.pm ↑ Summary lib/Mail/Decency/ContentFilter/DKIM.pm »