1 # Copyright (C) 2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use File::Temp qw/tempdir/;
7 use PublicInbox::Config;
8 my @mods = qw(Filesys::Notify::Simple PublicInbox::V2Writable);
9 foreach my $mod (@mods) {
11 plan skip_all => "$mod missing for watch_maildir_v2.t" if $@;
14 my $tmpdir = tempdir('watch_maildir-v2-XXXXXX', TMPDIR => 1, CLEANUP => 1);
15 my $mainrepo = "$tmpdir/v2";
16 my $maildir = "$tmpdir/md";
17 my $spamdir = "$tmpdir/spam";
18 use_ok 'PublicInbox::WatchMaildir';
19 use_ok 'PublicInbox::Emergency';
20 my $cfgpfx = "publicinbox.test";
21 my $addr = 'test-public@example.com';
22 my @cmd = ('blib/script/public-inbox-init', '-V2', 'test', $mainrepo,
23 'http://example.com/v2list', $addr);
24 local $ENV{PI_CONFIG} = "$tmpdir/pi_config";
25 is(system(@cmd), 0, 'public-inbox init OK');
28 From: user\@example.com
31 Message-Id: <a\@b.com>
32 Date: Sat, 18 Jun 2016 00:00:00 +0000
36 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
37 ok(POSIX::mkfifo("$maildir/cur/fifo", 0777),
38 'create FIFO to ensure we do not get stuck on it :P');
39 my $sem = PublicInbox::Emergency->new($spamdir); # create dirs
42 "$cfgpfx.address" => $addr,
43 "$cfgpfx.mainrepo" => $mainrepo,
44 "$cfgpfx.watch" => "maildir:$maildir",
45 "$cfgpfx.filter" => 'PublicInbox::Filter::Vger',
46 "publicinboxlearn.watchspam" => "maildir:$spamdir"
48 my $config = PublicInbox::Config->new({%orig});
49 my $ibx = $config->lookup_name('test');
50 ok($ibx, 'found inbox by name');
51 my $srch = $ibx->search;
53 PublicInbox::WatchMaildir->new($config)->scan('full');
54 my ($total, undef) = $srch->reopen->query('');
55 is($total, 1, 'got one revision');
57 # my $git = PublicInbox::Git->new("$mainrepo/git/0.git");
58 # my @list = $git->qx(qw(rev-list refs/heads/master));
59 # is(scalar @list, 1, 'one revision in rev-list');
61 my $write_spam = sub {
62 is(scalar glob("$spamdir/new/*"), undef, 'no spam existing');
65 my @new = glob("$spamdir/new/*");
67 my @p = split(m!/+!, $new[0]);
68 ok(link($new[0], "$spamdir/cur/".$p[-1].":2,S"));
69 is(unlink($new[0]), 1);
72 is(unlink(glob("$maildir/new/*")), 1, 'unlinked old spam');
73 PublicInbox::WatchMaildir->new($config)->scan('full');
74 is(($srch->reopen->query(''))[0], 0, 'deleted file');
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::WatchMaildir->new($config)->scan('full');
84 my ($nr, $msgs) = $srch->reopen->query('');
85 is($nr, 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::WatchMaildir->new($config)->scan('full');
92 ($nr, $msgs) = $srch->reopen->query('');
93 is($nr, 0, 'inbox is empty again');
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::WatchMaildir->new($config)->scan('full');
107 ($nr, $msgs) = $srch->reopen->query('');
108 is($nr, 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::WatchMaildir->new($config)->scan('full');
120 ($nr, $msgs) = $srch->reopen->query('');
121 is($nr, 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 = eval { local $/; <$fh> };
131 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
132 PublicInbox::WatchMaildir->new($config)->scan('full');
133 ($nr, $msgs) = $srch->reopen->query('dfpost:6e006fd7');
134 is($nr, 1, 'diff postimage found');
135 my $post = $msgs->[0];
136 ($nr, $msgs) = $srch->query('dfpre:090d998b6c2c');
137 is($nr, 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 is(system(qw(git init -q --bare), $v1repo), 0, 'v1 init OK');
147 my $config = PublicInbox::Config->new({
149 "$v1pfx.address" => $v1addr,
150 "$v1pfx.mainrepo" => $v1repo,
151 "$v1pfx.watch" => "maildir:$maildir",
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::WatchMaildir->new($config)->scan('full');
164 my ($total, $msgs) = $srch->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');