about summary refs log tree commit homepage
path: root/script
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2016-06-24 01:15:13 +0000
committerEric Wong <e@80x24.org>2016-06-24 02:00:26 +0000
commitc515264dd69156fc89c59685f788b1093afb8731 (patch)
treeada1a21aa82b369dbe1ac8c865474a9318228f7d /script
parent3c24b7e7e47be9646226d921897cc9ec92e9be8a (diff)
downloadpublic-inbox-c515264dd69156fc89c59685f788b1093afb8731.tar.gz
This should hopefully make it easier to try other anti-spam
systems (or none at all) in the future.
Diffstat (limited to 'script')
-rwxr-xr-xscript/public-inbox-learn21
-rwxr-xr-xscript/public-inbox-mda23
2 files changed, 11 insertions, 33 deletions
diff --git a/script/public-inbox-learn b/script/public-inbox-learn
index b05ef056..7ef2a312 100755
--- a/script/public-inbox-learn
+++ b/script/public-inbox-learn
@@ -14,12 +14,13 @@ use Email::MIME;
 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 {
@@ -27,19 +28,11 @@ 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 = $@;
diff --git a/script/public-inbox-mda b/script/public-inbox-mda
index 013642d0..f739ad06 100755
--- a/script/public-inbox-mda
+++ b/script/public-inbox-mda
@@ -24,7 +24,7 @@ use PublicInbox::Import;
 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
@@ -44,9 +44,9 @@ my $main_repo = $dst->{mainrepo} or do_exit(1);
 
 # 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);
@@ -90,20 +90,5 @@ if (defined $im->add($mime)) {
                         $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);