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