2 # Copyright (C) 2013-2015 all contributors <meta@public-inbox.org>
3 # License: AGPLv3 or later (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';
12 use Email::MIME::ContentType;
13 $Email::MIME::ContentType::STRICT_PARAMS = 0; # user input is imperfect
16 use PublicInbox::Filter;
17 use PublicInbox::Config;
18 use PublicInbox::Import;
21 # n.b: hopefully we can setup the emergency path without bailing due to
22 # user error, we really want to setup the emergency destination ASAP
23 # in case there's bugs in our code or user error.
24 my $emergency = $ENV{PI_EMERGENCY} || "$ENV{HOME}/.public-inbox/emergency/";
26 # this reads the message from stdin
27 my $filter = Email::Filter->new(emergency => $emergency);
28 my $config = PublicInbox::Config->new;
30 my $recipient = $ENV{ORIGINAL_RECIPIENT};
31 defined $recipient or die "ORIGINAL_RECIPIENT not defined in ENV\n";
32 my $dst = $config->lookup($recipient); # first check
33 defined $dst or exit(1);
34 my $main_repo = $dst->{mainrepo} or exit(1);
35 my $filtered; # string dest
37 if (PublicInbox::MDA->precheck($filter->simple, $dst->{address}) &&
38 do_spamc($filter->simple, \$filtered)) {
39 # update our message with SA headers (in case our filter rejects it)
40 my $msg = Email::MIME->new(\$filtered);
42 $filter->simple($msg);
45 my $fcfg = $dst->{filter};
46 if (!defined $fcfg || $filter eq 'reject') {
47 $filter_arg = $filter;
48 } elsif ($fcfg eq 'scrub') {
49 $filter_arg = undef; # the default for legacy versions
51 warn "publicinbox.$dst->{name}.filter=$fcfg invalid\n";
52 warn "must be either 'scrub' or 'reject' (the default)\n";
55 if (PublicInbox::Filter->run($msg, $filter_arg)) {
56 # run spamc again on the HTML-free message
57 if (do_spamc($msg, \$filtered)) {
58 $msg = Email::MIME->new(\$filtered);
59 PublicInbox::MDA->set_list_headers($msg, $dst);
60 $filter->simple($msg);
63 index_sync($main_repo) if ($? == 0);
65 my $git = PublicInbox::Git->new($main_repo);
66 my $im = PublicInbox::Import->new($git,
67 $dst->{name}, $recipient);
68 if (defined $im->add($msg)) {
70 $filter->ignore; # exits
72 # this message is similar to what ssoma-mda shows:
73 print STDERR "CONFLICT: Message-ID: ",
74 $msg->header_obj->header_raw('Message-ID'),
79 # Ensure emergency spam gets spamassassin headers.
80 # This makes it easier to prioritize obvious spam from less obvious
81 if (defined($filtered) && $filtered ne '') {
82 my $drop = Email::MIME->new(\$filtered);
84 $filter->simple($drop);
87 exit 0; # goes to emergency
89 # we depend on "report_safe 0" in /etc/spamassassin/*.cf with --headers
90 # not using Email::Filter->pipe here since we want the stdout of
91 # the command even on failure (spamc will set $? on error).
95 my $orig = $msg->as_string;
96 run([qw/spamc -E --headers/], \$orig, $out);
99 return ($@ || $? || !defined($$out) || $$out eq '') ? 0 : 1;
105 require PublicInbox::SearchIdx;
106 PublicInbox::SearchIdx->new($git_dir, 2)->index_sync;