1 # Copyright (C) 2016-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
7 use PublicInbox::Config;
8 use PublicInbox::TestCommon;
9 use PublicInbox::Import;
10 require_mods(qw(Filesys::Notify::Simple));
11 my ($tmpdir, $for_destroy) = tmpdir();
12 my $git_dir = "$tmpdir/test.git";
13 my $maildir = "$tmpdir/md";
14 my $spamdir = "$tmpdir/spam";
15 use_ok 'PublicInbox::WatchMaildir';
16 use_ok 'PublicInbox::Emergency';
17 my $cfgpfx = "publicinbox.test";
18 my $addr = 'test-public@example.com';
19 PublicInbox::Import::init_bare($git_dir);
22 From: user\@example.com
25 Message-Id: <a\@b.com>
26 Date: Sat, 18 Jun 2016 00:00:00 +0000
30 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
31 ok(POSIX::mkfifo("$maildir/cur/fifo", 0777),
32 'create FIFO to ensure we do not get stuck on it :P');
33 my $sem = PublicInbox::Emergency->new($spamdir); # create dirs
35 my $config = PublicInbox::Config->new(\<<EOF);
37 $cfgpfx.inboxdir=$git_dir
38 $cfgpfx.watch=maildir:$maildir
39 $cfgpfx.filter=PublicInbox::Filter::Vger
40 publicinboxlearn.watchspam=maildir:$spamdir
43 PublicInbox::WatchMaildir->new($config)->scan('full');
44 my $git = PublicInbox::Git->new($git_dir);
45 my @list = $git->qx(qw(rev-list refs/heads/master));
46 is(scalar @list, 1, 'one revision in rev-list');
48 my $write_spam = sub {
49 is(scalar glob("$spamdir/new/*"), undef, 'no spam existing');
52 my @new = glob("$spamdir/new/*");
54 my @p = split(m!/+!, $new[0]);
55 ok(link($new[0], "$spamdir/cur/".$p[-1].":2,S"));
56 is(unlink($new[0]), 1);
59 is(unlink(glob("$maildir/new/*")), 1, 'unlinked old spam');
60 PublicInbox::WatchMaildir->new($config)->scan('full');
61 @list = $git->qx(qw(rev-list refs/heads/master));
62 is(scalar @list, 2, 'two revisions in rev-list');
63 @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
64 is(scalar @list, 0, 'tree is empty');
66 # check with scrubbing
69 To unsubscribe from this list: send the line "unsubscribe git" in
70 the body of a message to majordomo\@vger.kernel.org
71 More majordomo info at http://vger.kernel.org/majordomo-info.html\n);
72 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
73 PublicInbox::WatchMaildir->new($config)->scan('full');
74 @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
75 is(scalar @list, 1, 'tree has one file');
76 my $mref = $git->cat_file('HEAD:'.$list[0]);
77 like($$mref, qr/something\n\z/s, 'message scrubbed on import');
79 is(unlink(glob("$maildir/new/*")), 1, 'unlinked spam');
81 PublicInbox::WatchMaildir->new($config)->scan('full');
82 @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
83 is(scalar @list, 0, 'tree is empty');
84 @list = $git->qx(qw(rev-list refs/heads/master));
85 is(scalar @list, 4, 'four revisions in rev-list');
89 my $fail_bin = getcwd()."/t/fail-bin";
90 ok(-x "$fail_bin/spamc", "mock spamc exists");
91 my $fail_path = "$fail_bin:$ENV{PATH}"; # for spamc ham mock
92 local $ENV{PATH} = $fail_path;
93 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
94 $config->{'publicinboxwatch.spamcheck'} = 'spamc';
96 local $SIG{__WARN__} = sub {}; # quiet spam check warning
97 PublicInbox::WatchMaildir->new($config)->scan('full');
99 @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
100 is(scalar @list, 0, 'tree has no files spamc checked');
101 is(unlink(glob("$maildir/new/*")), 1);
105 my $main_bin = getcwd()."/t/main-bin";
106 ok(-x "$main_bin/spamc", "mock spamc exists");
107 my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
108 local $ENV{PATH} = $main_path;
109 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
110 $config->{'publicinboxwatch.spamcheck'} = 'spamc';
111 @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
112 PublicInbox::WatchMaildir->new($config)->scan('full');
113 @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
114 is(scalar @list, 1, 'tree has one file after spamc checked');
116 # XXX: workaround some weird caching/memoization in cat-file,
117 # shouldn't be an issue in real-world use, though...
118 $git = PublicInbox::Git->new($git_dir);
120 my $mref = $git->cat_file('refs/heads/master:'.$list[0]);
121 like($$mref, qr/something\n\z/s, 'message scrubbed on import');
126 PublicInbox::WatchMaildir::is_maildir($dir);
129 is(is_maildir('maildir:/hello//world'), '/hello/world', 'extra slash gone');
130 is(is_maildir('maildir:/hello/world/'), '/hello/world', 'trailing slash gone');
131 is(is_maildir('faildir:/hello/world/'), undef, 'non-maildir rejected');