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 set_list_headers($simple, $dst);
46 $filter->simple($simple);
48 my $from = $filter->from;
49 my @from = Email::Address->parse($from);
50 my $name = $from[0]->name;
51 defined $name or $name = "";
52 my $email = $from[0]->address;
53 defined $email or $email = "";
54 local $ENV{GIT_AUTHOR_NAME} = $name;
55 local $ENV{GIT_AUTHOR_EMAIL} = $email;
56 local $ENV{GIT_AUTHOR_DATE} = $simple->header("Date");
57 local $ENV{GIT_COMMITTER_EMAIL} = $recipient;
58 local $ENV{GIT_COMMITTER_NAME} = $dst->{listname};
60 $filter->pipe(MDA, '-1', $main_repo);
64 exit 0; # goes to failbox
66 # we depend on "report_safe 0" in /etc/spamassassin/*.cf with --headers
67 # not using Email::Filter->pipe here since we want the stdout of
68 # the command even on failure (spamc will set $? on error).
70 my ($simple, $out) = @_;
72 my $orig = $simple->as_string;
73 run([qw/spamc -E --headers/], \$orig, $out);
76 return ($@ || $? || !defined($$out) || length($$out) == 0) ? 0 : 1;
80 sub set_list_headers {
81 my ($simple, $dst) = @_;
82 my $pa = "<$dst->{-primary_address}>";
83 $simple->header_set("List-Id", $pa);
84 $simple->header_set("List-Post", $pa);
86 my $url = $dst->{url};
88 $simple->header_set("List-Archive", "<$url>");
89 $simple->header_set("List-Help", "<${url}help>");