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 my ($tmpdir, $for_destroy) = tmpdir();
11 my $git_dir = "$tmpdir/test.git";
12 my $maildir = "$tmpdir/md";
13 my $spamdir = "$tmpdir/spam";
14 use_ok 'PublicInbox::WatchMaildir';
15 use_ok 'PublicInbox::Emergency';
16 my $cfgpfx = "publicinbox.test";
17 my $addr = 'test-public@example.com';
18 PublicInbox::Import::init_bare($git_dir);
21 From: user\@example.com
24 Message-Id: <a\@b.com>
25 Date: Sat, 18 Jun 2016 00:00:00 +0000
29 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
30 ok(POSIX::mkfifo("$maildir/cur/fifo", 0777),
31 'create FIFO to ensure we do not get stuck on it :P');
32 my $sem = PublicInbox::Emergency->new($spamdir); # create dirs
36 local $SIG{__WARN__} = sub { push @w, @_ };
37 my $config = PublicInbox::Config->new(\<<EOF);
39 $cfgpfx.inboxdir=$git_dir
40 $cfgpfx.watch=maildir:$spamdir
41 publicinboxlearn.watchspam=maildir:$spamdir
43 my $wm = PublicInbox::WatchMaildir->new($config);
44 is(scalar grep(/is a spam folder/, @w), 1, 'got warning about spam');
45 is_deeply($wm->{mdmap}, { "$spamdir/cur" => 'watchspam' },
46 'only got the spam folder to watch');
49 my $cfg_path = "$tmpdir/config";
51 open my $fh, '>', $cfg_path or BAIL_OUT $!;
52 print $fh <<EOF or BAIL_OUT $!;
56 watch = maildir:$maildir
57 filter = PublicInbox::Filter::Vger
59 watchspam = maildir:$spamdir
61 close $fh or BAIL_OUT $!;
64 my $config = PublicInbox::Config->new($cfg_path);
65 PublicInbox::WatchMaildir->new($config)->scan('full');
66 my $git = PublicInbox::Git->new($git_dir);
67 my @list = $git->qx(qw(rev-list refs/heads/master));
68 is(scalar @list, 1, 'one revision in rev-list');
70 my $write_spam = sub {
71 is(scalar glob("$spamdir/new/*"), undef, 'no spam existing');
74 my @new = glob("$spamdir/new/*");
76 my @p = split(m!/+!, $new[0]);
77 ok(link($new[0], "$spamdir/cur/".$p[-1].":2,S"));
78 is(unlink($new[0]), 1);
81 is(unlink(glob("$maildir/new/*")), 1, 'unlinked old spam');
82 PublicInbox::WatchMaildir->new($config)->scan('full');
83 @list = $git->qx(qw(rev-list refs/heads/master));
84 is(scalar @list, 2, 'two revisions in rev-list');
85 @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
86 is(scalar @list, 0, 'tree is empty');
88 # check with scrubbing
91 To unsubscribe from this list: send the line "unsubscribe git" in
92 the body of a message to majordomo\@vger.kernel.org
93 More majordomo info at http://vger.kernel.org/majordomo-info.html\n);
94 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
95 PublicInbox::WatchMaildir->new($config)->scan('full');
96 @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
97 is(scalar @list, 1, 'tree has one file');
98 my $mref = $git->cat_file('HEAD:'.$list[0]);
99 like($$mref, qr/something\n\z/s, 'message scrubbed on import');
101 is(unlink(glob("$maildir/new/*")), 1, 'unlinked spam');
103 PublicInbox::WatchMaildir->new($config)->scan('full');
104 @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
105 is(scalar @list, 0, 'tree is empty');
106 @list = $git->qx(qw(rev-list refs/heads/master));
107 is(scalar @list, 4, 'four revisions in rev-list');
111 my $fail_bin = getcwd()."/t/fail-bin";
112 ok(-x "$fail_bin/spamc", "mock spamc exists");
113 my $fail_path = "$fail_bin:$ENV{PATH}"; # for spamc ham mock
114 local $ENV{PATH} = $fail_path;
115 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
116 $config->{'publicinboxwatch.spamcheck'} = 'spamc';
118 local $SIG{__WARN__} = sub {}; # quiet spam check warning
119 PublicInbox::WatchMaildir->new($config)->scan('full');
121 @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
122 is(scalar @list, 0, 'tree has no files spamc checked');
123 is(unlink(glob("$maildir/new/*")), 1);
127 my $main_bin = getcwd()."/t/main-bin";
128 ok(-x "$main_bin/spamc", "mock spamc exists");
129 my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
130 local $ENV{PATH} = $main_path;
131 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
132 $config->{'publicinboxwatch.spamcheck'} = 'spamc';
133 @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
134 PublicInbox::WatchMaildir->new($config)->scan('full');
135 @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
136 is(scalar @list, 1, 'tree has one file after spamc checked');
138 # XXX: workaround some weird caching/memoization in cat-file,
139 # shouldn't be an issue in real-world use, though...
140 $git = PublicInbox::Git->new($git_dir);
142 my $mref = $git->cat_file('refs/heads/master:'.$list[0]);
143 like($$mref, qr/something\n\z/s, 'message scrubbed on import');
146 # end-to-end test which actually uses inotify/kevent
148 my $env = { PI_CONFIG => $cfg_path };
151 # n.b. --no-scan is only intended for testing atm
152 my $wm = start_script([qw(-watch --no-scan)], $env);
153 my $eml = eml_load('t/data/0001.patch');
154 $eml->header_set('Cc', $addr);
155 my $em = PublicInbox::Emergency->new($maildir);
156 $em->prepare(\($eml->as_string));
158 use_ok 'PublicInbox::InboxIdle';
159 use_ok 'PublicInbox::DS';
163 diag "message delivered to `$ibx->{name}'";
166 PublicInbox::DS->Reset;
167 my $ii = PublicInbox::InboxIdle->new($config);
168 my $obj = bless \$cb, 'PublicInbox::TestCommon::InboxWakeup';
169 $config->each_inbox(sub { $_[0]->subscribe_unlock('ident', $obj) });
170 PublicInbox::DS->SetPostLoopCallback(sub { $delivered == 0 });
172 # wait for -watch to setup inotify watches
174 if (eval { require Linux::Inotify2 } && -d "/proc/$wm->{pid}/fd") {
176 my (@ino, @ino_info);
179 (readlink($_)//'') =~ /\binotify\b/
180 } glob("/proc/$wm->{pid}/fd/*");
181 } until (@ino || time > $end || !tick);
182 if (scalar(@ino) == 1) {
183 my $ino_fd = (split('/', $ino[0]))[-1];
184 my $ino_fdinfo = "/proc/$wm->{pid}/fdinfo/$ino_fd";
185 while (time < $end && open(my $fh, '<', $ino_fdinfo)) {
186 @ino_info = grep(/^inotify wd:/, <$fh>);
187 last if @ino_info >= 4;
190 $sleep = undef if @ino_info >= 4;
194 diag "waiting ${sleep}s for -watch to start up";
198 $em->commit; # wake -watch up
199 diag 'waiting for -watch to import new message';
200 PublicInbox::DS->EventLoop;
204 PublicInbox::DS->Reset;
205 my $head = $git->qx(qw(cat-file commit HEAD));
206 my $subj = $eml->header('Subject');
207 like($head, qr/^\Q$subj\E/sm, 'new commit made');
212 PublicInbox::WatchMaildir::is_maildir($dir);
215 is(is_maildir('maildir:/hello//world'), '/hello/world', 'extra slash gone');
216 is(is_maildir('maildir:/hello/world/'), '/hello/world', 'trailing slash gone');
217 is(is_maildir('faildir:/hello/world/'), undef, 'non-maildir rejected');