]> Sergey Matveev's repositories - public-inbox.git/commitdiff
precheck uses recipient argument
authorEric Wong <e@80x24.org>
Mon, 31 Mar 2014 20:16:19 +0000 (20:16 +0000)
committerEric Wong <normalperson@yhbt.net>
Sat, 5 Apr 2014 06:55:35 +0000 (06:55 +0000)
We will also be using the RECIPIENT env in the future, since
that takes aliases into account.

Reducing the possible callsites to check ENV means we can more
easily update the code in the future.

lib/PublicInbox.pm
t/precheck.t

index 56137d1404986175f03c5a018607a6184eebdf68..a4aba34692852263c9d5abe8e927673f958a1444 100644 (file)
@@ -13,22 +13,21 @@ sub __drop_plus {
        $str_addr;
 }
 
-# do not allow Bcc, only Cc and To if ORIGINAL_RECIPIENT (postfix) env is set
+# do not allow Bcc, only Cc and To if recipient is set
 sub precheck {
-       my ($klass, $filter) = @_;
+       my ($klass, $filter, $recipient) = @_;
        my $simple = $filter->simple;
        return 0 unless $simple->header("Message-ID");
        return 0 unless defined($filter->from);
        return 0 if length($simple->as_string) > MAX_SIZE;
-       recipient_specified($filter);
+       recipient_specified($filter, $recipient);
 }
 
 sub recipient_specified {
-       my ($filter) = @_;
-       my $or = $ENV{ORIGINAL_RECIPIENT};
-       defined($or) or return 1; # for imports
-       my @or = Email::Address->parse($or);
-       my $oaddr = __drop_plus($or[0]->address);
+       my ($filter, $recipient) = @_;
+       defined($recipient) or return 1; # for mass imports
+       my @recip = Email::Address->parse($recipient);
+       my $oaddr = __drop_plus($recip[0]->address);
        $oaddr = qr/\b\Q$oaddr\E\b/i;
        my @to = Email::Address->parse($filter->to);
        my @cc = Email::Address->parse($filter->cc);
index f9d61d8ef13e19211e281a7581e53df54e0a7550..1bfa4c9f644f5d13028922485838d5b700896004 100644 (file)
@@ -11,23 +11,21 @@ sub do_checks {
        my ($s) = @_;
 
        my $f = Email::Filter->new(data => $s->as_string);
-       local %ENV;
-       delete $ENV{ORIGINAL_RECIPIENT};
 
-       ok(PublicInbox->precheck($f),
-               "ORIGINAL_RECIPIENT unset is OK");
+       ok(PublicInbox->precheck($f, undef),
+               "RECIPIENT unset is OK");
 
-       $ENV{ORIGINAL_RECIPIENT} = 'foo@example.com';
-       ok(!PublicInbox->precheck($f),
-               "wrong ORIGINAL_RECIPIENT rejected");
+       my $recipient = 'foo@example.com';
+       ok(!PublicInbox->precheck($f, $recipient),
+               "wrong RECIPIENT rejected");
 
-       $ENV{ORIGINAL_RECIPIENT} = 'b@example.com';
-       ok(PublicInbox->precheck($f),
-               "ORIGINAL_RECIPIENT in To: is OK");
+       $recipient = 'b@example.com';
+       ok(PublicInbox->precheck($f, $recipient),
+               "RECIPIENT in To: is OK");
 
-       $ENV{ORIGINAL_RECIPIENT} = 'c@example.com';
-       ok(PublicInbox->precheck($f),
-               "ORIGINAL_RECIPIENT in Cc: is OK");
+       $recipient = 'c@example.com';
+       ok(PublicInbox->precheck($f, $recipient),
+               "RECIPIENT in Cc: is OK");
 }
 
 {
@@ -59,7 +57,7 @@ sub do_checks {
 }
 
 {
-       $ENV{ORIGINAL_RECIPIENT} = 'b@example.com';
+       my $recipient = 'b@example.com';
        my $s = Email::Simple->create(
                header => [
                        To => 'b@example.com',
@@ -71,7 +69,7 @@ sub do_checks {
                body => "hello world\n",
        );
        my $f = Email::Filter->new(data => $s->as_string);
-       ok(!PublicInbox->precheck($f), "missing From: is rejected");
+       ok(!PublicInbox->precheck($f, $recipient), "missing From: is rejected");
 }
 
 done_testing();