package Bogofilter; # # Copyright (c) 2004, 2005 Josh Harding # http://www.statewidesoftware.com/projects.shtml # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # # Copyright (c) 2006 Peter Palfrader # Changes: # 2006-05-21: remove all of the code that creates per user # files for bogofilter based on Received header # lines. No longer pass -d to bogofilter. # It will therefore use $HOME/.bogofilter or something # equally sensible. use strict; use Mail::SpamAssassin; use Mail::SpamAssassin::Plugin; our @ISA = qw(Mail::SpamAssassin::Plugin); sub new { my ( $class, $mailsa ) = @_; $class = ref($class) || $class; my $self = $class->SUPER::new($mailsa); bless( $self, $class ); $self->register_eval_rule("check_bogofilter"); return $self; } sub check_bogofilter { my ( $self, $permsgstatus, $fulltext, $min, $max ) = @_; my $isspam = 0; my $header = ""; my $return = ""; Mail::SpamAssassin::Plugin::dbg("BOGOFILTER: lookin at the mail"); Mail::SpamAssassin::Plugin::dbg("BOGOFILTER: min: $min max: $max"); if ( ! exists $permsgstatus->{bogoscore} ) { my $tmpfile = $permsgstatus->create_fulltext_tmpfile($fulltext); Mail::SpamAssassin::dbg("BOGOFILTER: permsgstatus $permsgstatus"); my $cmd = "bogofilter -v < $tmpfile"; $return = `$cmd`; Mail::SpamAssassin::dbg("BOGOFILTER: ran $cmd"); Mail::SpamAssassin::dbg("BOGOFILTER: returned $return"); $permsgstatus->delete_fulltext_tmpfile(); $return =~ /spamicity\=([0-9\.]+)/; $permsgstatus->{bogoscore} = $1; if ($permsgstatus->{bogoscore} =~ /[A-Za-z]/) { $permsgstatus->{bogoscore} = 0.5; } } else { if ($permsgstatus->{bogoscore} =~ /[A-Za-z]/) { $permsgstatus->{bogoscore} = 0.5; } Mail::SpamAssassin::dbg("BOGOFILTER: already had a score!"); Mail::SpamAssassin::dbg("BOGOFILTER: the score is: " . $permsgstatus->{bogoscore}); } Mail::SpamAssassin::dbg("BOGOFILTER: spamicity=" . $permsgstatus->{bogoscore}); if ( $permsgstatus->{bogoscore} >= $min && $permsgstatus->{bogoscore} <= $max ) { $isspam = 1; } $permsgstatus->{main}->{conf}->{headers_spam}->{"SA-Bogofilter"} = $permsgstatus->{bogoscore}; $permsgstatus->{main}->{conf}->{headers_ham}->{"SA-Bogofilter"} = $permsgstatus->{bogoscore}; return $isspam; } sub autolearn { my ( $self, $opts ) = @_; my $isspam = $opts->{isspam}; Mail::SpamAssassin::Plugin::dbg( "BOGOFILTER: message being learned as $isspam"); my $to = $opts->{permsgstatus}->get('Received'); Mail::SpamAssassin::Plugin::dbg("BOGOFILTER: Received $to"); my $msg = $opts->{permsgstatus}->get_message(); my $fulltext = $msg->get_pristine(); my $tmpfile = $opts->{permsgstatus}->create_fulltext_tmpfile($fulltext); Mail::SpamAssassin::Plugin::dbg("BOGOFILTER: outut to $tmpfile"); my $bogofilterargs = ""; if ($isspam) { $bogofilterargs .= "-s"; } else { $bogofilterargs .= "-n"; } my $command = "bogofilter $bogofilterargs < $tmpfile"; Mail::SpamAssassin::Plugin::dbg("BOGOFILTER: would run $command"); system $command; $opts->{permsgstatus}->delete_fulltext_tmpfile(); } 1;