summaryrefslogtreecommitdiff
path: root/spamassassin/sa-bogofilter/Bogofilter.pm
blob: b6350ade5d059da626cd727b85ab9a62c0030dd5 (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
package Bogofilter;
#
# Copyright (c) 2004, 2005 Josh Harding <josh@statewidesoftware.com>
#   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 <directory> 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;