]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-mda
mda: skip MIME parsing if spam
[public-inbox.git] / script / public-inbox-mda
1 #!/usr/bin/perl -w
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>
4 #
5 # Mail delivery agent for public-inbox, run from your MTA upon mail delivery
6 use strict;
7 use warnings;
8 my $usage = 'public-inbox-mda [OPTIONS] < rfc2822_message';
9 my $precheck = grep(/\A--no-precheck\z/, @ARGV) ? 0 : 1;
10 my ($ems, $emm);
11
12 sub do_exit {
13         my ($code) = shift;
14         $emm = $ems = undef; # trigger DESTROY
15         exit $code;
16 }
17
18 use Email::Simple;
19 use PublicInbox::MIME;
20 use PublicInbox::MDA;
21 use PublicInbox::Config;
22 use PublicInbox::Emergency;
23 use PublicInbox::Filter::Base;
24 use PublicInbox::InboxWritable;
25 use PublicInbox::Spamcheck;
26
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;
34 $ems->prepare(\$str);
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);
40 my $dst;
41 my $recipient = $ENV{ORIGINAL_RECIPIENT};
42 if (defined $recipient) {
43         $dst = $config->lookup($recipient); # first check
44 }
45 if (!defined $dst) {
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";
49         }
50         defined $dst or do_exit(67); # EX_NOUSER 5.1.1 user unknown
51 }
52 $dst->{inboxdir} or do_exit(67);
53 $dst = PublicInbox::InboxWritable->new($dst);
54
55 # pre-check, MDA has stricter rules than an importer might;
56 if ($precheck && !PublicInbox::MDA->precheck($simple, $dst->{address})) {
57         do_exit(0);
58 }
59 $simple = undef;
60 my $spam_ok;
61 if ($spamc) {
62         $str = '';
63         $spam_ok = $spamc->spamcheck($ems->fh, \$str);
64         # update the emergency dump with the new message:
65         $emm = PublicInbox::Emergency->new($emergency);
66         $emm->prepare(\$str);
67         $ems = $ems->abort;
68 } else { # no spam checking configured:
69         $spam_ok = 1;
70         $emm = $ems;
71         my $fh = $emm->fh;
72         read($fh, $str, -s $fh);
73 }
74 do_exit(0) unless $spam_ok;
75
76 my $mime = PublicInbox::MIME->new(\$str);
77
78 # -mda defaults to the strict base filter which we won't use anywhere else
79 sub mda_filter_adjust ($) {
80         my ($ibx) = @_;
81         my $fcfg = $ibx->{filter} || '';
82         if ($fcfg eq '') {
83                 $ibx->{filter} = 'PublicInbox::Filter::Base';
84         } elsif ($fcfg eq 'scrub') { # legacy alias, undocumented, remove?
85                 $ibx->{filter} = 'PublicInbox::Filter::Mirror';
86         }
87 }
88
89 mda_filter_adjust($dst);
90
91 my $filter = $dst->filter;
92 my $ret = $filter->delivery($mime);
93 if (ref($ret) && $ret->isa('Email::MIME')) { # filter altered message
94         $mime = $ret;
95 } elsif ($ret == PublicInbox::Filter::Base::IGNORE) {
96         do_exit(0); # chuck it to emergency
97 } elsif ($ret == PublicInbox::Filter::Base::REJECT) {
98         $! = 65; # EX_DATAERR 5.6.0 data format error
99         die $filter->err, "\n";
100 } # else { accept
101 $filter = undef;
102
103 PublicInbox::MDA->set_list_headers($mime, $dst);
104 my $im = $dst->importer(0);
105 if (defined $im->add($mime)) {
106         $emm = $emm->abort;
107 } else {
108         # this message is similar to what ssoma-mda shows:
109         print STDERR "CONFLICT: Message-ID: ",
110                         $mime->header_obj->header_raw('Message-ID'),
111                         " exists\n";
112 }
113
114 $im->done;
115 do_exit(0);