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