]> Sergey Matveev's repositories - public-inbox.git/blob - public-inbox-mda
use ORIGINAL_RECIPIENT once again
[public-inbox.git] / public-inbox-mda
1 #!/usr/bin/perl -w
2 # Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors
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::Address;
10 use Encode qw/decode encode/;
11 use Encode::MIME::Header;
12 use File::Path::Expand qw/expand_filename/;
13 use IPC::Run qw(run);
14 use PublicInbox::MDA;
15 use PublicInbox::Filter;
16 use PublicInbox::Config;
17
18 # n.b: hopefully we can setup the emergency path without bailing due to
19 # user error, we really want to setup the emergency destination ASAP
20 # in case there's bugs in our code or user error.
21 my $emergency = $ENV{PI_EMERGENCY} || '~/.public-inbox/emergency/';
22 $emergency = expand_filename($emergency);
23
24 # this reads the message from stdin
25 my $filter = Email::Filter->new(emergency => $emergency);
26 my $config = PublicInbox::Config->new;
27
28 my $recipient = $ENV{ORIGINAL_RECIPIENT};
29 defined $recipient or die "ORIGINAL_RECIPIENT not defined in ENV\n";
30 my $dst = $config->lookup($recipient);
31 defined $dst or exit(1);
32 my $main_repo = $dst->{mainrepo} or exit(1);
33 my $filtered; # string dest
34
35 if (PublicInbox::MDA->precheck($filter, $recipient) &&
36     do_spamc($filter->simple, \$filtered)) {
37         # update our message with SA headers (in case our filter rejects it)
38         my $simple = Email::Simple->new($filtered);
39         $filtered = undef;
40         $filter->simple($simple);
41
42         if (PublicInbox::Filter->run($simple)) {
43                 # run spamc again on the HTML-free message
44                 if (do_spamc($simple, \$filtered)) {
45                         $simple = Email::Simple->new($filtered);
46                         PublicInbox::MDA->set_list_headers($simple, $dst);
47                         $filter->simple($simple);
48
49                         my ($name, $email, $date) =
50                                         PublicInbox::MDA->author_info($simple);
51                         local $ENV{GIT_AUTHOR_NAME} = $name;
52                         local $ENV{GIT_AUTHOR_EMAIL} = $email;
53                         local $ENV{GIT_AUTHOR_DATE} = $date;
54                         local $ENV{GIT_COMMITTER_EMAIL} = $recipient;
55                         local $ENV{GIT_COMMITTER_NAME} = $dst->{listname};
56
57                         $filter->pipe(PublicInbox::MDA->cmd, $main_repo);
58                 }
59         }
60 }
61 exit 0; # goes to emergency
62
63 # we depend on "report_safe 0" in /etc/spamassassin/*.cf with --headers
64 # not using Email::Filter->pipe here since we want the stdout of
65 # the command even on failure (spamc will set $? on error).
66 sub do_spamc {
67         my ($simple, $out) = @_;
68         eval {
69                 my $orig = $simple->as_string;
70                 run([qw/spamc -E --headers/], \$orig, $out);
71         };
72
73         return ($@ || $? || !defined($$out) || length($$out) == 0) ? 0 : 1;
74 }