lib/Mail/Decency/Policy/GeoWeight.pm untested

filter: Statementstmt 0.0% 0 / 72 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 135 CC 17 SCAR 44.9
CC
17
Cov
0%
init
156.0
handle
42.0
print_stats
2.0
CRAP 89.0
1 package Mail::Decency::Policy::GeoWeight;
2  
3 use Moose;
4 extends 'Mail::Decency::Policy::Core';
5  
6 use version 0.74; our $VERSION = qv( "v0.1.6" );
7  
8 use Geo::IP;
9 use Data::Dumper;
10 use DateTime;
11  
12 =head1 NAME
13  
14 Mail::Decency::Policy::GeoWeight
15  
16 =head1 DESCRIPTION
17  
18 Implements weighting and statistics by countries. It can be used for collecting stats from which countries the senders come from as well as for fighting spam by scoring certain countries (negative or positive).
19  
20 B<This module requires Geo::IP which itself requires various libraries on your OS.>
21  
22 =head1 CONFIG
23  
24 ---
25 disable: 0
26
27 enable_stats: 1
28
29 weight_classes:
30 -
31 countries:
32 - DE
33 - US
34 - AU
35 weight: 10
36 -
37 countries:
38 - SE
39 - DK
40 weight: 5
41 - { countries: [ 'XX' ], weight: -100 }
42
43 weight_default: -5
44  
45  
46 =head1 CLASS ATTRIBUTES
47  
48  
49 =head2 weight_by_country : HashRef
50  
51 Weight class, containging one or multiple country codes and a weighting (positive or negative)
52  
53 =cut
54  
55 has weight_by_country => ( is => 'rw', isa => 'HashRef[Int]', default => sub { {} } );
56  
57 =head2 weight_default : Int
58  
59 Weight for all values not specified via "weight_by_country"
60  
61 =cut
62  
63 has weight_default => ( is => 'rw', isa => 'Int', default => 0 );
64  
65 =head2 enable_stats : Bool
66  
67 Enable statistics by country (either stats or weight or both should be enabled)
68  
69 =cut
70  
71 has enable_stats => ( is => 'rw', isa => 'Bool', default => 0 );
72  
73 =head2 enable_weight : Bool
74  
75 Enable weighting (either stats or weight or both should be enabled)
76  
77 =cut
78  
79 has enable_weight => ( is => 'rw', isa => 'Bool', default => 1 );
80  
81 =head2 geo_ip : Geo::IP
82  
83 =cut
84  
85 has geo_ip => ( is => 'ro', isa => 'Geo::IP', default => sub { Geo::IP->new( GEOIP_STANDARD ) } );
86  
87 =head2 schema_definition
88  
89 Database schema
90  
91 =cut
92  
93 has schema_definition => ( is => 'ro', isa => 'HashRef[HashRef]', default => sub {
94 {
95 geo => {
96 stats => {
97 country => [ varchar => 2 ],
98 interval => [ varchar => 25 ],
99 counter => 'integer',
100 -unique => [ 'country', 'interval' ]
101 },
102 }
103 };
104 } );
105  
106  
107 =head1 METHODS
108  
109 =head2 init
110  
111 Checks weight classes
112  
113 =cut
114  
115 sub init {
116 my ( $self ) = @_;
117
118 # enable stats
119 $self->enable_weight( 0 )
120 if $self->config->{ disable_weight };
121
122 if ( $self->enable_weight ) {
123
124 # having weight classes -> check and setup
125 if ( defined ( my $weight_classes_ref = $self->config->{ weight_classes } ) ) {
126 my @classes = ref( $weight_classes_ref ) eq 'ARRAY'
127 ? @{ $weight_classes_ref }
128 : ( $weight_classes_ref )
129 ;
130
131 # check classes
132 foreach my $class_ref( @classes ) {
133 die "'$class_ref' is not a HashRef! weight_classes should be ArrayRef[HashRef]\n"
134 unless ref( $class_ref ) eq 'HASH';
135 die "Require 'countries' as ArrayRef\n"
136 unless defined $class_ref->{ countries } && ref( $class_ref->{ countries } ) eq 'ARRAY';
137 die "Require 'weight' for countries '". join( ", ", @{ $class_ref->{ countries } } ). "'\n"
138 unless defined $class_ref->{ weight } && $class_ref->{ weight } =~ /^\d+$/;
139
140 foreach my $country( @{ $class_ref->{ countries } } ) {
141 die "Please use 2-char country code format like 'DE' or 'US'.. '$country' does not fit\n"
142 unless length( $country ) == 2 && $country =~ /^[a-z]{2}$/i;
143 $self->weight_by_country->{ uc( $country ) } = $class_ref->{ weight };
144 }
145 }
146 }
147
148 # having default weight ..
149 $self->weight_default( $self->config->{ weight_default } )
150 if defined $self->config->{ weight_default };
151 }
152
153 # enable stats
154 $self->enable_stats( 1 )
155 if $self->config->{ enable_stats };
156
157 die "You have to enable at least one of stats or weight\n"
158 unless $self->enable_stats || $self->enable_weight;
159
160 return ;
161 }
162  
163  
164 =head2 handle
165  
166 Either build stats per country or score with negative or positve weight per country or do both
167  
168 =cut
169  
170 sub handle {
171 my ( $self, $server, $attrs_ref ) = @_;
172
173 # get client's country
174 my $country = $self->geo_ip->country_code_by_addr( $attrs_ref->{ client_address } );
175
176 # no country determiend .. probably LAN ip ..
177 return unless $country;
178
179 # write for stats
180 if ( $self->enable_stats ) {
181 my $dt = DateTime->now( time_zone => 'local' );
182 my @intervals = ( 'total' );
183 push @intervals, (
184 $dt->strftime( '%Y' ),
185 $dt->strftime( '%Y-%m' ),
186 $dt->strftime( '%Y-%m-%d' ),
187 );
188 $self->database->increment( geo => stats => {
189 country => $country,
190 interval => $_
191 }, 1, 'counter' ) for @intervals;
192 }
193
194 if ( $self->enable_weight ) {
195
196 # having determiend weight
197 my $weight = defined $self->weight_by_country->{ $country }
198 ? $self->weight_by_country->{ $country }
199 : $self->weight_default
200 ;
201 if ( $weight ) {
202 $self->add_spam_score( $weight, join( ";",
203 "Weight: $weight",
204 "Country: $country"
205 ), "GeoWeight: $weight" );
206 }
207 }
208
209 return ;
210 }
211  
212  
213  
214 =head2 print_stats
215  
216 Print statistics per country
217  
218 =cut
219  
220 sub print_stats {
221 my ( $self ) = @_;
222
223 my $dt = DateTime->now( time_zone => 'local' );
224 my @intervals = ( 'total' );
225 push @intervals, (
226 $dt->strftime( '%Y' ),
227 $dt->strftime( '%Y-%m' ),
228 $dt->strftime( '%Y-%m-%d' ),
229 );
230
231 print "\n# **** GEO STATS ****\n\n";
232 foreach my $interval( @intervals ) {
233
234 my ( $handle, $meth ) = $self->database->search_read( geo => stats => {
235 interval => $interval
236 } );
237 print "# Interval $interval\n";
238 while ( my $ref = $handle->$meth ) {
239 print "$ref->{ interval };$ref->{ country };$ref->{ counter }\n";
240 }
241 print "\n";
242 }
243 }
244  
245  
246  
247 =head1 AUTHOR
248  
249 Ulrich Kautz <uk@fortrabbit.de>
250  
251 =head1 COPYRIGHT
252  
253 Copyright (c) 2010 the L</AUTHOR> as listed above
254  
255 =head1 LICENCSE
256  
257 This library is free software and may be distributed under the same terms as perl itself.
258  
259 =cut
260  
261  
262  
263  
264 1;