2 # Copyright (C) 2013-2021 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
7 usage: public-inbox-mda [OPTIONS] </path/to/RFC2822_message
11 --no-precheck skip internal checks for spam messages
13 See public-inbox-mda(1) man page for full documentation.
16 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
17 my ($ems, $emm, $show_help);
19 GetOptions('precheck!' => \$precheck, 'help|h' => \$show_help) or
20 do { print STDERR $help; exit 1 };
24 $emm = $ems = undef; # trigger DESTROY
30 use PublicInbox::Config;
31 use PublicInbox::Emergency;
32 use PublicInbox::Filter::Base;
33 use PublicInbox::InboxWritable;
34 use PublicInbox::Spamcheck;
36 # n.b: hopefully we can setup the emergency path without bailing due to
37 # user error, we really want to setup the emergency destination ASAP
38 # in case there's bugs in our code or user error.
39 my $emergency = $ENV{PI_EMERGENCY} || "$ENV{HOME}/.public-inbox/emergency/";
40 $ems = PublicInbox::Emergency->new($emergency);
41 my $str = do { local $/; <STDIN> };
42 $str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
44 my $eml = PublicInbox::Eml->new(\$str);
45 my $cfg = PublicInbox::Config->new;
46 my $key = 'publicinboxmda.spamcheck';
47 my $default = 'PublicInbox::Spamcheck::Spamc';
48 my $spamc = PublicInbox::Spamcheck::get($cfg, $key, $default);
50 my $recipient = $ENV{ORIGINAL_RECIPIENT};
51 if (defined $recipient) {
52 my $ibx = $cfg->lookup($recipient); # first check
53 push @$dests, $ibx if $ibx;
55 if (!scalar(@$dests)) {
56 $dests = PublicInbox::MDA->inboxes_for_list_id($cfg, $eml);
57 if (!scalar(@$dests) && !defined($recipient)) {
58 die "ORIGINAL_RECIPIENT not defined in ENV\n";
60 scalar(@$dests) or $do_exit->(67); # EX_NOUSER 5.1.1 user unknown
65 my $ibx = PublicInbox::InboxWritable->new($_);
66 eval { $ibx->assert_usable_dir };
71 # pre-check, MDA has stricter rules than an importer might;
73 !!PublicInbox::MDA->precheck($eml, $ibx->{address});
79 $do_exit->(67) if $err && scalar(@$dests) == 0;
85 $spam_ok = $spamc->spamcheck($ems->fh, \$str);
86 # update the emergency dump with the new message:
87 $emm = PublicInbox::Emergency->new($emergency);
90 } else { # no spam checking configured:
94 read($fh, $str, -s $fh);
96 $do_exit->(0) unless $spam_ok;
98 # -mda defaults to the strict base filter which we won't use anywhere else
99 sub mda_filter_adjust ($) {
101 my $fcfg = $ibx->{filter} || '';
103 $ibx->{filter} = 'PublicInbox::Filter::Base';
104 } elsif ($fcfg eq 'scrub') { # legacy alias, undocumented, remove?
105 $ibx->{filter} = 'PublicInbox::Filter::Mirror';
110 for my $ibx (@$dests) {
111 mda_filter_adjust($ibx);
112 my $filter = $ibx->filter;
113 my $mime = PublicInbox::Eml->new($str);
114 my $ret = $filter->delivery($mime);
115 if (ref($ret) && ($ret->isa('PublicInbox::Eml') ||
116 $ret->isa('Email::MIME'))) { # filter altered message
118 } elsif ($ret == PublicInbox::Filter::Base::IGNORE) {
119 next; # nothing, keep looping
120 } elsif ($ret == PublicInbox::Filter::Base::REJECT) {
121 push @rejects, $filter->err;
125 PublicInbox::MDA->set_list_headers($mime, $ibx);
126 my $im = $ibx->importer(0);
127 if (defined $im->add($mime)) {
128 # ->abort is idempotent, no emergency if a single
129 # destination succeeds
132 my $mid = $mime->header_raw('Message-ID');
133 # this message is similar to what ssoma-mda shows:
134 print STDERR "CONFLICT: Message-ID: $mid exists\n";
139 if (scalar(@rejects) && scalar(@rejects) == scalar(@$dests)) {
140 $! = 65; # EX_DATAERR 5.6.0 data format error
141 die join("\n", @rejects, '');