From e7875ff77b4cd09831574cc4965e3f7012b81b89 Mon Sep 17 00:00:00 2001
From: Eric Wong <e@80x24.org>
Date: Mon, 28 Apr 2014 10:25:58 +0000
Subject: [PATCH] mda: support aliased addresses

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 | 29 ++++++++++++++++-------------
 public-inbox-mda       |  4 ++--
 t/precheck.t           |  7 ++++---
 3 files changed, 22 insertions(+), 18 deletions(-)

diff --git a/lib/PublicInbox/MDA.pm b/lib/PublicInbox/MDA.pm
index ed8d74da..ee4d0afe 100644
--- a/lib/PublicInbox/MDA.pm
+++ b/lib/PublicInbox/MDA.pm
@@ -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;
diff --git a/public-inbox-mda b/public-inbox-mda
index 50805da2..096421bb 100755
--- a/public-inbox-mda
+++ b/public-inbox-mda
@@ -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);
diff --git a/t/precheck.t b/t/precheck.t
index 974d6a38..89564097 100644
--- a/t/precheck.t
+++ b/t/precheck.t
@@ -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");
 }
 
 {
-- 
2.50.0