2 # Copyright (C) 2014, Eric Wong <normalperson@yhbt.net> and all contributors
3 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
4 my $usage = "$0 (spam|ham) < /path/to/message";
7 use PublicInbox::Config;
11 my $train = shift or die "usage: $usage\n";
12 if ($train !~ /\A(?:ham|spam)\z/) {
13 die "`$train' not recognized.\nusage: $usage\n";
16 my $pi_config = PublicInbox::Config->new;
17 my $mime = Email::MIME->new(eval { local $/; <> });
21 foreach my $h (qw(Cc To)) {
22 foreach my $recipient (Email::Address->parse($mime->header($h))) {
23 $dests{lc($recipient->address)} = 1;
27 my $in = $mime->as_string;
31 my @output = qw(> /dev/null > /dev/null);
33 # n.b. message may be cross-posted to multiple public-inboxes
34 foreach my $recipient (keys %dests) {
35 my $dst = $pi_config->lookup($recipient) or next;
36 my $git_dir = $dst->{mainrepo} or next;
37 my ($out, $err) = ("", "");
39 # We do not touch GIT_COMMITTER_* env here so we can track
40 # who trained the message.
41 # We will not touch GIT_AUTHOR_* when learning spam messages, either
42 if ($train eq "spam") {
43 # This needs to be idempotent, as my inotify trainer
44 # may train for each cross-posted message, and this
45 # script already learns for every list in
46 # ~/.public-inbox/config
47 if (!run(["ssoma-rm", $git_dir], \$in, \$out, \$err)) {
48 if ($err !~ /^git cat-file .+ failed: 32768$/) {
52 } else { # $train eq "ham"
53 require PublicInbox::MDA;
54 require PublicInbox::Filter;
56 # no checking for errors here, we assume the message has
57 # been reviewed by a human at this point:
58 PublicInbox::Filter->run($mime);
60 my ($name, $email, $date) =
61 PublicInbox::MDA->author_info($mime);
62 local $ENV{GIT_AUTHOR_NAME} = $name;
63 local $ENV{GIT_AUTHOR_EMAIL} = $email;
64 local $ENV{GIT_AUTHOR_DATE} = $date;
66 # Ham messages are trained when they're marked into
67 # a SEEN state, so this is idempotent:
68 run([PublicInbox::MDA->cmd, $git_dir], \$in, \$out, \$err);
69 if ($err !~ /CONFLICT/) {
73 if (!run([qw(spamc -L), $train], \$in, @output)) {