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>
7 use PublicInbox::MID qw(mid2path);
9 use PublicInbox::InboxWritable;
10 use PublicInbox::TestCommon;
11 use PublicInbox::Import;
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/";
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 committed ($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 PublicInbox::Import::init_bare($maindir);
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 $eml = 't/utf8.eml';
66 my $msg = PublicInbox::InboxWritable::mime_from_path($eml) or
67 die "failed to open $eml: $!";
68 my $from = $msg->header('From');
69 my ($author) = PublicInbox::Address::names($from);
70 my ($email) = PublicInbox::Address::emails($from);
71 my $date = $msg->header('Date');
74 encode('us-ascii', my $tmp = $author, Encode::HTMLCREF),
75 'HTML conversion is correct');
76 is($email, 'e@example.com', 'email parsed correctly');
77 is($date, 'Thu, 01 Jan 1970 00:00:00 +0000',
78 'message date parsed correctly');
85 local $ENV{PI_EMERGENCY} = $faildir;
86 local $ENV{HOME} = $home;
87 local $ENV{ORIGINAL_RECIPIENT} = $addr;
89 From: Me <me\@example.com>
90 To: You <you\@example.com>
92 Message-Id: <blah\@example.com>
94 Date: Thu, 01 Jan 1970 00:00:00 +0000
97 # ensure successful message delivery
99 local $ENV{PATH} = $main_path;
100 ok(run_script(['-mda'], undef, { 0 => \$in }));
101 my $rev = $git->qx(qw(rev-list HEAD));
102 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
104 my $cmt = $git->cat_file($rev);
105 like($$cmt, qr/^author Me <me\@example\.com> 0 \+0000\n/m,
106 "author info set correctly");
107 like($$cmt, qr/^committer test <test-public\@example\.com>/m,
108 "committer info set correctly");
112 # ensure failures work, fail with bad spamc
114 my @prev = <$faildir/new/*>;
115 is(scalar @prev, 0 , "nothing in PI_EMERGENCY before");
116 local $ENV{PATH} = $fail_path;
117 ok(run_script(['-mda'], undef, { 0 => \$in }));
118 my @revs = $git->qx(qw(rev-list HEAD));
119 is(scalar @revs, 1, "bad revision not committed");
120 my @new = <$faildir/new/*>;
121 is(scalar @new, 1, "PI_EMERGENCY is written to");
124 $fail_bad_header->($good_rev, "bad recipient", <<"");
125 From: Me <me\@example.com>
126 To: You <you\@example.com>
127 Message-Id: <bad-recipient\@example.com>
129 Date: Thu, 01 Jan 1970 00:00:00 +0000
131 my $fail = $fail_bad_header->($good_rev, "duplicate Message-ID", <<"");
132 From: Me <me\@example.com>
133 To: You <you\@example.com>
135 Message-ID: <blah\@example.com>
137 Date: Thu, 01 Jan 1970 00:00:00 +0000
139 like($fail->[2], qr/CONFLICT/, "duplicate Message-ID message");
141 $fail_bad_header->($good_rev, "missing From:", <<"");
143 Message-ID: <missing-from\@example.com>
145 Date: Thu, 01 Jan 1970 00:00:00 +0000
147 $fail_bad_header->($good_rev, "short subject:", <<"");
149 From: cat\@example.com
150 Message-ID: <short-subject\@example.com>
152 Date: Thu, 01 Jan 1970 00:00:00 +0000
154 $fail_bad_header->($good_rev, "no date", <<"");
157 Message-ID: <no-date\@example.com>
160 $fail_bad_header->($good_rev, "bad date", <<"");
163 Message-ID: <bad-date\@example.com>
171 local $ENV{PI_EMERGENCY} = $faildir;
172 local $ENV{HOME} = $home;
173 local $ENV{ORIGINAL_RECIPIENT} = $addr;
174 local $ENV{PATH} = $main_path;
175 my $mid = 'spam-train@example.com';
177 From: Spammer <spammer\@example.com>
178 To: You <you\@example.com>
181 Subject: this message will be trained as spam
182 Date: Thu, 01 Jan 1970 00:00:00 +0000
186 # deliver the spam message, first
187 ok(run_script(['-mda'], undef, { 0 => \$in }));
188 my $path = mid2path($mid);
189 my $msg = $git->cat_file("HEAD:$path");
190 like($$msg, qr/\Q$mid\E/, "message delivered");
193 local $ENV{GIT_AUTHOR_EMAIL} = 'trainer@example.com';
194 local $ENV{GIT_COMMITTER_EMAIL} = 'trainer@example.com';
195 local $ENV{GIT_COMMITTER_NAME};
196 delete $ENV{GIT_COMMITTER_NAME};
197 ok(run_script(['-learn', 'spam'], undef, { 0 => $msg }),
198 "no failure from learning spam");
199 ok(run_script(['-learn', 'spam'], undef, { 0 => $msg }),
200 "no failure from learning spam idempotently");
206 local $ENV{PI_EMERGENCY} = $faildir;
207 local $ENV{HOME} = $home;
208 local $ENV{ORIGINAL_RECIPIENT} = $addr;
209 local $ENV{PATH} = $main_path;
210 my $mid = 'ham-train@example.com';
212 From: False-positive <hammer\@example.com>
213 To: You <you\@example.com>
216 Subject: this message will be trained as spam
217 Date: Thu, 01 Jan 1970 00:00:00 +0000
221 # these should be overridden
222 local $ENV{GIT_AUTHOR_EMAIL} = 'trainer@example.com';
223 local $ENV{GIT_COMMITTER_EMAIL} = 'trainer@example.com';
225 ok(run_script(['-learn', 'ham'], undef, { 0 => \$in }),
226 "learned ham without failure");
227 my $path = mid2path($mid);
228 my $msg = $git->cat_file("HEAD:$path");
229 like($$msg, qr/\Q$mid\E/, "ham message delivered");
230 ok(run_script(['-learn', 'ham'], undef, { 0 => \$in }),
231 "learned ham idempotently ");
233 # ensure trained email is filtered, too
234 my $mime = mime_load 't/mda-mime.eml';
235 ($mid) = ($mime->header_raw('message-id') =~ /<([^>]+)>/);
237 $in = $mime->as_string;
238 ok(run_script(['-learn', 'ham'], undef, { 0 => \$in }),
239 "learned ham without failure");
240 my $path = mid2path($mid);
241 $msg = $git->cat_file("HEAD:$path");
242 like($$msg, qr/<\Q$mid\E>/, "ham message delivered");
243 unlike($$msg, qr/<html>/i, '<html> filtered');
247 # List-ID based delivery
249 local $ENV{PI_EMERGENCY} = $faildir;
250 local $ENV{HOME} = $home;
251 local $ENV{ORIGINAL_RECIPIENT} = undef;
252 delete $ENV{ORIGINAL_RECIPIENT};
253 local $ENV{PATH} = $main_path;
254 my $list_id = 'foo.example.com';
255 my $mid = 'list-id-delivery@example.com';
257 From: user <user\@example.com>
258 To: You <you\@example.com>
262 Subject: this message will be trained as spam
263 Date: Thu, 01 Jan 1970 00:00:00 +0000
266 xsys(qw(git config --file), $pi_config, "$cfgpfx.listid", $list_id);
267 $? == 0 or die "failed to set listid $?";
268 ok(run_script(['-mda'], undef, { 0 => \$in }),
269 'mda OK with List-Id match');
270 my $path = mid2path($mid);
271 my $msg = $git->cat_file("HEAD:$path");
272 like($$msg, qr/\Q$list_id\E/, 'delivered message w/ List-ID matches');
274 # try a message w/o precheck
276 To: You <you\@example.com>
279 this message would not be accepted without --no-precheck
281 my ($out, $err) = ('', '');
282 my $rdr = { 0 => \$in, 1 => \$out, 2 => \$err };
283 ok(run_script(['-mda', '--no-precheck'], undef, $rdr),
284 'mda OK with List-Id match and --no-precheck');
285 my $cur = $git->qx(qw(diff HEAD~1..HEAD));
286 like($cur, qr/this message would not be accepted without --no-precheck/,
287 '--no-precheck delivered message anyways');
289 # try a message with multiple List-ID headers
293 Message-ID: <2lids\@example>
294 Subject: two List-IDs
295 From: user <user\@example.com>
297 Date: Fri, 02 Oct 1993 00:00:00 +0000
300 ($out, $err) = ('', '');
301 ok(run_script(['-mda'], undef, $rdr),
302 'mda OK with multiple List-Id matches');
303 $cur = $git->qx(qw(diff HEAD~1..HEAD));
304 like($cur, qr/Message-ID: <2lids\@example>/,
305 'multi List-ID match delivered');
306 like($err, qr/multiple List-ID/, 'warned about multiple List-ID');