2 # Copyright (C) 2014-2015 all contributors <meta@public-inbox.org>
3 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
5 # Used for training spam (via SpamAssassin) and removing messages from a
7 my $usage = "$0 (spam|ham) < /path/to/message";
10 use PublicInbox::Config;
14 my $train = shift or die "usage: $usage\n";
15 if ($train !~ /\A(?:ham|spam)\z/) {
16 die "`$train' not recognized.\nusage: $usage\n";
19 my $pi_config = PublicInbox::Config->new;
20 my $mime = Email::MIME->new(eval { local $/; <> });
24 foreach my $h (qw(Cc To)) {
25 foreach my $recipient (Email::Address->parse($mime->header($h))) {
26 $dests{lc($recipient->address)} = 1;
30 my ($name, $email, $date);
32 if ($train eq "ham") {
33 require PublicInbox::MDA;
34 require PublicInbox::Filter;
35 PublicInbox::Filter->run($mime);
36 ($name, $email, $date) = PublicInbox::MDA->author_info($mime);
39 my $in = $mime->as_string;
41 my @output = qw(> /dev/null > /dev/null);
43 # n.b. message may be cross-posted to multiple public-inboxes
44 foreach my $recipient (keys %dests) {
45 my $dst = $pi_config->lookup($recipient) or next;
46 my $git_dir = $dst->{mainrepo} or next;
47 my ($out, $err) = ("", "");
49 # We do not touch GIT_COMMITTER_* env here so we can track
50 # who trained the message.
51 # We will not touch GIT_AUTHOR_* when learning spam messages, either
52 if ($train eq "spam") {
53 # This needs to be idempotent, as my inotify trainer
54 # may train for each cross-posted message, and this
55 # script already learns for every list in
56 # ~/.public-inbox/config
57 if (!run(["ssoma-rm", $git_dir], \$in, \$out, \$err)) {
58 if ($err !~ /^git cat-file .+ failed: 32768$/) {
62 } else { # $train eq "ham"
63 # no checking for spam here, we assume the message has
64 # been reviewed by a human at this point:
65 PublicInbox::MDA->set_list_headers($mime, $dst);
66 my $s = $mime->as_string;
68 local $ENV{GIT_AUTHOR_NAME} = $name;
69 local $ENV{GIT_AUTHOR_EMAIL} = $email;
70 local $ENV{GIT_AUTHOR_DATE} = $date;
72 # Ham messages are trained when they're marked into
73 # a SEEN state, so this is idempotent:
74 run([PublicInbox::MDA->cmd, $git_dir], \$s, \$out, \$err);
75 if ($err !~ /CONFLICT/) {
79 if (!run([qw(spamc -L), $train], \$in, @output)) {
84 require PublicInbox::SearchIdx;
85 PublicInbox::SearchIdx->new($git_dir, 2)->index_sync;