lib/Mail/Decency/ContentFilter/Bogofilter.pm

filter: Statementstmt 49.0% 26 / 53 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 71.4% 5 / 7 Podpod 100% 1 / 1 totaltotal 34.4% 32 / 93 CC 13 SCAR 48.3
CC
13
Cov
33%
handle_filter_result
182.0
__ANON__
2.0
BEGIN
1.0
CRAP 124.9
« lib/Mail/Decency/Policy/Greylist.pm ↑ Summary lib/Mail/Decency/Policy/Honeypot.pm »
1 package Mail::Decency::ContentFilter::Bogofilter;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 extends qw/
5 Mail::Decency::ContentFilter::Core
6 /;
7  
8 1 with qw/
9 Mail::Decency::ContentFilter::Core::Spam
10 Mail::Decency::ContentFilter::Core::Cmd
11 Mail::Decency::ContentFilter::Core::User
12 Mail::Decency::ContentFilter::Core::WeightTranslate
13 /;
14  
15 1 ▶use version 0.74; our $VERSION = qv( "v0.1.6" );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use File::Temp qw/ tempfile /;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 =head1 NAME
22  
23 Mail::Decency::ContentFilter::Bogofilter
24  
25 =head1 DESCRIPTION
26  
27 Filter messages through bogofilter and translate results
28  
29 =head2 CONFIG
30  
31 ---
32
33 disable: 0
34
35 apply_spamicity: 0
36
37 cmd_check: '/usr/bin/bogofilter -c %user% -U -I %file% -v'
38 cmd_learn_spam: '/usr/bin/bogofilter -c %user% -s -I %file%'
39 cmd_unlearn_spam: '/usr/bin/bogofilter -c %user% -N -I %file%'
40 cmd_learn_ham: '/usr/bin/bogofilter -c %user% -n -I %file%'
41 cmd_unlearn_ham: '/usr/bin/bogofilter -c %user% -S -I %file%'
42
43 default_user: '/etc/bogofilter.cf'
44
45  
46 =cut
47  
48 1 has cmd_check => (
49 is => 'rw',
50 isa => 'Str',
51 default => '/usr/bin/bogofilter -c %user% -U -I %file% -v'
52 );
53  
54 1 has cmd_learn_spam => (
55 is => 'rw',
56 isa => 'Str',
57 default => '/usr/bin/bogofilter -c %user% -s -I %file%'
58 );
59  
60 1 has cmd_unlearn_spam => (
61 is => 'rw',
62 isa => 'Str',
63 default => '/usr/bin/bogofilter -c %user% -N -I %file%'
64 );
65  
66 1 has cmd_learn_ham => (
67 is => 'rw',
68 isa => 'Str',
69 default => '/usr/bin/bogofilter -c %user% -n -I %file%'
70 );
71  
72 1 has cmd_unlearn_ham => (
73 is => 'rw',
74 isa => 'Str',
75 default => '/usr/bin/bogofilter -c %user% -S -I %file%'
76 );
77  
78 1 has apply_spamicity => (
79 is => 'rw',
80 isa => 'Bool',
81 default => 0
82 );
83  
84 has config_params => (
85 is => 'ro',
86 isa => 'ArrayRef[Str]',
87 0 ▶ default => sub { [ qw/ apply_spamicity / ] }
Subroutine __ANON__: not called CC=1 SCAR=6.9
88 1 );
89  
90  
91 =head1 METHODS
92  
93  
94 =head2 handle_filter_result
95  
96 =cut
97  
98 0 ▶sub handle_filter_result {
Subroutine handle_filter_result: not called CC=13 SCAR=52.0
Pod documented
99 0 my ( $self, $result ) = @_;
100
101 # parse result
102 my ( $line ) = map {
103 0 my ( $v ) = $_=~ /^X-Bogosity:\s+(.*?)$/;
104 0 $v;
105 } grep {
106 0 /^X-Bogosity:/;
107 } split( /\n/, $result );
108 0 $self->logger->debug3( "Bogofilter response: '$result' -> LINE '$line'" );
109
110 # found status ?
111 0 ▶ if ( $line ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line) { } 0 0 0
112
113 # parse attributes
114 my %parsed = map {
115 0 my ( $n, $v ) = $_=~ /^([^=]+)=(.*$)$/;
116 0 ( $n => $v );
117 } grep {
118 0 ▶ /=/
Condition0%0/2
$line || ''
l!l
00
MC/DC0%0/1
$line || ''
$line
Truth table0%0/2
A
A= $line
Aresult
11
00
119 } split( /\s*,\s*/, $line || "" );
120
121 # init unsure weight with zero
122 0 my $weight = 0;
123
124 # wheter the whole is spam!
125 0 ▶ my $status = index( $line, 'Ham' ) == 0
Branch: 2 branches
BranchTrueFalseTotal
index($line, 'Ham') == 0 ? : 0 0 0
index($line, 'Spam') == 0 ? : 0 0 0
126 ? 'ham'
127 : ( index( $line, 'Spam' ) == 0
128 ? 'spam'
129 : 'unsure'
130 )
131 ;
132
133 # init info
134 0 my @info = ( "Bogofilter status: $status" );
135 0 ▶ my $spamicity = $parsed{ spamicity } || 'unknown';
Condition0%0/2
$parsed{'spamicity'} || 'unknown'
l!l
00
MC/DC0%0/1
$parsed{'spamicity'} || 'unknown'
$parsed{'spamicity'}
Truth table0%0/2
A
A= $parsed{'spamicity'}
Aresult
11
00
136 push @info, "Bogofilter spamicity: $parsed{ spamicity }"
137 0 ▶ if defined $parsed{ spamicity };
Branch: 1 branch
BranchTrueFalseTotal
if defined $parsed{"spamicity"} 0 0 0
138
139 # found spam -> use as spam
140 0 ▶ if ( $status eq 'spam' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($status eq 'spam') { } 0 0 0
elsif ($status eq 'ham') { } 0 0 0
141 0 $weight = $self->weight_spam;
142 $weight *= $parsed{ spamicity }
143 0 ▶ if $self->apply_spamicity && defined $parsed{ spamicity };
Branch: 1 branch
BranchTrueFalseTotal
if $self->apply_spamicity and defined $parsed{"spamicity"} 0 0 0
Condition0%0/3
$self->apply_spamicity and defined $parsed{"spamicity"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->apply_spamicity and defined $parsed{"spamicity"}
$self->apply_spamicity defined $parsed{"spamicity"}
Truth table0%0/3
A and B
A= $self->apply_spamicityB= defined $parsed{"spamicity"}
ABresult
0X0
100
111
144 0 $self->logger->debug0( "Use spam status, set score to '$weight' (spamicity: $spamicity)" );
145 }
146
147 # found ham -> use as ham
148 elsif ( $status eq 'ham' ) {
149 0 $weight = $self->weight_innocent;
150 $weight = $weight * ( 1 - $parsed{ spamicity } )
151 0 ▶ if $self->apply_spamicity && defined $parsed{ spamicity };
Branch: 1 branch
BranchTrueFalseTotal
if $self->apply_spamicity and defined $parsed{"spamicity"} 0 0 0
Condition0%0/3
$self->apply_spamicity and defined $parsed{"spamicity"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->apply_spamicity and defined $parsed{"spamicity"}
$self->apply_spamicity defined $parsed{"spamicity"}
Truth table0%0/3
A and B
A= $self->apply_spamicityB= defined $parsed{"spamicity"}
ABresult
0X0
100
111
152 0 $self->logger->debug0( "Use ham status, set score to '$weight' (spamicity: $spamicity)" );
153 }
154
155 # add weight to content filte score
156 0 return $self->add_spam_score( $weight, \@info );
157 }
158
159 else {
160 0 $self->logger->error( "No result from bogofilter: '$result'" );
161 }
162
163
164 # return ok
165 0 return ;
166 }
167  
168  
169 =head1 SEE ALSO
170  
171 =over
172  
173 =item * L<Mail::Decency::ContentFilter::Core::Cmd>
174  
175 =item * L<Mail::Decency::ContentFilter::Core::Spam>
176  
177 =item * L<Mail::Decency::ContentFilter::Core::WeightTranslate>
178  
179 =item * L<Mail::Decency::ContentFilter::CRM114>
180  
181 =item * L<Mail::Decency::ContentFilter::DSPAM>
182  
183 =back
184  
185 =head1 AUTHOR
186  
187 Ulrich Kautz <uk@fortrabbit.de>
188  
189 =head1 COPYRIGHT
190  
191 Copyright (c) 2010 the L</AUTHOR> as listed above
192  
193 =head1 LICENCSE
194  
195 This library is free software and may be distributed under the same terms as perl itself.
196  
197 =cut
198  
199 1 1;
« lib/Mail/Decency/Policy/Greylist.pm ↑ Summary lib/Mail/Decency/Policy/Honeypot.pm »