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;
90 my $simple = Email::Simple->new(<<EOF);
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 my $in = $simple->as_string;
101 # ensure successful message delivery
103 local $ENV{PATH} = $main_path;
104 ok(run_script(['-mda'], undef, { 0 => \$in }));
105 my $rev = $git->qx(qw(rev-list HEAD));
106 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
108 my $cmt = $git->cat_file($rev);
109 like($$cmt, qr/^author Me <me\@example\.com> 0 \+0000\n/m,
110 "author info set correctly");
111 like($$cmt, qr/^committer test <test-public\@example\.com>/m,
112 "committer info set correctly");
116 # ensure failures work, fail with bad spamc
118 my @prev = <$faildir/new/*>;
119 is(scalar @prev, 0 , "nothing in PI_EMERGENCY before");
120 local $ENV{PATH} = $fail_path;
121 ok(run_script(['-mda'], undef, { 0 => \$in }));
122 my @revs = $git->qx(qw(rev-list HEAD));
123 is(scalar @revs, 1, "bad revision not committed");
124 my @new = <$faildir/new/*>;
125 is(scalar @new, 1, "PI_EMERGENCY is written to");
128 $fail_bad_header->($good_rev, "bad recipient", <<"");
129 From: Me <me\@example.com>
130 To: You <you\@example.com>
131 Message-Id: <bad-recipient\@example.com>
133 Date: Thu, 01 Jan 1970 00:00:00 +0000
135 my $fail = $fail_bad_header->($good_rev, "duplicate Message-ID", <<"");
136 From: Me <me\@example.com>
137 To: You <you\@example.com>
139 Message-ID: <blah\@example.com>
141 Date: Thu, 01 Jan 1970 00:00:00 +0000
143 like($fail->[2], qr/CONFLICT/, "duplicate Message-ID message");
145 $fail_bad_header->($good_rev, "missing From:", <<"");
147 Message-ID: <missing-from\@example.com>
149 Date: Thu, 01 Jan 1970 00:00:00 +0000
151 $fail_bad_header->($good_rev, "short subject:", <<"");
153 From: cat\@example.com
154 Message-ID: <short-subject\@example.com>
156 Date: Thu, 01 Jan 1970 00:00:00 +0000
158 $fail_bad_header->($good_rev, "no date", <<"");
161 Message-ID: <no-date\@example.com>
164 $fail_bad_header->($good_rev, "bad date", <<"");
167 Message-ID: <bad-date\@example.com>
175 local $ENV{PI_EMERGENCY} = $faildir;
176 local $ENV{HOME} = $home;
177 local $ENV{ORIGINAL_RECIPIENT} = $addr;
178 local $ENV{PATH} = $main_path;
179 my $mid = 'spam-train@example.com';
180 my $simple = Email::Simple->new(<<EOF);
181 From: Spammer <spammer\@example.com>
182 To: You <you\@example.com>
185 Subject: this message will be trained as spam
186 Date: Thu, 01 Jan 1970 00:00:00 +0000
189 my $in = $simple->as_string;
192 # deliver the spam message, first
193 ok(run_script(['-mda'], undef, { 0 => \$in }));
194 my $path = mid2path($mid);
195 my $msg = $git->cat_file("HEAD:$path");
196 like($$msg, qr/\Q$mid\E/, "message delivered");
199 local $ENV{GIT_AUTHOR_EMAIL} = 'trainer@example.com';
200 local $ENV{GIT_COMMITTER_EMAIL} = 'trainer@example.com';
201 local $ENV{GIT_COMMITTER_NAME};
202 delete $ENV{GIT_COMMITTER_NAME};
203 ok(run_script(['-learn', 'spam'], undef, { 0 => $msg }),
204 "no failure from learning spam");
205 ok(run_script(['-learn', 'spam'], undef, { 0 => $msg }),
206 "no failure from learning spam idempotently");
212 local $ENV{PI_EMERGENCY} = $faildir;
213 local $ENV{HOME} = $home;
214 local $ENV{ORIGINAL_RECIPIENT} = $addr;
215 local $ENV{PATH} = $main_path;
216 my $mid = 'ham-train@example.com';
217 my $simple = Email::Simple->new(<<EOF);
218 From: False-positive <hammer\@example.com>
219 To: You <you\@example.com>
222 Subject: this message will be trained as spam
223 Date: Thu, 01 Jan 1970 00:00:00 +0000
226 my $in = $simple->as_string;
229 # these should be overridden
230 local $ENV{GIT_AUTHOR_EMAIL} = 'trainer@example.com';
231 local $ENV{GIT_COMMITTER_EMAIL} = 'trainer@example.com';
233 ok(run_script(['-learn', 'ham'], undef, { 0 => \$in }),
234 "learned ham without failure");
235 my $path = mid2path($mid);
236 my $msg = $git->cat_file("HEAD:$path");
237 like($$msg, qr/\Q$mid\E/, "ham message delivered");
238 ok(run_script(['-learn', 'ham'], undef, { 0 => \$in }),
239 "learned ham idempotently ");
241 # ensure trained email is filtered, too
242 my $html_body = "<html><body>hi</body></html>";
246 content_type => 'text/html; charset=UTF-8',
247 encoding => 'base64',
253 content_type => 'text/plain',
254 encoding => 'quoted-printable',
256 body => 'hi = "bye"',
259 $mid = 'multipart-html-sucks@11';
260 $mime = Email::MIME->create(
262 From => 'a@example.com',
265 'Message-ID' => "<$mid>",
266 'Content-Type' => 'multipart/alternative',
272 $in = $mime->as_string;
273 ok(run_script(['-learn', 'ham'], undef, { 0 => \$in }),
274 "learned ham without failure");
275 my $path = mid2path($mid);
276 $msg = $git->cat_file("HEAD:$path");
277 like($$msg, qr/<\Q$mid\E>/, "ham message delivered");
278 unlike($$msg, qr/<html>/i, '<html> filtered');
282 # List-ID based delivery
284 local $ENV{PI_EMERGENCY} = $faildir;
285 local $ENV{HOME} = $home;
286 local $ENV{ORIGINAL_RECIPIENT} = undef;
287 delete $ENV{ORIGINAL_RECIPIENT};
288 local $ENV{PATH} = $main_path;
289 my $list_id = 'foo.example.com';
290 my $mid = 'list-id-delivery@example.com';
291 my $simple = Email::Simple->new(<<EOF);
292 From: user <user\@example.com>
293 To: You <you\@example.com>
297 Subject: this message will be trained as spam
298 Date: Thu, 01 Jan 1970 00:00:00 +0000
301 xsys(qw(git config --file), $pi_config, "$cfgpfx.listid", $list_id);
302 $? == 0 or die "failed to set listid $?";
303 my $in = $simple->as_string;
304 ok(run_script(['-mda'], undef, { 0 => \$in }),
305 'mda OK with List-Id match');
306 my $path = mid2path($mid);
307 my $msg = $git->cat_file("HEAD:$path");
308 like($$msg, qr/\Q$list_id\E/, 'delivered message w/ List-ID matches');
310 # try a message w/o precheck
311 $simple = Email::Simple->new(<<EOF);
312 To: You <you\@example.com>
315 this message would not be accepted without --no-precheck
317 $in = $simple->as_string;
318 my ($out, $err) = ('', '');
319 my $rdr = { 0 => \$in, 1 => \$out, 2 => \$err };
320 ok(run_script(['-mda', '--no-precheck'], undef, $rdr),
321 'mda OK with List-Id match and --no-precheck');
322 my $cur = $git->qx(qw(diff HEAD~1..HEAD));
323 like($cur, qr/this message would not be accepted without --no-precheck/,
324 '--no-precheck delivered message anyways');
326 # try a message with multiple List-ID headers
330 Message-ID: <2lids\@example>
331 Subject: two List-IDs
332 From: user <user\@example.com>
334 Date: Fri, 02 Oct 1993 00:00:00 +0000
337 ($out, $err) = ('', '');
338 ok(run_script(['-mda'], undef, $rdr),
339 'mda OK with multiple List-Id matches');
340 $cur = $git->qx(qw(diff HEAD~1..HEAD));
341 like($cur, qr/Message-ID: <2lids\@example>/,
342 'multi List-ID match delivered');
343 like($err, qr/multiple List-ID/, 'warned about multiple List-ID');