use strict;
use warnings;
use Email::Address;
+use constant MAX_SIZE => 1024 * 500; # same as spamc default
# drop plus addressing for matching
sub __drop_plus {
}
# do not allow Bcc, only Cc and To if ORIGINAL_RECIPIENT (postfix) env is set
-sub recipient_specified {
+sub precheck {
my ($klass, $filter) = @_;
+ return 0 unless defined($filter->from);
+ return 0 if length($filter->simple->as_string) > MAX_SIZE;
+ recipient_specified($filter);
+}
+
+sub recipient_specified {
+ my ($filter) = @_;
my $or = $ENV{ORIGINAL_RECIPIENT};
defined($or) or return 1; # for imports
my @or = Email::Address->parse($or);
my $filter = Email::Filter->new(emergency => "~/emergency.mbox");
my $main_repo = shift @ARGV or die "Usage: $usage\n";
my $fail_repo = shift @ARGV or die "Usage: $usage\n";
-my $max = 1024 * 500; # same as spamc
my $filtered;
-if ($filter->simple->header("From")
- && length($filter->simple->as_string) <= $max
- && PublicInbox->recipient_specified($filter)
- && do_spamc($filter->simple, \$filtered)) {
+if (PublicInbox->precheck($filter) && do_spamc($filter->simple, \$filtered)) {
# update our message with SA headers (in case our filter rejects it)
my $simple = Email::Simple->new($filtered);
$filtered = undef;
local %ENV;
delete $ENV{ORIGINAL_RECIPIENT};
- ok(PublicInbox->recipient_specified($f),
+ ok(PublicInbox->precheck($f),
"ORIGINAL_RECIPIENT unset is OK");
$ENV{ORIGINAL_RECIPIENT} = 'foo@example.com';
- ok(!PublicInbox->recipient_specified($f),
+ ok(!PublicInbox->precheck($f),
"wrong ORIGINAL_RECIPIENT rejected");
$ENV{ORIGINAL_RECIPIENT} = 'b@example.com';
- ok(PublicInbox->recipient_specified($f),
+ ok(PublicInbox->precheck($f),
"ORIGINAL_RECIPIENT in To: is OK");
$ENV{ORIGINAL_RECIPIENT} = 'c@example.com';
- ok(PublicInbox->recipient_specified($f),
+ ok(PublicInbox->precheck($f),
"ORIGINAL_RECIPIENT in Cc: is OK");
}
));
}
+{
+ $ENV{ORIGINAL_RECIPIENT} = 'b@example.com';
+ my $s = Email::Simple->create(
+ header => [
+ To => 'b@example.com',
+ Cc => 'c@example.com',
+ 'Content-Type' => 'text/plain',
+ Subject => 'this is a subject',
+ ],
+ body => "hello world\n",
+ );
+ my $f = Email::Filter->new(data => $s->as_string);
+ ok(!PublicInbox->precheck($f), "missing From: is rejected");
+}
+
done_testing();