]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-mda
mda, watch: wire up List-ID header support
[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 $dst;
40 my $recipient = $ENV{ORIGINAL_RECIPIENT};
41 if (defined $recipient) {
42         $dst = $config->lookup($recipient); # first check
43 }
44 if (!defined $dst) {
45         my $list_id = $simple->header('List-Id');
46         if (defined $list_id && $list_id =~ /<[ \t]*(.+)?[ \t]*>/) {
47                 $dst = $config->lookup_list_id($1);
48         }
49         if (!defined $dst && !defined $recipient) {
50                 die "ORIGINAL_RECIPIENT not defined in ENV\n";
51         }
52         defined $dst or do_exit(67); # EX_NOUSER 5.1.1 user unknown
53 }
54 $dst->{mainrepo} or do_exit(67);
55 $dst = PublicInbox::InboxWritable->new($dst);
56
57 # pre-check, MDA has stricter rules than an importer might;
58 do_exit(0) unless PublicInbox::MDA->precheck($simple, $dst->{address});
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
75 my $mime = PublicInbox::MIME->new(\$str);
76 do_exit(0) unless $spam_ok;
77
78 my $fcfg = $dst->{filter} || '';
79 # -mda defaults to the strict base filter
80 if ($fcfg eq '') {
81         $dst->{filter} = 'PublicInbox::Filter::Base';
82 } elsif ($fcfg eq 'scrub') { # legacy alias, undocumented, remove?
83         $dst->{filter} = 'PublicInbox::Filter::Mirror';
84 }
85 my $filter = $dst->filter;
86 my $ret = $filter->delivery($mime);
87 if (ref($ret) && $ret->isa('Email::MIME')) { # filter altered message
88         $mime = $ret;
89 } elsif ($ret == PublicInbox::Filter::Base::IGNORE) {
90         do_exit(0); # chuck it to emergency
91 } elsif ($ret == PublicInbox::Filter::Base::REJECT) {
92         $! = 65; # EX_DATAERR 5.6.0 data format error
93         die $filter->err, "\n";
94 } # else { accept
95 $filter = undef;
96
97 PublicInbox::MDA->set_list_headers($mime, $dst);
98 my $im = $dst->importer(0);
99 if (defined $im->add($mime)) {
100         $emm = $emm->abort;
101 } else {
102         # this message is similar to what ssoma-mda shows:
103         print STDERR "CONFLICT: Message-ID: ",
104                         $mime->header_obj->header_raw('Message-ID'),
105                         " exists\n";
106 }
107
108 $im->done;
109 do_exit(0);