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
14 use File::Path::Expand qw/expand_filename/;
17 use PublicInbox::Filter;
18 use PublicInbox::Config;
19 use PublicInbox::Import;
22 # n.b: hopefully we can setup the emergency path without bailing due to
23 # user error, we really want to setup the emergency destination ASAP
24 # in case there's bugs in our code or user error.
25 my $emergency = $ENV{PI_EMERGENCY} || '~/.public-inbox/emergency/';
26 $emergency = expand_filename($emergency);
28 # this reads the message from stdin
29 my $filter = Email::Filter->new(emergency => $emergency);
30 my $config = PublicInbox::Config->new;
32 my $recipient = $ENV{ORIGINAL_RECIPIENT};
33 defined $recipient or die "ORIGINAL_RECIPIENT not defined in ENV\n";
34 my $dst = $config->lookup($recipient); # first check
35 defined $dst or exit(1);
36 my $main_repo = $dst->{mainrepo} or exit(1);
37 my $filtered; # string dest
39 if (PublicInbox::MDA->precheck($filter, $dst->{address}) &&
40 do_spamc($filter->simple, \$filtered)) {
41 # update our message with SA headers (in case our filter rejects it)
42 my $msg = Email::MIME->new(\$filtered);
44 $filter->simple($msg);
47 my $fcfg = $dst->{filter};
48 if (!defined $fcfg || $filter eq 'reject') {
49 $filter_arg = $filter;
50 } elsif ($fcfg eq 'scrub') {
51 $filter_arg = undef; # the default for legacy versions
53 warn "publicinbox.$dst->{name}.filter=$fcfg invalid\n";
54 warn "must be either 'scrub' or 'reject' (the default)\n";
57 if (PublicInbox::Filter->run($msg, $filter_arg)) {
58 # run spamc again on the HTML-free message
59 if (do_spamc($msg, \$filtered)) {
60 $msg = Email::MIME->new(\$filtered);
61 PublicInbox::MDA->set_list_headers($msg, $dst);
62 $filter->simple($msg);
65 index_sync($main_repo) if ($? == 0);
67 my $git = PublicInbox::Git->new($main_repo);
68 my $im = PublicInbox::Import->new($git,
69 $dst->{name}, $recipient);
70 if (defined $im->add($msg)) {
72 $filter->ignore; # exits
74 # this message is similar to what ssoma-mda shows:
75 print STDERR "CONFLICT: Message-ID: ",
76 $msg->header_obj->header_raw('Message-ID'),
81 # Ensure emergency spam gets spamassassin headers.
82 # This makes it easier to prioritize obvious spam from less obvious
83 if (defined($filtered) && $filtered ne '') {
84 my $drop = Email::MIME->new(\$filtered);
86 $filter->simple($drop);
89 exit 0; # goes to emergency
91 # we depend on "report_safe 0" in /etc/spamassassin/*.cf with --headers
92 # not using Email::Filter->pipe here since we want the stdout of
93 # the command even on failure (spamc will set $? on error).
97 my $orig = $msg->as_string;
98 run([qw/spamc -E --headers/], \$orig, $out);
101 return ($@ || $? || !defined($$out) || $$out eq '') ? 0 : 1;
107 require PublicInbox::SearchIdx;
108 PublicInbox::SearchIdx->new($git_dir, 2)->index_sync;