2 # Copyright (C) 2013-2019 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
19 use PublicInbox::MIME;
21 use PublicInbox::Config;
22 use PublicInbox::Emergency;
23 use PublicInbox::Filter::Base;
24 use PublicInbox::InboxWritable;
25 use PublicInbox::Spamcheck;
27 # n.b: hopefully we can setup the emergency path without bailing due to
28 # user error, we really want to setup the emergency destination ASAP
29 # in case there's bugs in our code or user error.
30 my $emergency = $ENV{PI_EMERGENCY} || "$ENV{HOME}/.public-inbox/emergency/";
31 $ems = PublicInbox::Emergency->new($emergency);
32 my $str = eval { local $/; <STDIN> };
33 $str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
35 my $simple = Email::Simple->new(\$str);
36 my $config = PublicInbox::Config->new;
37 my $key = 'publicinboxmda.spamcheck';
38 my $default = 'PublicInbox::Spamcheck::Spamc';
39 my $spamc = PublicInbox::Spamcheck::get($config, $key, $default);
41 my $recipient = $ENV{ORIGINAL_RECIPIENT};
42 if (defined $recipient) {
43 $dst = $config->lookup($recipient); # first check
46 $dst = PublicInbox::MDA->inbox_for_list_id($config, $simple);
47 if (!defined $dst && !defined $recipient) {
48 die "ORIGINAL_RECIPIENT not defined in ENV\n";
50 defined $dst or do_exit(67); # EX_NOUSER 5.1.1 user unknown
53 $dst = PublicInbox::InboxWritable->new($dst);
54 eval { $dst->assert_usable_dir };
57 # pre-check, MDA has stricter rules than an importer might;
58 if ($precheck && !PublicInbox::MDA->precheck($simple, $dst->{address})) {
65 $spam_ok = $spamc->spamcheck($ems->fh, \$str);
66 # update the emergency dump with the new message:
67 $emm = PublicInbox::Emergency->new($emergency);
70 } else { # no spam checking configured:
74 read($fh, $str, -s $fh);
76 do_exit(0) unless $spam_ok;
78 my $mime = PublicInbox::MIME->new(\$str);
80 # -mda defaults to the strict base filter which we won't use anywhere else
81 sub mda_filter_adjust ($) {
83 my $fcfg = $ibx->{filter} || '';
85 $ibx->{filter} = 'PublicInbox::Filter::Base';
86 } elsif ($fcfg eq 'scrub') { # legacy alias, undocumented, remove?
87 $ibx->{filter} = 'PublicInbox::Filter::Mirror';
91 mda_filter_adjust($dst);
93 my $filter = $dst->filter;
94 my $ret = $filter->delivery($mime);
95 if (ref($ret) && $ret->isa('Email::MIME')) { # filter altered message
97 } elsif ($ret == PublicInbox::Filter::Base::IGNORE) {
98 do_exit(0); # chuck it to emergency
99 } elsif ($ret == PublicInbox::Filter::Base::REJECT) {
100 $! = 65; # EX_DATAERR 5.6.0 data format error
101 die $filter->err, "\n";
105 PublicInbox::MDA->set_list_headers($mime, $dst);
106 my $im = $dst->importer(0);
107 if (defined $im->add($mime)) {
110 # this message is similar to what ssoma-mda shows:
111 print STDERR "CONFLICT: Message-ID: ",
112 $mime->header_obj->header_raw('Message-ID'),