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';
10 use Encode qw/decode encode/;
11 use Encode::MIME::Header;
12 use File::Path::Expand qw/expand_filename/;
15 use PublicInbox::Filter;
16 use PublicInbox::Config;
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);
24 # this reads the message from stdin
25 my $filter = Email::Filter->new(emergency => $emergency);
26 my $config = PublicInbox::Config->new;
28 my $recipient = $ENV{RECIPIENT};
29 defined $recipient or die "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
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);
40 $filter->simple($simple);
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);
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};
57 $filter->pipe(PublicInbox::MDA->cmd, $main_repo);
61 exit 0; # goes to emergency
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).
67 my ($simple, $out) = @_;
69 my $orig = $simple->as_string;
70 run([qw/spamc -E --headers/], \$orig, $out);
73 return ($@ || $? || !defined($$out) || length($$out) == 0) ? 0 : 1;