]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-mda
02ca343163eaf00658b8081f025695f94b6c2e58
[public-inbox.git] / script / public-inbox-mda
1 #!/usr/bin/perl -w
2 # Copyright (C) 2013-2020 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 my $do_exit = sub {
13         my ($code) = shift;
14         $emm = $ems = undef; # trigger DESTROY
15         exit $code;
16 };
17
18 use PublicInbox::Eml;
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 = do { local $/; <STDIN> };
32 $str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
33 $ems->prepare(\$str);
34 my $eml = PublicInbox::Eml->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 $dests = [];
40 my $recipient = $ENV{ORIGINAL_RECIPIENT};
41 if (defined $recipient) {
42         my $ibx = $config->lookup($recipient); # first check
43         push @$dests, $ibx if $ibx;
44 }
45 if (!scalar(@$dests)) {
46         $dests = PublicInbox::MDA->inboxes_for_list_id($config, $eml);
47         if (!scalar(@$dests) && !defined($recipient)) {
48                 die "ORIGINAL_RECIPIENT not defined in ENV\n";
49         }
50         scalar(@$dests) or $do_exit->(67); # EX_NOUSER 5.1.1 user unknown
51 }
52
53 my $err;
54 @$dests = grep {
55         my $ibx = PublicInbox::InboxWritable->new($_);
56         eval { $ibx->assert_usable_dir };
57         if ($@) {
58                 warn $@;
59                 $err = 1;
60                 0;
61         # pre-check, MDA has stricter rules than an importer might;
62         } elsif ($precheck) {
63                 !!PublicInbox::MDA->precheck($eml, $ibx->{address});
64         } else {
65                 1;
66         }
67 } @$dests;
68
69 $do_exit->(67) if $err && scalar(@$dests) == 0;
70
71 $eml = undef;
72 my $spam_ok;
73 if ($spamc) {
74         $str = '';
75         $spam_ok = $spamc->spamcheck($ems->fh, \$str);
76         # update the emergency dump with the new message:
77         $emm = PublicInbox::Emergency->new($emergency);
78         $emm->prepare(\$str);
79         $ems = $ems->abort;
80 } else { # no spam checking configured:
81         $spam_ok = 1;
82         $emm = $ems;
83         my $fh = $emm->fh;
84         read($fh, $str, -s $fh);
85 }
86 $do_exit->(0) unless $spam_ok;
87
88 # -mda defaults to the strict base filter which we won't use anywhere else
89 sub mda_filter_adjust ($) {
90         my ($ibx) = @_;
91         my $fcfg = $ibx->{filter} || '';
92         if ($fcfg eq '') {
93                 $ibx->{filter} = 'PublicInbox::Filter::Base';
94         } elsif ($fcfg eq 'scrub') { # legacy alias, undocumented, remove?
95                 $ibx->{filter} = 'PublicInbox::Filter::Mirror';
96         }
97 }
98
99 my @rejects;
100 for my $ibx (@$dests) {
101         mda_filter_adjust($ibx);
102         my $filter = $ibx->filter;
103         my $mime = PublicInbox::Eml->new($str);
104         my $ret = $filter->delivery($mime);
105         if (ref($ret) && ($ret->isa('PublicInbox::Eml') ||
106                         $ret->isa('Email::MIME'))) { # filter altered message
107                 $mime = $ret;
108         } elsif ($ret == PublicInbox::Filter::Base::IGNORE) {
109                 next; # nothing, keep looping
110         } elsif ($ret == PublicInbox::Filter::Base::REJECT) {
111                 push @rejects, $filter->err;
112                 next;
113         }
114
115         PublicInbox::MDA->set_list_headers($mime, $ibx);
116         my $im = $ibx->importer(0);
117         if (defined $im->add($mime)) {
118                 # ->abort is idempotent, no emergency if a single
119                 # destination succeeds
120                 $emm->abort;
121         } else { # v1-only
122                 my $mid = $mime->header_raw('Message-ID');
123                 # this message is similar to what ssoma-mda shows:
124                 print STDERR "CONFLICT: Message-ID: $mid exists\n";
125         }
126         $im->done;
127 }
128
129 if (scalar(@rejects) && scalar(@rejects) == scalar(@$dests)) {
130         $! = 65; # EX_DATAERR 5.6.0 data format error
131         die join("\n", @rejects, '');
132 }
133
134 $do_exit->(0);