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 File::Path::Expand qw/expand_filename/;
12 use constant MDA => 'ssoma-mda';
14 use PublicInbox::Filter;
15 use PublicInbox::Config;
17 # n.b: hopefully we can setup the failbox 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 $failbox = $ENV{PI_FAILBOX} || '~/public-inbox-fail.mbox';
21 $failbox = expand_filename($failbox);
23 # this reads the message from stdin
24 my $filter = Email::Filter->new(emergency => $failbox);
25 my $config = PublicInbox::Config->new;
27 my $recipient = $ENV{RECIPIENT};
28 defined $recipient or die "RECIPIENT not defined in ENV\n";
29 my $dst = $config->lookup($recipient);
30 defined $dst or exit(1);
31 my $main_repo = $dst->{mainrepo} or exit(1);
32 my $filtered; # string dest
34 if (PublicInbox->precheck($filter, $recipient) &&
35 do_spamc($filter->simple, \$filtered)) {
36 # update our message with SA headers (in case our filter rejects it)
37 my $simple = Email::Simple->new($filtered);
39 $filter->simple($simple);
41 if (PublicInbox::Filter->run($simple)) {
42 # run spamc again on the HTML-free message
43 if (do_spamc($simple, \$filtered)) {
44 $simple = Email::Simple->new($filtered);
45 $filter->simple($simple);
47 my $from = $filter->from;
48 my @from = Email::Address->parse($from);
49 my $name = $from[0]->name;
50 defined $name or $name = "";
51 my $email = $from[0]->address;
52 defined $email or $email = "";
53 local $ENV{GIT_AUTHOR_NAME} = $name;
54 local $ENV{GIT_AUTHOR_EMAIL} = $email;
55 local $ENV{GIT_AUTHOR_DATE} = $simple->header("Date");
56 local $ENV{GIT_COMMITTER_EMAIL} = $recipient;
57 local $ENV{GIT_COMMITTER_NAME} = $dst->{listname};
59 $filter->pipe(MDA, '-1', $main_repo);
63 exit 0; # goes to failbox
65 # we depend on "report_safe 0" in /etc/spamassassin/*.cf with --headers
66 # not using Email::Filter->pipe here since we want the stdout of
67 # the command even on failure (spamc will set $? on error).
69 my ($simple, $out) = @_;
71 my $orig = $simple->as_string;
72 run([qw/spamc -E --headers/], \$orig, $out);
75 return ($@ || $? || !defined($$out) || length($$out) == 0) ? 0 : 1;