2 # Copyright (C) 2013-2018 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 < rfc2822_message';
13 $emm = $ems = undef; # trigger DESTROY
18 use PublicInbox::MIME;
20 use PublicInbox::Config;
21 use PublicInbox::Import;
23 use PublicInbox::Emergency;
24 use PublicInbox::Filter::Base;
25 use PublicInbox::Spamcheck::Spamc;
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;
38 my $recipient = $ENV{ORIGINAL_RECIPIENT};
39 defined $recipient or die "ORIGINAL_RECIPIENT not defined in ENV\n";
40 my $dst = $config->lookup($recipient); # first check
41 defined $dst or do_exit(1);
42 my $main_repo = $dst->{mainrepo} or do_exit(1);
44 # pre-check, MDA has stricter rules than an importer might;
45 do_exit(0) unless PublicInbox::MDA->precheck($simple, $dst->{address});
46 my $spamc = PublicInbox::Spamcheck::Spamc->new;
48 my $spam_ok = $spamc->spamcheck($ems->fh, \$str);
50 $emm = PublicInbox::Emergency->new($emergency);
53 my $mime = PublicInbox::MIME->new(\$str);
55 do_exit(0) unless $spam_ok;
57 my $fcfg = $dst->{filter} || '';
63 } elsif ($fcfg eq 'scrub') { # TODO:
64 require PublicInbox::Filter::Mirror;
65 $filter = PublicInbox::Filter::Mirror->new;
67 $filter = PublicInbox::Filter::Base->new;
70 my $ret = $filter->delivery($mime);
71 if (ref($ret) && $ret->isa('Email::MIME')) { # filter altered message
73 } elsif ($ret == PublicInbox::Filter::Base::IGNORE) {
74 do_exit(0); # chuck it to emergency
75 } elsif ($ret == PublicInbox::Filter::Base::REJECT) {
77 die $filter->err, "\n";
80 PublicInbox::MDA->set_list_headers($mime, $dst);
81 my $v = $dst->{version} || 1;
84 require PublicInbox::V2Writable;
85 $im = PublicInbox::V2Writable->new($dst);
86 $im->{parallel} = 0; # pointless to be parallel for a single message
89 $im = PublicInbox::Import->new($git, $dst->{name}, $recipient, $dst);
91 die "Unsupported inbox version: $v\n";
93 if (defined $im->add($mime)) {
96 # this message is similar to what ssoma-mda shows:
97 print STDERR "CONFLICT: Message-ID: ",
98 $mime->header_obj->header_raw('Message-ID'),