lib/Mail/Decency/LogParser/Core/CSV.pm

filter: Statementstmt 34.2% 25 / 73 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 45.4% 5 / 11 Podpod 100% 1 / 1 totaltotal 27.1% 31 / 114 CC 16 SCAR 39.7
CC
16
Cov
25%
__ANON__
110.0
__ANON__
42.0
add_csv_data
6.0
CRAP 52.8
« lib/Mail/Decency/ContentFilter/DSPAM.pm ↑ Summary lib/Mail/Decency/Policy/DNSBL.pm »
1 package Mail::Decency::LogParser::Core::CSV;
2  
3 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use version 0.74; our $VERSION = qv( "v0.1.4" );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Basename qw/ dirname /;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Path qw/ make_path /;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 Mail::Decency::LogParser::CSV
14  
15  
16 =head1 DESCRIPTION
17  
18 Logs
19  
20 =head1 CLASS ATTRIBUTES
21  
22 =cut
23  
24 1 has csv_log_classes => ( is => 'rw', isa => 'ArrayRef[Str]', predicate => 'enable_csv' );
25 1 has csv_log_ok => ( is => 'rw', isa => 'HashRef' );
26 1 has csv_log_file => ( is => 'rw', isa => 'Str' );
27 0 ▶has csv_log_fh => ( is => 'rw', isa => 'HashRef', default => sub { {} } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
28 0 ▶has csv_data => ( is => 'rw', isa => 'HashRef[ArrayRef]', default => sub { {} } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
29  
30 =head1 METHODS
31  
32  
33 =head2 init
34  
35 Extends init..
36  
37 =cut
38  
39 0 ▶before 'init' => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
40 0 my ( $self ) = @_;
41 0 ▶ return unless $self->config->{ csv_log };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->config->{"csv_log"} 0 0 0
42
43
44 # check wheter we have a csv file
45 die "Require 'file' in csv_log\n"
46 0 ▶ unless $self->config->{ csv_log }->{ file };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->config->{"csv_log"}{"file"} 0 0 0
47 0 my $dir = dirname( $self->config->{ csv_log }->{ file } );
48
49 # create dir, unless existing
50 0 ▶ make_path( $dir, { mode => 0700 } )
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
51 unless ( -d $dir );
52 0 ▶ die "Could not create directory '$dir' for csv files\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
53 unless ( -d $dir );
54
55 # using csv log ?
56 die "Require 'classes' in csv_log\n"
57 0 ▶ unless $self->config->{ csv_log }->{ classes };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->config->{"csv_log"}{"classes"} 0 0 0
58 0 $self->csv_log_classes( $self->config->{ csv_log }->{ classes } );
59
60 # remember names of valid log classes
61 $self->csv_log_ok( { map {
62 0 ( $_ => 1 );
63 0 } @{ $self->csv_log_classes } } );
64
65 # setup file
66 0 $self->csv_log_file( $self->config->{ csv_log }->{ file } );
67
68 0 return;
69 1 };
70  
71  
72 =head2 handle
73  
74 Checks wheter incoming mail is whilist for final recipient
75  
76 =cut
77  
78 0 ▶before 'handle' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
79 0 my ( $self ) = @_;
80
81 # empty data
82 0 $self->csv_data( {} );
83
84 0 return;
85 1 };
86  
87 0 ▶after 'handle' => sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
88 0 my ( $self, $parsed_ref ) = @_;
89
90 0 ▶ return unless $self->enable_csv;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->enable_csv 0 0 0
91
92 # check all possible csv classes
93 0 foreach my $class( @{ $self->csv_log_classes } ) {
94
95 # not having any data of this kind ?
96 0 ▶ next unless ( my $data_ref = $self->current_data->{ $class } );
Branch: 1 branch
BranchTrueFalseTotal
unless my $data_ref = $self->current_data->{$class} 0 0 0
97 #print Dumper $data_ref;
98
99 # get file handle
100 0 my $fh = $self->csv_log_fh->{ $class };
101 0 my $file = $self->csv_log_file. ".$class";
102
103 # no such file handle -> open now
104 0 ▶ unless ( $fh ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 0 0
105 0 ▶ my $mode = -f $file ? '>>' : '>';
Branch: 1 branch
BranchTrueFalseTotal
-f $file ? : 0 0 0
106 0 ▶ open $fh, $mode, $file
Branch: 1 branch
BranchTrueFalseTotal
open $fh, $mode, $file or die "Cannot open '${file}' for append: $!\n" 0 0 0
107 or die "Cannot open '$file' for append: $!\n";
108 0 $self->csv_log_fh->{ $class } = $fh;
109 0 $fh->autoflush( 1 );
110
111 0 ▶ if ( $mode eq '>' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq ">") 0 0 0
112 0 eval {
113 0 print $fh "time;". join( ";", sort keys %$data_ref ). "\n";
114 };
115 }
116 }
117
118 # try print
119 0 eval {
120 print $fh join( ";", time(), map {
121 0 my $s = $data_ref->{ $_ };
122 0 $s =~ s/;/\\;/g;
123 0 $s =~ s/\n/\\n/g;
124 0 $s;
125 } sort keys %$data_ref ). "\n";
126 };
127
128 # catch error once (may be rotated or some)
129 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
130 0 ▶ open $fh, '>>', $file
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">>", $file or die "Cannot open '${file}' for append: $!\n" 0 0 0
131 or die "Cannot open '$file' for append: $!\n";
132 0 $fh->autoflush( 1 );
133 0 $self->csv_log_fh->{ $class } = $fh;
134 0 print $fh join( ";", @{ $self->csv_data->{ $class } } );
135 }
136 }
137 1 };
138  
139  
140 =head2 add_csv_data
141  
142 Adding data to current queue
143  
144 =cut
145  
146 0 ▶sub add_csv_data {
Subroutine add_csv_data: not called CC=2 SCAR=17.9
Pod documented
147 0 my ( $self, $name, @data ) = @_;
148 0 ▶ push @{ $self->csv_data->{ $name } ||= [] }, @data;
Condition0%0/2
$self->csv_data->{$name} ||= []
l!l
00
MC/DC0%0/1
$self->csv_data->{$name} ||= []
$self->csv_data->{$name}
Truth table0%0/2
A
A= $self->csv_data->{$name}
Aresult
11
00
149 }
150  
151 =head1 AUTHOR
152  
153 Ulrich Kautz <uk@fortrabbit.de>
154  
155 =head1 COPYRIGHT
156  
157 Copyright (c) 2010 the L</AUTHOR> as listed above
158  
159 =head1 LICENCSE
160  
161 This library is free software and may be distributed under the same terms as perl itself.
162  
163 =cut
164  
165 1 1;
« lib/Mail/Decency/ContentFilter/DSPAM.pm ↑ Summary lib/Mail/Decency/Policy/DNSBL.pm »