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::TestCommon;
11 my ($tmpdir, $for_destroy) = tmpdir();
12 my $home = "$tmpdir/pi-home";
13 my $pi_home = "$home/.public-inbox";
14 my $pi_config = "$pi_home/config";
15 my $maindir = "$tmpdir/main.git";
16 my $main_bin = getcwd()."/t/main-bin";
17 my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
18 my $fail_bin = getcwd()."/t/fail-bin";
19 my $fail_path = "$fail_bin:$ENV{PATH}"; # for spamc spam mock
20 my $addr = 'test-public@example.com';
21 my $cfgpfx = "publicinbox.test";
22 my $faildir = "$home/faildir/";
24 my $git = PublicInbox::Git->new($maindir);
26 my $fail_bad_header = sub ($$$) {
27 my ($good_rev, $msg, $in) = @_;
28 my @f = glob("$faildir/*/*");
30 my ($out, $err) = ("", "");
31 my $opt = { 0 => \$in, 1 => \$out, 2 => \$err };
32 local $ENV{PATH} = $main_path;
33 ok(run_script(['-mda'], undef, $opt),
34 "no error on undeliverable ($msg)");
35 my $rev = $git->qx(qw(rev-list HEAD));
37 is($rev, $good_rev, "bad revision not commited ($msg)");
38 @f = glob("$faildir/*/*");
39 is(scalar @f, 1, "faildir written to");
44 ok(-x "$main_bin/spamc",
45 "spamc ham mock found (run in top of source tree");
46 ok(-x "$fail_bin/spamc",
47 "spamc mock found (run in top of source tree");
48 is(1, mkdir($home, 0755), "setup ~/ for testing");
49 is(1, mkdir($pi_home, 0755), "setup ~/.public-inbox");
50 is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
52 open my $fh, '>>', $pi_config or die;
53 print $fh <<EOF or die;
61 local $ENV{GIT_COMMITTER_NAME} = eval {
63 use PublicInbox::Address;
64 use Encode qw/encode/;
65 my $mbox = 't/utf8.mbox';
66 open(my $fh, '<', $mbox) or die "failed to open mbox: $mbox\n";
67 my $str = eval { local $/; <$fh> };
69 my $msg = Email::MIME->new($str);
71 my $from = $msg->header('From');
72 my ($author) = PublicInbox::Address::names($from);
73 my ($email) = PublicInbox::Address::emails($from);
74 my $date = $msg->header('Date');
77 encode('us-ascii', my $tmp = $author, Encode::HTMLCREF),
78 'HTML conversion is correct');
79 is($email, 'e@example.com', 'email parsed correctly');
80 is($date, 'Thu, 01 Jan 1970 00:00:00 +0000',
81 'message date parsed correctly');
88 local $ENV{PI_EMERGENCY} = $faildir;
89 local $ENV{HOME} = $home;
90 local $ENV{ORIGINAL_RECIPIENT} = $addr;
91 my $simple = Email::Simple->new(<<EOF);
92 From: Me <me\@example.com>
93 To: You <you\@example.com>
95 Message-Id: <blah\@example.com>
97 Date: Thu, 01 Jan 1970 00:00:00 +0000
100 my $in = $simple->as_string;
102 # ensure successful message delivery
104 local $ENV{PATH} = $main_path;
105 ok(run_script(['-mda'], undef, { 0 => \$in }));
106 my $rev = $git->qx(qw(rev-list HEAD));
107 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
109 my $cmt = $git->cat_file($rev);
110 like($$cmt, qr/^author Me <me\@example\.com> 0 \+0000\n/m,
111 "author info set correctly");
112 like($$cmt, qr/^committer test <test-public\@example\.com>/m,
113 "committer info set correctly");
117 # ensure failures work, fail with bad spamc
119 my @prev = <$faildir/new/*>;
120 is(scalar @prev, 0 , "nothing in PI_EMERGENCY before");
121 local $ENV{PATH} = $fail_path;
122 ok(run_script(['-mda'], undef, { 0 => \$in }));
123 my @revs = $git->qx(qw(rev-list HEAD));
124 is(scalar @revs, 1, "bad revision not committed");
125 my @new = <$faildir/new/*>;
126 is(scalar @new, 1, "PI_EMERGENCY is written to");
129 $fail_bad_header->($good_rev, "bad recipient", <<"");
130 From: Me <me\@example.com>
131 To: You <you\@example.com>
132 Message-Id: <bad-recipient\@example.com>
134 Date: Thu, 01 Jan 1970 00:00:00 +0000
136 my $fail = $fail_bad_header->($good_rev, "duplicate Message-ID", <<"");
137 From: Me <me\@example.com>
138 To: You <you\@example.com>
140 Message-ID: <blah\@example.com>
142 Date: Thu, 01 Jan 1970 00:00:00 +0000
144 like($fail->[2], qr/CONFLICT/, "duplicate Message-ID message");
146 $fail_bad_header->($good_rev, "missing From:", <<"");
148 Message-ID: <missing-from\@example.com>
150 Date: Thu, 01 Jan 1970 00:00:00 +0000
152 $fail_bad_header->($good_rev, "short subject:", <<"");
154 From: cat\@example.com
155 Message-ID: <short-subject\@example.com>
157 Date: Thu, 01 Jan 1970 00:00:00 +0000
159 $fail_bad_header->($good_rev, "no date", <<"");
162 Message-ID: <no-date\@example.com>
165 $fail_bad_header->($good_rev, "bad date", <<"");
168 Message-ID: <bad-date\@example.com>
176 local $ENV{PI_EMERGENCY} = $faildir;
177 local $ENV{HOME} = $home;
178 local $ENV{ORIGINAL_RECIPIENT} = $addr;
179 local $ENV{PATH} = $main_path;
180 my $mid = 'spam-train@example.com';
181 my $simple = Email::Simple->new(<<EOF);
182 From: Spammer <spammer\@example.com>
183 To: You <you\@example.com>
186 Subject: this message will be trained as spam
187 Date: Thu, 01 Jan 1970 00:00:00 +0000
190 my $in = $simple->as_string;
193 # deliver the spam message, first
194 ok(run_script(['-mda'], undef, { 0 => \$in }));
195 my $path = mid2path($mid);
196 my $msg = $git->cat_file("HEAD:$path");
197 like($$msg, qr/\Q$mid\E/, "message delivered");
200 local $ENV{GIT_AUTHOR_EMAIL} = 'trainer@example.com';
201 local $ENV{GIT_COMMITTER_EMAIL} = 'trainer@example.com';
202 local $ENV{GIT_COMMITTER_NAME};
203 delete $ENV{GIT_COMMITTER_NAME};
204 ok(run_script(['-learn', 'spam'], undef, { 0 => $msg }),
205 "no failure from learning spam");
206 ok(run_script(['-learn', 'spam'], undef, { 0 => $msg }),
207 "no failure from learning spam idempotently");
213 local $ENV{PI_EMERGENCY} = $faildir;
214 local $ENV{HOME} = $home;
215 local $ENV{ORIGINAL_RECIPIENT} = $addr;
216 local $ENV{PATH} = $main_path;
217 my $mid = 'ham-train@example.com';
218 my $simple = Email::Simple->new(<<EOF);
219 From: False-positive <hammer\@example.com>
220 To: You <you\@example.com>
223 Subject: this message will be trained as spam
224 Date: Thu, 01 Jan 1970 00:00:00 +0000
227 my $in = $simple->as_string;
230 # these should be overridden
231 local $ENV{GIT_AUTHOR_EMAIL} = 'trainer@example.com';
232 local $ENV{GIT_COMMITTER_EMAIL} = 'trainer@example.com';
234 ok(run_script(['-learn', 'ham'], undef, { 0 => \$in }),
235 "learned ham without failure");
236 my $path = mid2path($mid);
237 my $msg = $git->cat_file("HEAD:$path");
238 like($$msg, qr/\Q$mid\E/, "ham message delivered");
239 ok(run_script(['-learn', 'ham'], undef, { 0 => \$in }),
240 "learned ham idempotently ");
242 # ensure trained email is filtered, too
243 my $html_body = "<html><body>hi</body></html>";
247 content_type => 'text/html; charset=UTF-8',
248 encoding => 'base64',
254 content_type => 'text/plain',
255 encoding => 'quoted-printable',
257 body => 'hi = "bye"',
260 $mid = 'multipart-html-sucks@11';
261 $mime = Email::MIME->create(
263 From => 'a@example.com',
266 'Message-ID' => "<$mid>",
267 'Content-Type' => 'multipart/alternative',
273 $in = $mime->as_string;
274 ok(run_script(['-learn', 'ham'], undef, { 0 => \$in }),
275 "learned ham without failure");
276 my $path = mid2path($mid);
277 $msg = $git->cat_file("HEAD:$path");
278 like($$msg, qr/<\Q$mid\E>/, "ham message delivered");
279 unlike($$msg, qr/<html>/i, '<html> filtered');
283 # List-ID based delivery
285 local $ENV{PI_EMERGENCY} = $faildir;
286 local $ENV{HOME} = $home;
287 local $ENV{ORIGINAL_RECIPIENT} = undef;
288 delete $ENV{ORIGINAL_RECIPIENT};
289 local $ENV{PATH} = $main_path;
290 my $list_id = 'foo.example.com';
291 my $mid = 'list-id-delivery@example.com';
292 my $simple = Email::Simple->new(<<EOF);
293 From: user <user\@example.com>
294 To: You <you\@example.com>
298 Subject: this message will be trained as spam
299 Date: Thu, 01 Jan 1970 00:00:00 +0000
302 system(qw(git config --file), $pi_config, "$cfgpfx.listid", $list_id);
303 $? == 0 or die "failed to set listid $?";
304 my $in = $simple->as_string;
305 ok(run_script(['-mda'], undef, { 0 => \$in }),
306 'mda OK with List-Id match');
307 my $path = mid2path($mid);
308 my $msg = $git->cat_file("HEAD:$path");
309 like($$msg, qr/\Q$list_id\E/, 'delivered message w/ List-ID matches');
311 # try a message w/o precheck
312 $simple = Email::Simple->new(<<EOF);
313 To: You <you\@example.com>
316 this message would not be accepted without --no-precheck
318 $in = $simple->as_string;
319 my ($out, $err) = ('', '');
320 my $rdr = { 0 => \$in, 1 => \$out, 2 => \$err };
321 ok(run_script(['-mda', '--no-precheck'], undef, $rdr),
322 'mda OK with List-Id match and --no-precheck');
323 my $cur = $git->qx(qw(diff HEAD~1..HEAD));
324 like($cur, qr/this message would not be accepted without --no-precheck/,
325 '--no-precheck delivered message anyways');
327 # try a message with multiple List-ID headers
331 Message-ID: <2lids\@example>
332 Subject: two List-IDs
333 From: user <user\@example.com>
335 Date: Fri, 02 Oct 1993 00:00:00 +0000
338 ($out, $err) = ('', '');
339 ok(run_script(['-mda'], undef, $rdr),
340 'mda OK with multiple List-Id matches');
341 $cur = $git->qx(qw(diff HEAD~1..HEAD));
342 like($cur, qr/Message-ID: <2lids\@example>/,
343 'multi List-ID match delivered');
344 like($err, qr/multiple List-ID/, 'warned about multiple List-ID');