2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
7 use PublicInbox::TestCommon;
8 use PublicInbox::Import;
9 use PublicInbox::IO qw(write_file);
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 $addr = 'test-public@example.com';
17 my $default_branch = PublicInbox::Import::default_branch;
18 PublicInbox::Import::init_bare($git_dir);
20 From: user\@example.com
23 Message-Id: <a\@b.com>
24 Date: Sat, 18 Jun 2016 00:00:00 +0000
29 my $ibx_ro = create_inbox 'ro', sub { $_[0]->add(PublicInbox::Eml->new($msg)) };
31 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
32 ok(POSIX::mkfifo("$maildir/cur/fifo", 0777),
33 'create FIFO to ensure we do not get stuck on it :P');
34 my $sem = PublicInbox::Emergency->new($spamdir); # create dirs
38 local $SIG{__WARN__} = sub { push @w, @_ };
39 my $cfg = cfg_new $tmpdir, <<EOF;
43 watch = maildir:$spamdir
45 watchspam = maildir:$spamdir
47 my $wm = PublicInbox::Watch->new($cfg);
48 is(scalar grep(/is a spam folder/, @w), 1, 'got warning about spam');
49 is_deeply($wm->{d_map}, { "$spamdir/cur" => 'watchspam' },
50 'only got the spam folder to watch');
53 my $cfg = cfg_new $tmpdir, <<EOF;
57 watch = maildir:$maildir
58 filter = PublicInbox::Filter::Vger
60 watchspam = maildir:$spamdir
61 [publicinbox "test-ro"]
63 inboxdir = $ibx_ro->{inboxdir}
64 address = ro-test\@example.com
66 my $cfg_path = $cfg->{-f};
67 PublicInbox::Watch->new($cfg)->scan('full');
68 my $git = PublicInbox::Git->new($git_dir);
69 my @list = $git->qx('rev-list', $default_branch);
70 is(scalar @list, 1, 'one revision in rev-list');
72 my $write_spam = sub {
73 is(scalar glob("$spamdir/new/*"), undef, 'no spam existing');
76 my @new = glob("$spamdir/new/*");
78 my @p = split(m!/+!, $new[0]);
79 ok(link($new[0], "$spamdir/cur/".$p[-1].":2,S"));
80 is(unlink($new[0]), 1);
83 is(unlink(glob("$maildir/new/*")), 1, 'unlinked old spam');
84 PublicInbox::Watch->new($cfg)->scan('full');
85 @list = $git->qx('rev-list', $default_branch);
86 is(scalar @list, 2, 'two revisions in rev-list');
87 @list = $git->qx('ls-tree', '-r', '--name-only', $default_branch);
88 is(scalar @list, 0, 'tree is empty');
89 is(unlink(glob("$spamdir/cur/*")), 1, 'unlinked trained spam');
91 @list = $ibx_ro->git->qx(qw(ls-tree -r --name-only), $default_branch);
93 is scalar(@list), 1, 'read-only inbox is unchanged';
95 # check with scrubbing
98 To unsubscribe from this list: send the line "unsubscribe git" in
99 the body of a message to majordomo\@vger.kernel.org
100 More majordomo info at http://vger.kernel.org/majordomo-info.html\n);
101 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
102 PublicInbox::Watch->new($cfg)->scan('full');
103 @list = $git->qx('ls-tree', '-r', '--name-only', $default_branch);
104 is(scalar @list, 1, 'tree has one file');
106 my $mref = $git->cat_file('HEAD:'.$list[0]);
107 like($$mref, qr/something\n\z/s, 'message scrubbed on import');
109 is(unlink(glob("$maildir/new/*")), 1, 'unlinked spam');
111 PublicInbox::Watch->new($cfg)->scan('full');
112 @list = $git->qx('ls-tree', '-r', '--name-only', $default_branch);
113 is(scalar @list, 0, 'tree is empty');
114 @list = $git->qx('rev-list', $default_branch);
115 is(scalar @list, 4, 'four revisions in rev-list');
116 is(unlink(glob("$spamdir/cur/*")), 1, 'unlinked trained spam');
120 my $fail_bin = getcwd()."/t/fail-bin";
121 ok(-x "$fail_bin/spamc", "mock spamc exists");
122 my $fail_path = "$fail_bin:$ENV{PATH}"; # for spamc ham mock
123 local $ENV{PATH} = $fail_path;
124 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
125 $cfg->{'publicinboxwatch.spamcheck'} = 'spamc';
127 local $SIG{__WARN__} = sub {}; # quiet spam check warning
128 PublicInbox::Watch->new($cfg)->scan('full');
130 @list = $git->qx('ls-tree', '-r', '--name-only', $default_branch);
131 is(scalar @list, 0, 'tree has no files spamc checked');
132 is(unlink(glob("$maildir/new/*")), 1);
136 my $main_bin = getcwd()."/t/main-bin";
137 ok(-x "$main_bin/spamc", "mock spamc exists");
138 my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
139 local $ENV{PATH} = $main_path;
140 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
141 $cfg->{'publicinboxwatch.spamcheck'} = 'spamc';
142 @list = $git->qx('ls-tree', '-r', '--name-only', $default_branch);
143 PublicInbox::Watch->new($cfg)->scan('full');
144 @list = $git->qx('ls-tree', '-r', '--name-only', $default_branch);
145 is(scalar @list, 1, 'tree has one file after spamc checked');
148 my $mref = $git->cat_file($default_branch.':'.$list[0]);
149 like($$mref, qr/something\n\z/s, 'message scrubbed on import');
152 # end-to-end test which actually uses inotify/kevent
154 my $env = { PI_CONFIG => $cfg_path };
157 write_file '>>', $cfg_path, <<EOM;
159 dropUniqueUnsubscribe
161 # n.b. --no-scan is only intended for testing atm
162 my $wm = start_script([qw(-watch --no-scan)], $env);
163 no_pollerfd($wm->{pid});
164 my $eml = eml_load('t/data/0001.patch');
165 $eml->header_set('Cc', $addr);
166 my $em = PublicInbox::Emergency->new($maildir);
167 $em->prepare(\($eml->as_string));
169 use_ok 'PublicInbox::InboxIdle';
170 use_ok 'PublicInbox::DS';
174 diag "message delivered to `$ibx->{name}'";
177 PublicInbox::DS->Reset;
178 my $ii = PublicInbox::InboxIdle->new($cfg);
179 my $obj = bless \$cb, 'PublicInbox::TestCommon::InboxWakeup';
180 $cfg->each_inbox(sub { $_[0]->subscribe_unlock('ident', $obj) });
181 local @PublicInbox::DS::post_loop_do = (sub { $delivered == 0 });
183 # wait for -watch to setup inotify watches
185 if (eval { require PublicInbox::Inotify } && -d "/proc/$wm->{pid}/fd") {
187 my (@ino, @ino_info);
190 (readlink($_)//'') =~ /\binotify\b/
191 } glob("/proc/$wm->{pid}/fd/*");
192 } until (@ino || time > $end || !tick);
193 if (scalar(@ino) == 1) {
194 my $ino_fd = (split('/', $ino[0]))[-1];
195 my $ino_fdinfo = "/proc/$wm->{pid}/fdinfo/$ino_fd";
196 while (time < $end && open(my $fh, '<', $ino_fdinfo)) {
197 @ino_info = grep(/^inotify wd:/, <$fh>);
198 last if @ino_info >= 3;
201 $sleep = undef if @ino_info >= 3;
205 diag "waiting ${sleep}s for -watch to start up";
209 $em->commit; # wake -watch up
210 diag 'waiting for -watch to import new message';
211 PublicInbox::DS::event_loop();
213 my $head = $git->qx(qw(cat-file commit HEAD));
214 my $subj = $eml->header('Subject');
215 like($head, qr/^\Q$subj\E/sm, 'new commit made');
217 # try dropUniqueUnsubscribe
219 $eml->header_set('Message-ID', '<unsubscribe@example>');
220 $eml->header_set('List-Unsubscribe',
221 '<https://example.com/some-UUID-here/test');
222 $eml->header_set('List-Unsubscribe-Post', 'List-Unsubscribe=One-Click');
223 $em = PublicInbox::Emergency->new($maildir);
224 $em->prepare(\($eml->as_string));
225 $em->commit; # wake -watch up
226 diag 'waiting for -watch to import dropUniqueUnsubscribe message';
227 PublicInbox::DS::event_loop();
228 my $cur = $git->qx(qw(diff HEAD~1..HEAD));
229 like $cur, qr/Message-ID: <unsubscribe\@example>/,
230 'unsubscribe@example imported';
231 unlike $cur, qr/List-Unsubscribe\b/,
232 'List-Unsubscribe-* headers gone w/ dropUniqueUnsubscribe';
237 PublicInbox::DS->Reset;
242 PublicInbox::Watch::is_maildir($dir);
245 is(is_maildir('maildir:/hello//world'), '/hello/world', 'extra slash gone');
246 is(is_maildir('maildir:/hello/world/'), '/hello/world', 'trailing slash gone');
247 is(is_maildir('faildir:/hello/world/'), undef, 'non-maildir rejected');