]> Sergey Matveev's repositories - public-inbox.git/blob - public-inbox-mda
mda: encoding-aware From: for GIT_ authorship
[public-inbox.git] / public-inbox-mda
1 #!/usr/bin/perl -w
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)
4 use strict;
5 use warnings;
6 my $usage = 'public-inbox-mda < rfc2822_message';
7
8 use Email::Filter;
9 use Email::Address;
10 use Encode qw/decode encode/;
11 use Encode::MIME::Header;
12 use File::Path::Expand qw/expand_filename/;
13 use IPC::Run qw(run);
14 use constant MDA => 'ssoma-mda';
15 use PublicInbox;
16 use PublicInbox::Filter;
17 use PublicInbox::Config;
18
19 # n.b: hopefully we can setup the failbox path without bailing due to
20 # user error, we really want to setup the emergency destination ASAP
21 # in case there's bugs in our code or user error.
22 my $failbox = $ENV{PI_FAILBOX} || '~/public-inbox-fail.mbox';
23 $failbox = expand_filename($failbox);
24
25 # this reads the message from stdin
26 my $filter = Email::Filter->new(emergency => $failbox);
27 my $config = PublicInbox::Config->new;
28
29 my $recipient = $ENV{RECIPIENT};
30 defined $recipient or die "RECIPIENT not defined in ENV\n";
31 my $dst = $config->lookup($recipient);
32 defined $dst or exit(1);
33 my $main_repo = $dst->{mainrepo} or exit(1);
34 my $filtered; # string dest
35
36 if (PublicInbox->precheck($filter, $recipient) &&
37     do_spamc($filter->simple, \$filtered)) {
38         # update our message with SA headers (in case our filter rejects it)
39         my $simple = Email::Simple->new($filtered);
40         $filtered = undef;
41         $filter->simple($simple);
42
43         if (PublicInbox::Filter->run($simple)) {
44                 # run spamc again on the HTML-free message
45                 if (do_spamc($simple, \$filtered)) {
46                         $simple = Email::Simple->new($filtered);
47                         set_list_headers($simple, $dst);
48                         $filter->simple($simple);
49
50                         my $from = decode('MIME-Header', $filter->from);
51                         $from = encode("utf8", $from);
52                         my @from = Email::Address->parse($from);
53                         my $name = $from[0]->name;
54                         defined $name or $name = "";
55                         my $email = $from[0]->address;
56                         defined $email or $email = "";
57                         local $ENV{GIT_AUTHOR_NAME} = $name;
58                         local $ENV{GIT_AUTHOR_EMAIL} = $email;
59                         local $ENV{GIT_AUTHOR_DATE} = $simple->header("Date");
60                         local $ENV{GIT_COMMITTER_EMAIL} = $recipient;
61                         local $ENV{GIT_COMMITTER_NAME} = $dst->{listname};
62
63                         $filter->pipe(MDA, '-1', $main_repo);
64                 }
65         }
66 }
67 exit 0; # goes to failbox
68
69 # we depend on "report_safe 0" in /etc/spamassassin/*.cf with --headers
70 # not using Email::Filter->pipe here since we want the stdout of
71 # the command even on failure (spamc will set $? on error).
72 sub do_spamc {
73         my ($simple, $out) = @_;
74         eval {
75                 my $orig = $simple->as_string;
76                 run([qw/spamc -E --headers/], \$orig, $out);
77         };
78
79         return ($@ || $? || !defined($$out) || length($$out) == 0) ? 0 : 1;
80 }
81
82 # RFC2919 and RFC2369
83 sub set_list_headers {
84         my ($simple, $dst) = @_;
85         my $pa = "<$dst->{-primary_address}>";
86         $simple->header_set("List-Id", $pa);
87         $simple->header_set("List-Post", $pa);
88
89         my $url = $dst->{url};
90         if (defined $url) {
91                 $simple->header_set("List-Archive", "<$url>");
92                 $simple->header_set("List-Help", "<${url}help>");
93         }
94 }