]> Sergey Matveev's repositories - public-inbox.git/commitdiff
mda: support aliased addresses
authorEric Wong <e@80x24.org>
Mon, 28 Apr 2014 10:25:58 +0000 (10:25 +0000)
committerEric Wong <e@80x24.org>
Mon, 28 Apr 2014 10:25:58 +0000 (10:25 +0000)
This mimics functionality found in -learn.  Originally the design
allowed for only one address per-list, but when migrating/hijacking
existing mailing lists, having multiple addresses map to the same
inbox is useful.

lib/PublicInbox/MDA.pm
public-inbox-mda
t/precheck.t

index ed8d74dac3556ad735d499f35539b6f246b9bc2f..ee4d0afe9715c27349d737ef034475ee80b93fdc 100644 (file)
@@ -18,7 +18,7 @@ sub __drop_plus {
 
 # do not allow Bcc, only Cc and To if recipient is set
 sub precheck {
-       my ($klass, $filter, $recipient) = @_;
+       my ($klass, $filter, $address) = @_;
        my $simple = $filter->simple;
        my $mid = $simple->header("Message-ID");
        return 0 unless usable_str(length('<m@h>'), $mid) && $mid =~ /\@/;
@@ -26,7 +26,7 @@ sub precheck {
        return 0 unless usable_str(length(':o'), $simple->header("Subject"));
        return 0 unless usable_date($simple->header("Date"));
        return 0 if length($simple->as_string) > MAX_SIZE;
-       recipient_specified($filter, $recipient);
+       alias_specified($filter, $address);
 }
 
 sub usable_str {
@@ -39,17 +39,20 @@ sub usable_date {
        scalar @t;
 }
 
-sub recipient_specified {
-       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);
-       foreach my $addr (@to, @cc) {
-               if (__drop_plus($addr->address) =~ $oaddr) {
-                       return 1;
+sub alias_specified {
+       my ($filter, $address) = @_;
+
+       my @address = ref($address) eq 'ARRAY' ? @$address : ($address);
+       my %ok = map {
+               my @recip = Email::Address->parse($_);
+               lc(__drop_plus($recip[0]->address)) => 1;
+       } @address;
+
+       foreach my $line ($filter->cc, $filter->to) {
+               foreach my $addr (Email::Address->parse($line)) {
+                       if ($ok{lc(__drop_plus($addr->address))}) {
+                               return 1;
+                       }
                }
        }
        return 0;
index 50805da2e6ecc99b679cc69dacfea1caa6e37e21..096421bbeaf82aa067acb1d6bc1eaa31b5b6a992 100755 (executable)
@@ -26,12 +26,12 @@ my $config = PublicInbox::Config->new;
 
 my $recipient = $ENV{ORIGINAL_RECIPIENT};
 defined $recipient or die "ORIGINAL_RECIPIENT not defined in ENV\n";
-my $dst = $config->lookup($recipient);
+my $dst = $config->lookup($recipient); # first check
 defined $dst or exit(1);
 my $main_repo = $dst->{mainrepo} or exit(1);
 my $filtered; # string dest
 
-if (PublicInbox::MDA->precheck($filter, $recipient) &&
+if (PublicInbox::MDA->precheck($filter, $dst->{address}) &&
     do_spamc($filter->simple, \$filtered)) {
        # update our message with SA headers (in case our filter rejects it)
        my $msg = Email::Simple->new($filtered);
index 974d6a3879d2d0440a56f193f588004b41beca5a..89564097bb1e645952a9606e7d689a1300e6cdc1 100644 (file)
@@ -12,9 +12,6 @@ sub do_checks {
 
        my $f = Email::Filter->new(data => $s->as_string);
 
-       ok(PublicInbox::MDA->precheck($f, undef),
-               "ORIGINAL_RECIPIENT unset is OK");
-
        my $recipient = 'foo@example.com';
        ok(!PublicInbox::MDA->precheck($f, $recipient),
                "wrong ORIGINAL_RECIPIENT rejected");
@@ -26,6 +23,10 @@ sub do_checks {
        $recipient = 'c@example.com';
        ok(PublicInbox::MDA->precheck($f, $recipient),
                "ORIGINAL_RECIPIENT in Cc: is OK");
+
+       $recipient = [ 'c@example.com', 'd@example.com' ];
+       ok(PublicInbox::MDA->precheck($f, $recipient),
+               "alias list is OK");
 }
 
 {