MtPolicyd/Plugin/Condition.pm

filter: Statementstmt 92.4% 49 / 53 Branchbran 72.2% 13 / 18 Conditioncond 41.6% 5 / 12 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 100% 5 / 5 Podpod 100% 1 / 1 totaltotal 76.2% 74 / 97 CC 14 SCAR 22.7
CC
14
Cov
81%
_match
15.0
run
6.0
BEGIN
1.0
CRAP 9.7
« MtPolicyd/Client/App.pm ↑ Summary MtPolicyd/Plugin/SqlUserConfig.pm »
1 package Mail::MtPolicyd::Plugin::Condition;
2  
3 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '1.11'; # VERSION
7 # ABSTRACT: mtpolicyd plugin for conditions based on session values
8  
9 2 extends 'Mail::MtPolicyd::Plugin';
10  
11 2 with 'Mail::MtPolicyd::Plugin::Role::Scoring';
12 2 with 'Mail::MtPolicyd::Plugin::Role::UserConfig' => {
13 'uc_attributes' => [ 'score', 'action' ],
14 };
15 2 with 'Mail::MtPolicyd::Plugin::Role::PluginChain';
16  
17 2 ▶use Mail::MtPolicyd::Plugin::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20 2 has 'key' => ( is => 'ro', isa => 'Str', required => 1 );
21  
22 2 has 'score' => ( is => 'rw', isa => 'Maybe[Num]' );
23 2 has 'action' => ( is => 'rw', isa => 'Maybe[Str]' );
24  
25 2 has 'match' => ( is => 'ro', isa => 'Maybe[Str]' );
26 2 has 're_match' => ( is => 'ro', isa => 'Maybe[Str]' );
27  
28 2 has 'gt_match' => ( is => 'ro', isa => 'Maybe[Num]' );
29 2 has 'lt_match' => ( is => 'ro', isa => 'Maybe[Num]' );
30  
31 sub _match {
32 3 ▶ my ( $self, $value ) = @_;
Subroutine _match: called CC=9 SCAR=27.1
33  
34 3 ▶ if( defined $self->match &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->match and $value eq $self->match) 2 1 3
Condition66%2/3
defined $self->match and $value eq $self->match
!ll&&!rl&&r
012
MC/DC50%1/2
defined $self->match and $value eq $self->match
defined $self->match $value eq $self->match
Truth table66%2/3
A and B
A= defined $self->matchB= $value eq $self->match
ABresult
0X0
100
111
35 $value eq $self->match ) {
36 2 return 1;
37 }
38  
39 1 my $regex = $self->re_match;
40 1 ▶ if( defined $regex && $value =~ m/$regex/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $regex and $value =~ /$regex/) 0 1 1
Condition33%1/3
defined $regex and $value =~ /$regex/
!ll&&!rl&&r
100
MC/DC0%0/2
defined $regex and $value =~ /$regex/
defined $regex $value =~ /$regex/
Truth table33%1/3
A and B
A= defined $regexB= $value =~ /$regex/
ABresult
0X0
100
111
41 0 return 1;
42 }
43  
44 1 ▶ if( defined $self->lt_match &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->lt_match and $value < $self->lt_match) 0 1 1
Condition33%1/3
defined $self->lt_match and $value < $self->lt_match
!ll&&!rl&&r
100
MC/DC0%0/2
defined $self->lt_match and $value < $self->lt_match
defined $self->lt_match $value < $self->lt_match
Truth table33%1/3
A and B
A= defined $self->lt_matchB= $value < $self->lt_match
ABresult
0X0
100
111
45 $value < $self->lt_match ) {
46 0 return 1;
47 }
48  
49 1 ▶ if( defined $self->gt_match &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->gt_match and $value > $self->gt_match) 0 1 1
Condition33%1/3
defined $self->gt_match and $value > $self->gt_match
!ll&&!rl&&r
100
MC/DC0%0/2
defined $self->gt_match and $value > $self->gt_match
defined $self->gt_match $value > $self->gt_match
Truth table33%1/3
A and B
A= defined $self->gt_matchB= $value > $self->gt_match
ABresult
0X0
100
111
50 $value > $self->gt_match ) {
51 0 return 1;
52 }
53  
54 1 return 0;
55 }
56  
57 sub run {
58 4 ▶ my ( $self, $r ) = @_;
Subroutine run: called CC=6 SCAR=18.0
Pod documented
59 4 my $key = $self->key;
60 4 my $session = $r->session;
61
62 4 my $value = $session->{$key};
63 4 ▶ if( ! defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 1 3 4
64 1 return;
65 }
66  
67 3 ▶ if( $self->_match($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_match($value)) 2 1 3
68 2 $self->log($r, $key.' matched '.$value);
69 2 my $score = $self->get_uc($session, 'score');
70 2 ▶ if( defined $score ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $score) 0 2 2
71 0 $self->add_score($r, $self->name => $score);
72 }
73 2 my $action = $self->get_uc($session, 'action');
74 2 ▶ if( defined $action ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $action) 1 1 2
75 1 return Mail::MtPolicyd::Plugin::Result->new(
76 action => $action,
77 abort => 1,
78 );
79 }
80 1 ▶ if( defined $self->chain ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->chain) 1 0 1
81 1 my $chain_result = $self->chain->run( $r );
82 1 return( @{$chain_result->plugin_results} );
83 }
84 }
85  
86 1 return;
87 }
88  
89 2 __PACKAGE__->meta->make_immutable;
90  
91 2 1;
92  
93  
94 __END__
95 =pod
96  
97 =head1 NAME
98  
99 Mail::MtPolicyd::Plugin::Condition - mtpolicyd plugin for conditions based on session values
100  
101 =head1 VERSION
102  
103 version 1.11
104  
105 =head1 DESCRIPTION
106  
107 Will return an action, score or execute futher plugins if the specified condition matched.
108  
109 =head1 PARAMETERS
110  
111 =over
112  
113 =item key (required)
114  
115 The name of the variable within the session to check.
116  
117 =back
118  
119 At least one of the following parameters should be given or your condition will
120 never match:
121  
122 =over
123  
124 =item match (default: empty)
125  
126 Simple string equal match.
127  
128 =item re_match (default: empty)
129  
130 Match content of the session variable against an regex.
131  
132 =item lt_match (default: empty)
133  
134 Match if numerical less than.
135  
136 =item gt_match (default: empty)
137  
138 Match if numerical greater than.
139  
140 =back
141  
142 Finally an action must be specified.
143  
144 First the score will be applied the the action will be executed
145 or if specified additional plugins will be executed.
146  
147 =over
148  
149 =item action (default: empty)
150  
151 The action to return when the condition matched.
152  
153 =item score (default: empty)
154  
155 The score to add if the condition matched.
156  
157 =item Plugin (default: empty)
158  
159 Execute this plugins when the condition matched.
160  
161 =back
162  
163 =head1 EXAMPLE: execute postgrey action in postfix
164  
165 If the session variable "greylisting" is "on" return the postfix action "postgrey":
166  
167 <Plugin trigger-greylisting>
168 module = "Condition"
169 key = "greylisting"
170 match = "on"
171 action = "postgrey"
172 </Plugin>
173  
174 The variable may be set by a UserConfig module like SqlUserConfig.
175  
176 The postgrey action in postfix may look like:
177  
178 smtpd_restriction_classes = postgrey
179 postgrey = check_policy_service inet:127.0.0.1:11023
180  
181 =head1 AUTHOR
182  
183 Markus Benning <ich@markusbenning.de>
184  
185 =head1 COPYRIGHT AND LICENSE
186  
187 This software is Copyright (c) 2014 by Markus Benning <ich@markusbenning.de>.
188  
189 This is free software, licensed under:
190  
191 The GNU General Public License, Version 2, June 1991
192  
193 =cut
194  
« MtPolicyd/Client/App.pm ↑ Summary MtPolicyd/Plugin/SqlUserConfig.pm »