qpsmtpd Wiki

[[plugins:spam:spambayes]]

You are here: start » plugins » spam » spambayes

Login

You are currently not logged in! Enter your authentication credentials below to log in. You need to have cookies enabled to log in.

Login

You don't have an account yet? Just get one: Register

Forgotten your password? Get a new one: Set new password

Plug-in Summary

Plug-in name: spambayes
Info: Process email with SpamBayes
Author: James Turnbull
Email: james@lovedthanlost.net
Compatibility: All
Download: in-line

spambayes

This is a simple plug-in designed to process email through SpamBayes (http://spambayes.sourceforge.net/). It is based on Devin Carraway's spamassassin_spamc plug-in. Currently it just processes email through sb_filter.py and adds the X-Spambayes-Classification header to the email.

=head1 NAME

spambayes - SpamBayes integration for qpsmtpd via sb_filter.py

=head1 DESCRIPTION

Plugin that checks if the mail is spam by using the "spambayes" sb_filter.py
command

=head1 AUTHOR

The spamassassin_spamc plugin was originally written by Devin Carraway. This
version adapted to use spambayes by James Turnbull.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

=head1 SEE ALSO

sb_filter.py(1)

=cut

use File::Temp qw/tempfile/;

sub register {
  my ($self, $qp, @args) = @_;
  $self->register_hook("data_post", "check_spam");

  $self->log(LOGERROR, "Bad parameters for the spambayes plugin")
    if @_ % 2;

  %{$self->{_args}} = @args;

}

sub check_spam {
  my ($self, $transaction) = @_;

  my $spool_dir = $self->qp->config('spool_dir') ?
          $self->qp->config('spool_dir')
        : Qpsmtpd::Utils::tildeexp('~/tmp/');
  $spool_dir =~ /(.*)/ and $spool_dir = $1;
  for (keys %{$self->{_args}}) {
        $self->{_args}->{$_} = $1 if $self->{_args}->{$_} =~ /(.*)/;
  }

  $self->{_args}->{max_size} ||= 1024 * 64;
  if ($transaction->body_size > $self->{_args}->{max_size}) {
        $self->log(LOGWARN, 'Mail too large for spambayes scan ('.
                        $transaction->body_size .
                        " vs $self->{_args}->{max_size})");
        return (DECLINED);
  }

  $transaction->header->delete($_)
        for ('X-Spambayes-Classification');

  my ($tmp, $tmp_fn) = tempfile(DIR => $spool_dir, SUFFIX => '.spamc');
  ($tmp && $tmp_fn) or return DECLINED;
  print $tmp $transaction->header->as_string, "\n";
  my $line;
  print $tmp $line while $line = $transaction->body_getline;
  $transaction->body_resetpos;
  close $tmp;

  my $cmd = "/usr/bin/sb_filter.py";

  
  open(RD, "$cmd < '$tmp_fn'|") or do {
        $self->log(LOGCRIT,"$cmd: $!");
        unlink $tmp_fn or $self->log(1, "unlink: $tmp_fn: $!");
        return DECLINED;
  };

  my $h = new Mail::Header;
  $h->read(\*RD);
  while (<RD>) {
        chomp;
        $self->log(LOGDEBUG,"spambayes returned body: [$_]");
  }
  close RD;
  unlink $tmp_fn or $self->log(LOGERROR, "unlink: $tmp_fn: $!");

  my $status = $h->get('X-Spambayes-Classification');
  unless ($status) {
        $self->log(LOGWARN, "Spam Bayes didn't return an X-Spambayes-Classification header; ".
                "doing nothing");
        return DECLINED;
  }
  chomp $status;

  $transaction->header($h);
  
  return DECLINED;
}