lib/Mail/Decency/ContentFilter/SpamAssassin.pm untested

filter: Statementstmt 0.0% 0 / 74 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 130 CC 14 SCAR 33.2
CC
14
Cov
0%
handle
56.0
get_client
30.0
train
20.0
CRAP 27.7
1 package Mail::Decency::ContentFilter::SpamAssassin;
2  
3 use Moose;
4 extends qw/
5 Mail::Decency::ContentFilter::Core
6 /;
7 with qw/
8 Mail::Decency::ContentFilter::Core::User
9 Mail::Decency::ContentFilter::Core::Spam
10 Mail::Decency::ContentFilter::Core::WeightTranslate
11 /;
12  
13 use version 0.74; our $VERSION = qv( "v0.1.6" );
14  
15 use mro 'c3';
16 use Data::Dumper;
17 use Mail::SpamAssassin::Client;
18 use File::Temp qw/ tempfile /;
19  
20 =head1 NAME
21  
22 Mail::Decency::ContentFilter::SpamAssassin
23  
24 =head1 DESCRIPTION
25  
26 Filter messages through spamc and translate results
27  
28 =head2 CONFIG
29  
30 ---
31
32 disable: 0
33 #max_size: 0
34 #timeout: 30
35
36 cmd_check: '/usr/bin/spamc -u %user% --headers'
37
38  
39 =head1 CLASS ATTRIBUTES
40  
41  
42 =head2 host
43  
44 Spamassassin host .. use this or socket
45  
46 =cut
47  
48 has host => (
49 is => 'rw',
50 isa => 'Str',
51 default => 'localhost'
52 );
53  
54  
55 =head2 port
56  
57 Spamassassin port .. if host is used
58  
59 =cut
60  
61 has port => (
62 is => 'rw',
63 isa => 'Int',
64 default => 783
65 );
66  
67  
68 =head2 socket
69  
70 Spamassassin socket .. instead of host and port
71  
72 =cut
73  
74 has socket => (
75 is => 'rw',
76 isa => 'Str',
77 predicate => 'has_socket'
78 );
79  
80  
81 =head1 METHODS
82  
83  
84 =head2 pre_init
85  
86 =cut
87  
88 sub pre_init {
89 my ( $self ) = @_;
90 push @{ $self->{ config_params } ||=[] }, qw/ host port socket /;
91 return ;
92 }
93  
94  
95 =head2 handle
96  
97 Use L<Mail::SpamAssassin::Client> to retreive filter result from SpamAssassin
98  
99 =cut
100  
101 sub handle {
102 my ( $self ) = @_;
103
104 my $client = $self->get_client();
105 return unless $client;
106
107 # process mail
108 my $ref = $client->process( $self->mime->stringify );
109
110 # no result ?
111 unless ( $ref ) {
112 $self->logger->error( "Failed to receive result from spamd" );
113 return ;
114 }
115
116 # calc weight
117 my $weight = 0;
118
119 # use scoring ?
120 if ( $self->has_weight_translate ) {
121 $weight = $self->translate_weight( $ref->{ score } );
122 }
123
124 # is HAM ?
125 elsif ( $ref->{ isspam } eq 'False' ) {
126 $weight = $self->weight_innocent;
127 }
128
129 # is SPAM..
130 else {
131 $weight = $self->weight_spam;
132 }
133 $self->logger->debug0( "Score mail to '$weight'" );
134
135 # get header
136 my ( $last_header, %header ) = ();
137 foreach my $l( split( /\n/, $ref->{ message } ) ) {
138 if ( $l =~ /^X-Spam-(\S+):\s*(.*?)$/ ) {
139 ( $last_header, my $value ) = ( $1, $2 );
140 $header{ $last_header } = $value;
141 }
142 elsif ( $last_header && $l =~ /^\s+(.+?)$/ ) {
143 $header{ $last_header } .= $1;
144 }
145 else {
146 last;
147 }
148 }
149
150 # add weight to content filte score
151 return $self->add_spam_score( $weight, [
152 "SpamAssassin Status: ". ( $header{ Status } || "UNKNOWN" )
153 ] );
154 }
155  
156  
157 =head2 train
158  
159 Train mails into SpamAssassin
160  
161 =cut
162  
163 sub train {
164 my ( $self, $mode ) = @_;
165
166 die "Train mode has to be 'spam' or 'ham'\n"
167 unless $mode eq 'spam' || $mode eq 'ham';
168
169 my $client = $self->get_client();
170 return ( 0, undef, 1 ) unless $client;
171
172 my $learned = $client->learn( $self->mime->stringify, $mode eq 'spam' ? 0 : 1 );
173 return ( $learned, "OK", 0 );
174 }
175  
176  
177 =head2 get_client
178  
179 Creates instance of L<Mail::SpamAssassin::Client> and returns it
180  
181 =cut
182  
183 sub get_client {
184 my ( $self ) = @_;
185
186 my $user = $self->get_user();
187 my $client;
188 eval {
189 $client = $self->has_socket
190 ? Mail::SpamAssassin::Client->new( {
191 socketpath => $self->socket,
192 user => $user
193 } )
194 : Mail::SpamAssassin::Client->new( {
195 host => $self->host,
196 port => $self->port,
197 user => $user
198 } )
199 ;
200 };
201
202 # errro setup client
203 if ( $@ ) {
204 warn "> ERR $@\n";
205 $self->logger->error( "Error connecting to spamd: $@" );
206 return;
207 }
208 elsif ( ! $client ) {
209 warn "> OOPS \n";
210 $self->logger->error( "Could not create SpamAssassin client" );
211 return;
212 }
213
214 # cannotping spamd
215 unless ( $client->ping ) {
216 $self->logger->error( "Cannot ping spamd. Down ?" );
217 return ;
218 }
219
220 return $client;
221 }
222  
223 =head1 AUTHOR
224  
225 Ulrich Kautz <uk@fortrabbit.de>
226  
227 =head1 COPYRIGHT
228  
229 Copyright (c) 2010 the L</AUTHOR> as listed above
230  
231 =head1 LICENCSE
232  
233 This library is free software and may be distributed under the same terms as perl itself.
234  
235 =cut
236  
237  
238 1;