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 my $from = $simple->header("From");
58 my @from = Email::Address->parse($from);
59 my $name = $from[0]->name;
60 defined $name or $name = "";
61 my $email = $from[0]->address;
62 defined $email or $email = "";
63 local $ENV{GIT_AUTHOR_NAME} = $name;
64 local $ENV{GIT_AUTHOR_EMAIL} = $email;
65 local $ENV{GIT_AUTHOR_DATE} = $simple->header("Date");
67 # Ham messages are trained when they're marked into
68 # a SEEN state, so this is idempotent
69 run([qw(ssoma-mda -1), $git_dir], \$in, \$out, \$err);
70 if ($err !~ /CONFLICT/) {
74 if (!run([qw(spamc -L), $train], \$in, @output)) {