1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use PublicInbox::TestCommon;
7 use PublicInbox::Import;
9 require_mods(qw(Xapian DBD::SQLite));
10 require PublicInbox::V2Writable;
11 my ($tmpdir, $for_destroy) = tmpdir();
12 my $inboxdir = "$tmpdir/v2";
13 my $maildir = "$tmpdir/md";
14 my $spamdir = "$tmpdir/spam";
15 use_ok 'PublicInbox::Watch';
16 use_ok 'PublicInbox::Emergency';
17 my $cfgpfx = "publicinbox.test";
18 my $addr = 'test-public@example.com';
19 my @cmd = ('-init', '-V2', 'test', $inboxdir,
20 'http://example.com/v2list', $addr);
21 local $ENV{PI_CONFIG} = "$tmpdir/pi_config";
22 ok(run_script(\@cmd), 'public-inbox init OK');
25 From: user\@example.com
28 Message-Id: <a\@b.com>
29 Date: Sat, 18 Jun 2016 00:00:00 +0000
33 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
34 ok(POSIX::mkfifo("$maildir/cur/fifo", 0777),
35 'create FIFO to ensure we do not get stuck on it :P');
36 my $sem = PublicInbox::Emergency->new($spamdir); # create dirs
42 watch = maildir:$maildir
43 filter = PublicInbox::Filter::Vger
45 watchspam = maildir:$spamdir
47 my $cfg = cfg_new $tmpdir, $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);
150 my $cfg = cfg_new $tmpdir, <<EOF;
155 watch = maildir:$maildir
158 From: user\@example.com
161 Message-Id: <both\@b.com>
162 Date: Sat, 18 Jun 2016 00:00:00 +0000
166 PublicInbox::Emergency->new($maildir)->prepare(\$both);
167 PublicInbox::Watch->new($cfg)->scan('full');
168 my $mset = $ibx->search->reopen->mset('m:both@b.com');
169 my $msgs = $ibx->search->mset_to_smsg($ibx, $mset);
170 my $v1 = $cfg->lookup_name('v1');
171 my $msg = $v1->git->cat_file($msgs->[0]->{blob});
172 is($both, $$msg, 'got original message back from v1');
173 $msg = $ibx->git->cat_file($msgs->[0]->{blob});
174 is($both, $$msg, 'got original message back from v2');
179 From: <u@example.com>
180 List-Id: <i.want.you.to.want.me>
181 Message-ID: <do.want@example.com>
183 my $do_not_want = <<'EOF';
184 From: <u@example.com>
185 List-Id: <do.not.want>
186 X-Mailing-List: no@example.com
187 Message-ID: <do.not.want@example.com>
189 PublicInbox::Emergency->new($maildir)->prepare(\$want);
190 PublicInbox::Emergency->new($maildir)->prepare(\$do_not_want);
194 listid = i.want.you.to.want.me
196 my $cfg = cfg_new $tmpdir, $raw;
197 PublicInbox::Watch->new($cfg)->scan('full');
198 $ibx = $cfg->lookup_name('test');
199 my $num = $ibx->mm->num_for('do.want@example.com');
200 ok(defined $num, 'List-ID matched for watch');
201 $num = $ibx->mm->num_for('do.not.want@example.com');
202 is($num, undef, 'unaccepted List-ID matched for watch');
203 $raw .= "\twatchheader = X-Mailing-List:no\@example.com\n";
204 $cfg = cfg_new $tmpdir, $raw;
205 PublicInbox::Watch->new($cfg)->scan('full');
206 $ibx = $cfg->lookup_name('test');
207 $num = $ibx->mm->num_for('do.not.want@example.com');
208 ok(defined $num, 'X-Mailing-List matched');