1 # Copyright (C) 2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use File::Temp qw/tempdir/;
7 use PublicInbox::Config;
8 my @mods = qw(Filesys::Notify::Simple PublicInbox::V2Writable);
9 foreach my $mod (@mods) {
11 plan skip_all => "$mod missing for watch_maildir_v2.t" if $@;
14 my $tmpdir = tempdir('watch_maildir-v2-XXXXXX', TMPDIR => 1, CLEANUP => 1);
15 my $mainrepo = "$tmpdir/v2";
16 my $maildir = "$tmpdir/md";
17 my $spamdir = "$tmpdir/spam";
18 use_ok 'PublicInbox::WatchMaildir';
19 use_ok 'PublicInbox::Emergency';
20 my $cfgpfx = "publicinbox.test";
21 my $addr = 'test-public@example.com';
22 my @cmd = ('blib/script/public-inbox-init', '-V2', 'test', $mainrepo,
23 'http://example.com/v2list', $addr);
24 local $ENV{PI_CONFIG} = "$tmpdir/pi_config";
25 is(system(@cmd), 0, 'public-inbox init OK');
28 From: user\@example.com
31 Message-Id: <a\@b.com>
32 Date: Sat, 18 Jun 2016 00:00:00 +0000
36 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
37 ok(POSIX::mkfifo("$maildir/cur/fifo", 0777),
38 'create FIFO to ensure we do not get stuck on it :P');
39 my $sem = PublicInbox::Emergency->new($spamdir); # create dirs
41 my $config = PublicInbox::Config->new({
42 "$cfgpfx.address" => $addr,
43 "$cfgpfx.mainrepo" => $mainrepo,
44 "$cfgpfx.watch" => "maildir:$maildir",
45 "$cfgpfx.filter" => 'PublicInbox::Filter::Vger',
46 "publicinboxlearn.watchspam" => "maildir:$spamdir",
48 my $ibx = $config->lookup_name('test');
49 ok($ibx, 'found inbox by name');
50 my $srch = $ibx->search;
52 PublicInbox::WatchMaildir->new($config)->scan('full');
53 my ($total, undef) = $srch->reopen->query('');
54 is($total, 1, 'got one revision');
56 # my $git = PublicInbox::Git->new("$mainrepo/git/0.git");
57 # my @list = $git->qx(qw(rev-list refs/heads/master));
58 # is(scalar @list, 1, 'one revision in rev-list');
60 my $write_spam = sub {
61 is(scalar glob("$spamdir/new/*"), undef, 'no spam existing');
64 my @new = glob("$spamdir/new/*");
66 my @p = split(m!/+!, $new[0]);
67 ok(link($new[0], "$spamdir/cur/".$p[-1].":2,S"));
68 is(unlink($new[0]), 1);
71 is(unlink(glob("$maildir/new/*")), 1, 'unlinked old spam');
72 PublicInbox::WatchMaildir->new($config)->scan('full');
73 is(($srch->reopen->query(''))[0], 0, 'deleted file');
75 # check with scrubbing
78 To unsubscribe from this list: send the line "unsubscribe git" in
79 the body of a message to majordomo\@vger.kernel.org
80 More majordomo info at http://vger.kernel.org/majordomo-info.html\n);
81 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
82 PublicInbox::WatchMaildir->new($config)->scan('full');
83 my ($nr, $msgs) = $srch->reopen->query('');
84 is($nr, 1, 'got one file back');
85 my $mref = $ibx->msg_by_smsg($msgs->[0]);
86 like($$mref, qr/something\n\z/s, 'message scrubbed on import');
88 is(unlink(glob("$maildir/new/*")), 1, 'unlinked spam');
90 PublicInbox::WatchMaildir->new($config)->scan('full');
91 ($nr, $msgs) = $srch->reopen->query('');
92 is($nr, 0, 'inbox is empty again');
96 my $fail_bin = getcwd()."/t/fail-bin";
97 ok(-x "$fail_bin/spamc", "mock spamc exists");
98 my $fail_path = "$fail_bin:$ENV{PATH}"; # for spamc ham mock
99 local $ENV{PATH} = $fail_path;
100 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
101 $config->{'publicinboxwatch.spamcheck'} = 'spamc';
103 local $SIG{__WARN__} = sub {}; # quiet spam check warning
104 PublicInbox::WatchMaildir->new($config)->scan('full');
106 ($nr, $msgs) = $srch->reopen->query('');
107 is($nr, 0, 'inbox is still empty');
108 is(unlink(glob("$maildir/new/*")), 1);
112 my $main_bin = getcwd()."/t/main-bin";
113 ok(-x "$main_bin/spamc", "mock spamc exists");
114 my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
115 local $ENV{PATH} = $main_path;
116 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
117 $config->{'publicinboxwatch.spamcheck'} = 'spamc';
118 PublicInbox::WatchMaildir->new($config)->scan('full');
119 ($nr, $msgs) = $srch->reopen->query('');
120 is($nr, 1, 'inbox has one mail after spamc OK-ed a message');
121 my $mref = $ibx->msg_by_smsg($msgs->[0]);
122 like($$mref, qr/something\n\z/s, 'message scrubbed on import');