MtPolicyd/Plugin/GeoIPAction.pm

filter: Statementstmt 37.9% 22 / 58 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 42.8% 3 / 7 Podpod 50.0% 1 / 2 totaltotal 30.2% 26 / 86 CC 9 SCAR 36.8
CC
9
Cov
29%
run
72.0
is_in_country_codes
6.0
__ANON__
2.0
CRAP 39.7
« MtPolicyd/Plugin/SPF.pm ↑ Summary MtPolicyd/Plugin/Greylist.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 = '1.12'; # 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' ],
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=8 SCAR=42.8
Pod documented
47 0 my $ip = $r->attr('client_address');
48 0 my $session = $r->session;
49  
50 0 my $enabled = $self->get_uc( $session, 'enabled' );
51 0 ▶ if( $enabled eq 'off' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enabled eq "off") 0 0 0
52 0 return;
53 }
54  
55 0 my $result_key = 'geoip-'.$self->result_from.'-result';
56 0 ▶ if( ! defined $session->{$result_key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $session->{$result_key}) 0 0 0
57 0 $self->log( $r, 'no GeoIP check result for '.$self->name.' found!');
58 0 return;
59 }
60 0 my ( $country_code ) = @{$session->{$result_key}};
61  
62 0 ▶ if( ! $self->is_in_country_codes( $country_code ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_in_country_codes($country_code)) 0 0 0
63 0 $self->log( $r, 'country_code '.$country_code.' of IP not in country_code list'.$self->name);
64 0 return;
65 }
66  
67 0 $self->log( $r, 'country code '.$country_code.' on list'.$self->name );
68 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
69 0 $self->add_score($r, $self->name => $self->score);
70 }
71  
72 0 ▶ if( $self->mode eq 'reject' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode eq "reject") 0 0 0
73 0 return Mail::MtPolicyd::Plugin::Result->new(
74 action => $self->_get_reject_action($ip, $country_code ),
75 abort => 1,
76 );
77 }
78 0 ▶ if( $self->mode eq 'accept' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode eq "accept") 0 0 0
79 0 return Mail::MtPolicyd::Plugin::Result->new_dunno;
80 }
81  
82 0 return;
83 }
84  
85 sub _get_reject_action {
86 0 ▶ my ( $self, $ip, $cc ) = @_;
Subroutine _get_reject_action: not called CC=1 SCAR=6.9
87 0 my $message = $self->reject_message;
88 0 $message =~ s/%IP%/$ip/;
89 0 $message =~ s/%CC%/$cc/;
90 0 return('reject '.$message);
91 }
92  
93 1 __PACKAGE__->meta->make_immutable;
94  
95 1 1;
96  
97  
98 __END__
99 =pod
100  
101 =head1 NAME
102  
103 Mail::MtPolicyd::Plugin::GeoIPAction - mtpolicyd plugin for checking geo information of an ip
104  
105 =head1 VERSION
106  
107 version 1.12
108  
109 =head1 DESCRIPTION
110  
111 This plugin will execute an action or score based on a previous lookup
112 done with GeoIPLookup plugin.
113  
114 =head1 PARAMETERS
115  
116 =over
117  
118 =item result_from (required)
119  
120 Take the GeoIP information from the result of this plugin.
121  
122 The plugin in must be executed before this plugin.
123  
124 =item (uc_)enabled (default: on)
125  
126 Enable/disable this plugin.
127  
128 =item country_codes (required)
129  
130 A comma seperated list of 2 letter country codes to match.
131  
132 =item mode (default: reject)
133  
134 If set to 'passive' no action will be returned.
135  
136 =item reject_message (default: 'delivery from %CC% (%IP%) rejected)
137  
138 Could be used to specify an custom reject message.
139  
140 =item score (default: empty)
141  
142 A score to apply to the message.
143  
144 =back
145  
146 =head1 AUTHOR
147  
148 Markus Benning <ich@markusbenning.de>
149  
150 =head1 COPYRIGHT AND LICENSE
151  
152 This software is Copyright (c) 2014 by Markus Benning <ich@markusbenning.de>.
153  
154 This is free software, licensed under:
155  
156 The GNU General Public License, Version 2, June 1991
157  
158 =cut
159  
« MtPolicyd/Plugin/SPF.pm ↑ Summary MtPolicyd/Plugin/Greylist.pm »