package Bogofilter; 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 $to = $permsgstatus->get('Received'); $to =~ /for (\S+\@\S+);/; my $received = $1; $received = lc($received); $received = $self->stripUser($received); my $cmd = "bogofilter -d /var/lib/bogofilter/$received -v < $tmpfile"; $return = `$cmd`; Mail::SpamAssassin::dbg("BOGOFILTER: ran $cmd for $received"); 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 createUserDir { my $self = shift; my $user = shift; my $dir = '/var/lib/bogofilter/' . $user; if ( !-e $dir ) { Mail::SpamAssassin::dbg("BOGOFILTER: gotta create directory! $dir"); mkdir $dir; } } sub stripUser { my $self = shift; my $email = shift; $email =~ /([A-Za-z\-\_]+?)\@/; return $1; } 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"); $to =~ /for (\S+\@\S+);/; my $received = $1; $received = lc($received); Mail::SpamAssassin::Plugin::dbg("BOGOFILTER: delivery to: $received"); $to = $self->stripUser($received); Mail::SpamAssassin::Plugin::dbg("BOGOFILTER: message was sent to: $to"); $self->createUserDir($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 = "-d /var/lib/bogofilter/$to "; 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;