lib/Mail/Decency/ContentFilter/DSPAM.pm

filter: Statementstmt 34.7% 33 / 95 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 63.6% 7 / 11 Podpod 75.0% 3 / 4 totaltotal 27.5% 43 / 156 CC 20 SCAR 40.8
CC
20
Cov
24%
retreive_result
132.0
handle
30.0
train
30.0
CRAP 59.0
« lib/Mail/Decency/LogParser/Aggregator.pm ↑ Summary lib/Mail/Decency/LogParser/Core/CSV.pm »
1 package Mail::Decency::ContentFilter::DSPAM;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 extends qw/
5 Mail::Decency::ContentFilter::Core
6 /;
7 1 with qw/
8 Mail::Decency::ContentFilter::Core::Spam
9 Mail::Decency::ContentFilter::Core::User
10 /;
11  
12 1 ▶use version 0.74; our $VERSION = qv( "v0.1.6" );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Net::LMTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Mail::Decency::ContentFilter::Core::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 =head1 NAME
21  
22 Mail::Decency::ContentFilter::DSPAM
23  
24 =head1 DESCRIPTION
25  
26 Uses LMTP to connect directly to running DSPAM server an retreive filter result
27  
28 =head1 DSPAM CONFIG
29  
30 You have to configure decency accordingly to the DSPAM settings. Modify in dspam.conf:
31  
32 =over
33  
34 =item * ServerPass.*
35  
36 DSPAM:
37 ServerPass.Relay1 "secret"
38  
39 dececny:
40 dspam_client_ident: 'secret@Relay1'
41  
42 =item * ServerHost, ServerPort
43  
44 DSPAM:
45 ServerHost 127.0.0.1
46 ServerPort 17000
47  
48 decency:
49 dspam_host: '127.0.0.1'
50 dspam_port: 17000
51  
52 =back
53  
54 =head1 CLASS ATTRIBUTES
55  
56 =head2 dspam_client_ident : Str
57  
58 The DSPAM auth string, as set for ClientIdent in dspam.conf
59  
60 Defaults: secret@Relay1
61  
62 =cut
63  
64 1 has dspam_client_ident => (
65 is => 'rw',
66 isa => 'Str',
67 default => 'secret@Relay1'
68 );
69  
70 =head2 dspam_host : Str
71  
72 Host string/ip where DSPAM runs
73  
74 Default: 127.0.0.1
75  
76 =cut
77  
78 1 has dspam_host => (
79 is => 'rw',
80 isa => 'Str',
81 default => '127.0.0.1'
82 );
83  
84 =head2 dspam_port : Int
85  
86 Port where DSPAM listens
87  
88 Default: 1024
89  
90 =cut
91  
92 1 has dspam_port => (
93 is => 'rw',
94 isa => 'Str',
95 default => '1024'
96 );
97  
98  
99 =pod
100  
101 Private variables
102  
103 =cut
104  
105 1 has mode_check => (
106 is => 'ro',
107 isa => 'Str',
108 default => '--user %user% --client --classify --stdout'
109 );
110  
111 1 has mode_learn_spam => (
112 is => 'ro',
113 isa => 'Str',
114 default => '--client --user %user% --mode=teft --source=corpus --class=spam --deliver=spam --stdout'
115 );
116  
117 1 has mode_unlearn_spam => (
118 is => 'ro',
119 isa => 'Str',
120 default => '--client --user %user% --mode=toe --source=corpus --class=innocent --deliver=innocent --stdout'
121 );
122  
123 1 has mode_learn_ham => (
124 is => 'ro',
125 isa => 'Str',
126 default => '--client --user %user% --mode=teft --source=corpus --class=innocent --deliver=innocent --stdout'
127 );
128  
129 1 has mode_unlearn_ham => (
130 is => 'ro',
131 isa => 'Str',
132 default => '--client --user %user% --mode=toe --source=corpus --class=spam --deliver=spam --stdout'
133 );
134  
135 =head1 METHODS
136  
137  
138 =head2 init
139  
140 =cut
141  
142 0 ▶sub pre_init {
Subroutine pre_init: not called CC=2 SCAR=17.9
Pod undocumented
143 0 my ( $self ) = @_;
144 0 ▶ push @{ $self->{ config_params } ||=[] }, qw/ dspam_client_ident dspam_host dspam_port /;
Condition0%0/2
$self->{"config_params"} ||= []
l!l
00
MC/DC0%0/1
$self->{"config_params"} ||= []
$self->{"config_params"}
Truth table0%0/2
A
A= $self->{"config_params"}
Aresult
11
00
145 0 return ;
146 }
147  
148 =head2 handle
149  
150 Pipeps mails through DSPAM server, retreives result
151  
152 =cut
153  
154  
155 0 ▶sub handle {
Subroutine handle: not called CC=5 SCAR=34.0
Pod documented
156 0 my ( $self ) = @_;
157
158 # get result from dspam
159 0 my $result = $self->retreive_result( 'check' );
160
161 # no result -> do not bother
162 0 ▶ return unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
163
164 0 $self->logger->debug2( "DSPAM result: '$result'" );
165
166 # oops, wrong dspam_client_ident
167 0 ▶ if ( $result =~ /Need MAIL FROM here/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result =~ /Need MAIL FROM here/) 0 0 0
168 0 $self->logger->error( "Wrong auth credentials for DSPAM. Please set dspam_client_ident the same as your ServerPass.* in dspam.conf" );
169 0 return ;
170 }
171
172 # parse result
173 my %parsed = map {
174 0 my ( $n, $v ) = split( /\s*[:=]\s*/, $_, 2 );
175 0 $v =~ s/^"//;
176 0 $v =~ s/"$//;
177 0 ( $n => lc( $v ) );
178 } split( /\s*;\s*/, $result );
179
180 # get weighting
181 0 my $weight = 0;
182 0 my @info = ();
183 0 ▶ if ( $parsed{ result } eq 'innocent' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($parsed{'result'} eq 'innocent') { } 0 0 0
elsif ($parsed{'result'} eq 'spam') { } 0 0 0
184 0 $weight = $self->weight_innocent;
185 }
186 elsif ( $parsed{ result } eq 'spam' ) {
187 0 $weight = $self->weight_spam;
188 }
189 0 $self->logger->debug0( "Score mail to '$weight'" );
190
191 # add info for noisy headers
192 0 push @info, (
193 "DSPAM result: $parsed{ result }",
194 "DSPAM confidence: $parsed{ confidence }",
195 "DSPAM probability: $parsed{ probability }",
196 "DSPAM class: $parsed{ class }",
197 );
198
199 # add weight to content filte score
200 0 return $self->add_spam_score( $weight, \@info );
201 }
202  
203  
204 =head2 train
205  
206 =cut
207  
208 0 ▶sub train {
Subroutine train: not called CC=5 SCAR=34.0
Pod documented
209 0 my ( $self, $mode ) = @_;
210
211 0 ▶ die "Train mode has to be 'spam' or 'ham'\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $mode eq "spam" or $mode eq "ham" 0 0 0
Condition0%0/3
$mode eq "spam" or $mode eq "ham"
l!l&&r!l&&!r
000
MC/DC0%0/2
$mode eq "spam" or $mode eq "ham"
$mode eq "spam" $mode eq "ham"
Truth table0%0/3
A or B
A= $mode eq "spam"B= $mode eq "ham"
ABresult
1X1
011
000
212 unless $mode eq 'spam' || $mode eq 'ham';
213
214 0 my $result = $self->retreive_result( "learn_${mode}" );
215 0 print "> R $result\n";
216 0 ▶ return ( $result ? 1 : 0, $result, $result ? 0 : 1 );
Branch: 2 branches
BranchTrueFalseTotal
$result ? : 0 0 0
$result ? : 0 0 0
217 }
218  
219  
220 =head2 retreive_result
221  
222 Pass mail via L<Net::LMTP> to DSPAM an retreive result
223  
224 =cut
225  
226 0 ▶sub retreive_result {
Subroutine retreive_result: not called CC=11 SCAR=48.8
Pod documented
227 0 my ( $self, $mode ) = @_;
228
229 # determine mode
230 0 my $mode_method = "mode_${mode}";
231 0 ▶ die "Cannot use mode '$mode'. Not defined!\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($mode_method) 0 0 0
232 unless $self->can( $mode_method );
233 0 my $mode_cmd = $self->$mode_method;
234
235 # determine user for mode
236 0 ▶ if ( $mode_cmd =~ /%user%/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode_cmd =~ /%user%/) 0 0 0
237 0 my $user = $self->get_user();
238 0 $mode_cmd =~ s/%user%/$user/g;
239 }
240
241
242 # determine timeout
243 0 my $timeout = $self->timeout - 1;
244 0 ▶ $timeout = 300 if $timeout <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout <= 0 0 0 0
245
246 # connect via lmtp
247 0 my $lmtp;
248 0 eval {
249 $lmtp = Net::LMTP->new(
250 $self->dspam_host, $self->dspam_port,
251 Timeout => $timeout,
252 Helo => 'decency',
253 0 ▶ Debug => $ENV{ DEBUG_DSPAM } || 0
Condition0%0/2
$ENV{'DEBUG_DSPAM'} || 0
l!l
00
MC/DC0%0/1
$ENV{'DEBUG_DSPAM'} || 0
$ENV{'DEBUG_DSPAM'}
Truth table0%0/2
A
A= $ENV{'DEBUG_DSPAM'}
Aresult
11
00
254 );
255 };
256
257 # error in connection
258 0 ▶ if ( $@ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 0 0 0
elsif (not $lmtp) { } 0 0 0
259 0 $self->logger->error( "Error connecting to dspam (". $self->dspam_host. ":". $self->dspam_port. "): $@" );
260 0 return;
261 }
262 elsif ( ! $lmtp ) {
263 0 $self->logger->error( "Could not connect to dspam (". $self->dspam_host. ":". $self->dspam_port. "): $@" );
264 0 return ;
265 }
266
267 # send hello, authentify
268 0 $lmtp->_MAIL( "FROM: <". $self->dspam_client_ident. "> DSPAMPROCESSMODE=\"$mode_cmd\"" );
269
270 # send mail
271 0 $lmtp->data;
272
273 # retreive check result (maybe dspam refuses)
274 0 my ( $check ) = $lmtp->message;
275 0 ▶ if ( $check && $check =~ /DSPAM agent misconfigured:/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check and $check =~ /DSPAM agent misconfigured:/) 0 0 0
Condition0%0/3
$check and $check =~ /DSPAM agent misconfigured:/
!ll&&!rl&&r
000
MC/DC0%0/2
$check and $check =~ /DSPAM agent misconfigured:/
$check $check =~ /DSPAM agent misconfigured:/
Truth table0%0/3
A and B
A= $checkB= $check =~ /DSPAM agent misconfigured:/
ABresult
0X0
100
111
276 0 $self->logger->error( "Error communicating with DSPAM: $check" );
277 0 return ;
278 }
279
280 # write data to dspam
281 0 ▶ open my $fh, "<", $self->file or die "Cannot open current file '". $self->file. "' for read!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $self->file or die q[Cannot open current file '] . $self->file . "' for read!\n" 0 0 0
282 0 ▶ while ( my $l = <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $l = readline $fh)) 0 0 0
283 0 chomp $l;
284 0 $lmtp->datasend( $l. CRLF );
285 }
286 0 close $fh;
287 0 $lmtp->dataend;
288
289 # retreive result
290 0 my ( $result ) = $lmtp->getline;
291 0 chomp( $result );
292
293 # quit
294 0 $lmtp->quit;
295
296 0 return $result;
297 }
298  
299  
300 =head1 SEE ALSO
301  
302 =over
303  
304 =item * L<Mail::Decency::ContentFilter::Core::Cmd>
305  
306 =item * L<Mail::Decency::ContentFilter::Core::Spam>
307  
308 =item * L<Mail::Decency::ContentFilter::Bogofilter>
309  
310 =item * L<Mail::Decency::ContentFilter::CRM114>
311  
312  
313 =back
314  
315 =head1 AUTHOR
316  
317 Ulrich Kautz <uk@fortrabbit.de>
318  
319 =head1 COPYRIGHT
320  
321 Copyright (c) 2010 the L</AUTHOR> as listed above
322  
323 =head1 LICENCSE
324  
325 This library is free software and may be distributed under the same terms as perl itself.
326  
327 =cut
328  
329 1 1;
« lib/Mail/Decency/LogParser/Aggregator.pm ↑ Summary lib/Mail/Decency/LogParser/Core/CSV.pm »