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;
10 require_mods(qw(Search::Xapian DBD::SQLite Filesys::Notify::Simple));
11 require PublicInbox::V2Writable;
12 my ($tmpdir, $for_destroy) = tmpdir();
13 my $inboxdir = "$tmpdir/v2";
14 my $maildir = "$tmpdir/md";
15 my $spamdir = "$tmpdir/spam";
16 use_ok 'PublicInbox::WatchMaildir';
17 use_ok 'PublicInbox::Emergency';
18 my $cfgpfx = "publicinbox.test";
19 my $addr = 'test-public@example.com';
20 my @cmd = ('-init', '-V2', 'test', $inboxdir,
21 'http://example.com/v2list', $addr);
22 local $ENV{PI_CONFIG} = "$tmpdir/pi_config";
23 ok(run_script(\@cmd), 'public-inbox init OK');
26 From: user\@example.com
29 Message-Id: <a\@b.com>
30 Date: Sat, 18 Jun 2016 00:00:00 +0000
34 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
35 ok(POSIX::mkfifo("$maildir/cur/fifo", 0777),
36 'create FIFO to ensure we do not get stuck on it :P');
37 my $sem = PublicInbox::Emergency->new($spamdir); # create dirs
41 $cfgpfx.inboxdir=$inboxdir
42 $cfgpfx.watch=maildir:$maildir
43 $cfgpfx.filter=PublicInbox::Filter::Vger
44 publicinboxlearn.watchspam=maildir:$spamdir
46 my $config = PublicInbox::Config->new(\$orig);
47 my $ibx = $config->lookup_name('test');
48 ok($ibx, 'found inbox by name');
49 my $srch = $ibx->search;
51 PublicInbox::WatchMaildir->new($config)->scan('full');
52 my ($total, undef) = $srch->reopen->query('');
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::WatchMaildir->new($config)->scan('full');
72 is(($srch->reopen->query(''))[0], 0, 'deleted file');
74 # check with scrubbing
77 To unsubscribe from this list: send the line "unsubscribe git" in
78 the body of a message to majordomo\@vger.kernel.org
79 More majordomo info at http://vger.kernel.org/majordomo-info.html\n);
80 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
81 PublicInbox::WatchMaildir->new($config)->scan('full');
82 my ($nr, $msgs) = $srch->reopen->query('');
83 is($nr, 1, 'got one file back');
84 my $mref = $ibx->msg_by_smsg($msgs->[0]);
85 like($$mref, qr/something\n\z/s, 'message scrubbed on import');
87 is(unlink(glob("$maildir/new/*")), 1, 'unlinked spam');
89 PublicInbox::WatchMaildir->new($config)->scan('full');
90 ($nr, $msgs) = $srch->reopen->query('');
91 is($nr, 0, 'inbox is empty again');
95 my $fail_bin = getcwd()."/t/fail-bin";
96 ok(-x "$fail_bin/spamc", "mock spamc exists");
97 my $fail_path = "$fail_bin:$ENV{PATH}"; # for spamc ham mock
98 local $ENV{PATH} = $fail_path;
99 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
100 $config->{'publicinboxwatch.spamcheck'} = 'spamc';
102 local $SIG{__WARN__} = sub {}; # quiet spam check warning
103 PublicInbox::WatchMaildir->new($config)->scan('full');
105 my ($nr, $msgs) = $srch->reopen->query('');
106 is($nr, 0, 'inbox is still empty');
107 is(unlink(glob("$maildir/new/*")), 1);
111 my $main_bin = getcwd()."/t/main-bin";
112 ok(-x "$main_bin/spamc", "mock spamc exists");
113 my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
114 local $ENV{PATH} = $main_path;
115 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
116 $config->{'publicinboxwatch.spamcheck'} = 'spamc';
117 PublicInbox::WatchMaildir->new($config)->scan('full');
118 my ($nr, $msgs) = $srch->reopen->query('');
119 is($nr, 1, 'inbox has one mail after spamc OK-ed a message');
120 my $mref = $ibx->msg_by_smsg($msgs->[0]);
121 like($$mref, qr/something\n\z/s, 'message scrubbed on import');
122 delete $config->{'publicinboxwatch.spamcheck'};
126 my $patch = 't/data/0001.patch';
127 open my $fh, '<', $patch or die "failed to open $patch: $!\n";
128 $msg = eval { local $/; <$fh> };
129 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
130 PublicInbox::WatchMaildir->new($config)->scan('full');
131 my ($nr, $msgs) = $srch->reopen->query('dfpost:6e006fd7');
132 is($nr, 1, 'diff postimage found');
133 my $post = $msgs->[0];
134 ($nr, $msgs) = $srch->query('dfpre:090d998b6c2c');
135 is($nr, 1, 'diff preimage found');
136 is($post->{blob}, $msgs->[0]->{blob}, 'same message');
139 # multiple inboxes in the same maildir
141 my $v1repo = "$tmpdir/v1";
142 my $v1pfx = "publicinbox.v1";
143 my $v1addr = 'v1-public@example.com';
144 is(system(qw(git init -q --bare), $v1repo), 0, 'v1 init OK');
146 $orig$v1pfx.address=$v1addr
147 $v1pfx.inboxdir=$v1repo
148 $v1pfx.watch=maildir:$maildir
150 my $config = PublicInbox::Config->new(\$cfg2);
152 From: user\@example.com
155 Message-Id: <both\@b.com>
156 Date: Sat, 18 Jun 2016 00:00:00 +0000
160 PublicInbox::Emergency->new($maildir)->prepare(\$both);
161 PublicInbox::WatchMaildir->new($config)->scan('full');
162 my ($total, $msgs) = $srch->reopen->query('m:both@b.com');
163 my $v1 = $config->lookup_name('v1');
164 my $msg = $v1->git->cat_file($msgs->[0]->{blob});
165 is($both, $$msg, 'got original message back from v1');
166 $msg = $ibx->git->cat_file($msgs->[0]->{blob});
167 is($both, $$msg, 'got original message back from v2');
172 From: <u@example.com>
173 List-Id: <i.want.you.to.want.me>
174 Message-ID: <do.want@example.com>
176 my $do_not_want = <<'EOF';
177 From: <u@example.com>
178 List-Id: <do.not.want>
179 X-Mailing-List: no@example.com
180 Message-ID: <do.not.want@example.com>
182 my $cfg = $orig."$cfgpfx.listid=i.want.you.to.want.me\n";
183 PublicInbox::Emergency->new($maildir)->prepare(\$want);
184 PublicInbox::Emergency->new($maildir)->prepare(\$do_not_want);
185 my $config = PublicInbox::Config->new(\$cfg);
186 PublicInbox::WatchMaildir->new($config)->scan('full');
187 $ibx = $config->lookup_name('test');
188 my $num = $ibx->mm->num_for('do.want@example.com');
189 ok(defined $num, 'List-ID matched for watch');
190 $num = $ibx->mm->num_for('do.not.want@example.com');
191 is($num, undef, 'unaccepted List-ID matched for watch');
193 $cfg = $orig."$cfgpfx.watchheader=X-Mailing-List:no\@example.com\n";
194 $config = PublicInbox::Config->new(\$cfg);
195 PublicInbox::WatchMaildir->new($config)->scan('full');
196 $ibx = $config->lookup_name('test');
197 $num = $ibx->mm->num_for('do.not.want@example.com');
198 ok(defined $num, 'X-Mailing-List matched');