]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-mda
bb78c4e605ff72fa13619e1d7e1206a5c88f9925
[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
10 use Email::Filter;
11 use Email::MIME;
12 use Email::MIME::ContentType;
13 $Email::MIME::ContentType::STRICT_PARAMS = 0; # user input is imperfect
14 use File::Path::Expand qw/expand_filename/;
15 use IPC::Run qw(run);
16 use PublicInbox::MDA;
17 use PublicInbox::Filter;
18 use PublicInbox::Config;
19 use PublicInbox::Import;
20 use PublicInbox::Git;
21
22 # n.b: hopefully we can setup the emergency path without bailing due to
23 # user error, we really want to setup the emergency destination ASAP
24 # in case there's bugs in our code or user error.
25 my $emergency = $ENV{PI_EMERGENCY} || '~/.public-inbox/emergency/';
26 $emergency = expand_filename($emergency);
27
28 # this reads the message from stdin
29 my $filter = Email::Filter->new(emergency => $emergency);
30 my $config = PublicInbox::Config->new;
31
32 my $recipient = $ENV{ORIGINAL_RECIPIENT};
33 defined $recipient or die "ORIGINAL_RECIPIENT not defined in ENV\n";
34 my $dst = $config->lookup($recipient); # first check
35 defined $dst or exit(1);
36 my $main_repo = $dst->{mainrepo} or exit(1);
37 my $filtered; # string dest
38
39 if (PublicInbox::MDA->precheck($filter, $dst->{address}) &&
40     do_spamc($filter->simple, \$filtered)) {
41         # update our message with SA headers (in case our filter rejects it)
42         my $msg = Email::MIME->new(\$filtered);
43         $filtered = undef;
44         $filter->simple($msg);
45
46         my $filter_arg;
47         my $fcfg = $dst->{filter};
48         if (!defined $fcfg || $filter eq 'reject') {
49                 $filter_arg = $filter;
50         } elsif ($fcfg eq 'scrub') {
51                 $filter_arg = undef; # the default for legacy versions
52         } else {
53                 warn "publicinbox.$dst->{name}.filter=$fcfg invalid\n";
54                 warn "must be either 'scrub' or 'reject' (the default)\n";
55         }
56
57         if (PublicInbox::Filter->run($msg, $filter_arg)) {
58                 # run spamc again on the HTML-free message
59                 if (do_spamc($msg, \$filtered)) {
60                         $msg = Email::MIME->new(\$filtered);
61                         PublicInbox::MDA->set_list_headers($msg, $dst);
62                         $filter->simple($msg);
63
64                         END {
65                                 index_sync($main_repo) if ($? == 0);
66                         };
67                         my $git = PublicInbox::Git->new($main_repo);
68                         my $im = PublicInbox::Import->new($git,
69                                                 $dst->{name}, $recipient);
70                         if (defined $im->add($msg)) {
71                                 $im->done;
72                                 $filter->ignore; # exits
73                         }
74                         # this message is similar to what ssoma-mda shows:
75                         print STDERR "CONFLICT: Message-ID: ",
76                                 $msg->header_obj->header_raw('Message-ID'),
77                                 " exists\n";
78                 }
79         }
80 } else {
81         # Ensure emergency spam gets spamassassin headers.
82         # This makes it easier to prioritize obvious spam from less obvious
83         if (defined($filtered) && $filtered ne '') {
84                 my $drop = Email::MIME->new(\$filtered);
85                 $filtered = undef;
86                 $filter->simple($drop);
87         }
88 }
89 exit 0; # goes to emergency
90
91 # we depend on "report_safe 0" in /etc/spamassassin/*.cf with --headers
92 # not using Email::Filter->pipe here since we want the stdout of
93 # the command even on failure (spamc will set $? on error).
94 sub do_spamc {
95         my ($msg, $out) = @_;
96         eval {
97                 my $orig = $msg->as_string;
98                 run([qw/spamc -E --headers/], \$orig, $out);
99         };
100
101         return ($@ || $? || !defined($$out) || $$out eq '') ? 0 : 1;
102 }
103
104 sub index_sync {
105         my ($git_dir) = @_;
106         eval {
107                 require PublicInbox::SearchIdx;
108                 PublicInbox::SearchIdx->new($git_dir, 2)->index_sync;
109         };
110 }