1 # Copyright (C) 2018-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;
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::Watch';
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');
27 From: user\@example.com
30 Message-Id: <a\@b.com>
31 Date: Sat, 18 Jun 2016 00:00:00 +0000
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
42 $cfgpfx.inboxdir=$inboxdir
43 $cfgpfx.watch=maildir:$maildir
44 $cfgpfx.filter=PublicInbox::Filter::Vger
45 publicinboxlearn.watchspam=maildir:$spamdir
47 my $cfg = PublicInbox::Config->new(\$orig);
48 my $ibx = $cfg->lookup_name('test');
49 ok($ibx, 'found inbox by name');
50 $ibx->{-no_fsync} = 1;
52 PublicInbox::Watch->new($cfg)->scan('full');
53 my $total = scalar @{$ibx->over->recent};
54 is($total, 1, 'got one revision');
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');
60 my $write_spam = sub {
61 is(scalar glob("$spamdir/new/*"), undef, 'no spam existing');
64 my @new = glob("$spamdir/new/*");
66 my @p = split(m!/+!, $new[0]);
67 ok(link($new[0], "$spamdir/cur/".$p[-1].":2,S"));
68 is(unlink($new[0]), 1);
71 is(unlink(glob("$maildir/new/*")), 1, 'unlinked old spam');
72 PublicInbox::Watch->new($cfg)->scan('full');
73 is_deeply($ibx->over->recent, [], 'deleted file');
74 is(unlink(glob("$spamdir/cur/*")), 1, 'unlinked trained spam');
76 # check with scrubbing
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::Watch->new($cfg)->scan('full');
84 my $msgs = $ibx->over->recent;
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');
89 is(unlink(glob("$maildir/new/*")), 1, 'unlinked spam');
91 PublicInbox::Watch->new($cfg)->scan('full');
92 $msgs = $ibx->over->recent;
93 is(scalar(@$msgs), 0, 'inbox is empty again');
94 is(unlink(glob("$spamdir/cur/*")), 1, 'unlinked trained spam');
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 $cfg->{'publicinboxwatch.spamcheck'} = 'spamc';
105 local $SIG{__WARN__} = sub {}; # quiet spam check warning
106 PublicInbox::Watch->new($cfg)->scan('full');
108 my $msgs = $ibx->over->recent;
109 is(scalar(@$msgs), 0, 'inbox is still empty');
110 is(unlink(glob("$maildir/new/*")), 1);
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 $cfg->{'publicinboxwatch.spamcheck'} = 'spamc';
120 PublicInbox::Watch->new($cfg)->scan('full');
121 my $msgs = $ibx->over->recent;
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 $cfg->{'publicinboxwatch.spamcheck'};
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::Watch->new($cfg)->scan('full');
134 my $post = $ibx->search->reopen->mset('dfpost:6e006fd7');
135 is($post->size, 1, 'diff postimage found');
136 my $pre = $ibx->search->mset('dfpre:090d998b6c2c');
137 is($pre->size, 1, 'diff preimage found');
138 $pre = $ibx->search->mset_to_smsg($ibx, $pre);
139 $post = $ibx->search->mset_to_smsg($ibx, $post);
140 is(scalar(@$pre), 1, 'diff preimage found');
141 is($post->[0]->{blob}, $pre->[0]->{blob}, 'same message');
144 # multiple inboxes in the same maildir
146 my $v1repo = "$tmpdir/v1";
147 my $v1pfx = "publicinbox.v1";
148 my $v1addr = 'v1-public@example.com';
149 PublicInbox::Import::init_bare($v1repo);
151 $orig$v1pfx.address=$v1addr
152 $v1pfx.inboxdir=$v1repo
153 $v1pfx.watch=maildir:$maildir
155 my $cfg = PublicInbox::Config->new(\$raw);
157 From: user\@example.com
160 Message-Id: <both\@b.com>
161 Date: Sat, 18 Jun 2016 00:00:00 +0000
165 PublicInbox::Emergency->new($maildir)->prepare(\$both);
166 PublicInbox::Watch->new($cfg)->scan('full');
167 my $mset = $ibx->search->reopen->mset('m:both@b.com');
168 my $msgs = $ibx->search->mset_to_smsg($ibx, $mset);
169 my $v1 = $cfg->lookup_name('v1');
170 my $msg = $v1->git->cat_file($msgs->[0]->{blob});
171 is($both, $$msg, 'got original message back from v1');
172 $msg = $ibx->git->cat_file($msgs->[0]->{blob});
173 is($both, $$msg, 'got original message back from v2');
178 From: <u@example.com>
179 List-Id: <i.want.you.to.want.me>
180 Message-ID: <do.want@example.com>
182 my $do_not_want = <<'EOF';
183 From: <u@example.com>
184 List-Id: <do.not.want>
185 X-Mailing-List: no@example.com
186 Message-ID: <do.not.want@example.com>
188 my $raw = $orig."$cfgpfx.listid=i.want.you.to.want.me\n";
189 PublicInbox::Emergency->new($maildir)->prepare(\$want);
190 PublicInbox::Emergency->new($maildir)->prepare(\$do_not_want);
191 my $cfg = PublicInbox::Config->new(\$raw);
192 PublicInbox::Watch->new($cfg)->scan('full');
193 $ibx = $cfg->lookup_name('test');
194 my $num = $ibx->mm->num_for('do.want@example.com');
195 ok(defined $num, 'List-ID matched for watch');
196 $num = $ibx->mm->num_for('do.not.want@example.com');
197 is($num, undef, 'unaccepted List-ID matched for watch');
199 $raw = $orig."$cfgpfx.watchheader=X-Mailing-List:no\@example.com\n";
200 $cfg = PublicInbox::Config->new(\$raw);
201 PublicInbox::Watch->new($cfg)->scan('full');
202 $ibx = $cfg->lookup_name('test');
203 $num = $ibx->mm->num_for('do.not.want@example.com');
204 ok(defined $num, 'X-Mailing-List matched');