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 $msgs = $ibx->search->reopen->query('dfpost:6e006fd7');
134 is(scalar(@$msgs), 1, 'diff postimage found');
135 my $post = $msgs->[0];
136 $msgs = $ibx->search->query('dfpre:090d998b6c2c');
137 is(scalar(@$msgs), 1, 'diff preimage found');
138 is($post->{blob}, $msgs->[0]->{blob}, 'same message');
141 # multiple inboxes in the same maildir
143 my $v1repo = "$tmpdir/v1";
144 my $v1pfx = "publicinbox.v1";
145 my $v1addr = 'v1-public@example.com';
146 PublicInbox::Import::init_bare($v1repo);
148 $orig$v1pfx.address=$v1addr
149 $v1pfx.inboxdir=$v1repo
150 $v1pfx.watch=maildir:$maildir
152 my $config = PublicInbox::Config->new(\$cfg2);
154 From: user\@example.com
157 Message-Id: <both\@b.com>
158 Date: Sat, 18 Jun 2016 00:00:00 +0000
162 PublicInbox::Emergency->new($maildir)->prepare(\$both);
163 PublicInbox::Watch->new($config)->scan('full');
164 my $msgs = $ibx->search->reopen->query('m:both@b.com');
165 my $v1 = $config->lookup_name('v1');
166 my $msg = $v1->git->cat_file($msgs->[0]->{blob});
167 is($both, $$msg, 'got original message back from v1');
168 $msg = $ibx->git->cat_file($msgs->[0]->{blob});
169 is($both, $$msg, 'got original message back from v2');
174 From: <u@example.com>
175 List-Id: <i.want.you.to.want.me>
176 Message-ID: <do.want@example.com>
178 my $do_not_want = <<'EOF';
179 From: <u@example.com>
180 List-Id: <do.not.want>
181 X-Mailing-List: no@example.com
182 Message-ID: <do.not.want@example.com>
184 my $cfg = $orig."$cfgpfx.listid=i.want.you.to.want.me\n";
185 PublicInbox::Emergency->new($maildir)->prepare(\$want);
186 PublicInbox::Emergency->new($maildir)->prepare(\$do_not_want);
187 my $config = PublicInbox::Config->new(\$cfg);
188 PublicInbox::Watch->new($config)->scan('full');
189 $ibx = $config->lookup_name('test');
190 my $num = $ibx->mm->num_for('do.want@example.com');
191 ok(defined $num, 'List-ID matched for watch');
192 $num = $ibx->mm->num_for('do.not.want@example.com');
193 is($num, undef, 'unaccepted List-ID matched for watch');
195 $cfg = $orig."$cfgpfx.watchheader=X-Mailing-List:no\@example.com\n";
196 $config = PublicInbox::Config->new(\$cfg);
197 PublicInbox::Watch->new($config)->scan('full');
198 $ibx = $config->lookup_name('test');
199 $num = $ibx->mm->num_for('do.not.want@example.com');
200 ok(defined $num, 'X-Mailing-List matched');