MtPolicyd/Plugin/GeoIPAction.pm

filter: Statementstmt 36.0% 22 / 61 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 42.8% 3 / 7 Podpod 50.0% 1 / 2 totaltotal 27.0% 26 / 96 CC 11 SCAR 41.9
CC
11
Cov
27%
run
110.0
is_in_country_codes
6.0
__ANON__
2.0
CRAP 65.8
« MtPolicyd/Plugin/CtIpRep.pm ↑ Summary MtPolicyd/Plugin/SPF.pm »
1 package Mail::MtPolicyd::Plugin::GeoIPAction;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '2.01'; # VERSION
7 # ABSTRACT: mtpolicyd plugin for checking geo information of an ip
8  
9  
10 1 extends 'Mail::MtPolicyd::Plugin';
11 1 with 'Mail::MtPolicyd::Plugin::Role::Scoring';
12 1 with 'Mail::MtPolicyd::Plugin::Role::UserConfig' => {
13 'uc_attributes' => [ 'enabled', 'mode' ],
14 };
15  
16 1 ▶use Mail::MtPolicyd::Plugin::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 has 'result_from' => ( is => 'rw', isa => 'Str', required => 1 );
19 1 has 'enabled' => ( is => 'rw', isa => 'Str', default => 'on' );
20 1 has 'mode' => ( is => 'rw', isa => 'Str', default => 'reject' );
21  
22 1 has 'country_codes' => ( is => 'rw', isa => 'Str', required => 1 );
23 has '_country_codes' => (
24 is => 'ro', isa => 'ArrayRef', lazy => 1,
25 default => sub {
26 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
27 0 return [ split(/\s*,\s*/, $self->country_codes) ];
28 },
29 1 );
30  
31 sub is_in_country_codes {
32 0 ▶ my ( $self, $cc ) = @_;
Subroutine is_in_country_codes: not called CC=2 SCAR=17.9
Pod undocumented
33 0 ▶ if ( grep { $_ eq $cc } @{$self->_country_codes} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $cc;} @{$self->_country_codes;}) 0 0 0
34 0 return(1);
35 }
36 0 return(0);
37 }
38  
39 1 has 'reject_message' => (
40 is => 'ro', isa => 'Str', default => 'delivery from %CC% (%IP%) rejected',
41 );
42  
43 1 has 'score' => ( is => 'rw', isa => 'Maybe[Num]' );
44  
45 sub run {
46 0 ▶ my ( $self, $r ) = @_;
Subroutine run: not called CC=10 SCAR=47.0
Pod documented
47 0 my $ip = $r->attr('client_address');
48 0 my $session = $r->session;
49 0 my $mode = $self->get_uc( $session, 'mode' );
50 0 my $enabled = $self->get_uc( $session, 'enabled' );
51  
52 0 ▶ if( $enabled eq 'off' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enabled eq "off") 0 0 0
53 0 return;
54 }
55  
56 0 my $result_key = 'geoip-'.$self->result_from.'-result';
57 0 ▶ if( ! defined $session->{$result_key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $session->{$result_key}) 0 0 0
58 0 $self->log( $r, 'no GeoIP check result for '.$self->name.' found!');
59 0 return;
60 }
61 0 my ( $country_code ) = @{$session->{$result_key}};
62 0 ▶ if( ! defined $country_code ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $country_code) 0 0 0
63 0 return;
64 }
65  
66 0 ▶ if( ! $self->is_in_country_codes( $country_code ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_in_country_codes($country_code)) 0 0 0
67 0 $self->log( $r, 'country_code '.$country_code.' of IP not in country_code list'.$self->name);
68 0 return;
69 }
70  
71 0 $self->log( $r, 'country code '.$country_code.' on list'.$self->name );
72 0 ▶ if( defined $self->score && ! $r->is_already_done('geoip-'.$self->name.'-score') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->score and not $r->is_already_done('geoip-' . $self->name . '-score')) 0 0 0
Condition0%0/3
defined $self->score and not $r->is_already_done('geoip-' . $self->name . '-score')
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->score and not $r->is_already_done('geoip-' . $self->name . '-score')
defined $self->score not $r->is_already_done('geoip-' . $self->name . '-score')
Truth table0%0/3
A and not(B)
A= defined $self->scoreB= not $r->is_already_done('geoip-' . $self->name . '-score')
ABresult
0X0
100
111
73 0 $self->add_score($r, $self->name => $self->score);
74 }
75  
76 0 ▶ if( $mode eq 'reject' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "reject") 0 0 0
77 0 return Mail::MtPolicyd::Plugin::Result->new(
78 action => $self->_get_reject_action($ip, $country_code ),
79 abort => 1,
80 );
81 }
82 0 ▶ if( $mode eq 'accept' || $mode eq 'dunno' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "accept" or $mode eq "dunno") 0 0 0
Condition0%0/3
$mode eq "accept" or $mode eq "dunno"
l!l&&r!l&&!r
000
MC/DC0%0/2
$mode eq "accept" or $mode eq "dunno"
$mode eq "accept" $mode eq "dunno"
Truth table0%0/3
A or B
A= $mode eq "accept"B= $mode eq "dunno"
ABresult
1X1
011
000
83 0 return Mail::MtPolicyd::Plugin::Result->new_dunno;
84 }
85  
86 0 return;
87 }
88  
89 sub _get_reject_action {
90 0 ▶ my ( $self, $ip, $cc ) = @_;
Subroutine _get_reject_action: not called CC=1 SCAR=6.9
91 0 my $message = $self->reject_message;
92 0 $message =~ s/%IP%/$ip/;
93 0 $message =~ s/%CC%/$cc/;
94 0 return('reject '.$message);
95 }
96  
97 1 __PACKAGE__->meta->make_immutable;
98  
99 1 1;
100  
101 __END__
102  
103 =pod
104  
105 =encoding UTF-8
106  
107 =head1 NAME
108  
109 Mail::MtPolicyd::Plugin::GeoIPAction - mtpolicyd plugin for checking geo information of an ip
110  
111 =head1 VERSION
112  
113 version 2.01
114  
115 =head1 DESCRIPTION
116  
117 This plugin will execute an action or score based on a previous lookup
118 done with GeoIPLookup plugin.
119  
120 =head1 PARAMETERS
121  
122 =over
123  
124 =item result_from (required)
125  
126 Take the GeoIP information from the result of this plugin.
127  
128 The plugin in must be executed before this plugin.
129  
130 =item (uc_)enabled (default: on)
131  
132 Enable/disable this plugin.
133  
134 =item country_codes (required)
135  
136 A comma separated list of 2 letter country codes to match.
137  
138 =item (uc_)mode (default: reject)
139  
140 If set to 'passive' no action will be returned.
141  
142 =item reject_message (default: 'delivery from %CC% (%IP%) rejected)
143  
144 Could be used to specify an custom reject message.
145  
146 =item score (default: empty)
147  
148 A score to apply to the message.
149  
150 =back
151  
152 =head1 AUTHOR
153  
154 Markus Benning <ich@markusbenning.de>
155  
156 =head1 COPYRIGHT AND LICENSE
157  
158 This software is Copyright (c) 2014 by Markus Benning <ich@markusbenning.de>.
159  
160 This is free software, licensed under:
161  
162 The GNU General Public License, Version 2, June 1991
163  
164 =cut
« MtPolicyd/Plugin/CtIpRep.pm ↑ Summary MtPolicyd/Plugin/SPF.pm »