]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-mda
use PublicInbox::MIME consistently
[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::Import;
22 use PublicInbox::Git;
23 use PublicInbox::Emergency;
24 use PublicInbox::Filter::Base;
25 use PublicInbox::Spamcheck::Spamc;
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
38 my $recipient = $ENV{ORIGINAL_RECIPIENT};
39 defined $recipient or die "ORIGINAL_RECIPIENT not defined in ENV\n";
40 my $dst = $config->lookup($recipient); # first check
41 defined $dst or do_exit(1);
42 my $main_repo = $dst->{mainrepo} or do_exit(1);
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 my $filter;
59 if ($fcfg =~ /::/) {
60         eval "require $fcfg";
61         die $@ if $@;
62         $filter = $fcfg->new;
63 } elsif ($fcfg eq 'scrub') { # TODO:
64         require PublicInbox::Filter::Mirror;
65         $filter = PublicInbox::Filter::Mirror->new;
66 } else {
67         $filter = PublicInbox::Filter::Base->new;
68 }
69
70 my $ret = $filter->delivery($mime);
71 if (ref($ret) && $ret->isa('Email::MIME')) { # filter altered message
72         $mime = $ret;
73 } elsif ($ret == PublicInbox::Filter::Base::IGNORE) {
74         do_exit(0); # chuck it to emergency
75 } elsif ($ret == PublicInbox::Filter::Base::REJECT) {
76         $! = $ret;
77         die $filter->err, "\n";
78 } # else { accept
79
80 PublicInbox::MDA->set_list_headers($mime, $dst);
81 my $git = PublicInbox::Git->new($main_repo);
82 my $im = PublicInbox::Import->new($git, $dst->{name}, $recipient);
83 if (defined $im->add($mime)) {
84         $emm = $emm->abort;
85 } else {
86         # this message is similar to what ssoma-mda shows:
87         print STDERR "CONFLICT: Message-ID: ",
88                         $mime->header_obj->header_raw('Message-ID'),
89                         " exists\n";
90 }
91
92 $im->done;
93 do_exit(0);