| Subcribe via RSS

Akismet Spam Assassin Plugin

December 9th, 2009 | No Comments | Posted in Uncategorized

This is a plugin I wrote a while back to test using Akismet with spam assassin for outbound email coming off our web cluster. It seemed to work nicely and caught a large volume of spam with not too many false positives. We didn’t end up using it because of the prices of the commercial license the Akismet folks wanted. Anyways I thought I would post it here in case anyone was interested and before it got lost forever in a clutter of files.


#package sa_akismet;
use strict;
use Mail::SpamAssassin;
use Mail::SpamAssassin::Plugin;
use Akismet;

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_sa_akismet");
return $self;
}

sub parse_config {
my ($self, $opts) = @_;

my $key = $opts->{key};

if ($key eq 'akismet_api_key') {
$opts->{conf}->{api_key} = $opts->{value};
$self->inhibit_further_callbacks();
return 1;
}
if ($key eq 'akismet_url') {
$opts->{conf}->{akismet_url} = $opts->{value};
$self->inhibit_further_callbacks();
return 1;
}

return 0;
}

sub check_sa_akismet {
my ($self, $permsgstatus) = @_;
my $array = $permsgstatus->get_decoded_stripped_body_text_array();
my $str = join (' ', @$array);
$str =~ s/\s+/ /gs;

my $rule_name = "check_sa_akismet";
my $api_key = $permsgstatus->{conf}->{api_key};
my $url = $permsgstatus->{conf}->{akismet_url};
my $email_name = $permsgstatus->get("From:name");
my $email_addr = $permsgstatus->get("From:addr");
my $userip = "10.10.10.11";
$userip = $permsgstatus->get("X-Remote-Ip:");
if($userip eq "") {
$userip = "10.10.10.11";
}
my $akismet = Net::Akismet->new(
KEY => "$api_key",
URL => "$url",
) or die('Key verification failure!');

my $verdict = $akismet->check(
USER_IP => "$userip",
COMMENT_USER_AGENT => 'Mozilla/5.0',
COMMENT_CONTENT => "$str",
COMMENT_AUTHOR => "$email_name",
COMENT_AUTHOR_EMAIL => "$email_addr",
REFERRER => "$url",
) or die("Is the server here? $api_key $url $userip ");

if ('true' eq $verdict) {
$permsgstatus->got_hit("$rule_name", "");
}

return 0;
}

1;