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;
20 $simple = Email::Simple->new(<>);
25 foreach my $h (qw(Cc To)) {
26 foreach my $recipient (Email::Address->parse($simple->header($h))) {
27 $dests{lc($recipient->address)} = 1;
31 my $in = $simple->as_string;
32 $simple->body_set("");
35 my @output = qw(> /dev/null > /dev/null);
37 # n.b. message may be cross-posted to multiple public-inboxes
38 foreach my $recipient (keys %dests) {
39 my $dst = $pi_config->lookup($recipient) or next;
40 my $git_dir = $dst->{mainrepo} or next;
41 my ($out, $err) = ("", "");
43 # We do not touch GIT_COMMITTER_* env here so we can track
44 # who trained the message.
45 # We will not touch GIT_AUTHOR_* when learning spam messages, either
46 if ($train eq "spam") {
47 # This needs to be idempotent, as my inotify trainer
48 # may train for each cross-posted message, and this
49 # script already learns for every list in
50 # ~/.public-inbox/config
51 if (!run(["ssoma-rm", $git_dir], \$in, \$out, \$err)) {
52 if ($err !~ /^git cat-file .+ failed: 32768$/) {
56 } else { # $train eq "ham"
57 require PublicInbox::MDA;
58 require PublicInbox::Filter;
60 # no checking for errors here, we assume the message has
61 # been reviewed by a human at this point:
62 PublicInbox::Filter->run($simple);
64 my ($name, $email, $date) =
65 PublicInbox::MDA->author_info($simple);
66 local $ENV{GIT_AUTHOR_NAME} = $name;
67 local $ENV{GIT_AUTHOR_EMAIL} = $email;
68 local $ENV{GIT_AUTHOR_DATE} = $date;
70 # Ham messages are trained when they're marked into
71 # a SEEN state, so this is idempotent:
72 run([PublicInbox::MDA->cmd, $git_dir], \$in, \$out, \$err);
73 if ($err !~ /CONFLICT/) {
77 if (!run([qw(spamc -L), $train], \$in, @output)) {