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
$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
# 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;
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]);
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();