]> Sergey Matveev's repositories - public-inbox.git/blob - public-inbox-mda
terminology: replies => followups
[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::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                         local $ENV{GIT_AUTHOR_NAME} = $name;
51                         local $ENV{GIT_AUTHOR_EMAIL} = $email;
52                         local $ENV{GIT_AUTHOR_DATE} = $date;
53                         local $ENV{GIT_COMMITTER_EMAIL} = $recipient;
54                         local $ENV{GIT_COMMITTER_NAME} = $dst->{listname};
55
56                         $filter->pipe(PublicInbox::MDA->cmd, $main_repo);
57                 }
58         }
59 } else {
60         # Ensure emergency spam gets spamassassin headers.
61         # This makes it easier to prioritize obvious spam from less obvious
62         if (defined($filtered) && length($filtered)) {
63                 my $drop = Email::MIME->new(\$filtered);
64                 $filtered = undef;
65                 $filter->simple($drop);
66         }
67 }
68 exit 0; # goes to emergency
69
70 # we depend on "report_safe 0" in /etc/spamassassin/*.cf with --headers
71 # not using Email::Filter->pipe here since we want the stdout of
72 # the command even on failure (spamc will set $? on error).
73 sub do_spamc {
74         my ($msg, $out) = @_;
75         eval {
76                 my $orig = $msg->as_string;
77                 run([qw/spamc -E --headers/], \$orig, $out);
78         };
79
80         return ($@ || $? || !defined($$out) || length($$out) == 0) ? 0 : 1;
81 }