| 1 | # | ||
| 2 | # Courier::Filter::Module::SpamAssassin class | ||
| 3 | # | ||
| 4 | # (C) 2005-2008 Julian Mehnle <julian@mehnle.net> | ||
| 5 | # $Id: SpamAssassin.pm 211 2008-03-23 01:25:20Z julian $ | ||
| 6 | # | ||
| 7 | ############################################################################### | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Courier::Filter::Module::SpamAssassin - SpamAssassin message filter module for | ||
| 12 | the Courier::Filter framework | ||
| 13 | |||
| 14 | =cut | ||
| 15 | |||
| 16 | package Courier::Filter::Module::SpamAssassin; | ||
| 17 | |||
| 18 | use warnings; | ||
| 19 | use strict; | ||
| 20 | |||
| 21 | use base 'Courier::Filter::Module'; | ||
| 22 | |||
| 23 | use Mail::SpamAssassin; | ||
| 24 | |||
| 25 | use constant TRUE => (0 == 0); | ||
| 26 | use constant FALSE => not TRUE; | ||
| 27 | |||
| 28 | =head1 SYNOPSIS | ||
| 29 | |||
| 30 | use Courier::Filter::Module::SpamAssassin; | ||
| 31 | |||
| 32 | my $module = Courier::Filter::Module::SpamAssassin->new( | ||
| 33 | prefs_file => '/etc/courier/filters/courier-filter-spamassassin.cf', | ||
| 34 | sa_options => { | ||
| 35 | # any Mail::SpamAssassin options | ||
| 36 | }, | ||
| 37 | |||
| 38 | logger => $logger, | ||
| 39 | inverse => 0, | ||
| 40 | trusting => 0, | ||
| 41 | testing => 0, | ||
| 42 | debugging => 0 | ||
| 43 | ); | ||
| 44 | |||
| 45 | my $filter = Courier::Filter->new( | ||
| 46 | ... | ||
| 47 | modules => [ $module ], | ||
| 48 | ... | ||
| 49 | ); | ||
| 50 | |||
| 51 | =head1 DESCRIPTION | ||
| 52 | |||
| 53 | This class is a filter module class for use with Courier::Filter. It matches a | ||
| 54 | message if its SpamAssassin spam score exceeds the configured threshold. | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | # Implementation: | ||
| 59 | ############################################################################### | ||
| 60 | |||
| 61 | =head2 Constructor | ||
| 62 | |||
| 63 | The following constructor is provided: | ||
| 64 | |||
| 65 | =over | ||
| 66 | |||
| 67 | =item B<new(%options)>: returns I<Courier::Filter::Module::SpamAssassin> | ||
| 68 | |||
| 69 | Creates a new B<SpamAssassin> filter module. | ||
| 70 | |||
| 71 | %options is a list of key/value pairs representing any of the following | ||
| 72 | options: | ||
| 73 | |||
| 74 | =over | ||
| 75 | |||
| 76 | =item B<prefs_file> | ||
| 77 | |||
| 78 | The path of a SpamAssassin preferences file. If this option is specified, its | ||
| 79 | value is passed to L<< the Mail::SpamAssassin constructor's C<userprefs_filename> | ||
| 80 | option | Mail::SpamAssassin/userprefs_filename >>. If B<undef>, SpamAssassin | ||
| 81 | is instructed not to read any preferences besides its default configuration | ||
| 82 | files. Defaults to B<undef>. | ||
| 83 | |||
| 84 | =item B<sa_options> | ||
| 85 | |||
| 86 | A hash-ref specifying options for the Mail::SpamAssassin object used by this | ||
| 87 | filter module. See L<Mail::SpamAssassin/new> for the supported options. | ||
| 88 | |||
| 89 | =back | ||
| 90 | |||
| 91 | All options of the B<Courier::Filter::Module> constructor are also supported. | ||
| 92 | Please see L<Courier::Filter::Module/new> for their descriptions. | ||
| 93 | |||
| 94 | =cut | ||
| 95 | |||
| 96 | sub new { | ||
| 97 | my ($class, %options) = @_; | ||
| 98 | |||
| 99 | my $use_user_prefs = defined($options{prefs_file}); | ||
| 100 | $options{sa_options}->{userprefs_filename} = $options{prefs_file}; | ||
| 101 | |||
| 102 | my $spamassassin = Mail::SpamAssassin->new( $options{sa_options} ); | ||
| 103 | $spamassassin->compile_now($use_user_prefs); | ||
| 104 | |||
| 105 | my $self = $class->SUPER::new( | ||
| 106 | %options, | ||
| 107 | spamassassin => $spamassassin | ||
| 108 | ); | ||
| 109 | |||
| 110 | return $self; | ||
| 111 | } | ||
| 112 | |||
| 113 | =back | ||
| 114 | |||
| 115 | =head2 Instance methods | ||
| 116 | |||
| 117 | See L<Courier::Filter::Module/"Instance methods"> for a description of the | ||
| 118 | provided instance methods. | ||
| 119 | |||
| 120 | =cut | ||
| 121 | |||
| 122 | sub match { | ||
| 123 | my ($self, $message) = @_; | ||
| 124 | |||
| 125 | my $spamassassin = $self->{spamassassin}; | ||
| 126 | my $sa_message = $spamassassin->parse($message->text); | ||
| 127 | my $status = $spamassassin->check($sa_message); | ||
| 128 | |||
| 129 | my $is_spam = $status->is_spam; | ||
| 130 | my $score = $status->get_score; | ||
| 131 | my $tests_hit = $status->get_names_of_tests_hit; | ||
| 132 | |||
| 133 | $status->finish(); | ||
| 134 | $sa_message->finish(); | ||
| 135 | |||
| 136 | return 'SpamAssassin: Message looks like spam (score: ' . $score . '; ' . $tests_hit . ')' | ||
| 137 | if $is_spam; | ||
| 138 | |||
| 139 | return undef; | ||
| 140 | # otherwise. | ||
| 141 | } | ||
| 142 | |||
| 143 | =head1 SEE ALSO | ||
| 144 | |||
| 145 | L<Courier::Filter::Module>, L<Courier::Filter::Overview>. | ||
| 146 | |||
| 147 | For AVAILABILITY, SUPPORT, COPYRIGHT, and LICENSE information, see | ||
| 148 | L<Courier::Filter::Overview>. | ||
| 149 | |||
| 150 | =head1 AUTHOR | ||
| 151 | |||
| 152 | Julian Mehnle <julian@mehnle.net> | ||
| 153 | |||
| 154 | =cut | ||
| 155 | |||
| 156 | TRUE; |