]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-mda
mda: use InboxWritable
[public-inbox.git] / script / public-inbox-mda
1 #!/usr/bin/perl -w
2 # Copyright (C) 2013-2018 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::Spamcheck::Spamc;
24 use PublicInbox::InboxWritable;
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
37 my $recipient = $ENV{ORIGINAL_RECIPIENT};
38 defined $recipient or die "ORIGINAL_RECIPIENT not defined in ENV\n";
39 my $dst = $config->lookup($recipient); # first check
40 defined $dst or do_exit(67); # EX_NOUSER 5.1.1 user unknown
41 $dst->{mainrepo} or do_exit(67);
42 $dst = PublicInbox::InboxWritable->new($dst);
43
44 # pre-check, MDA has stricter rules than an importer might;
45 do_exit(0) unless PublicInbox::MDA->precheck($simple, $dst->{address});
46 my $spamc = PublicInbox::Spamcheck::Spamc->new;
47 $str = '';
48 my $spam_ok = $spamc->spamcheck($ems->fh, \$str);
49 $simple = undef;
50 $emm = PublicInbox::Emergency->new($emergency);
51 $emm->prepare(\$str);
52 $ems = $ems->abort;
53 my $mime = PublicInbox::MIME->new(\$str);
54 $str = '';
55 do_exit(0) unless $spam_ok;
56
57 my $fcfg = $dst->{filter} || '';
58 # -mda defaults to the strict base filter
59 if ($fcfg eq '') {
60         $dst->{filter} = 'PublicInbox::Filter::Base';
61 } elsif ($fcfg eq 'scrub') { # legacy alias, undocumented, remove?
62         $dst->{filter} = 'PublicInbox::Filter::Mirror';
63 }
64 my $filter = $dst->filter;
65 my $ret = $filter->delivery($mime);
66 if (ref($ret) && $ret->isa('Email::MIME')) { # filter altered message
67         $mime = $ret;
68 } elsif ($ret == PublicInbox::Filter::Base::IGNORE) {
69         do_exit(0); # chuck it to emergency
70 } elsif ($ret == PublicInbox::Filter::Base::REJECT) {
71         $! = 65; # EX_DATAERR 5.6.0 data format error
72         die $filter->err, "\n";
73 } # else { accept
74
75 PublicInbox::MDA->set_list_headers($mime, $dst);
76 my $im = $dst->importer(0);
77 if (defined $im->add($mime)) {
78         $emm = $emm->abort;
79 } else {
80         # this message is similar to what ssoma-mda shows:
81         print STDERR "CONFLICT: Message-ID: ",
82                         $mime->header_obj->header_raw('Message-ID'),
83                         " exists\n";
84 }
85
86 $im->done;
87 do_exit(0);