2 # Copyright (C) 2013-2020 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
5 # Mail delivery agent for public-inbox, run from your MTA upon mail delivery
8 my $usage = 'public-inbox-mda [OPTIONS] < rfc2822_message';
9 my $precheck = grep(/\A--no-precheck\z/, @ARGV) ? 0 : 1;
14 $emm = $ems = undef; # trigger DESTROY
20 use PublicInbox::Config;
21 use PublicInbox::Emergency;
22 use PublicInbox::Filter::Base;
23 use PublicInbox::InboxWritable;
24 use PublicInbox::Spamcheck;
26 # n.b: hopefully we can setup the emergency path without bailing due to
27 # user error, we really want to setup the emergency destination ASAP
28 # in case there's bugs in our code or user error.
29 my $emergency = $ENV{PI_EMERGENCY} || "$ENV{HOME}/.public-inbox/emergency/";
30 $ems = PublicInbox::Emergency->new($emergency);
31 my $str = do { local $/; <STDIN> };
32 $str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
34 my $eml = PublicInbox::Eml->new(\$str);
35 my $config = PublicInbox::Config->new;
36 my $key = 'publicinboxmda.spamcheck';
37 my $default = 'PublicInbox::Spamcheck::Spamc';
38 my $spamc = PublicInbox::Spamcheck::get($config, $key, $default);
40 my $recipient = $ENV{ORIGINAL_RECIPIENT};
41 if (defined $recipient) {
42 my $ibx = $config->lookup($recipient); # first check
43 push @$dests, $ibx if $ibx;
45 if (!scalar(@$dests)) {
46 $dests = PublicInbox::MDA->inboxes_for_list_id($config, $eml);
47 if (!scalar(@$dests) && !defined($recipient)) {
48 die "ORIGINAL_RECIPIENT not defined in ENV\n";
50 scalar(@$dests) or $do_exit->(67); # EX_NOUSER 5.1.1 user unknown
55 my $ibx = PublicInbox::InboxWritable->new($_);
56 eval { $ibx->assert_usable_dir };
61 # pre-check, MDA has stricter rules than an importer might;
63 !!PublicInbox::MDA->precheck($eml, $ibx->{address});
69 $do_exit->(67) if $err && scalar(@$dests) == 0;
75 $spam_ok = $spamc->spamcheck($ems->fh, \$str);
76 # update the emergency dump with the new message:
77 $emm = PublicInbox::Emergency->new($emergency);
80 } else { # no spam checking configured:
84 read($fh, $str, -s $fh);
86 $do_exit->(0) unless $spam_ok;
88 # -mda defaults to the strict base filter which we won't use anywhere else
89 sub mda_filter_adjust ($) {
91 my $fcfg = $ibx->{filter} || '';
93 $ibx->{filter} = 'PublicInbox::Filter::Base';
94 } elsif ($fcfg eq 'scrub') { # legacy alias, undocumented, remove?
95 $ibx->{filter} = 'PublicInbox::Filter::Mirror';
100 for my $ibx (@$dests) {
101 mda_filter_adjust($ibx);
102 my $filter = $ibx->filter;
103 my $mime = PublicInbox::Eml->new($str);
104 my $ret = $filter->delivery($mime);
105 if (ref($ret) && ($ret->isa('PublicInbox::Eml') ||
106 $ret->isa('Email::MIME'))) { # filter altered message
108 } elsif ($ret == PublicInbox::Filter::Base::IGNORE) {
109 next; # nothing, keep looping
110 } elsif ($ret == PublicInbox::Filter::Base::REJECT) {
111 push @rejects, $filter->err;
115 PublicInbox::MDA->set_list_headers($mime, $ibx);
116 my $im = $ibx->importer(0);
117 if (defined $im->add($mime)) {
118 # ->abort is idempotent, no emergency if a single
119 # destination succeeds
122 my $mid = $mime->header_raw('Message-ID');
123 # this message is similar to what ssoma-mda shows:
124 print STDERR "CONFLICT: Message-ID: $mid exists\n";
129 if (scalar(@rejects) && scalar(@rejects) == scalar(@$dests)) {
130 $! = 65; # EX_DATAERR 5.6.0 data format error
131 die join("\n", @rejects, '');