1 # Copyright (C) 2018-2019 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 require './t/common.perl';
10 my @mods = qw(Search::Xapian DBD::SQLite Filesys::Notify::Simple);
11 foreach my $mod (@mods) {
13 plan skip_all => "$mod missing for watch_maildir_v2.t" if $@;
15 require PublicInbox::V2Writable;
16 my $tmpdir = tempdir('watch_maildir-v2-XXXXXX', TMPDIR => 1, CLEANUP => 1);
17 my $inboxdir = "$tmpdir/v2";
18 my $maildir = "$tmpdir/md";
19 my $spamdir = "$tmpdir/spam";
20 use_ok 'PublicInbox::WatchMaildir';
21 use_ok 'PublicInbox::Emergency';
22 my $cfgpfx = "publicinbox.test";
23 my $addr = 'test-public@example.com';
24 my @cmd = ('blib/script/public-inbox-init', '-V2', 'test', $inboxdir,
25 'http://example.com/v2list', $addr);
26 local $ENV{PI_CONFIG} = "$tmpdir/pi_config";
27 is(system(@cmd), 0, 'public-inbox init OK');
30 From: user\@example.com
33 Message-Id: <a\@b.com>
34 Date: Sat, 18 Jun 2016 00:00:00 +0000
38 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
39 ok(POSIX::mkfifo("$maildir/cur/fifo", 0777),
40 'create FIFO to ensure we do not get stuck on it :P');
41 my $sem = PublicInbox::Emergency->new($spamdir); # create dirs
45 $cfgpfx.inboxdir=$inboxdir
46 $cfgpfx.watch=maildir:$maildir
47 $cfgpfx.filter=PublicInbox::Filter::Vger
48 publicinboxlearn.watchspam=maildir:$spamdir
50 my $config = PublicInbox::Config->new(\$orig);
51 my $ibx = $config->lookup_name('test');
52 ok($ibx, 'found inbox by name');
53 my $srch = $ibx->search;
55 PublicInbox::WatchMaildir->new($config)->scan('full');
56 my ($total, undef) = $srch->reopen->query('');
57 is($total, 1, 'got one revision');
59 # my $git = PublicInbox::Git->new("$inboxdir/git/0.git");
60 # my @list = $git->qx(qw(rev-list refs/heads/master));
61 # is(scalar @list, 1, 'one revision in rev-list');
63 my $write_spam = sub {
64 is(scalar glob("$spamdir/new/*"), undef, 'no spam existing');
67 my @new = glob("$spamdir/new/*");
69 my @p = split(m!/+!, $new[0]);
70 ok(link($new[0], "$spamdir/cur/".$p[-1].":2,S"));
71 is(unlink($new[0]), 1);
74 is(unlink(glob("$maildir/new/*")), 1, 'unlinked old spam');
75 PublicInbox::WatchMaildir->new($config)->scan('full');
76 is(($srch->reopen->query(''))[0], 0, 'deleted file');
78 # check with scrubbing
81 To unsubscribe from this list: send the line "unsubscribe git" in
82 the body of a message to majordomo\@vger.kernel.org
83 More majordomo info at http://vger.kernel.org/majordomo-info.html\n);
84 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
85 PublicInbox::WatchMaildir->new($config)->scan('full');
86 my ($nr, $msgs) = $srch->reopen->query('');
87 is($nr, 1, 'got one file back');
88 my $mref = $ibx->msg_by_smsg($msgs->[0]);
89 like($$mref, qr/something\n\z/s, 'message scrubbed on import');
91 is(unlink(glob("$maildir/new/*")), 1, 'unlinked spam');
93 PublicInbox::WatchMaildir->new($config)->scan('full');
94 ($nr, $msgs) = $srch->reopen->query('');
95 is($nr, 0, 'inbox is empty again');
99 my $fail_bin = getcwd()."/t/fail-bin";
100 ok(-x "$fail_bin/spamc", "mock spamc exists");
101 my $fail_path = "$fail_bin:$ENV{PATH}"; # for spamc ham mock
102 local $ENV{PATH} = $fail_path;
103 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
104 $config->{'publicinboxwatch.spamcheck'} = 'spamc';
106 local $SIG{__WARN__} = sub {}; # quiet spam check warning
107 PublicInbox::WatchMaildir->new($config)->scan('full');
109 ($nr, $msgs) = $srch->reopen->query('');
110 is($nr, 0, 'inbox is still empty');
111 is(unlink(glob("$maildir/new/*")), 1);
115 my $main_bin = getcwd()."/t/main-bin";
116 ok(-x "$main_bin/spamc", "mock spamc exists");
117 my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
118 local $ENV{PATH} = $main_path;
119 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
120 $config->{'publicinboxwatch.spamcheck'} = 'spamc';
121 PublicInbox::WatchMaildir->new($config)->scan('full');
122 ($nr, $msgs) = $srch->reopen->query('');
123 is($nr, 1, 'inbox has one mail after spamc OK-ed a message');
124 my $mref = $ibx->msg_by_smsg($msgs->[0]);
125 like($$mref, qr/something\n\z/s, 'message scrubbed on import');
126 delete $config->{'publicinboxwatch.spamcheck'};
130 my $patch = 't/data/0001.patch';
131 open my $fh, '<', $patch or die "failed to open $patch: $!\n";
132 $msg = eval { local $/; <$fh> };
133 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
134 PublicInbox::WatchMaildir->new($config)->scan('full');
135 ($nr, $msgs) = $srch->reopen->query('dfpost:6e006fd7');
136 is($nr, 1, 'diff postimage found');
137 my $post = $msgs->[0];
138 ($nr, $msgs) = $srch->query('dfpre:090d998b6c2c');
139 is($nr, 1, 'diff preimage found');
140 is($post->{blob}, $msgs->[0]->{blob}, 'same message');
143 # multiple inboxes in the same maildir
145 my $v1repo = "$tmpdir/v1";
146 my $v1pfx = "publicinbox.v1";
147 my $v1addr = 'v1-public@example.com';
148 is(system(qw(git init -q --bare), $v1repo), 0, 'v1 init OK');
150 $orig$v1pfx.address=$v1addr
151 $v1pfx.inboxdir=$v1repo
152 $v1pfx.watch=maildir:$maildir
154 my $config = PublicInbox::Config->new(\$cfg2);
156 From: user\@example.com
159 Message-Id: <both\@b.com>
160 Date: Sat, 18 Jun 2016 00:00:00 +0000
164 PublicInbox::Emergency->new($maildir)->prepare(\$both);
165 PublicInbox::WatchMaildir->new($config)->scan('full');
166 my ($total, $msgs) = $srch->reopen->query('m:both@b.com');
167 my $v1 = $config->lookup_name('v1');
168 my $msg = $v1->git->cat_file($msgs->[0]->{blob});
169 is($both, $$msg, 'got original message back from v1');
170 $msg = $ibx->git->cat_file($msgs->[0]->{blob});
171 is($both, $$msg, 'got original message back from v2');
176 From: <u@example.com>
177 List-Id: <i.want.you.to.want.me>
178 Message-ID: <do.want@example.com>
180 my $do_not_want = <<'EOF';
181 From: <u@example.com>
182 List-Id: <do.not.want>
183 X-Mailing-List: no@example.com
184 Message-ID: <do.not.want@example.com>
186 my $cfg = $orig."$cfgpfx.listid=i.want.you.to.want.me\n";
187 PublicInbox::Emergency->new($maildir)->prepare(\$want);
188 PublicInbox::Emergency->new($maildir)->prepare(\$do_not_want);
189 my $config = PublicInbox::Config->new(\$cfg);
190 PublicInbox::WatchMaildir->new($config)->scan('full');
191 $ibx = $config->lookup_name('test');
192 my $num = $ibx->mm->num_for('do.want@example.com');
193 ok(defined $num, 'List-ID matched for watch');
194 $num = $ibx->mm->num_for('do.not.want@example.com');
195 is($num, undef, 'unaccepted List-ID matched for watch');
197 $cfg = $orig."$cfgpfx.watchheader=X-Mailing-List:no\@example.com\n";
198 $config = PublicInbox::Config->new(\$cfg);
199 PublicInbox::WatchMaildir->new($config)->scan('full');
200 $ibx = $config->lookup_name('test');
201 $num = $ibx->mm->num_for('do.not.want@example.com');
202 ok(defined $num, 'X-Mailing-List matched');