]> Sergey Matveev's repositories - public-inbox.git/blob - public-inbox-mda
remove failrepo config
[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 File::Path::Expand qw/expand_filename/;
11 use IPC::Run qw(run);
12 use constant MDA => 'ssoma-mda';
13 use PublicInbox;
14 use PublicInbox::Filter;
15 use PublicInbox::Config;
16
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);
22
23 # this reads the message from stdin
24 my $filter = Email::Filter->new(emergency => $failbox);
25 my $config = PublicInbox::Config->new;
26
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
33
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);
38         $filtered = undef;
39         $filter->simple($simple);
40
41         if (PublicInbox::Filter->run($simple)) {
42                 # run spamc again on the HTML-free message
43                 if (do_spamc($simple, \$filtered)) {
44                         $filter->simple(Email::Simple->new($filtered));
45                         $filter->pipe(MDA, $main_repo);
46                 }
47         }
48 }
49 exit 0; # goes to failbox
50
51 # we depend on "report_safe 0" in /etc/spamassassin/*.cf with --headers
52 # not using Email::Filter->pipe here since we want the stdout of
53 # the command even on failure (spamc will set $? on error).
54 sub do_spamc {
55         my ($simple, $out) = @_;
56         eval {
57                 my $orig = $simple->as_string;
58                 run([qw/spamc -E --headers/], \$orig, $out);
59         };
60
61         return ($@ || $? || !defined($$out) || length($$out) == 0) ? 0 : 1;
62 }