1 # Copyright (C) 2014-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
8 use PublicInbox::MID qw(mid2path);
10 use PublicInbox::InboxWritable;
11 use PublicInbox::TestCommon;
12 my ($tmpdir, $for_destroy) = tmpdir();
13 my $home = "$tmpdir/pi-home";
14 my $pi_home = "$home/.public-inbox";
15 my $pi_config = "$pi_home/config";
16 my $maindir = "$tmpdir/main.git";
17 my $main_bin = getcwd()."/t/main-bin";
18 my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
19 my $fail_bin = getcwd()."/t/fail-bin";
20 my $fail_path = "$fail_bin:$ENV{PATH}"; # for spamc spam mock
21 my $addr = 'test-public@example.com';
22 my $cfgpfx = "publicinbox.test";
23 my $faildir = "$home/faildir/";
25 my $git = PublicInbox::Git->new($maindir);
27 my $fail_bad_header = sub ($$$) {
28 my ($good_rev, $msg, $in) = @_;
29 my @f = glob("$faildir/*/*");
31 my ($out, $err) = ("", "");
32 my $opt = { 0 => \$in, 1 => \$out, 2 => \$err };
33 local $ENV{PATH} = $main_path;
34 ok(run_script(['-mda'], undef, $opt),
35 "no error on undeliverable ($msg)");
36 my $rev = $git->qx(qw(rev-list HEAD));
38 is($rev, $good_rev, "bad revision not committed ($msg)");
39 @f = glob("$faildir/*/*");
40 is(scalar @f, 1, "faildir written to");
45 ok(-x "$main_bin/spamc",
46 "spamc ham mock found (run in top of source tree");
47 ok(-x "$fail_bin/spamc",
48 "spamc mock found (run in top of source tree");
49 is(1, mkdir($home, 0755), "setup ~/ for testing");
50 is(1, mkdir($pi_home, 0755), "setup ~/.public-inbox");
51 is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
53 open my $fh, '>>', $pi_config or die;
54 print $fh <<EOF or die;
62 local $ENV{GIT_COMMITTER_NAME} = eval {
64 use PublicInbox::Address;
65 use Encode qw/encode/;
66 my $eml = 't/utf8.eml';
67 my $msg = PublicInbox::InboxWritable::mime_from_path($eml) or
68 die "failed to open $eml: $!";
69 my $from = $msg->header('From');
70 my ($author) = PublicInbox::Address::names($from);
71 my ($email) = PublicInbox::Address::emails($from);
72 my $date = $msg->header('Date');
75 encode('us-ascii', my $tmp = $author, Encode::HTMLCREF),
76 'HTML conversion is correct');
77 is($email, 'e@example.com', 'email parsed correctly');
78 is($date, 'Thu, 01 Jan 1970 00:00:00 +0000',
79 'message date parsed correctly');
86 local $ENV{PI_EMERGENCY} = $faildir;
87 local $ENV{HOME} = $home;
88 local $ENV{ORIGINAL_RECIPIENT} = $addr;
89 my $simple = Email::Simple->new(<<EOF);
90 From: Me <me\@example.com>
91 To: You <you\@example.com>
93 Message-Id: <blah\@example.com>
95 Date: Thu, 01 Jan 1970 00:00:00 +0000
98 my $in = $simple->as_string;
100 # ensure successful message delivery
102 local $ENV{PATH} = $main_path;
103 ok(run_script(['-mda'], undef, { 0 => \$in }));
104 my $rev = $git->qx(qw(rev-list HEAD));
105 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
107 my $cmt = $git->cat_file($rev);
108 like($$cmt, qr/^author Me <me\@example\.com> 0 \+0000\n/m,
109 "author info set correctly");
110 like($$cmt, qr/^committer test <test-public\@example\.com>/m,
111 "committer info set correctly");
115 # ensure failures work, fail with bad spamc
117 my @prev = <$faildir/new/*>;
118 is(scalar @prev, 0 , "nothing in PI_EMERGENCY before");
119 local $ENV{PATH} = $fail_path;
120 ok(run_script(['-mda'], undef, { 0 => \$in }));
121 my @revs = $git->qx(qw(rev-list HEAD));
122 is(scalar @revs, 1, "bad revision not committed");
123 my @new = <$faildir/new/*>;
124 is(scalar @new, 1, "PI_EMERGENCY is written to");
127 $fail_bad_header->($good_rev, "bad recipient", <<"");
128 From: Me <me\@example.com>
129 To: You <you\@example.com>
130 Message-Id: <bad-recipient\@example.com>
132 Date: Thu, 01 Jan 1970 00:00:00 +0000
134 my $fail = $fail_bad_header->($good_rev, "duplicate Message-ID", <<"");
135 From: Me <me\@example.com>
136 To: You <you\@example.com>
138 Message-ID: <blah\@example.com>
140 Date: Thu, 01 Jan 1970 00:00:00 +0000
142 like($fail->[2], qr/CONFLICT/, "duplicate Message-ID message");
144 $fail_bad_header->($good_rev, "missing From:", <<"");
146 Message-ID: <missing-from\@example.com>
148 Date: Thu, 01 Jan 1970 00:00:00 +0000
150 $fail_bad_header->($good_rev, "short subject:", <<"");
152 From: cat\@example.com
153 Message-ID: <short-subject\@example.com>
155 Date: Thu, 01 Jan 1970 00:00:00 +0000
157 $fail_bad_header->($good_rev, "no date", <<"");
160 Message-ID: <no-date\@example.com>
163 $fail_bad_header->($good_rev, "bad date", <<"");
166 Message-ID: <bad-date\@example.com>
174 local $ENV{PI_EMERGENCY} = $faildir;
175 local $ENV{HOME} = $home;
176 local $ENV{ORIGINAL_RECIPIENT} = $addr;
177 local $ENV{PATH} = $main_path;
178 my $mid = 'spam-train@example.com';
179 my $simple = Email::Simple->new(<<EOF);
180 From: Spammer <spammer\@example.com>
181 To: You <you\@example.com>
184 Subject: this message will be trained as spam
185 Date: Thu, 01 Jan 1970 00:00:00 +0000
188 my $in = $simple->as_string;
191 # deliver the spam message, first
192 ok(run_script(['-mda'], undef, { 0 => \$in }));
193 my $path = mid2path($mid);
194 my $msg = $git->cat_file("HEAD:$path");
195 like($$msg, qr/\Q$mid\E/, "message delivered");
198 local $ENV{GIT_AUTHOR_EMAIL} = 'trainer@example.com';
199 local $ENV{GIT_COMMITTER_EMAIL} = 'trainer@example.com';
200 local $ENV{GIT_COMMITTER_NAME};
201 delete $ENV{GIT_COMMITTER_NAME};
202 ok(run_script(['-learn', 'spam'], undef, { 0 => $msg }),
203 "no failure from learning spam");
204 ok(run_script(['-learn', 'spam'], undef, { 0 => $msg }),
205 "no failure from learning spam idempotently");
211 local $ENV{PI_EMERGENCY} = $faildir;
212 local $ENV{HOME} = $home;
213 local $ENV{ORIGINAL_RECIPIENT} = $addr;
214 local $ENV{PATH} = $main_path;
215 my $mid = 'ham-train@example.com';
216 my $simple = Email::Simple->new(<<EOF);
217 From: False-positive <hammer\@example.com>
218 To: You <you\@example.com>
221 Subject: this message will be trained as spam
222 Date: Thu, 01 Jan 1970 00:00:00 +0000
225 my $in = $simple->as_string;
228 # these should be overridden
229 local $ENV{GIT_AUTHOR_EMAIL} = 'trainer@example.com';
230 local $ENV{GIT_COMMITTER_EMAIL} = 'trainer@example.com';
232 ok(run_script(['-learn', 'ham'], undef, { 0 => \$in }),
233 "learned ham without failure");
234 my $path = mid2path($mid);
235 my $msg = $git->cat_file("HEAD:$path");
236 like($$msg, qr/\Q$mid\E/, "ham message delivered");
237 ok(run_script(['-learn', 'ham'], undef, { 0 => \$in }),
238 "learned ham idempotently ");
240 # ensure trained email is filtered, too
241 my $html_body = "<html><body>hi</body></html>";
245 content_type => 'text/html; charset=UTF-8',
246 encoding => 'base64',
252 content_type => 'text/plain',
253 encoding => 'quoted-printable',
255 body => 'hi = "bye"',
258 $mid = 'multipart-html-sucks@11';
259 $mime = Email::MIME->create(
261 From => 'a@example.com',
264 'Message-ID' => "<$mid>",
265 'Content-Type' => 'multipart/alternative',
271 $in = $mime->as_string;
272 ok(run_script(['-learn', 'ham'], undef, { 0 => \$in }),
273 "learned ham without failure");
274 my $path = mid2path($mid);
275 $msg = $git->cat_file("HEAD:$path");
276 like($$msg, qr/<\Q$mid\E>/, "ham message delivered");
277 unlike($$msg, qr/<html>/i, '<html> filtered');
281 # List-ID based delivery
283 local $ENV{PI_EMERGENCY} = $faildir;
284 local $ENV{HOME} = $home;
285 local $ENV{ORIGINAL_RECIPIENT} = undef;
286 delete $ENV{ORIGINAL_RECIPIENT};
287 local $ENV{PATH} = $main_path;
288 my $list_id = 'foo.example.com';
289 my $mid = 'list-id-delivery@example.com';
290 my $simple = Email::Simple->new(<<EOF);
291 From: user <user\@example.com>
292 To: You <you\@example.com>
296 Subject: this message will be trained as spam
297 Date: Thu, 01 Jan 1970 00:00:00 +0000
300 system(qw(git config --file), $pi_config, "$cfgpfx.listid", $list_id);
301 $? == 0 or die "failed to set listid $?";
302 my $in = $simple->as_string;
303 ok(run_script(['-mda'], undef, { 0 => \$in }),
304 'mda OK with List-Id match');
305 my $path = mid2path($mid);
306 my $msg = $git->cat_file("HEAD:$path");
307 like($$msg, qr/\Q$list_id\E/, 'delivered message w/ List-ID matches');
309 # try a message w/o precheck
310 $simple = Email::Simple->new(<<EOF);
311 To: You <you\@example.com>
314 this message would not be accepted without --no-precheck
316 $in = $simple->as_string;
317 my ($out, $err) = ('', '');
318 my $rdr = { 0 => \$in, 1 => \$out, 2 => \$err };
319 ok(run_script(['-mda', '--no-precheck'], undef, $rdr),
320 'mda OK with List-Id match and --no-precheck');
321 my $cur = $git->qx(qw(diff HEAD~1..HEAD));
322 like($cur, qr/this message would not be accepted without --no-precheck/,
323 '--no-precheck delivered message anyways');
325 # try a message with multiple List-ID headers
329 Message-ID: <2lids\@example>
330 Subject: two List-IDs
331 From: user <user\@example.com>
333 Date: Fri, 02 Oct 1993 00:00:00 +0000
336 ($out, $err) = ('', '');
337 ok(run_script(['-mda'], undef, $rdr),
338 'mda OK with multiple List-Id matches');
339 $cur = $git->qx(qw(diff HEAD~1..HEAD));
340 like($cur, qr/Message-ID: <2lids\@example>/,
341 'multi List-ID match delivered');
342 like($err, qr/multiple List-ID/, 'warned about multiple List-ID');