summaryrefslogtreecommitdiff
path: root/spamassassin/sa-bogofilter/Bogofilter.pm
blob: 9cb1f09af9db4d95c77237852cf7a47b8ae18101 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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;