about summary refs log tree commit homepage
path: root/script/public-inbox-mda
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2019-10-28 10:45:25 +0000
committerEric Wong <e@80x24.org>2019-10-30 08:48:20 +0000
commit4315455300e29e4ef0ea2f2d68bf4e86d261ae1d (patch)
treef4cb8f8e0cfe271598458f38f4faa823794d5f52 /script/public-inbox-mda
parent6c559dae69e244895fd7e6c5a9ae29f58d03058e (diff)
downloadpublic-inbox-4315455300e29e4ef0ea2f2d68bf4e86d261ae1d.tar.gz
Multiple List-ID headers will be supported in the next commit
Diffstat (limited to 'script/public-inbox-mda')
-rwxr-xr-xscript/public-inbox-mda92
1 files changed, 55 insertions, 37 deletions
diff --git a/script/public-inbox-mda b/script/public-inbox-mda
index c122984f..821bd9cc 100755
--- a/script/public-inbox-mda
+++ b/script/public-inbox-mda
@@ -37,27 +37,39 @@ my $config = PublicInbox::Config->new;
 my $key = 'publicinboxmda.spamcheck';
 my $default = 'PublicInbox::Spamcheck::Spamc';
 my $spamc = PublicInbox::Spamcheck::get($config, $key, $default);
-my $dst;
+my $dests = [];
 my $recipient = $ENV{ORIGINAL_RECIPIENT};
 if (defined $recipient) {
-        $dst = $config->lookup($recipient); # first check
+        my $ibx = $config->lookup($recipient); # first check
+        push @$dests, $ibx if $ibx;
 }
-if (!defined $dst) {
-        $dst = PublicInbox::MDA->inbox_for_list_id($config, $simple);
-        if (!defined $dst && !defined $recipient) {
+if (!scalar(@$dests)) {
+        my $ibx = PublicInbox::MDA->inbox_for_list_id($config, $simple);
+        if (!defined($ibx) && !defined($recipient)) {
                 die "ORIGINAL_RECIPIENT not defined in ENV\n";
         }
-        defined $dst or do_exit(67); # EX_NOUSER 5.1.1 user unknown
+        defined($ibx) or do_exit(67); # EX_NOUSER 5.1.1 user unknown
+        push @$dests, $ibx;
 }
 
-$dst = PublicInbox::InboxWritable->new($dst);
-eval { $dst->assert_usable_dir };
-do_exit(67) if $@;
+my $err;
+@$dests = grep {
+        my $ibx = PublicInbox::InboxWritable->new($_);
+        eval { $ibx->assert_usable_dir };
+        if ($@) {
+                warn $@;
+                $err = 1;
+                0;
+        # pre-check, MDA has stricter rules than an importer might;
+        } elsif ($precheck) {
+                !!PublicInbox::MDA->precheck($simple, $ibx->{address});
+        } else {
+                1;
+        }
+} @$dests;
+
+do_exit(67) if $err && scalar(@$dests) == 0;
 
-# pre-check, MDA has stricter rules than an importer might;
-if ($precheck && !PublicInbox::MDA->precheck($simple, $dst->{address})) {
-        do_exit(0);
-}
 $simple = undef;
 my $spam_ok;
 if ($spamc) {
@@ -75,8 +87,6 @@ if ($spamc) {
 }
 do_exit(0) unless $spam_ok;
 
-my $mime = PublicInbox::MIME->new(\$str);
-
 # -mda defaults to the strict base filter which we won't use anywhere else
 sub mda_filter_adjust ($) {
         my ($ibx) = @_;
@@ -88,30 +98,38 @@ sub mda_filter_adjust ($) {
         }
 }
 
-mda_filter_adjust($dst);
+my @rejects;
+for my $ibx (@$dests) {
+        mda_filter_adjust($ibx);
+        my $filter = $ibx->filter;
+        my $mime = PublicInbox::MIME->new($str);
+        my $ret = $filter->delivery($mime);
+        if (ref($ret) && $ret->isa('Email::MIME')) { # filter altered message
+                $mime = $ret;
+        } elsif ($ret == PublicInbox::Filter::Base::IGNORE) {
+                next; # nothing, keep looping
+        } elsif ($ret == PublicInbox::Filter::Base::REJECT) {
+                push @rejects, $filter->err;
+                next;
+        }
 
-my $filter = $dst->filter;
-my $ret = $filter->delivery($mime);
-if (ref($ret) && $ret->isa('Email::MIME')) { # filter altered message
-        $mime = $ret;
-} elsif ($ret == PublicInbox::Filter::Base::IGNORE) {
-        do_exit(0); # chuck it to emergency
-} elsif ($ret == PublicInbox::Filter::Base::REJECT) {
-        $! = 65; # EX_DATAERR 5.6.0 data format error
-        die $filter->err, "\n";
-} # else { accept
-$filter = undef;
+        PublicInbox::MDA->set_list_headers($mime, $ibx);
+        my $im = $ibx->importer(0);
+        if (defined $im->add($mime)) {
+                # ->abort is idempotent, no emergency if a single
+                # destination succeeds
+                $emm->abort;
+        } else { # v1-only
+                my $mid = $mime->header_obj->header_raw('Message-ID');
+                # this message is similar to what ssoma-mda shows:
+                print STDERR "CONFLICT: Message-ID: $mid exists\n";
+        }
+        $im->done;
+}
 
-PublicInbox::MDA->set_list_headers($mime, $dst);
-my $im = $dst->importer(0);
-if (defined $im->add($mime)) {
-        $emm = $emm->abort;
-} else {
-        # this message is similar to what ssoma-mda shows:
-        print STDERR "CONFLICT: Message-ID: ",
-                        $mime->header_obj->header_raw('Message-ID'),
-                        " exists\n";
+if (scalar(@rejects) && scalar(@rejects) == scalar(@$dests)) {
+        $! = 65; # EX_DATAERR 5.6.0 data format error
+        die join("\n", @rejects, '');
 }
 
-$im->done;
 do_exit(0);