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::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');
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');
50 my $srch = $ibx->search;
52 PublicInbox::WatchMaildir->new($config)->scan('full');
53 my ($total, undef) = $srch->reopen->query('');
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::WatchMaildir->new($config)->scan('full');
73 is(($srch->reopen->query(''))[0], 0, 'deleted file');
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::WatchMaildir->new($config)->scan('full');
83 my ($nr, $msgs) = $srch->reopen->query('');
84 is($nr, 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::WatchMaildir->new($config)->scan('full');
91 ($nr, $msgs) = $srch->reopen->query('');
92 is($nr, 0, 'inbox is empty again');
96 my $fail_bin = getcwd()."/t/fail-bin";
97 ok(-x "$fail_bin/spamc", "mock spamc exists");
98 my $fail_path = "$fail_bin:$ENV{PATH}"; # for spamc ham mock
99 local $ENV{PATH} = $fail_path;
100 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
101 $config->{'publicinboxwatch.spamcheck'} = 'spamc';
103 local $SIG{__WARN__} = sub {}; # quiet spam check warning
104 PublicInbox::WatchMaildir->new($config)->scan('full');
106 my ($nr, $msgs) = $srch->reopen->query('');
107 is($nr, 0, 'inbox is still empty');
108 is(unlink(glob("$maildir/new/*")), 1);
112 my $main_bin = getcwd()."/t/main-bin";
113 ok(-x "$main_bin/spamc", "mock spamc exists");
114 my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
115 local $ENV{PATH} = $main_path;
116 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
117 $config->{'publicinboxwatch.spamcheck'} = 'spamc';
118 PublicInbox::WatchMaildir->new($config)->scan('full');
119 my ($nr, $msgs) = $srch->reopen->query('');
120 is($nr, 1, 'inbox has one mail after spamc OK-ed a message');
121 my $mref = $ibx->msg_by_smsg($msgs->[0]);
122 like($$mref, qr/something\n\z/s, 'message scrubbed on import');
123 delete $config->{'publicinboxwatch.spamcheck'};
127 my $patch = 't/data/0001.patch';
128 open my $fh, '<', $patch or die "failed to open $patch: $!\n";
129 $msg = do { local $/; <$fh> };
130 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
131 PublicInbox::WatchMaildir->new($config)->scan('full');
132 my ($nr, $msgs) = $srch->reopen->query('dfpost:6e006fd7');
133 is($nr, 1, 'diff postimage found');
134 my $post = $msgs->[0];
135 ($nr, $msgs) = $srch->query('dfpre:090d998b6c2c');
136 is($nr, 1, 'diff preimage found');
137 is($post->{blob}, $msgs->[0]->{blob}, 'same message');
140 # multiple inboxes in the same maildir
142 my $v1repo = "$tmpdir/v1";
143 my $v1pfx = "publicinbox.v1";
144 my $v1addr = 'v1-public@example.com';
145 PublicInbox::Import::init_bare($v1repo);
147 $orig$v1pfx.address=$v1addr
148 $v1pfx.inboxdir=$v1repo
149 $v1pfx.watch=maildir:$maildir
151 my $config = PublicInbox::Config->new(\$cfg2);
153 From: user\@example.com
156 Message-Id: <both\@b.com>
157 Date: Sat, 18 Jun 2016 00:00:00 +0000
161 PublicInbox::Emergency->new($maildir)->prepare(\$both);
162 PublicInbox::WatchMaildir->new($config)->scan('full');
163 my ($total, $msgs) = $srch->reopen->query('m:both@b.com');
164 my $v1 = $config->lookup_name('v1');
165 my $msg = $v1->git->cat_file($msgs->[0]->{blob});
166 is($both, $$msg, 'got original message back from v1');
167 $msg = $ibx->git->cat_file($msgs->[0]->{blob});
168 is($both, $$msg, 'got original message back from v2');
173 From: <u@example.com>
174 List-Id: <i.want.you.to.want.me>
175 Message-ID: <do.want@example.com>
177 my $do_not_want = <<'EOF';
178 From: <u@example.com>
179 List-Id: <do.not.want>
180 X-Mailing-List: no@example.com
181 Message-ID: <do.not.want@example.com>
183 my $cfg = $orig."$cfgpfx.listid=i.want.you.to.want.me\n";
184 PublicInbox::Emergency->new($maildir)->prepare(\$want);
185 PublicInbox::Emergency->new($maildir)->prepare(\$do_not_want);
186 my $config = PublicInbox::Config->new(\$cfg);
187 PublicInbox::WatchMaildir->new($config)->scan('full');
188 $ibx = $config->lookup_name('test');
189 my $num = $ibx->mm->num_for('do.want@example.com');
190 ok(defined $num, 'List-ID matched for watch');
191 $num = $ibx->mm->num_for('do.not.want@example.com');
192 is($num, undef, 'unaccepted List-ID matched for watch');
194 $cfg = $orig."$cfgpfx.watchheader=X-Mailing-List:no\@example.com\n";
195 $config = PublicInbox::Config->new(\$cfg);
196 PublicInbox::WatchMaildir->new($config)->scan('full');
197 $ibx = $config->lookup_name('test');
198 $num = $ibx->mm->num_for('do.not.want@example.com');
199 ok(defined $num, 'X-Mailing-List matched');