]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-mda
mda: hook up new filter functionality
[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::Spawn qw(popen_rd);
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 $ems->prepare(\$str);
36 my $simple = Email::Simple->new(\$str);
37 my $config = PublicInbox::Config->new;
38
39 my $recipient = $ENV{ORIGINAL_RECIPIENT};
40 defined $recipient or die "ORIGINAL_RECIPIENT not defined in ENV\n";
41 my $dst = $config->lookup($recipient); # first check
42 defined $dst or do_exit(1);
43 my $main_repo = $dst->{mainrepo} or do_exit(1);
44
45 # pre-check, MDA has stricter rules than an importer might;
46 do_exit(0) unless PublicInbox::MDA->precheck($simple, $dst->{address});
47
48 $str = '';
49 my $spam_ok = do_spamc($ems->fh, \$str);
50 $simple = undef;
51 $emm = PublicInbox::Emergency->new($emergency);
52 $emm->prepare(\$str);
53 $ems = $ems->abort;
54 my $mime = Email::MIME->new(\$str);
55 $str = '';
56 do_exit(0) unless $spam_ok;
57
58 my $fcfg = $dst->{filter} || '';
59 my $filter;
60 if ($fcfg eq 'scrub') { # TODO:
61         require PublicInbox::Filter::Mirror;
62         $filter = PublicInbox::Filter::Mirror->new;
63 } else {
64         $filter = PublicInbox::Filter::Base->new;
65 }
66
67 my $ret = $filter->delivery($mime);
68 if (ref($ret) && $ret->isa('Email::MIME')) { # filter altered message
69         $mime = $ret;
70 } elsif ($ret == PublicInbox::Filter::Base::IGNORE) {
71         do_exit(0); # chuck it to emergency
72 } elsif ($ret == PublicInbox::Filter::Base::REJECT) {
73         $! = $ret;
74         die $filter->err, "\n";
75 } # else { accept
76
77 PublicInbox::MDA->set_list_headers($mime, $dst);
78 END { index_sync($main_repo) if $? == 0 };
79 my $git = PublicInbox::Git->new($main_repo);
80 my $im = PublicInbox::Import->new($git, $dst->{name}, $recipient);
81 if (defined $im->add($mime)) {
82         $im->done;
83         $emm = $emm->abort;
84 } else {
85         # this message is similar to what ssoma-mda shows:
86         print STDERR "CONFLICT: Message-ID: ",
87                         $mime->header_obj->header_raw('Message-ID'),
88                         " exists\n";
89 }
90 do_exit(0);
91
92 # we depend on "report_safe 0" in /etc/spamassassin/*.cf with --headers
93 sub do_spamc {
94         my ($in, $out) = @_;
95         my $rdr = { 0 => fileno($in) };
96         my ($fh, $pid) = popen_rd([qw/spamc -E --headers/], undef, $rdr);
97         my $r;
98         do {
99                 $r = sysread($fh, $$out, 65536, length($$out));
100         } while (defined($r) && $r != 0);
101         close $fh or die "close failed: $!\n";
102         waitpid($pid, 0);
103
104         ($? || $$out eq '') ? 0 : 1;
105 }
106
107 sub index_sync {
108         my ($git_dir) = @_;
109         eval {
110                 require PublicInbox::SearchIdx;
111                 PublicInbox::SearchIdx->new($git_dir, 2)->index_sync;
112         };
113 }