From c0bd75bdc2b2bd90b89aef113b19f89db72aff20 Mon Sep 17 00:00:00 2001 From: Peter Palfrader Date: Sat, 20 May 2006 23:59:10 +0000 Subject: Add original sa-bogofilter git-svn-id: svn+ssh://asteria.noreply.org/svn/weaselutils/trunk@95 bc3d92e2-beff-0310-a7cd-cc87d7ac0ede --- spamassassin/sa-bogofilter/Bogofilter.pm | 111 +++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 spamassassin/sa-bogofilter/Bogofilter.pm (limited to 'spamassassin/sa-bogofilter/Bogofilter.pm') diff --git a/spamassassin/sa-bogofilter/Bogofilter.pm b/spamassassin/sa-bogofilter/Bogofilter.pm new file mode 100644 index 0000000..9cb1f09 --- /dev/null +++ b/spamassassin/sa-bogofilter/Bogofilter.pm @@ -0,0 +1,111 @@ +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; -- cgit v1.2.3