]> Sergey Matveev's repositories - public-inbox.git/blob - public-inbox-learn
add spam/ham learning wrapper script
[public-inbox.git] / public-inbox-learn
1 #!/usr/bin/perl -w
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";
5 use strict;
6 use warnings;
7 use PublicInbox::Config;
8 use Email::Simple;
9 use Email::Address;
10 use IPC::Run qw/run/;
11 my $train = shift or die "usage: $usage\n";
12 if ($train !~ /\A(?:ham|spam)\z/) {
13         die "`$train' not recognized.\nusage: $usage\n";
14 }
15
16 my $pi_config = PublicInbox::Config->new;
17 my $simple;
18 {
19         local $/;
20         $simple = Email::Simple->new(<>);
21 }
22
23 # get all recipients
24 my %dests;
25 foreach my $h (qw(Cc To)) {
26         foreach my $recipient (Email::Address->parse($simple->header($h))) {
27                 $dests{lc($recipient->address)} = 1;
28         }
29 }
30
31 my $in = $simple->as_string;
32 $simple->body_set("");
33
34 my $err = 0;
35 my @output = qw(> /dev/null > /dev/null);
36
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) = ("", "");
42
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$/) {
53                                 $err = 1;
54                         }
55                 }
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");
66
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/) {
71                         $err = 1;
72                 }
73         }
74         if (!run([qw(spamc -L), $train], \$in, @output)) {
75                 $err = 1;
76         }
77 }
78 exit $err;