1 # Copyright (C) 2016-2021 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::Watch';
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 $cfg = PublicInbox::Config->new(\<<EOF);
39 $cfgpfx.inboxdir=$git_dir
40 $cfgpfx.watch=maildir:$spamdir
41 publicinboxlearn.watchspam=maildir:$spamdir
43 my $wm = PublicInbox::Watch->new($cfg);
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 $cfg = PublicInbox::Config->new($cfg_path);
65 PublicInbox::Watch->new($cfg)->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::Watch->new($cfg)->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');
87 is(unlink(glob("$spamdir/cur/*")), 1, 'unlinked trained spam');
89 # check with scrubbing
92 To unsubscribe from this list: send the line "unsubscribe git" in
93 the body of a message to majordomo\@vger.kernel.org
94 More majordomo info at http://vger.kernel.org/majordomo-info.html\n);
95 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
96 PublicInbox::Watch->new($cfg)->scan('full');
97 @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
98 is(scalar @list, 1, 'tree has one file');
99 my $mref = $git->cat_file('HEAD:'.$list[0]);
100 like($$mref, qr/something\n\z/s, 'message scrubbed on import');
102 is(unlink(glob("$maildir/new/*")), 1, 'unlinked spam');
104 PublicInbox::Watch->new($cfg)->scan('full');
105 @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
106 is(scalar @list, 0, 'tree is empty');
107 @list = $git->qx(qw(rev-list refs/heads/master));
108 is(scalar @list, 4, 'four revisions in rev-list');
109 is(unlink(glob("$spamdir/cur/*")), 1, 'unlinked trained spam');
113 my $fail_bin = getcwd()."/t/fail-bin";
114 ok(-x "$fail_bin/spamc", "mock spamc exists");
115 my $fail_path = "$fail_bin:$ENV{PATH}"; # for spamc ham mock
116 local $ENV{PATH} = $fail_path;
117 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
118 $cfg->{'publicinboxwatch.spamcheck'} = 'spamc';
120 local $SIG{__WARN__} = sub {}; # quiet spam check warning
121 PublicInbox::Watch->new($cfg)->scan('full');
123 @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
124 is(scalar @list, 0, 'tree has no files spamc checked');
125 is(unlink(glob("$maildir/new/*")), 1);
129 my $main_bin = getcwd()."/t/main-bin";
130 ok(-x "$main_bin/spamc", "mock spamc exists");
131 my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
132 local $ENV{PATH} = $main_path;
133 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
134 $cfg->{'publicinboxwatch.spamcheck'} = 'spamc';
135 @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
136 PublicInbox::Watch->new($cfg)->scan('full');
137 @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
138 is(scalar @list, 1, 'tree has one file after spamc checked');
140 # XXX: workaround some weird caching/memoization in cat-file,
141 # shouldn't be an issue in real-world use, though...
142 $git = PublicInbox::Git->new($git_dir);
144 my $mref = $git->cat_file('refs/heads/master:'.$list[0]);
145 like($$mref, qr/something\n\z/s, 'message scrubbed on import');
148 # end-to-end test which actually uses inotify/kevent
150 my $env = { PI_CONFIG => $cfg_path };
153 # n.b. --no-scan is only intended for testing atm
154 my $wm = start_script([qw(-watch --no-scan)], $env);
155 my $eml = eml_load('t/data/0001.patch');
156 $eml->header_set('Cc', $addr);
157 my $em = PublicInbox::Emergency->new($maildir);
158 $em->prepare(\($eml->as_string));
160 use_ok 'PublicInbox::InboxIdle';
161 use_ok 'PublicInbox::DS';
165 diag "message delivered to `$ibx->{name}'";
168 PublicInbox::DS->Reset;
169 my $ii = PublicInbox::InboxIdle->new($cfg);
170 my $obj = bless \$cb, 'PublicInbox::TestCommon::InboxWakeup';
171 $cfg->each_inbox(sub { $_[0]->subscribe_unlock('ident', $obj) });
172 PublicInbox::DS->SetPostLoopCallback(sub { $delivered == 0 });
174 # wait for -watch to setup inotify watches
176 if (eval { require Linux::Inotify2 } && -d "/proc/$wm->{pid}/fd") {
178 my (@ino, @ino_info);
181 (readlink($_)//'') =~ /\binotify\b/
182 } glob("/proc/$wm->{pid}/fd/*");
183 } until (@ino || time > $end || !tick);
184 if (scalar(@ino) == 1) {
185 my $ino_fd = (split('/', $ino[0]))[-1];
186 my $ino_fdinfo = "/proc/$wm->{pid}/fdinfo/$ino_fd";
187 while (time < $end && open(my $fh, '<', $ino_fdinfo)) {
188 @ino_info = grep(/^inotify wd:/, <$fh>);
189 last if @ino_info >= 3;
192 $sleep = undef if @ino_info >= 3;
196 diag "waiting ${sleep}s for -watch to start up";
200 $em->commit; # wake -watch up
201 diag 'waiting for -watch to import new message';
202 PublicInbox::DS::event_loop();
206 PublicInbox::DS->Reset;
207 my $head = $git->qx(qw(cat-file commit HEAD));
208 my $subj = $eml->header('Subject');
209 like($head, qr/^\Q$subj\E/sm, 'new commit made');
214 PublicInbox::Watch::is_maildir($dir);
217 is(is_maildir('maildir:/hello//world'), '/hello/world', 'extra slash gone');
218 is(is_maildir('maildir:/hello/world/'), '/hello/world', 'trailing slash gone');
219 is(is_maildir('faildir:/hello/world/'), undef, 'non-maildir rejected');