]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-mda
add xt/mem-nntpd-tls maintainer test
[public-inbox.git] / script / public-inbox-mda
1 #!/usr/bin/perl -w
2 # Copyright (C) 2013-2021 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 my $help = <<EOF;
7 usage: public-inbox-mda [OPTIONS] </path/to/RFC2822_message
8
9 options:
10
11   --no-precheck  skip internal checks for spam messages
12
13 See public-inbox-mda(1) man page for full documentation.
14 EOF
15 use strict;
16 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
17 my ($ems, $emm, $show_help);
18 my $precheck = 1;
19 GetOptions('precheck!' => \$precheck, 'help|h' => \$show_help) or
20         do { print STDERR $help; exit 1 };
21
22 my $do_exit = sub {
23         my ($code) = shift;
24         $emm = $ems = undef; # trigger DESTROY
25         exit $code;
26 };
27
28 use PublicInbox::Eml;
29 use PublicInbox::MDA;
30 use PublicInbox::Config;
31 use PublicInbox::Emergency;
32 use PublicInbox::Filter::Base;
33 use PublicInbox::InboxWritable;
34 use PublicInbox::Spamcheck;
35
36 # n.b: hopefully we can setup the emergency path without bailing due to
37 # user error, we really want to setup the emergency destination ASAP
38 # in case there's bugs in our code or user error.
39 my $emergency = $ENV{PI_EMERGENCY} || "$ENV{HOME}/.public-inbox/emergency/";
40 $ems = PublicInbox::Emergency->new($emergency);
41 my $str = do { local $/; <STDIN> };
42 $str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
43 $ems->prepare(\$str);
44 my $eml = PublicInbox::Eml->new(\$str);
45 my $cfg = PublicInbox::Config->new;
46 my $key = 'publicinboxmda.spamcheck';
47 my $default = 'PublicInbox::Spamcheck::Spamc';
48 my $spamc = PublicInbox::Spamcheck::get($cfg, $key, $default);
49 my $dests = [];
50 my $recipient = $ENV{ORIGINAL_RECIPIENT};
51 if (defined $recipient) {
52         my $ibx = $cfg->lookup($recipient); # first check
53         push @$dests, $ibx if $ibx;
54 }
55 if (!scalar(@$dests)) {
56         $dests = PublicInbox::MDA->inboxes_for_list_id($cfg, $eml);
57         if (!scalar(@$dests) && !defined($recipient)) {
58                 die "ORIGINAL_RECIPIENT not defined in ENV\n";
59         }
60         scalar(@$dests) or $do_exit->(67); # EX_NOUSER 5.1.1 user unknown
61 }
62
63 my $err;
64 @$dests = grep {
65         my $ibx = PublicInbox::InboxWritable->new($_);
66         eval { $ibx->assert_usable_dir };
67         if ($@) {
68                 warn $@;
69                 $err = 1;
70                 0;
71         # pre-check, MDA has stricter rules than an importer might;
72         } elsif ($precheck) {
73                 !!PublicInbox::MDA->precheck($eml, $ibx->{address});
74         } else {
75                 1;
76         }
77 } @$dests;
78
79 $do_exit->(67) if $err && scalar(@$dests) == 0;
80
81 $eml = undef;
82 my $spam_ok;
83 if ($spamc) {
84         $str = '';
85         $spam_ok = $spamc->spamcheck($ems->fh, \$str);
86         # update the emergency dump with the new message:
87         $emm = PublicInbox::Emergency->new($emergency);
88         $emm->prepare(\$str);
89         $ems = $ems->abort;
90 } else { # no spam checking configured:
91         $spam_ok = 1;
92         $emm = $ems;
93         my $fh = $emm->fh;
94         read($fh, $str, -s $fh);
95 }
96 $do_exit->(0) unless $spam_ok;
97
98 # -mda defaults to the strict base filter which we won't use anywhere else
99 sub mda_filter_adjust ($) {
100         my ($ibx) = @_;
101         my $fcfg = $ibx->{filter} || '';
102         if ($fcfg eq '') {
103                 $ibx->{filter} = 'PublicInbox::Filter::Base';
104         } elsif ($fcfg eq 'scrub') { # legacy alias, undocumented, remove?
105                 $ibx->{filter} = 'PublicInbox::Filter::Mirror';
106         }
107 }
108
109 my @rejects;
110 for my $ibx (@$dests) {
111         mda_filter_adjust($ibx);
112         my $filter = $ibx->filter;
113         my $mime = PublicInbox::Eml->new($str);
114         my $ret = $filter->delivery($mime);
115         if (ref($ret) && ($ret->isa('PublicInbox::Eml') ||
116                         $ret->isa('Email::MIME'))) { # filter altered message
117                 $mime = $ret;
118         } elsif ($ret == PublicInbox::Filter::Base::IGNORE) {
119                 next; # nothing, keep looping
120         } elsif ($ret == PublicInbox::Filter::Base::REJECT) {
121                 push @rejects, $filter->err;
122                 next;
123         }
124
125         PublicInbox::MDA->set_list_headers($mime, $ibx);
126         my $im = $ibx->importer(0);
127         if (defined $im->add($mime)) {
128                 # ->abort is idempotent, no emergency if a single
129                 # destination succeeds
130                 $emm->abort;
131         } else { # v1-only
132                 my $mid = $mime->header_raw('Message-ID');
133                 # this message is similar to what ssoma-mda shows:
134                 print STDERR "CONFLICT: Message-ID: $mid exists\n";
135         }
136         $im->done;
137 }
138
139 if (scalar(@rejects) && scalar(@rejects) == scalar(@$dests)) {
140         $! = 65; # EX_DATAERR 5.6.0 data format error
141         die join("\n", @rejects, '');
142 }
143
144 $do_exit->(0);