1 # Copyright (C) 2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
5 use PublicInbox::TestCommon;
8 use PublicInbox::Config;
9 require_mods(qw(Filesys::Notify::Simple DBD::SQLite Search::Xapian));
10 use_ok 'PublicInbox::WatchMaildir';
11 use_ok 'PublicInbox::Emergency';
12 my ($tmpdir, $for_destroy) = tmpdir();
13 local $ENV{PI_CONFIG} = "$tmpdir/pi_config";
17 if (require_git(2.6, 1)) {
18 use_ok 'PublicInbox::V2Writable';
21 skip 'git 2.6+ needed for V2', 40;
27 $SIG{__WARN__} = sub { push @warn, @_ };
28 my $cfgpfx = "publicinbox.$v";
29 my $inboxdir = "$tmpdir/$v";
30 my $maildir = "$tmpdir/md-$v";
31 my $spamdir = "$tmpdir/spam-$v";
32 my $addr = "test-$v\@example.com";
33 my @cmd = ('-init', "-$v", $v, $inboxdir,
34 "http://example.com/$v", $addr);
35 ok(run_script(\@cmd), 'public-inbox init OK');
37 ok(run_script(['-index', $inboxdir]), 'v1 indexed');
39 PublicInbox::Emergency->new($spamdir);
43 From: user\@example.com
47 Message-Id: <a.$i\@b.com>
48 Date: Sat, 05 Jan 2019 04:19:17 +0000
52 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
56 From: spammer\@example.com
60 Message-Id: <a.99\@b.com>
61 Date: Sat, 05 Jan 2019 04:19:17 +0000
65 PublicInbox::Emergency->new($maildir)->prepare(\"$spam");
69 $cfgpfx.inboxdir=$inboxdir
70 $cfgpfx.watch=maildir:$maildir
71 $cfgpfx.filter=PublicInbox::Filter::RubyLang
72 $cfgpfx.altid=serial:alerts:file=msgmap.sqlite3
73 publicinboxwatch.watchspam=maildir:$spamdir
75 my $config = PublicInbox::Config->new(\$orig);
76 my $ibx = $config->lookup_name($v);
77 ok($ibx, 'found inbox by name');
79 my $w = PublicInbox::WatchMaildir->new($config);
84 # make sure all serials are searchable:
87 ($tot, $msgs) = $ibx->search->query("alerts:$i");
88 is($tot, 1, "got one result for alerts:$i");
89 is($msgs->[0]->{mid}, "a.$i\@b.com", "got expected MID for $i");
91 ($tot, undef) = $ibx->search->query('b:spam');
92 is($tot, 1, 'got spam message');
94 my $nr = unlink <$maildir/new/*>;
97 PublicInbox::Emergency->new($spamdir)->prepare(\$spam);
98 my @new = glob("$spamdir/new/*");
99 my @p = split(m!/+!, $new[0]);
100 ok(link($new[0], "$spamdir/cur/".$p[-1].":2,S"));
101 is(unlink($new[0]), 1);
105 $config = PublicInbox::Config->new(\$orig);
106 $ibx = $config->lookup_name($v);
107 ($tot, undef) = $ibx->search->reopen->query('b:spam');
108 is($tot, 0, 'spam removed');
110 is_deeply([], \@warn, 'no warnings');