# # Requires the spamd patch attached to this spamassassin bug: # http://bugzilla.spamassassin.org/show_bug.cgi?id=660 # # The patch is going to be included in SpamAssassin 2.40. # # ... or you can change REPORT_IFSPAM to REPORT below; but the headers # will be a bit different than you are used to. # # use Socket qw(:DEFAULT :crlf); use IO::Handle; sub register { my ($self, $qp) = @_; $self->register_hook("data_post", "check_spam"); } #my $rv = check_spam(); #die "failure!" unless defined $rv; #print "rv: $rv\n"; sub check_spam { my ($self, $transaction) = @_; return (DECLINED) if $transaction->body_size > 500_000; my $remote = 'localhost'; my $port = 783; if ($port =~ /\D/) { $port = getservbyname($port, 'tcp') } die "No port" unless $port; my $iaddr = inet_aton($remote) or $self->log(1, "Could not resolve host: $remote") and return (DECLINED); my $paddr = sockaddr_in($port, $iaddr); my $proto = getprotobyname('tcp'); socket(SPAMD, PF_INET, SOCK_STREAM, $proto) or $self->log(1, "Could not open socket: $!") and return (DECLINED); connect(SPAMD, $paddr) or $self->log(1, "Could not connect to spamassassin daemon: $!") and return DECLINED; SPAMD->autoflush(1); $transaction->body_resetpos; print SPAMD "REPORT_IFSPAM SPAMC/1.0" . CRLF; # or CHECK or REPORT or SYMBOLS print SPAMD join CRLF, split /\n/, $transaction->header->as_string; print SPAMD CRLF; while (my $line = $transaction->body_getline) { chomp $line; print SPAMD $line, CRLF; } print SPAMD CRLF; shutdown(SPAMD, 1); my $line0 = ; # get the first protocol lines out if ($line0) { $transaction->header->add("X-Spam-Check-By", $self->qp->config('me')); } while () { warn "GOT FROM SPAMD1: $_"; next unless m/\S/; s/\r?\n$/\n/; my @h = split /: /, $_, 2; $transaction->header->add(@h); last if $h[0] eq "Spam" and $h[1] =~ m/^False/; } return (DECLINED); }