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 use PublicInbox::Import;
13 my ($tmpdir, $for_destroy) = tmpdir();
14 my $home = "$tmpdir/pi-home";
15 my $pi_home = "$home/.public-inbox";
16 my $pi_config = "$pi_home/config";
17 my $maindir = "$tmpdir/main.git";
18 my $main_bin = getcwd()."/t/main-bin";
19 my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
20 my $fail_bin = getcwd()."/t/fail-bin";
21 my $fail_path = "$fail_bin:$ENV{PATH}"; # for spamc spam mock
22 my $addr = 'test-public@example.com';
23 my $cfgpfx = "publicinbox.test";
24 my $faildir = "$home/faildir/";
26 my $git = PublicInbox::Git->new($maindir);
28 my $fail_bad_header = sub ($$$) {
29 my ($good_rev, $msg, $in) = @_;
30 my @f = glob("$faildir/*/*");
32 my ($out, $err) = ("", "");
33 my $opt = { 0 => \$in, 1 => \$out, 2 => \$err };
34 local $ENV{PATH} = $main_path;
35 ok(run_script(['-mda'], undef, $opt),
36 "no error on undeliverable ($msg)");
37 my $rev = $git->qx(qw(rev-list HEAD));
39 is($rev, $good_rev, "bad revision not committed ($msg)");
40 @f = glob("$faildir/*/*");
41 is(scalar @f, 1, "faildir written to");
46 ok(-x "$main_bin/spamc",
47 "spamc ham mock found (run in top of source tree");
48 ok(-x "$fail_bin/spamc",
49 "spamc mock found (run in top of source tree");
50 is(1, mkdir($home, 0755), "setup ~/ for testing");
51 is(1, mkdir($pi_home, 0755), "setup ~/.public-inbox");
52 PublicInbox::Import::init_bare($maindir);
54 open my $fh, '>>', $pi_config or die;
55 print $fh <<EOF or die;
63 local $ENV{GIT_COMMITTER_NAME} = eval {
65 use PublicInbox::Address;
66 use Encode qw/encode/;
67 my $eml = 't/utf8.eml';
68 my $msg = PublicInbox::InboxWritable::mime_from_path($eml) or
69 die "failed to open $eml: $!";
70 my $from = $msg->header('From');
71 my ($author) = PublicInbox::Address::names($from);
72 my ($email) = PublicInbox::Address::emails($from);
73 my $date = $msg->header('Date');
76 encode('us-ascii', my $tmp = $author, Encode::HTMLCREF),
77 'HTML conversion is correct');
78 is($email, 'e@example.com', 'email parsed correctly');
79 is($date, 'Thu, 01 Jan 1970 00:00:00 +0000',
80 'message date parsed correctly');
87 local $ENV{PI_EMERGENCY} = $faildir;
88 local $ENV{HOME} = $home;
89 local $ENV{ORIGINAL_RECIPIENT} = $addr;
91 From: Me <me\@example.com>
92 To: You <you\@example.com>
94 Message-Id: <blah\@example.com>
96 Date: Thu, 01 Jan 1970 00:00:00 +0000
99 # ensure successful message delivery
101 local $ENV{PATH} = $main_path;
102 ok(run_script(['-mda'], undef, { 0 => \$in }));
103 my $rev = $git->qx(qw(rev-list HEAD));
104 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
106 my $cmt = $git->cat_file($rev);
107 like($$cmt, qr/^author Me <me\@example\.com> 0 \+0000\n/m,
108 "author info set correctly");
109 like($$cmt, qr/^committer test <test-public\@example\.com>/m,
110 "committer info set correctly");
114 # ensure failures work, fail with bad spamc
116 my @prev = <$faildir/new/*>;
117 is(scalar @prev, 0 , "nothing in PI_EMERGENCY before");
118 local $ENV{PATH} = $fail_path;
119 ok(run_script(['-mda'], undef, { 0 => \$in }));
120 my @revs = $git->qx(qw(rev-list HEAD));
121 is(scalar @revs, 1, "bad revision not committed");
122 my @new = <$faildir/new/*>;
123 is(scalar @new, 1, "PI_EMERGENCY is written to");
126 $fail_bad_header->($good_rev, "bad recipient", <<"");
127 From: Me <me\@example.com>
128 To: You <you\@example.com>
129 Message-Id: <bad-recipient\@example.com>
131 Date: Thu, 01 Jan 1970 00:00:00 +0000
133 my $fail = $fail_bad_header->($good_rev, "duplicate Message-ID", <<"");
134 From: Me <me\@example.com>
135 To: You <you\@example.com>
137 Message-ID: <blah\@example.com>
139 Date: Thu, 01 Jan 1970 00:00:00 +0000
141 like($fail->[2], qr/CONFLICT/, "duplicate Message-ID message");
143 $fail_bad_header->($good_rev, "missing From:", <<"");
145 Message-ID: <missing-from\@example.com>
147 Date: Thu, 01 Jan 1970 00:00:00 +0000
149 $fail_bad_header->($good_rev, "short subject:", <<"");
151 From: cat\@example.com
152 Message-ID: <short-subject\@example.com>
154 Date: Thu, 01 Jan 1970 00:00:00 +0000
156 $fail_bad_header->($good_rev, "no date", <<"");
159 Message-ID: <no-date\@example.com>
162 $fail_bad_header->($good_rev, "bad date", <<"");
165 Message-ID: <bad-date\@example.com>
173 local $ENV{PI_EMERGENCY} = $faildir;
174 local $ENV{HOME} = $home;
175 local $ENV{ORIGINAL_RECIPIENT} = $addr;
176 local $ENV{PATH} = $main_path;
177 my $mid = 'spam-train@example.com';
179 From: Spammer <spammer\@example.com>
180 To: You <you\@example.com>
183 Subject: this message will be trained as spam
184 Date: Thu, 01 Jan 1970 00:00:00 +0000
188 # deliver the spam message, first
189 ok(run_script(['-mda'], undef, { 0 => \$in }));
190 my $path = mid2path($mid);
191 my $msg = $git->cat_file("HEAD:$path");
192 like($$msg, qr/\Q$mid\E/, "message delivered");
195 local $ENV{GIT_AUTHOR_EMAIL} = 'trainer@example.com';
196 local $ENV{GIT_COMMITTER_EMAIL} = 'trainer@example.com';
197 local $ENV{GIT_COMMITTER_NAME};
198 delete $ENV{GIT_COMMITTER_NAME};
199 ok(run_script(['-learn', 'spam'], undef, { 0 => $msg }),
200 "no failure from learning spam");
201 ok(run_script(['-learn', 'spam'], undef, { 0 => $msg }),
202 "no failure from learning spam idempotently");
208 local $ENV{PI_EMERGENCY} = $faildir;
209 local $ENV{HOME} = $home;
210 local $ENV{ORIGINAL_RECIPIENT} = $addr;
211 local $ENV{PATH} = $main_path;
212 my $mid = 'ham-train@example.com';
214 From: False-positive <hammer\@example.com>
215 To: You <you\@example.com>
218 Subject: this message will be trained as spam
219 Date: Thu, 01 Jan 1970 00:00:00 +0000
223 # these should be overridden
224 local $ENV{GIT_AUTHOR_EMAIL} = 'trainer@example.com';
225 local $ENV{GIT_COMMITTER_EMAIL} = 'trainer@example.com';
227 ok(run_script(['-learn', 'ham'], undef, { 0 => \$in }),
228 "learned ham without failure");
229 my $path = mid2path($mid);
230 my $msg = $git->cat_file("HEAD:$path");
231 like($$msg, qr/\Q$mid\E/, "ham message delivered");
232 ok(run_script(['-learn', 'ham'], undef, { 0 => \$in }),
233 "learned ham idempotently ");
235 # ensure trained email is filtered, too
236 $mime = mime_load 't/mda-mime.eml', sub {
237 my $html_body = "<html><body>hi</body></html>";
241 content_type => 'text/html; charset=UTF-8',
242 encoding => 'base64',
248 content_type => 'text/plain',
249 encoding => 'quoted-printable',
251 body => 'hi = "bye"',
254 $mid = 'multipart-html-sucks@11';
257 From => 'a@example.com',
260 'Message-ID' => "<$mid>",
261 'Content-Type' => 'multipart/alternative',
267 $in = $mime->as_string;
268 ok(run_script(['-learn', 'ham'], undef, { 0 => \$in }),
269 "learned ham without failure");
270 my $path = mid2path($mid);
271 $msg = $git->cat_file("HEAD:$path");
272 like($$msg, qr/<\Q$mid\E>/, "ham message delivered");
273 unlike($$msg, qr/<html>/i, '<html> filtered');
277 # List-ID based delivery
279 local $ENV{PI_EMERGENCY} = $faildir;
280 local $ENV{HOME} = $home;
281 local $ENV{ORIGINAL_RECIPIENT} = undef;
282 delete $ENV{ORIGINAL_RECIPIENT};
283 local $ENV{PATH} = $main_path;
284 my $list_id = 'foo.example.com';
285 my $mid = 'list-id-delivery@example.com';
287 From: user <user\@example.com>
288 To: You <you\@example.com>
292 Subject: this message will be trained as spam
293 Date: Thu, 01 Jan 1970 00:00:00 +0000
296 xsys(qw(git config --file), $pi_config, "$cfgpfx.listid", $list_id);
297 $? == 0 or die "failed to set listid $?";
298 ok(run_script(['-mda'], undef, { 0 => \$in }),
299 'mda OK with List-Id match');
300 my $path = mid2path($mid);
301 my $msg = $git->cat_file("HEAD:$path");
302 like($$msg, qr/\Q$list_id\E/, 'delivered message w/ List-ID matches');
304 # try a message w/o precheck
306 To: You <you\@example.com>
309 this message would not be accepted without --no-precheck
311 my ($out, $err) = ('', '');
312 my $rdr = { 0 => \$in, 1 => \$out, 2 => \$err };
313 ok(run_script(['-mda', '--no-precheck'], undef, $rdr),
314 'mda OK with List-Id match and --no-precheck');
315 my $cur = $git->qx(qw(diff HEAD~1..HEAD));
316 like($cur, qr/this message would not be accepted without --no-precheck/,
317 '--no-precheck delivered message anyways');
319 # try a message with multiple List-ID headers
323 Message-ID: <2lids\@example>
324 Subject: two List-IDs
325 From: user <user\@example.com>
327 Date: Fri, 02 Oct 1993 00:00:00 +0000
330 ($out, $err) = ('', '');
331 ok(run_script(['-mda'], undef, $rdr),
332 'mda OK with multiple List-Id matches');
333 $cur = $git->qx(qw(diff HEAD~1..HEAD));
334 like($cur, qr/Message-ID: <2lids\@example>/,
335 'multi List-ID match delivered');
336 like($err, qr/multiple List-ID/, 'warned about multiple List-ID');