]> Sergey Matveev's repositories - public-inbox.git/commitdiff
mda: allow configuring globally without spamc support
authorEric Wong <e@80x24.org>
Sun, 29 Jul 2018 10:05:13 +0000 (10:05 +0000)
committerEric Wong <e@80x24.org>
Sun, 29 Jul 2018 10:20:36 +0000 (10:20 +0000)
This reuses some of the configuration from -watch, but remains
independent since some configurations will use -watch for some
inboxes and -mda for others.

The default remains "spamc" for -mda users so nothing changes
without explicit configuration.

Per-inbox configurations may also be supported in the future.

Documentation/public-inbox-config.pod
Documentation/public-inbox-mda.pod
MANIFEST
lib/PublicInbox/Spamcheck.pm [new file with mode: 0644]
lib/PublicInbox/WatchMaildir.pm
script/public-inbox-mda
t/v2mda.t

index 22ee9095d8940da72428f1c34e689becf6610c6a..f7353dcaff7e326e410087c10f16f83143861870 100644 (file)
@@ -91,6 +91,15 @@ C<nntp://news.gmane.org/gmane.mail.public-inbox.general>
 
 Default: none
 
+=item publicinboxmda.spamcheck
+
+This may be set to C<none> to disable the use of SpamAssassin
+L<spamc(1)> for filtering spam before it is imported into git
+history.  Other spam filtering backends may be supported in
+the future.
+
+Default: spamc
+
 =item publicinboxwatch.spamcheck
 
 This may be set to C<spamc> to enable the use of SpamAssassin
index a6c704e3c7753fa579140d79d831f158cb18d62b..1a5ade847dfc18b2e91852725bc8baeb644f3314 100644 (file)
@@ -12,6 +12,10 @@ Mail Delivery Agent (MDA) for public-inbox installations.
 Each system user may have their own public-inbox instances.
 This may be invoked via L<procmail(1)> or similar tools.
 
+By default, it relies on L<spamc(1)> for filtering mail,
+but may be disabled via
+L<public-inbox-config(5)/publicinboxmda.spamcheck>
+
 =head1 ENVIRONMENT
 
 =over 8
index 003c3c5be8582a0462d867e86f14a8a71f3609d1..a455dbafb511ff8b4c67be1b113c3652bf73fc90 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -98,6 +98,7 @@ lib/PublicInbox/SearchIdxPart.pm
 lib/PublicInbox/SearchMsg.pm
 lib/PublicInbox/SearchThread.pm
 lib/PublicInbox/SearchView.pm
+lib/PublicInbox/Spamcheck.pm
 lib/PublicInbox/Spamcheck/Spamc.pm
 lib/PublicInbox/Spawn.pm
 lib/PublicInbox/SpawnPP.pm
diff --git a/lib/PublicInbox/Spamcheck.pm b/lib/PublicInbox/Spamcheck.pm
new file mode 100644 (file)
index 0000000..062479d
--- /dev/null
@@ -0,0 +1,25 @@
+# Copyright (C) 2018 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+package PublicInbox::Spamcheck;
+use strict;
+use warnings;
+
+sub get {
+       my ($config, $key, $default) = @_;
+       my $spamcheck = $config->{$key};
+       $spamcheck = $default unless $spamcheck;
+
+       return if !$spamcheck || $spamcheck eq 'none';
+
+       if ($spamcheck eq 'spamc') {
+               $spamcheck = 'PublicInbox::Spamcheck::Spamc';
+       }
+       if ($spamcheck =~ /::/) {
+               eval "require $spamcheck";
+               return $spamcheck->new;
+       }
+       warn "unsupported $key=$spamcheck\n";
+       undef;
+}
+
+1;
index 10dc618416a377387652f1ebab198be4bb91f60b..13dea168deb8d5e3a0e4604dd5694f4a865370d6 100644 (file)
@@ -14,6 +14,7 @@ use PublicInbox::Spawn qw(spawn);
 use PublicInbox::InboxWritable;
 use File::Temp qw//;
 use PublicInbox::Filter::Base;
+use PublicInbox::Spamcheck;
 *REJECT = *PublicInbox::Filter::Base::REJECT;
 
 sub new {
@@ -40,19 +41,9 @@ sub new {
        }
 
        my $k = 'publicinboxwatch.spamcheck';
-       my $spamcheck = $config->{$k};
-       if ($spamcheck) {
-               if ($spamcheck eq 'spamc') {
-                       $spamcheck = 'PublicInbox::Spamcheck::Spamc';
-               }
-               if ($spamcheck =~ /::/) {
-                       eval "require $spamcheck";
-                       $spamcheck = _spamcheck_cb($spamcheck->new);
-               } else {
-                       warn "unsupported $k=$spamcheck\n";
-                       $spamcheck = undef;
-               }
-       }
+       my $default = undef;
+       my $spamcheck = PublicInbox::Spamcheck::get($config, $k, $default);
+       $spamcheck = _spamcheck_cb($spamcheck) if $spamcheck;
 
        # need to make all inboxes writable for spam removal:
        $config->each_inbox(sub { PublicInbox::InboxWritable->new($_[0]) });
index 2b7f298ccd58cf49a2d4aa9dc5fe888e13432240..183b915da86275e416ef5dc7a9935100d2c005a4 100755 (executable)
@@ -20,8 +20,8 @@ use PublicInbox::MDA;
 use PublicInbox::Config;
 use PublicInbox::Emergency;
 use PublicInbox::Filter::Base;
-use PublicInbox::Spamcheck::Spamc;
 use PublicInbox::InboxWritable;
+use PublicInbox::Spamcheck;
 
 # n.b: hopefully we can setup the emergency path without bailing due to
 # user error, we really want to setup the emergency destination ASAP
@@ -33,7 +33,9 @@ $str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
 $ems->prepare(\$str);
 my $simple = Email::Simple->new(\$str);
 my $config = PublicInbox::Config->new;
-
+my $key = 'publicinboxmda.spamcheck';
+my $default = 'PublicInbox::Spamcheck::Spamc';
+my $spamc = PublicInbox::Spamcheck::get($config, $key, $default);
 my $recipient = $ENV{ORIGINAL_RECIPIENT};
 defined $recipient or die "ORIGINAL_RECIPIENT not defined in ENV\n";
 my $dst = $config->lookup($recipient); # first check
@@ -43,13 +45,22 @@ $dst = PublicInbox::InboxWritable->new($dst);
 
 # 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 = $spamc->spamcheck($ems->fh, \$str);
 $simple = undef;
-$emm = PublicInbox::Emergency->new($emergency);
-$emm->prepare(\$str);
-$ems = $ems->abort;
+my $spam_ok;
+if ($spamc) {
+       $str = '';
+       $spam_ok = $spamc->spamcheck($ems->fh, \$str);
+       # update the emergency dump with the new message:
+       $emm = PublicInbox::Emergency->new($emergency);
+       $emm->prepare(\$str);
+       $ems = $ems->abort;
+} else { # no spam checking configured:
+       $spam_ok = 1;
+       $emm = $ems;
+       my $fh = $emm->fh;
+       read($fh, $str, -s $fh);
+}
+
 my $mime = PublicInbox::MIME->new(\$str);
 do_exit(0) unless $spam_ok;
 
index 61457208327702cf671a746a0693eaaf19e02a6b..d041ffd42e114948b022ba9037a2fb08355bac09 100644 (file)
--- a/t/v2mda.t
+++ b/t/v2mda.t
@@ -35,11 +35,13 @@ my $mime = PublicInbox::MIME->create(
 my $mda = "blib/script/public-inbox-mda";
 ok(-f "blib/script/public-inbox-mda", '-mda exists');
 my $main_bin = getcwd()."/t/main-bin";
+my $fail_bin = getcwd()."/t/fail-bin";
 local $ENV{PI_DIR} = "$tmpdir/foo";
+my $fail_path = "$fail_bin:blib/script:$ENV{PATH}";
 local $ENV{PATH} = "$main_bin:blib/script:$ENV{PATH}";
-local $ENV{PI_EMERGENCY} = "$tmpdir/fail";
-ok(mkdir "$tmpdir/fail");
-
+my $faildir = "$tmpdir/fail";
+local $ENV{PI_EMERGENCY} = $faildir;
+ok(mkdir $faildir);
 my @cmd = (qw(public-inbox-init), "-V$V", $ibx->{name},
                $ibx->{mainrepo}, 'http://localhost/test',
                $ibx->{address}->[0]);
@@ -62,17 +64,54 @@ if ($V == 1) {
        ok(PublicInbox::Import::run_die($cmd, undef, $rdr), 'v1 indexed');
 }
 my $msgs = $ibx->search->query('');
+is(scalar(@$msgs), 1, 'only got one message');
 my $saved = $ibx->smsg_mime($msgs->[0]);
 is($saved->{mime}->as_string, $mime->as_string, 'injected message');
 
-my $patch = 't/data/0001.patch';
-open my $fh, '<', $patch or die "failed to open $patch: $!\n";
-$rdr = { 0 => fileno($fh) };
-ok(PublicInbox::Import::run_die(['public-inbox-mda'], undef, $rdr),
-       'mda delivered a patch');
-my $post = $ibx->search->reopen->query('dfpost:6e006fd7');
-is(scalar(@$post), 1, 'got one result for dfpost');
-my $pre = $ibx->search->query('dfpre:090d998');
-is(scalar(@$pre), 1, 'got one result for dfpre');
-is($post->[0]->{blob}, $pre->[0]->{blob}, 'same message in both cases');
+{
+       my @new = glob("$faildir/new/*");
+       is_deeply(\@new, [], 'nothing in faildir');
+       local $ENV{PATH} = $fail_path;
+       $mime->header_set('Message-ID', '<bar@foo>');
+       ok($tmp->sysseek(0, SEEK_SET) &&
+                       $tmp->truncate(0) &&
+                       $tmp->print($mime->as_string) &&
+                       $tmp->flush &&
+                       $tmp->sysseek(0, SEEK_SET),
+               'rewound and rewrite temporary file');
+       my $cmd = ['public-inbox-mda'];
+       ok(PublicInbox::Import::run_die($cmd, undef, $rdr),
+               'mda did not die on "spam"');
+       @new = glob("$faildir/new/*");
+       is(scalar(@new), 1, 'got a message in faildir');
+       $msgs = $ibx->search->reopen->query('');
+       is(scalar(@$msgs), 1, 'no new message');
+
+       my $config = "$ENV{PI_DIR}/config";
+       ok(-f $config, 'config exists');
+       my $k = 'publicinboxmda.spamcheck';
+       is(system('git', 'config', "--file=$config", $k, 'none'), 0,
+               'disabled spamcheck for mda');
+       ok($tmp->sysseek(0, SEEK_SET), 'rewound input file');
+
+       ok(PublicInbox::Import::run_die($cmd, undef, $rdr), 'mda did not die');
+       my @again = glob("$faildir/new/*");
+       is_deeply(\@again, \@new, 'no new message in faildir');
+       $msgs = $ibx->search->reopen->query('');
+       is(scalar(@$msgs), 2, 'new message added OK');
+}
+
+{
+       my $patch = 't/data/0001.patch';
+       open my $fh, '<', $patch or die "failed to open $patch: $!\n";
+       $rdr = { 0 => fileno($fh) };
+       ok(PublicInbox::Import::run_die(['public-inbox-mda'], undef, $rdr),
+               'mda delivered a patch');
+       my $post = $ibx->search->reopen->query('dfpost:6e006fd7');
+       is(scalar(@$post), 1, 'got one result for dfpost');
+       my $pre = $ibx->search->query('dfpre:090d998');
+       is(scalar(@$pre), 1, 'got one result for dfpre');
+       is($post->[0]->{blob}, $pre->[0]->{blob}, 'same message in both cases');
+}
+
 done_testing();