]> Sergey Matveev's repositories - public-inbox.git/blobdiff - script/public-inbox-mda
replace most uses of PublicInbox::MIME with Eml
[public-inbox.git] / script / public-inbox-mda
index dca8a0ea0e8aa64f859676773e736d4cae77a3a1..42d0e00cbb72687bbdaf5e67e1fb0c6c379ba9b1 100755 (executable)
@@ -1,5 +1,5 @@
 #!/usr/bin/perl -w
-# Copyright (C) 2013-2019 all contributors <meta@public-inbox.org>
+# Copyright (C) 2013-2020 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 #
 # Mail delivery agent for public-inbox, run from your MTA upon mail delivery
@@ -9,14 +9,13 @@ my $usage = 'public-inbox-mda [OPTIONS] < rfc2822_message';
 my $precheck = grep(/\A--no-precheck\z/, @ARGV) ? 0 : 1;
 my ($ems, $emm);
 
-sub do_exit {
+my $do_exit = sub {
        my ($code) = shift;
        $emm = $ems = undef; # trigger DESTROY
        exit $code;
-}
+};
 
-use Email::Simple;
-use PublicInbox::MIME;
+use PublicInbox::Eml;
 use PublicInbox::MDA;
 use PublicInbox::Config;
 use PublicInbox::Emergency;
@@ -29,10 +28,10 @@ use PublicInbox::Spamcheck;
 # in case there's bugs in our code or user error.
 my $emergency = $ENV{PI_EMERGENCY} || "$ENV{HOME}/.public-inbox/emergency/";
 $ems = PublicInbox::Emergency->new($emergency);
-my $str = eval { local $/; <STDIN> };
+my $str = do { local $/; <STDIN> };
 $str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
 $ems->prepare(\$str);
-my $simple = Email::Simple->new(\$str);
+my $eml = PublicInbox::Eml->new(\$str);
 my $config = PublicInbox::Config->new;
 my $key = 'publicinboxmda.spamcheck';
 my $default = 'PublicInbox::Spamcheck::Spamc';
@@ -44,11 +43,11 @@ if (defined $recipient) {
        push @$dests, $ibx if $ibx;
 }
 if (!scalar(@$dests)) {
-       $dests = PublicInbox::MDA->inboxes_for_list_id($config, $simple);
+       $dests = PublicInbox::MDA->inboxes_for_list_id($config, $eml);
        if (!scalar(@$dests) && !defined($recipient)) {
                die "ORIGINAL_RECIPIENT not defined in ENV\n";
        }
-       scalar(@$dests) or do_exit(67); # EX_NOUSER 5.1.1 user unknown
+       scalar(@$dests) or $do_exit->(67); # EX_NOUSER 5.1.1 user unknown
 }
 
 my $err;
@@ -61,15 +60,15 @@ my $err;
                0;
        # pre-check, MDA has stricter rules than an importer might;
        } elsif ($precheck) {
-               !!PublicInbox::MDA->precheck($simple, $ibx->{address});
+               !!PublicInbox::MDA->precheck($eml, $ibx->{address});
        } else {
                1;
        }
 } @$dests;
 
-do_exit(67) if $err && scalar(@$dests) == 0;
+$do_exit->(67) if $err && scalar(@$dests) == 0;
 
-$simple = undef;
+$eml = undef;
 my $spam_ok;
 if ($spamc) {
        $str = '';
@@ -84,7 +83,7 @@ if ($spamc) {
        my $fh = $emm->fh;
        read($fh, $str, -s $fh);
 }
-do_exit(0) unless $spam_ok;
+$do_exit->(0) unless $spam_ok;
 
 # -mda defaults to the strict base filter which we won't use anywhere else
 sub mda_filter_adjust ($) {
@@ -101,9 +100,10 @@ my @rejects;
 for my $ibx (@$dests) {
        mda_filter_adjust($ibx);
        my $filter = $ibx->filter;
-       my $mime = PublicInbox::MIME->new($str);
+       my $mime = PublicInbox::Eml->new($str);
        my $ret = $filter->delivery($mime);
-       if (ref($ret) && $ret->isa('Email::MIME')) { # filter altered message
+       if (ref($ret) && ($ret->isa('PublicInbox::Eml') ||
+                       $ret->isa('Email::MIME'))) { # filter altered message
                $mime = $ret;
        } elsif ($ret == PublicInbox::Filter::Base::IGNORE) {
                next; # nothing, keep looping
@@ -131,4 +131,4 @@ if (scalar(@rejects) && scalar(@rejects) == scalar(@$dests)) {
        die join("\n", @rejects, '');
 }
 
-do_exit(0);
+$do_exit->(0);