File Coverage

blib/lib/Mail/Qmail/Filter/SpamAssassin.pm
Criterion Covered Total %
statement 8 24 33.3
branch 0 10 0.0
condition 0 6 0.0
subroutine 3 4 75.0
pod 1 1 100.0
total 12 45 26.6


line stmt bran cond sub pod time code
1 1     1   783 use 5.014;
  1         3  
2 1     1   4 use warnings;
  1         1  
  1         52  
3              
4              
5             our $VERSION = '1.01';
6              
7             use Mo qw(coerce default);
8 1     1   6 extends 'Mail::Qmail::Filter';
  1         1  
  1         4  
9              
10             has 'dump_spam_to';
11             has 'mark';
12             has 'reject_score';
13             has 'reject_text' => 'I think this message is spam.';
14              
15             my $self = shift;
16             my $message = $self->message;
17 0     0 1   my $body_ref = $message->body_ref;
18 0            
19 0           require Mail::SpamAssassin; # lazy load because filter might be skipped
20             my $sa = Mail::SpamAssassin->new;
21 0           my $mail = $sa->parse($body_ref);
22 0           my $status = $sa->check($mail);
23 0           $self->debug( 'spam score' => my $score = $status->get_score );
24 0            
25 0           if ( $status->is_spam ) {
26             if ( defined( my $dir = $self->dump_spam_to ) ) {
27 0 0         require Path::Tiny and Path::Tiny->import('path')
28 0 0         unless defined &path;
29 0 0 0       path( $dir, my $file = join '_', $^T, $$, $score )
30             ->spew($$body_ref);
31 0           $self->debug( 'dumped message to' => $file );
32             path( $dir, $file . '_report' )->spew( $status->get_report );
33 0           }
34 0           $self->reject( $self->reject_text )
35             if $self->reject_score && $score >= $self->reject_score;
36 0 0 0       $$body_ref = $status->rewrite_mail if $self->mark;
37             }
38 0 0         }
39              
40             1;
41              
42              
43             =head1 NAME
44              
45             Mail::Qmail::Filter::SpamAssassin -
46             check if message is spam
47              
48             =head1 SYNOPSIS
49              
50             use Mail::Qmail::Filter;
51            
52             Mail::Qmail::Filter->new->add_filter(
53             '::SpamAssassin' => {
54             skip_if_relayclient => 1,
55             skip_for_rcpt => [ 'postmaster', 'postmaster@' . $mydomain ],
56             dump_spam_to => '/var/tmp/spam',
57             reject_score => 5.2,
58             },
59             '::Queue',
60             )->run;
61              
62             =head1 DESCRIPTION
63              
64             This L<Mail::Qmail::Filter> plugin checks if the incoming e-mail message
65             is probably spam.
66              
67             =head1 OPTIONAL PARAMETERS
68              
69             =head2 dump_spam_to
70              
71             If the message is spam, copy it into a file in the given directory.
72             The file will be named
73             C<E<lt>epoch_time_when_script_startedE<gt>_E<lt>pidE<gt>_E<lt>spam_scoreE<gt>>
74              
75             A spam report will be written to another file named
76             C<E<lt>epoch_time_when_script_startedE<gt>_E<lt>pidE<gt>_E<lt>spam_scoreE<gt>_report>
77              
78             =head2 mark
79              
80             Mark the message if it is spam and is not rejected.
81              
82             =head2 reject_score
83              
84             To reject the message if it has at least the spam score given.
85              
86             =head2 reject_text
87              
88             Reply text to send to the client when the message is rejected.
89              
90             Default: C<I think this message is spam.>
91              
92             =head1 SEE ALSO
93              
94             L<Mail::Qmail::Filter/COMMON OPTIONS FOR ALL FILTERS>, L<Mail::SpamAssassin>
95              
96             =head1 LICENSE AND COPYRIGHT
97              
98             Copyright 2019 Martin Sluka.
99              
100             This module is free software; you can redistribute it and/or modify it
101             under the terms of the the Artistic License (2.0). You may obtain a
102             copy of the full license at:
103              
104             L<http://www.perlfoundation.org/artistic_license_2_0>
105              
106             Any use, modification, and distribution of the Standard or Modified
107             Versions is governed by this Artistic License. By using, modifying or
108             distributing the Package, you accept this license. Do not use, modify,
109             or distribute the Package, if you do not accept this license.
110              
111             If your Modified Version has been derived from a Modified Version made
112             by someone other than you, you are nevertheless required to ensure that
113             your Modified Version complies with the requirements of this license.
114              
115             This license does not grant you the right to use any trademark, service
116             mark, tradename, or logo of the Copyright Holder.
117              
118             This license includes the non-exclusive, worldwide, free-of-charge
119             patent license to make, have made, use, offer to sell, sell, import and
120             otherwise transfer the Package with respect to any patent claims
121             licensable by the Copyright Holder that are necessarily infringed by the
122             Package. If you institute patent litigation (including a cross-claim or
123             counterclaim) against any party alleging that the Package constitutes
124             direct or contributory patent infringement, then this Artistic License
125             to you shall terminate on the date that such litigation is filed.
126              
127             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
128             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
129             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
130             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
131             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
132             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
133             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
134             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
135              
136             =cut