Mail/Qmail/Filter.pm

filter: Statementstmt 46.7% 50 / 107 Branchbran 5.0% 2 / 40 Conditioncond 3.3% 1 / 30 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 61.9% 13 / 21 Podpod 100% 7 / 7 totaltotal 32.4% 73 / 225 CC 32 SCAR 43.9
CC
32
Cov
30%
run
240.0
add_filters
56.0
_exit
20.0
CRAP 80.3
« Mail/Qmail/Filter/Base/CalloutVerify.pm ↑ Summary Mail/Qmail/Filter/SpamAssassin.pm »
1 1 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Mail::Qmail::Filter;
5  
6 1 our $VERSION = '1.71';
7  
8 1 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use FindBin ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use IO::Handle ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Mail::Qmail::Filter::Util qw(addresses_to_hash match_address);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use MailX::Qmail::Queue::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # Must be under namespace::clean for coercion to work:
18 1 ▶use Mo qw(coerce default);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 my $feedback_fh; # Open ASAP before the handle gets reused:
21  
22 1 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.6
23 1 ▶ $feedback_fh = IO::Handle->new_from_fd( 4, 'w' )
Branch: 1 branch
BranchTrueFalseTotal
$feedback_fh = "IO::Handle"->new_from_fd(4, "w") or warn "Cannot open feedback handle: $!" 0 1 1
24 or warn "Cannot open feedback handle: $!";
25 }
26  
27 1 has 'defer_only';
28 1 has 'feedback_fh' => $feedback_fh;
29 1 has 'filters' => [];
30 1 has 'log_fh';
31 1 has 'reject_text' => 'Rejected.';
32 1 has 'skip_if';
33 1 has 'skip_for_from' => coerce => \&addresses_to_hash;
34 1 has 'skip_for_rcpt' => coerce => \&addresses_to_hash;
35 1 has 'skip_for_sender' => coerce => \&addresses_to_hash;
36 1 has 'skip_if_remote_ip_matches';
37 1 has 'skip_if_relayclient';
38  
39 1 my @debug;
40  
41 1 ▶sub debug {
Subroutine debug: called CC=3 SCAR=16.0
Pod documented
42 1 my $self = shift;
43 1 push @debug, my $message = join ': ', @_;
44 1 ▶ if ( $self->log_fh ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->log_fh) 0 1 1
45 0 my $package = ref($self) . ' ';
46 0 ▶ $self->log_fh->say( $message !~ /^\Q$package/ && $package, $message );
Condition0%0/3
!($message =~ /^\Q$package\E/u) && $package
!ll&&!rl&&r
000
MC/DC0%0/2
!($message =~ /^\Q$package\E/u) && $package
!($message =~ /^\Q$package\E/u) $package
Truth table0%0/3
not(A) && B
A= !($message =~ /^\Q$package\E/u)B= $package
ABresult
0X0
100
111
47 }
48 }
49  
50 0 ▶$SIG{__DIE__} //= sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
51 0 ▶ return if $^S || !defined $^S;
Branch: 1 branch
BranchTrueFalseTotal
if $^S or not defined $^S 0 0 0
Condition0%0/3
$^S or not defined $^S
l!l&&r!l&&!r
000
MC/DC0%0/2
$^S or not defined $^S
$^S not defined $^S
Truth table0%0/3
A or not(B)
A= $^SB= not defined $^S
ABresult
1X1
011
000
52 0 __PACKAGE__->debug( died => "@_" );
53 0 die @_;
54 1 ▶};
Condition33%1/3
$SIG{"__DIE__"} //= sub { return if $^S or not defined $^S; __PACKAGE__->debug("died", join($", @_)); die @_; }
l!l&&r!l&&!r
010
MC/DC0%0/2
$SIG{"__DIE__"} //= sub { return if $^S or not defined $^S; __PACKAGE__->debug("died", join($", @_)); die @_; }
$SIG{"__DIE__"} sub { return if $^S or not defined $^S; __PACKAGE__->debug("died", join($", @_)); die @_; }
Truth table33%1/3
A //= B
A= $SIG{"__DIE__"}B= sub { return if $^S or not defined $^S; __PACKAGE__->debug("died", join($", @_)); die @_; }
ABresult
1X1
011
000
55  
56 0 ▶sub add_filters {
Subroutine add_filters: not called CC=7 SCAR=40.3
Pod documented
57 0 my $self = shift;
58 0 ▶ while ( defined( my $filter = shift ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $filter = shift())) 0 0 0
59 0 ▶ unless ( blessed($filter) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($filter)) 0 0 0
60 0 ▶ my $opt = shift if @_ && 'HASH' eq ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if @_ and "HASH" eq ref $_[0] 0 0 0
Condition0%0/3
@_ and "HASH" eq ref $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and "HASH" eq ref $_[0]
@_ "HASH" eq ref $_[0]
Truth table0%0/3
A and B
A= @_B= "HASH" eq ref $_[0]
ABresult
0X0
100
111
61 0 ▶ $filter = __PACKAGE__ . $filter if $filter =~ /^::/;
Branch: 1 branch
BranchTrueFalseTotal
if $filter =~ /^::/u 0 0 0
62 0 eval "use $filter";
63 0 ▶ confess $@ if length $@;
Branch: 1 branch
BranchTrueFalseTotal
if length $@ 0 0 0
64 0 $filter = $filter->new(%$opt);
65 }
66 0 push @{ $self->{filters} }, $filter;
67 }
68 0 $self;
69 }
70  
71 0 ▶sub defer {
Subroutine defer: not called CC=1 SCAR=6.9
Pod documented
72 0 my $self = shift;
73 0 $self->debug( action => 'defer' );
74 0 $self->_exit( Z => @_ );
75 }
76  
77 0 ▶sub filter {
Subroutine filter: not called CC=2 SCAR=17.9
Pod documented
78 0 my $self = shift;
79  
80 0 $_->run for @{ $self->filters };
81 }
82  
83 0 ▶sub message {
Subroutine message: not called CC=2 SCAR=17.9
Pod documented
84 0 ▶ state $message = MailX::Qmail::Queue::Message->receive
Branch: 1 branch
BranchTrueFalseTotal
state $message = "MailX::Qmail::Queue::Message"->receive or die "Invalid message\n" 0 0 0
85 or die "Invalid message\n";
86 }
87  
88 0 ▶sub reject {
Subroutine reject: not called CC=2 SCAR=17.9
Pod documented
89 0 my $self = shift;
90 0 $self->debug( action => 'reject' );
91 0 ▶ $self->_exit( $self->defer_only ? 'Z' : 'D', @_ );
Branch: 1 branch
BranchTrueFalseTotal
$self->defer_only ? : 0 0 0
92 }
93  
94 0 ▶sub run {
Subroutine run: not called CC=15 SCAR=54.8
Pod documented
95 0 my $self = shift;
96  
97 0 my $package = ref $self;
98  
99 0 ▶ return if $self->skip_if && $self->skip_if->($self);
Branch: 1 branch
BranchTrueFalseTotal
if $self->skip_if and $self->skip_if->($self) 0 0 0
Condition0%0/3
$self->skip_if and $self->skip_if->($self)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->skip_if and $self->skip_if->($self)
$self->skip_if $self->skip_if->($self)
Truth table0%0/3
A and B
A= $self->skip_ifB= $self->skip_if->($self)
ABresult
0X0
100
111
100  
101 0 ▶ if ( exists $ENV{RELAYCLIENT} && $self->skip_if_relayclient ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{"RELAYCLIENT"} and $self->skip_if_relayclient) 0 0 0
Condition0%0/3
exists $ENV{"RELAYCLIENT"} and $self->skip_if_relayclient
!ll&&!rl&&r
000
MC/DC0%0/2
exists $ENV{"RELAYCLIENT"} and $self->skip_if_relayclient
exists $ENV{"RELAYCLIENT"} $self->skip_if_relayclient
Truth table0%0/3
A and B
A= exists $ENV{"RELAYCLIENT"}B= $self->skip_if_relayclient
ABresult
0X0
100
111
102 0 $self->debug("$package skipped");
103 0 return;
104 }
105  
106 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->skip_if_remote_ip_matches and my $match = $self->skip_if_remote_ip_matches->match_ip($self->message->remote_ip)) 0 0 0
Condition0%0/3
$self->skip_if_remote_ip_matches and my $match = $self->skip_if_remote_ip_matches->match_ip($self->message->remote_ip)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->skip_if_remote_ip_matches and my $match = $self->skip_if_remote_ip_matches->match_ip($self->message->remote_ip)
$self->skip_if_remote_ip_matches my $match = $self->skip_if_remote_ip_matches->match_ip($self->message->remote_ip)
Truth table0%0/3
A and B
A= $self->skip_if_remote_ip_matchesB= my $match = $self->skip_if_remote_ip_matches->match_ip($self->message->remote_ip)
ABresult
0X0
100
111
107 $self->skip_if_remote_ip_matches
108 && (
109 my $match = $self->skip_if_remote_ip_matches->match_ip(
110 $self->message->remote_ip
111 )
112 )
113 )
114 {
115 0 $self->debug("$package skipped because remote IP belongs to $match");
116 0 return;
117 }
118  
119 0 ▶ if ( my $skip_for_sender = $self->skip_for_sender ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $skip_for_sender = $self->skip_for_sender) 0 0 0
120 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (match_address($skip_for_sender, my $sender = $self->message->from)) 0 0 0
121 match_address(
122 $skip_for_sender, my $sender = $self->message->from
123 )
124 )
125 {
126 0 $self->debug( "$package skipped because of sender", $sender );
127 0 return;
128 }
129 }
130  
131 0 ▶ if ( ( my $skip_for_from = $self->skip_for_from )
Branch: 1 branch
BranchTrueFalseTotal
if (my $skip_for_from = $self->skip_for_from and my $from = $self->message->header_from) 0 0 0
Condition0%0/3
my $skip_for_from = $self->skip_for_from and my $from = $self->message->header_from
!ll&&!rl&&r
000
MC/DC0%0/2
my $skip_for_from = $self->skip_for_from and my $from = $self->message->header_from
my $skip_for_from = $self->skip_for_from my $from = $self->message->header_from
Truth table0%0/3
A and B
A= my $skip_for_from = $self->skip_for_fromB= my $from = $self->message->header_from
ABresult
0X0
100
111
132 && ( my $from = $self->message->header_from ) )
133 {
134 0 ▶ if ( match_address( $skip_for_from, $from = $from->address ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (match_address($skip_for_from, $from = $from->address)) 0 0 0
135 0 $self->debug( "$package skipped because of RFC5322.From", $from );
136 0 return;
137 }
138 }
139  
140 0 ▶ if ( my $skip_for_rcpt = $self->skip_for_rcpt ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $skip_for_rcpt = $self->skip_for_rcpt) 0 0 0
141 0 for ( $self->message->to ) {
142 0 ▶ next unless match_address( $skip_for_rcpt, $_ );
Branch: 1 branch
BranchTrueFalseTotal
unless match_address($skip_for_rcpt, $_) 0 0 0
143 0 $self->debug( "$package skipped because of rcpt", $_ );
144 0 return;
145 }
146 }
147  
148 0 $self->debug("$package started");
149 0 $self->filter;
150 }
151  
152 0 ▶sub _exit {
Subroutine _exit: not called CC=4 SCAR=30.0
153 0 my $self = shift;
154 0 my $status = shift;
155 0 ▶ my $reject_text = shift // $self->reject_text;
Condition0%0/3
shift() // $self->reject_text
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() // $self->reject_text
shift() $self->reject_text
Truth table0%0/3
A // B
A= shift()B= $self->reject_text
ABresult
1X1
011
000
156 0 ▶ $reject_text = $reject_text->(@_)
Branch: 1 branch
BranchTrueFalseTotal
if ref $reject_text and "CODE" eq ref $reject_text 0 0 0
Condition0%0/3
ref $reject_text and "CODE" eq ref $reject_text
!ll&&!rl&&r
000
MC/DC0%0/2
ref $reject_text and "CODE" eq ref $reject_text
ref $reject_text "CODE" eq ref $reject_text
Truth table0%0/3
A and B
A= ref $reject_textB= "CODE" eq ref $reject_text
ABresult
0X0
100
111
157 if ref $reject_text && 'CODE' eq ref $reject_text;
158 0 $reject_text =~ y/\n/ /;
159 0 $self->feedback_fh->print( $status, $reject_text );
160  
161 0 exit 88;
162 }
163  
164 1 ▶END {
Subroutine END: called CC=1 SCAR=0.0
165 1 __PACKAGE__->debug( 'exit code' => $? );
166 1 say STDERR "$FindBin::Script\[$$]: " . join '; ', @debug;
167 }
168  
169 __END__
« Mail/Qmail/Filter/Base/CalloutVerify.pm ↑ Summary Mail/Qmail/Filter/SpamAssassin.pm »