]> Sergey Matveev's repositories - public-inbox.git/blob - t/watch_maildir_v2.t
59ec247ecfda337f8c0412231fcafdcf2583b5d5
[public-inbox.git] / t / watch_maildir_v2.t
1 # Copyright (C) 2018-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use Test::More;
5 use PublicInbox::Eml;
6 use Cwd;
7 use PublicInbox::Config;
8 use PublicInbox::TestCommon;
9 use PublicInbox::Import;
10 require_git(2.6);
11 require_mods(qw(Search::Xapian DBD::SQLite));
12 require PublicInbox::V2Writable;
13 my ($tmpdir, $for_destroy) = tmpdir();
14 my $inboxdir = "$tmpdir/v2";
15 my $maildir = "$tmpdir/md";
16 my $spamdir = "$tmpdir/spam";
17 use_ok 'PublicInbox::WatchMaildir';
18 use_ok 'PublicInbox::Emergency';
19 my $cfgpfx = "publicinbox.test";
20 my $addr = 'test-public@example.com';
21 my @cmd = ('-init', '-V2', 'test', $inboxdir,
22         'http://example.com/v2list', $addr);
23 local $ENV{PI_CONFIG} = "$tmpdir/pi_config";
24 ok(run_script(\@cmd), 'public-inbox init OK');
25
26 my $msg = <<EOF;
27 From: user\@example.com
28 To: $addr
29 Subject: spam
30 Message-Id: <a\@b.com>
31 Date: Sat, 18 Jun 2016 00:00:00 +0000
32
33 something
34 EOF
35 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
36 ok(POSIX::mkfifo("$maildir/cur/fifo", 0777),
37         'create FIFO to ensure we do not get stuck on it :P');
38 my $sem = PublicInbox::Emergency->new($spamdir); # create dirs
39
40 my $orig = <<EOF;
41 $cfgpfx.address=$addr
42 $cfgpfx.inboxdir=$inboxdir
43 $cfgpfx.watch=maildir:$maildir
44 $cfgpfx.filter=PublicInbox::Filter::Vger
45 publicinboxlearn.watchspam=maildir:$spamdir
46 EOF
47 my $config = PublicInbox::Config->new(\$orig);
48 my $ibx = $config->lookup_name('test');
49 ok($ibx, 'found inbox by name');
50 my $srch = $ibx->search;
51
52 PublicInbox::WatchMaildir->new($config)->scan('full');
53 my $total = scalar @{$srch->reopen->query('')};
54 is($total, 1, 'got one revision');
55
56 # my $git = PublicInbox::Git->new("$inboxdir/git/0.git");
57 # my @list = $git->qx(qw(rev-list refs/heads/master));
58 # is(scalar @list, 1, 'one revision in rev-list');
59
60 my $write_spam = sub {
61         is(scalar glob("$spamdir/new/*"), undef, 'no spam existing');
62         $sem->prepare(\$msg);
63         $sem->commit;
64         my @new = glob("$spamdir/new/*");
65         is(scalar @new, 1);
66         my @p = split(m!/+!, $new[0]);
67         ok(link($new[0], "$spamdir/cur/".$p[-1].":2,S"));
68         is(unlink($new[0]), 1);
69 };
70 $write_spam->();
71 is(unlink(glob("$maildir/new/*")), 1, 'unlinked old spam');
72 PublicInbox::WatchMaildir->new($config)->scan('full');
73 is_deeply($srch->reopen->query(''), [], 'deleted file');
74 is(unlink(glob("$spamdir/cur/*")), 1, 'unlinked trained spam');
75
76 # check with scrubbing
77 {
78         $msg .= qq(--
79 To unsubscribe from this list: send the line "unsubscribe git" in
80 the body of a message to majordomo\@vger.kernel.org
81 More majordomo info at  http://vger.kernel.org/majordomo-info.html\n);
82         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
83         PublicInbox::WatchMaildir->new($config)->scan('full');
84         my $msgs = $srch->reopen->query('');
85         is(scalar(@$msgs), 1, 'got one file back');
86         my $mref = $ibx->msg_by_smsg($msgs->[0]);
87         like($$mref, qr/something\n\z/s, 'message scrubbed on import');
88
89         is(unlink(glob("$maildir/new/*")), 1, 'unlinked spam');
90         $write_spam->();
91         PublicInbox::WatchMaildir->new($config)->scan('full');
92         $msgs = $srch->reopen->query('');
93         is(scalar(@$msgs), 0, 'inbox is empty again');
94         is(unlink(glob("$spamdir/cur/*")), 1, 'unlinked trained spam');
95 }
96
97 {
98         my $fail_bin = getcwd()."/t/fail-bin";
99         ok(-x "$fail_bin/spamc", "mock spamc exists");
100         my $fail_path = "$fail_bin:$ENV{PATH}"; # for spamc ham mock
101         local $ENV{PATH} = $fail_path;
102         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
103         $config->{'publicinboxwatch.spamcheck'} = 'spamc';
104         {
105                 local $SIG{__WARN__} = sub {}; # quiet spam check warning
106                 PublicInbox::WatchMaildir->new($config)->scan('full');
107         }
108         my $msgs = $srch->reopen->query('');
109         is(scalar(@$msgs), 0, 'inbox is still empty');
110         is(unlink(glob("$maildir/new/*")), 1);
111 }
112
113 {
114         my $main_bin = getcwd()."/t/main-bin";
115         ok(-x "$main_bin/spamc", "mock spamc exists");
116         my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
117         local $ENV{PATH} = $main_path;
118         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
119         $config->{'publicinboxwatch.spamcheck'} = 'spamc';
120         PublicInbox::WatchMaildir->new($config)->scan('full');
121         my $msgs = $srch->reopen->query('');
122         is(scalar(@$msgs), 1, 'inbox has one mail after spamc OK-ed a message');
123         my $mref = $ibx->msg_by_smsg($msgs->[0]);
124         like($$mref, qr/something\n\z/s, 'message scrubbed on import');
125         delete $config->{'publicinboxwatch.spamcheck'};
126 }
127
128 {
129         my $patch = 't/data/0001.patch';
130         open my $fh, '<', $patch or die "failed to open $patch: $!\n";
131         $msg = do { local $/; <$fh> };
132         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
133         PublicInbox::WatchMaildir->new($config)->scan('full');
134         my $msgs = $srch->reopen->query('dfpost:6e006fd7');
135         is(scalar(@$msgs), 1, 'diff postimage found');
136         my $post = $msgs->[0];
137         $msgs = $srch->query('dfpre:090d998b6c2c');
138         is(scalar(@$msgs), 1, 'diff preimage found');
139         is($post->{blob}, $msgs->[0]->{blob}, 'same message');
140 }
141
142 # multiple inboxes in the same maildir
143 {
144         my $v1repo = "$tmpdir/v1";
145         my $v1pfx = "publicinbox.v1";
146         my $v1addr = 'v1-public@example.com';
147         PublicInbox::Import::init_bare($v1repo);
148         my $cfg2 = <<EOF;
149 $orig$v1pfx.address=$v1addr
150 $v1pfx.inboxdir=$v1repo
151 $v1pfx.watch=maildir:$maildir
152 EOF
153         my $config = PublicInbox::Config->new(\$cfg2);
154         my $both = <<EOF;
155 From: user\@example.com
156 To: $addr, $v1addr
157 Subject: both
158 Message-Id: <both\@b.com>
159 Date: Sat, 18 Jun 2016 00:00:00 +0000
160
161 both
162 EOF
163         PublicInbox::Emergency->new($maildir)->prepare(\$both);
164         PublicInbox::WatchMaildir->new($config)->scan('full');
165         my $msgs = $srch->reopen->query('m:both@b.com');
166         my $v1 = $config->lookup_name('v1');
167         my $msg = $v1->git->cat_file($msgs->[0]->{blob});
168         is($both, $$msg, 'got original message back from v1');
169         $msg = $ibx->git->cat_file($msgs->[0]->{blob});
170         is($both, $$msg, 'got original message back from v2');
171 }
172
173 {
174         my $want = <<'EOF';
175 From: <u@example.com>
176 List-Id: <i.want.you.to.want.me>
177 Message-ID: <do.want@example.com>
178 EOF
179         my $do_not_want = <<'EOF';
180 From: <u@example.com>
181 List-Id: <do.not.want>
182 X-Mailing-List: no@example.com
183 Message-ID: <do.not.want@example.com>
184 EOF
185         my $cfg = $orig."$cfgpfx.listid=i.want.you.to.want.me\n";
186         PublicInbox::Emergency->new($maildir)->prepare(\$want);
187         PublicInbox::Emergency->new($maildir)->prepare(\$do_not_want);
188         my $config = PublicInbox::Config->new(\$cfg);
189         PublicInbox::WatchMaildir->new($config)->scan('full');
190         $ibx = $config->lookup_name('test');
191         my $num = $ibx->mm->num_for('do.want@example.com');
192         ok(defined $num, 'List-ID matched for watch');
193         $num = $ibx->mm->num_for('do.not.want@example.com');
194         is($num, undef, 'unaccepted List-ID matched for watch');
195
196         $cfg = $orig."$cfgpfx.watchheader=X-Mailing-List:no\@example.com\n";
197         $config = PublicInbox::Config->new(\$cfg);
198         PublicInbox::WatchMaildir->new($config)->scan('full');
199         $ibx = $config->lookup_name('test');
200         $num = $ibx->mm->num_for('do.not.want@example.com');
201         ok(defined $num, 'X-Mailing-List matched');
202 }
203
204 done_testing;