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)
6 my $usage = 'public-inbox-mda < rfc2822_message';
11 use File::Path::Expand qw/expand_filename/;
14 use PublicInbox::Filter;
15 use PublicInbox::Config;
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);
23 # this reads the message from stdin
24 my $filter = Email::Filter->new(emergency => $emergency);
25 my $config = PublicInbox::Config->new;
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
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);
39 $filter->simple($msg);
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);
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};
56 $filter->pipe(PublicInbox::MDA->cmd, $main_repo);
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);
65 $filter->simple($drop);
68 exit 0; # goes to emergency
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).
76 my $orig = $msg->as_string;
77 run([qw/spamc -E --headers/], \$orig, $out);
80 return ($@ || $? || !defined($$out) || length($$out) == 0) ? 0 : 1;