| 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
| |||||||||||||||||||||||||||||
| 41 | LINE: | ||||||||||||||||||||||||||||
| 42 | 96 | ▶ | while (<$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 58 | 49 | next LINE; | |||||||||||||||||||||||||||
| 59 | }; | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # assert: $read == 2; | ||||||||||||||||||||||||||||
| 62 | 164 | ▶ | /^ Subject: (.*)/i && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 1 | push @{$log->{buffer}}, Log::Procmail::Abstract->new() | |||||||||||||||||||||||||||
| 64 | 49 | ▶ | unless @{$log->{buffer}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 73 | 0 | push @{$log->{buffer}}, Log::Procmail::Abstract->new() | |||||||||||||||||||||||||||
| 74 | 50 | ▶ | unless @{$log->{buffer}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
not $read or @{$log->{"buffer"};} == 0
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
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # return ASAP | ||||||||||||||||||||||||||||
| 96 | 7 | ▶ | last READ if @{$log->{buffer}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # go to next file | ||||||||||||||||||||||||||||
| 99 | 7 | ▶ | redo READ if $log->_open_next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 109 | # the folder field is required | ||||||||||||||||||||||||||||
| 110 | 51 | ▶ | goto READ unless defined $rec->folder; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 147 | 13 | $file = shift @{ $log->{files} }; | |||||||||||||||||||||||||||
| 148 | 3 | } else { return 0 }; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 13 | ▶ | if ( ref $file eq 'GLOB' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 151 | 1 | $log->{fh} = *$file{IO}; | |||||||||||||||||||||||||||
| 152 | 1 | ▶ | carp "Closed filehandle $log->{fh}" unless $log->{fh}->opened; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | ▶ | elsif ( ref $file && $file->isa('IO::Handle') ) { | |||||||||||||||||||||||||||
|
Condition66%2/3
ref $file and $file->isa("IO::Handle")
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")
| |||||||||||||||||||||||||||||
| 155 | 1 | $log->{fh} = $file; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | else { | ||||||||||||||||||||||||||||
| 158 | 11 | ▶ | $log->{fh}->open($file) or carp "Can't open $file: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
$self->{"fh"} and $self->{"fh"}->opened
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 195 | if @_; | ||||||||||||||||||||||||||||
| 196 | 3 | ▶ | return undef unless defined $self->{date}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | 3 | $self->{date} =~ /^$DATE$/o; | |||||||||||||||||||||||||||
| 198 | 3 | ▶ | return undef unless $1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | 2 | return sprintf( "%04d%02d%02d$3$4$5", $6, $month{$1} + 1, $2 ); | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 4 | 1; | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | __END__ | ||||||||||||||||||||||||||||