]> Sergey Matveev's repositories - public-inbox.git/blob - public-inbox-mda
update copyright headers and email addresses
[public-inbox.git] / 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 use strict;
5 use warnings;
6 my $usage = 'public-inbox-mda < rfc2822_message';
7
8 use Email::Filter;
9 use Email::MIME;
10 use Email::Address;
11 use File::Path::Expand qw/expand_filename/;
12 use IPC::Run qw(run);
13 use PublicInbox::MDA;
14 use PublicInbox::Filter;
15 use PublicInbox::Config;
16
17 # n.b: hopefully we can setup the emergency path without bailing due to
18 # user error, we really want to setup the emergency destination ASAP
19 # in case there's bugs in our code or user error.
20 my $emergency = $ENV{PI_EMERGENCY} || '~/.public-inbox/emergency/';
21 $emergency = expand_filename($emergency);
22
23 # this reads the message from stdin
24 my $filter = Email::Filter->new(emergency => $emergency);
25 my $config = PublicInbox::Config->new;
26
27 my $recipient = $ENV{ORIGINAL_RECIPIENT};
28 defined $recipient or die "ORIGINAL_RECIPIENT not defined in ENV\n";
29 my $dst = $config->lookup($recipient); # first check
30 defined $dst or exit(1);
31 my $main_repo = $dst->{mainrepo} or exit(1);
32 my $filtered; # string dest
33
34 if (PublicInbox::MDA->precheck($filter, $dst->{address}) &&
35     do_spamc($filter->simple, \$filtered)) {
36         # update our message with SA headers (in case our filter rejects it)
37         my $msg = Email::MIME->new(\$filtered);
38         $filtered = undef;
39         $filter->simple($msg);
40
41         if (PublicInbox::Filter->run($msg, $filter)) {
42                 # run spamc again on the HTML-free message
43                 if (do_spamc($msg, \$filtered)) {
44                         $msg = Email::MIME->new(\$filtered);
45                         PublicInbox::MDA->set_list_headers($msg, $dst);
46                         $filter->simple($msg);
47
48                         my ($name, $email, $date) =
49                                         PublicInbox::MDA->author_info($msg);
50
51                         END {
52                                 search_index_sync($main_repo) if ($? == 0);
53                         };
54
55                         local $ENV{GIT_AUTHOR_NAME} = $name;
56                         local $ENV{GIT_AUTHOR_EMAIL} = $email;
57                         local $ENV{GIT_AUTHOR_DATE} = $date;
58                         local $ENV{GIT_COMMITTER_EMAIL} = $recipient;
59                         local $ENV{GIT_COMMITTER_NAME} = $dst->{listname};
60
61                         $filter->pipe(PublicInbox::MDA->cmd, $main_repo);
62                 }
63         }
64 } else {
65         # Ensure emergency spam gets spamassassin headers.
66         # This makes it easier to prioritize obvious spam from less obvious
67         if (defined($filtered) && $filtered ne '') {
68                 my $drop = Email::MIME->new(\$filtered);
69                 $filtered = undef;
70                 $filter->simple($drop);
71         }
72 }
73 exit 0; # goes to emergency
74
75 # we depend on "report_safe 0" in /etc/spamassassin/*.cf with --headers
76 # not using Email::Filter->pipe here since we want the stdout of
77 # the command even on failure (spamc will set $? on error).
78 sub do_spamc {
79         my ($msg, $out) = @_;
80         eval {
81                 my $orig = $msg->as_string;
82                 run([qw/spamc -E --headers/], \$orig, $out);
83         };
84
85         return ($@ || $? || !defined($$out) || $$out eq '') ? 0 : 1;
86 }
87
88 sub search_index_sync {
89         my ($git_dir) = @_;
90         eval {
91                 require PublicInbox::SearchIdx;
92                 PublicInbox::SearchIdx->new($git_dir, 2)->index_sync;
93         };
94 }