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>
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 $config = PublicInbox::Config->new(\$orig);
48 my $ibx = $config->lookup_name('test');
49 ok($ibx, 'found inbox by name');
51 PublicInbox::Watch->new($config)->scan('full');
52 my $total = scalar @{$ibx->over->recent};
53 is($total, 1, 'got one revision');
55 # my $git = PublicInbox::Git->new("$inboxdir/git/0.git");
56 # my @list = $git->qx(qw(rev-list refs/heads/master));
57 # is(scalar @list, 1, 'one revision in rev-list');
59 my $write_spam = sub {
60 is(scalar glob("$spamdir/new/*"), undef, 'no spam existing');
63 my @new = glob("$spamdir/new/*");
65 my @p = split(m!/+!, $new[0]);
66 ok(link($new[0], "$spamdir/cur/".$p[-1].":2,S"));
67 is(unlink($new[0]), 1);
70 is(unlink(glob("$maildir/new/*")), 1, 'unlinked old spam');
71 PublicInbox::Watch->new($config)->scan('full');
72 is_deeply($ibx->over->recent, [], 'deleted file');
73 is(unlink(glob("$spamdir/cur/*")), 1, 'unlinked trained spam');
75 # check with scrubbing
78 To unsubscribe from this list: send the line "unsubscribe git" in
79 the body of a message to majordomo\@vger.kernel.org
80 More majordomo info at http://vger.kernel.org/majordomo-info.html\n);
81 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
82 PublicInbox::Watch->new($config)->scan('full');
83 my $msgs = $ibx->over->recent;
84 is(scalar(@$msgs), 1, 'got one file back');
85 my $mref = $ibx->msg_by_smsg($msgs->[0]);
86 like($$mref, qr/something\n\z/s, 'message scrubbed on import');
88 is(unlink(glob("$maildir/new/*")), 1, 'unlinked spam');
90 PublicInbox::Watch->new($config)->scan('full');
91 $msgs = $ibx->over->recent;
92 is(scalar(@$msgs), 0, 'inbox is empty again');
93 is(unlink(glob("$spamdir/cur/*")), 1, 'unlinked trained spam');
97 my $fail_bin = getcwd()."/t/fail-bin";
98 ok(-x "$fail_bin/spamc", "mock spamc exists");
99 my $fail_path = "$fail_bin:$ENV{PATH}"; # for spamc ham mock
100 local $ENV{PATH} = $fail_path;
101 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
102 $config->{'publicinboxwatch.spamcheck'} = 'spamc';
104 local $SIG{__WARN__} = sub {}; # quiet spam check warning
105 PublicInbox::Watch->new($config)->scan('full');
107 my $msgs = $ibx->over->recent;
108 is(scalar(@$msgs), 0, 'inbox is still empty');
109 is(unlink(glob("$maildir/new/*")), 1);
113 my $main_bin = getcwd()."/t/main-bin";
114 ok(-x "$main_bin/spamc", "mock spamc exists");
115 my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
116 local $ENV{PATH} = $main_path;
117 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
118 $config->{'publicinboxwatch.spamcheck'} = 'spamc';
119 PublicInbox::Watch->new($config)->scan('full');
120 my $msgs = $ibx->over->recent;
121 is(scalar(@$msgs), 1, 'inbox has one mail after spamc OK-ed a message');
122 my $mref = $ibx->msg_by_smsg($msgs->[0]);
123 like($$mref, qr/something\n\z/s, 'message scrubbed on import');
124 delete $config->{'publicinboxwatch.spamcheck'};
128 my $patch = 't/data/0001.patch';
129 open my $fh, '<', $patch or die "failed to open $patch: $!\n";
130 $msg = do { local $/; <$fh> };
131 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
132 PublicInbox::Watch->new($config)->scan('full');
133 my $post = $ibx->search->reopen->mset('dfpost:6e006fd7');
134 is($post->size, 1, 'diff postimage found');
135 my $pre = $ibx->search->mset('dfpre:090d998b6c2c');
136 is($pre->size, 1, 'diff preimage found');
137 $pre = $ibx->search->mset_to_smsg($ibx, $pre);
138 $post = $ibx->search->mset_to_smsg($ibx, $post);
139 is(scalar(@$pre), 1, 'diff preimage found');
140 is($post->[0]->{blob}, $pre->[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 PublicInbox::Import::init_bare($v1repo);
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::Watch->new($config)->scan('full');
166 my $mset = $ibx->search->reopen->mset('m:both@b.com');
167 my $msgs = $ibx->search->mset_to_smsg($ibx, $mset);
168 my $v1 = $config->lookup_name('v1');
169 my $msg = $v1->git->cat_file($msgs->[0]->{blob});
170 is($both, $$msg, 'got original message back from v1');
171 $msg = $ibx->git->cat_file($msgs->[0]->{blob});
172 is($both, $$msg, 'got original message back from v2');
177 From: <u@example.com>
178 List-Id: <i.want.you.to.want.me>
179 Message-ID: <do.want@example.com>
181 my $do_not_want = <<'EOF';
182 From: <u@example.com>
183 List-Id: <do.not.want>
184 X-Mailing-List: no@example.com
185 Message-ID: <do.not.want@example.com>
187 my $cfg = $orig."$cfgpfx.listid=i.want.you.to.want.me\n";
188 PublicInbox::Emergency->new($maildir)->prepare(\$want);
189 PublicInbox::Emergency->new($maildir)->prepare(\$do_not_want);
190 my $config = PublicInbox::Config->new(\$cfg);
191 PublicInbox::Watch->new($config)->scan('full');
192 $ibx = $config->lookup_name('test');
193 my $num = $ibx->mm->num_for('do.want@example.com');
194 ok(defined $num, 'List-ID matched for watch');
195 $num = $ibx->mm->num_for('do.not.want@example.com');
196 is($num, undef, 'unaccepted List-ID matched for watch');
198 $cfg = $orig."$cfgpfx.watchheader=X-Mailing-List:no\@example.com\n";
199 $config = PublicInbox::Config->new(\$cfg);
200 PublicInbox::Watch->new($config)->scan('full');
201 $ibx = $config->lookup_name('test');
202 $num = $ibx->mm->num_for('do.not.want@example.com');
203 ok(defined $num, 'X-Mailing-List matched');