blib/lib/Log/Procmail.pm

filter: Statementstmt 99.1% 120 / 121 Branchbran 87.0% 47 / 54 Conditioncond 77.7% 7 / 9 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 19 / 19 Podpod 100% 6 / 6 totaltotal 94.4% 203 / 215 CC 31 SCAR 20.2
CC
31
Cov
95%
next
16.0
_open_next
8.2
ymd
4.1
CRAP 7.6
↑ Summary
1 package Log::Procmail;
2 4 $Log::Procmail::VERSION = '0.14';
3 4 require 5.005;
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use UNIVERSAL ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶use vars qw/ $VERSION /;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 local $^W = 1;
12  
13 4 $VERSION = '0.12';
14  
15 4 my %month;
16 4 @month{qw/ Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec /} = ( 0 .. 11 );
17  
18 4 my $DATE = qr/(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ([ \d]\d) (\d\d):(\d\d):(\d\d) .*(\d\d\d\d)/;
19  
20 10 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
21 10 my $class = shift;
22 10 return bless {
23 fh => new IO::File,
24 files => [@_],
25 errors => 0,
26 buffer => [],
27 }, $class;
28 }
29  
30 91 ▶sub next {
Subroutine next: called CC=16 SCAR=27.7
Pod documented
31 91 my $log = shift; # who needs $self?
32  
33 # try to read a record (3 lines)
34 91 my $fh = $log->fh();
35 READ:
36 {
37 92 my $read;
38  
39 # ensure we can read past the previous end of file
40 96 ▶ $fh->clearerr if $fh->eof;
Branch: 1 branch
BranchTrueFalseTotal
if $fh->eof 7 89 96
41 LINE:
42 96 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 214 7 221
43 214 $read++;
44  
45 # should carp if doesn't get what's expected
46 # (From, then Subject, then Folder)
47  
48 # From create a new Abstract
49 214 ▶ /^From (.+?) +($DATE)$/o && do {
Branch: 1 branch
BranchTrueFalseTotal
/^From (.+?) +($DATE)$/o and do { CORE::push @{$$log{"buffer"};}, "Log::Procmail::Abstract"->new; $log->{"buffer"}[-1]->from($1); $log->{"buffer"}[-1]->date($2); last READ if @{$log->{"buffer"};} > 1; next LINE } 50 164 214
50 50 push @{$log->{buffer}}, Log::Procmail::Abstract->new();
51  
52 # assert: $read == 1;
53 50 $log->{buffer}[-1]->from($1);
54 50 $log->{buffer}[-1]->date($2);
55  
56 # return ASAP
57 50 ▶ last READ if @{$log->{buffer}} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @{$log->{"buffer"};} > 1 1 49 50
58 49 next LINE;
59 };
60  
61 # assert: $read == 2;
62 164 ▶ /^ Subject: (.*)/i && do {
Branch: 1 branch
BranchTrueFalseTotal
/^ Subject: (.*)/i and do { CORE::push @{$$log{"buffer"};}, "Log::Procmail::Abstract"->new unless @{$log->{"buffer"};}; $log->{"buffer"}[0]->subject($1); next LINE } 49 115 164
63 1 push @{$log->{buffer}}, Log::Procmail::Abstract->new()
64 49 ▶ unless @{$log->{buffer}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$log->{"buffer"};} 1 48 49
65 49 $log->{buffer}[0]->subject($1);
66 49 next LINE;
67 };
68  
69 # procmail tabulates with tabs and spaces... :-(
70 # assert: $read == 3;
71 # Folder means the end of this record
72 115 ▶ /^ Folder: (.*?)\s+(\d+)$/ && do {
Branch: 1 branch
BranchTrueFalseTotal
/^ Folder: (.*?)\s+(\d+)$/ and do { CORE::push @{$$log{"buffer"};}, "Log::Procmail::Abstract"->new unless @{$log->{"buffer"};}; $log->{"buffer"}[0]->folder($1); $log->{"buffer"}[0]->size($2); last READ } 50 65 115
73 0 push @{$log->{buffer}}, Log::Procmail::Abstract->new()
74 50 ▶ unless @{$log->{buffer}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$log->{"buffer"};} 0 50 50
75  
76 # assert: $read == 3;
77 50 $log->{buffer}[0]->folder($1);
78 50 $log->{buffer}[0]->size($2);
79 50 last READ;
80 };
81  
82 # fall through: some error message
83 # shall we ignore it?
84 65 ▶ next LINE unless $log->{errors};
Branch: 1 branch
BranchTrueFalseTotal
unless $log->{"errors"} 27 38 65
85  
86 # or return it?
87 38 chomp;
88 38 push @{$log->{buffer}}, $_;
89 38 last LINE;
90 }
91  
92 # in case we couldn't read the first line
93 45 ▶ if ( !$read or @{$log->{buffer}} == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $read or @{$log->{"buffer"};} == 0) 7 38 45
Condition100%3/3
not $read or @{$log->{"buffer"};} == 0
l!l&&r!l&&!r
6138
MC/DC100%2/2
not $read or @{$log->{"buffer"};} == 0
not $read @{$log->{"buffer"};} == 0
Truth table100%3/3
not(A) or B
A= not $readB= @{$log->{"buffer"};} == 0
ABresult
1X1
011
000
94  
95 # return ASAP
96 7 ▶ last READ if @{$log->{buffer}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$log->{"buffer"};} 0 7 7
97  
98 # go to next file
99 7 ▶ redo READ if $log->_open_next;
Branch: 1 branch
BranchTrueFalseTotal
if $log->_open_next 4 3 7
100  
101 # unless it's the last one
102 3 return;
103 }
104 }
105  
106 # we have an abstract
107 89 my $rec = shift @{$log->{buffer}};
108 89 ▶ if(UNIVERSAL::isa( $rec, 'Log::Procmail::Abstract')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($rec, "Log::Procmail::Abstract")) 51 38 89
109 # the folder field is required
110 51 ▶ goto READ unless defined $rec->folder;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rec->folder 1 50 51
111 50 $rec->{source} = $log->{source};
112 }
113  
114 88 return $rec;
115 }
116  
117 3 ▶sub push {
Subroutine push: called CC=1 SCAR=0.0
Pod documented
118 3 my $log = shift;
119 3 push @{ $log->{files} }, @_;
120 }
121  
122 6 ▶sub errors {
Subroutine errors: called CC=2 SCAR=6.9
Pod documented
123 6 my $self = shift;
124 6 ▶ return @_ ? $self->{errors} = shift() : $self->{errors};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 4 2 6
125 }
126  
127 95 ▶sub fh {
Subroutine fh: called CC=2 SCAR=6.9
Pod documented
128 95 my $log = shift;
129 95 ▶ $log->_open_next unless $log->{fh}->opened();
Branch: 1 branch
BranchTrueFalseTotal
unless $log->{"fh"}->opened 9 86 95
130 95 return $log->{fh};
131 }
132  
133 3 ▶sub select {
Subroutine select: called CC=1 SCAR=0.0
Pod documented
134 3 my $log = shift;
135 3 $log->fh(); # make sure the file is correctly opened and select is updated
136 3 return $log->{select};
137 }
138  
139 # *internal method*
140 # opens a file or replace the old filehandle by the new one
141 # push() can therefore accept refs to typeglobs, IO::Handle, or filenames
142 16 ▶sub _open_next {
Subroutine _open_next: called CC=8 SCAR=21.0
143 16 my ( $log ) = @_;
144 16 my $file;
145  
146 16 ▶ if ( @{ $log->{files} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$log->{'files'};}) { } 13 3 16
147 13 $file = shift @{ $log->{files} };
148 3 } else { return 0 };
149  
150 13 ▶ if ( ref $file eq 'GLOB' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $file eq 'GLOB') { } 1 12 13
elsif (ref $file and $file->isa('IO::Handle')) { } 1 11 12
151 1 $log->{fh} = *$file{IO};
152 1 ▶ carp "Closed filehandle $log->{fh}" unless $log->{fh}->opened;
Branch: 1 branch
BranchTrueFalseTotal
unless $log->{"fh"}->opened 0 1 1
153 }
154 ▶ elsif ( ref $file && $file->isa('IO::Handle') ) {
Condition66%2/3
ref $file and $file->isa("IO::Handle")
!ll&&!rl&&r
1101
MC/DC50%1/2
ref $file and $file->isa("IO::Handle")
ref $file $file->isa("IO::Handle")
Truth table66%2/3
A and B
A= ref $fileB= $file->isa("IO::Handle")
ABresult
0X0
100
111
155 1 $log->{fh} = $file;
156 }
157 else {
158 11 ▶ $log->{fh}->open($file) or carp "Can't open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
$log->{"fh"}->open($file) or carp("Can't open ${file}: $!") 11 0 11
159 }
160 13 $log->{source} = $file;
161 $log->{select} = ( grep $^O eq $_, qw(MSWin32 NetWare dos VMS riscos beos) )
162 ? undef
163 13 ▶ : IO::Select->new( $log->{fh} );
Branch: 1 branch
BranchTrueFalseTotal
grep(($^O eq $_), ('MSWin32', 'NetWare', 'dos', 'VMS', 'riscos', 'beos')) ? : 0 13 13
164 13 1;
165 }
166  
167 10 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.0
168 10 my $self = shift;
169 10 ▶ if ( $self->{fh} && $self->{fh}->opened ) { $self->{fh}->close }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"fh"} and $self->{"fh"}->opened) 9 1 10
Condition66%2/3
$self->{"fh"} and $self->{"fh"}->opened
!ll&&!rl&&r
019
MC/DC50%1/2
$self->{"fh"} and $self->{"fh"}->opened
$self->{"fh"} $self->{"fh"}->opened
Truth table66%2/3
A and B
A= $self->{"fh"}B= $self->{"fh"}->opened
ABresult
0X0
100
111
170 }
171  
172 #
173 # a small class for the abstracts themselves
174 #
175 package Log::Procmail::Abstract;
176 4 $Log::Procmail::Abstract::VERSION = '0.14';
177 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
178  
179 52 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
180 52 my $class = shift;
181 52 return bless {@_}, $class;
182 }
183  
184 4 for my $attr (qw( from date subject size folder source ) ) {
185 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
186 408 ▶ *$attr = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
187 408 my $self = shift;
188 408 ▶ @_ ? $self->{$attr} = shift: $self->{$attr};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 252 156 408
189 }
190 24 }
191  
192 3 ▶sub ymd {
Subroutine ymd: called CC=4 SCAR=14.0
193 3 my $self = shift;
194 3 ▶ croak("Log::Procmail::Abstract::ymd cannot be used to set the date")
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 3 3
195 if @_;
196 3 ▶ return undef unless defined $self->{date};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"date"} 0 3 3
197 3 $self->{date} =~ /^$DATE$/o;
198 3 ▶ return undef unless $1;
Branch: 1 branch
BranchTrueFalseTotal
unless $1 1 2 3
199 2 return sprintf( "%04d%02d%02d$3$4$5", $6, $month{$1} + 1, $2 );
200 }
201  
202 4 1;
203  
204 __END__
↑ Summary