]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-mda
mda: always call Import::done, even on dupes
[public-inbox.git] / script / public-inbox-mda
1 #!/usr/bin/perl -w
2 # Copyright (C) 2013-2015 all contributors <meta@public-inbox.org>
3 # License: AGPLv3 or later (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 Email::MIME;
19 use Email::MIME::ContentType;
20 $Email::MIME::ContentType::STRICT_PARAMS = 0; # user input is imperfect
21 use PublicInbox::MDA;
22 use PublicInbox::Config;
23 use PublicInbox::Import;
24 use PublicInbox::Git;
25 use PublicInbox::Emergency;
26 use PublicInbox::Filter::Base;
27 use PublicInbox::Spamcheck::Spamc;
28
29 # n.b: hopefully we can setup the emergency path without bailing due to
30 # user error, we really want to setup the emergency destination ASAP
31 # in case there's bugs in our code or user error.
32 my $emergency = $ENV{PI_EMERGENCY} || "$ENV{HOME}/.public-inbox/emergency/";
33 $ems = PublicInbox::Emergency->new($emergency);
34 my $str = eval { local $/; <STDIN> };
35 $str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
36 $ems->prepare(\$str);
37 my $simple = Email::Simple->new(\$str);
38 my $config = PublicInbox::Config->new;
39
40 my $recipient = $ENV{ORIGINAL_RECIPIENT};
41 defined $recipient or die "ORIGINAL_RECIPIENT not defined in ENV\n";
42 my $dst = $config->lookup($recipient); # first check
43 defined $dst or do_exit(1);
44 my $main_repo = $dst->{mainrepo} or do_exit(1);
45
46 # pre-check, MDA has stricter rules than an importer might;
47 do_exit(0) unless PublicInbox::MDA->precheck($simple, $dst->{address});
48 my $spamc = PublicInbox::Spamcheck::Spamc->new;
49 $str = '';
50 my $spam_ok = $spamc->spamcheck($ems->fh, \$str);
51 $simple = undef;
52 $emm = PublicInbox::Emergency->new($emergency);
53 $emm->prepare(\$str);
54 $ems = $ems->abort;
55 my $mime = Email::MIME->new(\$str);
56 $str = '';
57 do_exit(0) unless $spam_ok;
58
59 my $fcfg = $dst->{filter} || '';
60 my $filter;
61 if ($fcfg =~ /::/) {
62         eval "require $fcfg";
63         die $@ if $@;
64         $filter = $fcfg->new;
65 } elsif ($fcfg eq 'scrub') { # TODO:
66         require PublicInbox::Filter::Mirror;
67         $filter = PublicInbox::Filter::Mirror->new;
68 } else {
69         $filter = PublicInbox::Filter::Base->new;
70 }
71
72 my $ret = $filter->delivery($mime);
73 if (ref($ret) && $ret->isa('Email::MIME')) { # filter altered message
74         $mime = $ret;
75 } elsif ($ret == PublicInbox::Filter::Base::IGNORE) {
76         do_exit(0); # chuck it to emergency
77 } elsif ($ret == PublicInbox::Filter::Base::REJECT) {
78         $! = $ret;
79         die $filter->err, "\n";
80 } # else { accept
81
82 PublicInbox::MDA->set_list_headers($mime, $dst);
83 my $git = PublicInbox::Git->new($main_repo);
84 my $im = PublicInbox::Import->new($git, $dst->{name}, $recipient);
85 if (defined $im->add($mime)) {
86         $emm = $emm->abort;
87 } else {
88         # this message is similar to what ssoma-mda shows:
89         print STDERR "CONFLICT: Message-ID: ",
90                         $mime->header_obj->header_raw('Message-ID'),
91                         " exists\n";
92 }
93
94 $im->done;
95 do_exit(0);