lib/PublicInbox/SearchIdx.pm
lib/PublicInbox/SearchMsg.pm
lib/PublicInbox/SearchView.pm
+lib/PublicInbox/Spamcheck/Spamc.pm
lib/PublicInbox/Spawn.pm
lib/PublicInbox/SpawnPP.pm
lib/PublicInbox/Thread.pm
t/psgi_mount.t
t/qspawn.t
t/search.t
+t/spamcheck_spamc.t
t/spawn.t
t/utf8.mbox
t/view.t
--- /dev/null
+# Copyright (C) 2016 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+package PublicInbox::Spamcheck::Spamc;
+use strict;
+use warnings;
+use PublicInbox::Spawn qw(popen_rd spawn);
+use IO::File;
+use Fcntl qw(:DEFAULT SEEK_SET);
+
+sub new {
+ my ($class) = @_;
+ bless {
+ checkcmd => [qw(spamc -E --headers)],
+ hamcmd => [qw(spamc -L ham)],
+ spamcmd => [qw(spamc -L spam)],
+ }, $class;
+}
+
+sub spamcheck {
+ my ($self, $msg, $out) = @_;
+
+ my $tmp;
+ my $fd = _msg_to_fd($self, $msg, \$tmp);
+ my $rdr = { 0 => $fd };
+ my ($fh, $pid) = popen_rd($self->{checkcmd}, undef, $rdr);
+ defined $pid or die "failed to popen_rd spamc: $!\n";
+ my $r;
+ unless (ref $out) {
+ my $buf = '';
+ $out = \$buf;
+ }
+ do {
+ $r = sysread($fh, $$out, 65536, length($$out));
+ } while (defined($r) && $r != 0);
+ defined $r or die "read failed: $!";
+ close $fh or die "close failed: $!";
+ waitpid($pid, 0);
+ ($? || $$out eq '') ? 0 : 1;
+}
+
+sub hamlearn {
+ my ($self, $msg, $rdr) = @_;
+ _learn($self, $msg, $rdr, 'hamcmd');
+}
+
+sub spamlearn {
+ my ($self, $msg, $rdr) = @_;
+ _learn($self, $msg, $rdr, 'spamcmd');
+}
+
+sub _learn {
+ my ($self, $msg, $rdr, $field) = @_;
+ $rdr ||= {};
+ $rdr->{1} ||= $self->_devnull;
+ $rdr->{2} ||= $self->_devnull;
+ my $tmp;
+ $rdr->{0} = _msg_to_fd($self, $msg, \$tmp);
+ my $pid = spawn($self->{$field}, undef, $rdr);
+ waitpid($pid, 0);
+ !$?;
+}
+
+sub _devnull {
+ my ($self) = @_;
+ my $fd = $self->{-devnullfd};
+ return $fd if defined $fd;
+ open my $fh, '+>', '/dev/null' or
+ die "failed to open /dev/null: $!";
+ $self->{-devnull} = $fh;
+ $self->{-devnullfd} = fileno($fh);
+}
+
+sub _msg_to_fd {
+ my ($self, $msg, $tmpref) = @_;
+ my $tmpfh;
+ my $fd;
+ if (my $ref = ref($msg)) {
+
+ return $msg->fileno if $ref ne 'SCALAR' && $msg->can('fileno');
+
+ $tmpfh = IO::File->new_tmpfile;
+ $tmpfh->autoflush(1);
+ $msg = \($msg->as_string) if $ref ne 'SCALAR';
+ print $tmpfh $$msg or die "failed to print: $!";
+ sysseek($tmpfh, 0, SEEK_SET) or
+ die "sysseek(fh) failed: $!";
+ $$tmpref = $tmpfh;
+
+ return fileno($tmpfh);
+ }
+ $msg;
+}
+
+1;
use Email::MIME::ContentType;
$Email::MIME::ContentType::STRICT_PARAMS = 0; # user input is imperfect
use PublicInbox::Address;
-use PublicInbox::Spawn qw(spawn);
+use PublicInbox::Spamcheck::Spamc;
my $train = shift or die "usage: $usage\n";
if ($train !~ /\A(?:ham|spam)\z/) {
die "`$train' not recognized.\nusage: $usage\n";
}
+my $spamc = PublicInbox::Spamcheck::Spamc->new;
my $pi_config = PublicInbox::Config->new;
my $err;
my $mime = Email::MIME->new(eval {
my $data = scalar <STDIN>;
$data =~ s/\AFrom [^\r\n]*\r?\n//s;
eval {
- my @cmd = (qw(spamc -L), $train);
- my ($r, $w);
- pipe($r, $w) or die "pipe failed: $!";
- open my $null, '>', '/dev/null' or
- die "failed to open /dev/null: $!";
- my $nullfd = fileno($null);
- my %rdr = (0 => fileno($r), 1 => $nullfd, 2 => $nullfd);
- my $pid = spawn(\@cmd, undef, \%rdr);
- close $null;
- close $r or die "close \$r failed: $!";
- print $w $data or die "print \$w failed: $!";
- close $w or die "close \$w failed: $!";
- waitpid($pid, 0);
+ if ($train eq 'ham') {
+ $spamc->hamlearn(\$data);
+ } else {
+ $spamc->spamlearn(\$data);
+ }
die "spamc failed with: $?\n" if $?;
};
$err = $@;
use PublicInbox::Git;
use PublicInbox::Emergency;
use PublicInbox::Filter::Base;
-use PublicInbox::Spawn qw(popen_rd);
+use PublicInbox::Spamcheck::Spamc;
# n.b: hopefully we can setup the emergency path without bailing due to
# user error, we really want to setup the emergency destination ASAP
# pre-check, MDA has stricter rules than an importer might;
do_exit(0) unless PublicInbox::MDA->precheck($simple, $dst->{address});
-
+my $spamc = PublicInbox::Spamcheck::Spamc->new;
$str = '';
-my $spam_ok = do_spamc($ems->fh, \$str);
+my $spam_ok = $spamc->spamcheck($ems->fh, \$str);
$simple = undef;
$emm = PublicInbox::Emergency->new($emergency);
$emm->prepare(\$str);
$mime->header_obj->header_raw('Message-ID'),
" exists\n";
}
-do_exit(0);
-
-# we depend on "report_safe 0" in /etc/spamassassin/*.cf with --headers
-sub do_spamc {
- my ($in, $out) = @_;
- my $rdr = { 0 => fileno($in) };
- my ($fh, $pid) = popen_rd([qw/spamc -E --headers/], undef, $rdr);
- defined $pid or die "failed to popen_rd spamc: $!\n";
- my $r;
- do {
- $r = sysread($fh, $$out, 65536, length($$out));
- } while (defined($r) && $r != 0);
- close $fh or die "close failed: $!\n";
- waitpid($pid, 0);
- ($? || $$out eq '') ? 0 : 1;
-}
+do_exit(0);
--- /dev/null
+# Copyright (C) 2016 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+use strict;
+use warnings;
+use Test::More;
+use Cwd;
+use Email::Simple;
+use IO::File;
+use File::Temp qw/tempdir/;
+use Fcntl qw(:DEFAULT SEEK_SET);
+my $tmpdir = tempdir('spamcheck_spamc-XXXXXX', TMPDIR => 1, CLEANUP => 1);
+
+use_ok 'PublicInbox::Spamcheck::Spamc';
+my $spamc = PublicInbox::Spamcheck::Spamc->new;
+$spamc->{checkcmd} = [qw(cat)];
+
+{
+ open my $fh, '+>', "$tmpdir/file" or die "open failed: $!";
+ ok(!$spamc->spamcheck($fh), 'empty '.ref($fh));
+}
+ok(!$spamc->spamcheck(IO::File->new_tmpfile), 'IO::File->new_tmpfile');
+
+my $dst = '';
+my $src = <<'EOF';
+Date: Thu, 01 Jan 1970 00:00:00 +0000
+To: <e@example.com>
+From: <e@example.com>
+Subject: test
+Message-ID: <testmessage@example.com>
+
+EOF
+ok($spamc->spamcheck(Email::Simple->new($src), \$dst), 'Email::Simple works');
+is($dst, $src, 'input == output');
+
+$dst = '';
+$spamc->{checkcmd} = ['sh', '-c', 'cat; false'];
+ok(!$spamc->spamcheck(Email::Simple->new($src), \$dst), 'Failed check works');
+is($dst, $src, 'input == output for spammy example');
+
+for my $l (qw(ham spam)) {
+ my $file = "$tmpdir/$l.out";
+ $spamc->{$l.'cmd'} = ['tee', $file ];
+ my $method = $l.'learn';
+ ok($spamc->$method(Email::Simple->new($src)), "$method OK");
+ open my $fh, '<', $file or die "failed to open $file: $!";
+ is(eval { local $/, <$fh> }, $src, "$l command ran alright");
+}
+
+done_testing();