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);
52 search_index_sync($main_repo) if ($? == 0);
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};
61 $filter->pipe(PublicInbox::MDA->cmd, $main_repo);
65 # Ensure emergency spam gets spamassassin headers.
66 # This makes it easier to prioritize obvious spam from less obvious
67 if (defined($filtered) && length($filtered)) {
68 my $drop = Email::MIME->new(\$filtered);
70 $filter->simple($drop);
73 exit 0; # goes to emergency
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).
81 my $orig = $msg->as_string;
82 run([qw/spamc -E --headers/], \$orig, $out);
85 return ($@ || $? || !defined($$out) || length($$out) == 0) ? 0 : 1;
88 sub search_index_sync {
91 require PublicInbox::Search;
92 umask 0022; # XXX FIXME use git config core.sharedRepository
93 my $s = PublicInbox::SearchIdx->new($git_dir, 2);