]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-mda
4e6e04e2174ebd9ab64d815df691c68294bae99f
[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 < rfc2822_message';
9 my ($ems, $emm);
10
11 sub do_exit {
12         my ($code) = shift;
13         $emm = $ems = undef; # trigger DESTROY
14         exit $code;
15 }
16
17 use Email::Simple;
18 use PublicInbox::MIME;
19 use PublicInbox::MDA;
20 use PublicInbox::Config;
21 use PublicInbox::Emergency;
22 use PublicInbox::Filter::Base;
23 use PublicInbox::InboxWritable;
24 use PublicInbox::Spamcheck;
25
26 # n.b: hopefully we can setup the emergency path without bailing due to
27 # user error, we really want to setup the emergency destination ASAP
28 # in case there's bugs in our code or user error.
29 my $emergency = $ENV{PI_EMERGENCY} || "$ENV{HOME}/.public-inbox/emergency/";
30 $ems = PublicInbox::Emergency->new($emergency);
31 my $str = eval { local $/; <STDIN> };
32 $str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
33 $ems->prepare(\$str);
34 my $simple = Email::Simple->new(\$str);
35 my $config = PublicInbox::Config->new;
36 my $key = 'publicinboxmda.spamcheck';
37 my $default = 'PublicInbox::Spamcheck::Spamc';
38 my $spamc = PublicInbox::Spamcheck::get($config, $key, $default);
39 my $recipient = $ENV{ORIGINAL_RECIPIENT};
40 defined $recipient or die "ORIGINAL_RECIPIENT not defined in ENV\n";
41 my $dst = $config->lookup($recipient); # first check
42 defined $dst or do_exit(67); # EX_NOUSER 5.1.1 user unknown
43 $dst->{mainrepo} or do_exit(67);
44 $dst = PublicInbox::InboxWritable->new($dst);
45
46 # pre-check, MDA has stricter rules than an importer might;
47 do_exit(0) unless PublicInbox::MDA->precheck($simple, $dst->{address});
48 $simple = undef;
49 my $spam_ok;
50 if ($spamc) {
51         $str = '';
52         $spam_ok = $spamc->spamcheck($ems->fh, \$str);
53         # update the emergency dump with the new message:
54         $emm = PublicInbox::Emergency->new($emergency);
55         $emm->prepare(\$str);
56         $ems = $ems->abort;
57 } else { # no spam checking configured:
58         $spam_ok = 1;
59         $emm = $ems;
60         my $fh = $emm->fh;
61         read($fh, $str, -s $fh);
62 }
63
64 my $mime = PublicInbox::MIME->new(\$str);
65 do_exit(0) unless $spam_ok;
66
67 my $fcfg = $dst->{filter} || '';
68 # -mda defaults to the strict base filter
69 if ($fcfg eq '') {
70         $dst->{filter} = 'PublicInbox::Filter::Base';
71 } elsif ($fcfg eq 'scrub') { # legacy alias, undocumented, remove?
72         $dst->{filter} = 'PublicInbox::Filter::Mirror';
73 }
74 my $filter = $dst->filter;
75 my $ret = $filter->delivery($mime);
76 if (ref($ret) && $ret->isa('Email::MIME')) { # filter altered message
77         $mime = $ret;
78 } elsif ($ret == PublicInbox::Filter::Base::IGNORE) {
79         do_exit(0); # chuck it to emergency
80 } elsif ($ret == PublicInbox::Filter::Base::REJECT) {
81         $! = 65; # EX_DATAERR 5.6.0 data format error
82         die $filter->err, "\n";
83 } # else { accept
84 $filter = undef;
85
86 PublicInbox::MDA->set_list_headers($mime, $dst);
87 my $im = $dst->importer(0);
88 if (defined $im->add($mime)) {
89         $emm = $emm->abort;
90 } else {
91         # this message is similar to what ssoma-mda shows:
92         print STDERR "CONFLICT: Message-ID: ",
93                         $mime->header_obj->header_raw('Message-ID'),
94                         " exists\n";
95 }
96
97 $im->done;
98 do_exit(0);