]> Sergey Matveev's repositories - public-inbox.git/blob - public-inbox-learn
huge refactor of encoding handling
[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::MIME;
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 $mime = Email::MIME->new(eval { local $/; <> });
18
19 # get all recipients
20 my %dests;
21 foreach my $h (qw(Cc To)) {
22         foreach my $recipient (Email::Address->parse($mime->header($h))) {
23                 $dests{lc($recipient->address)} = 1;
24         }
25 }
26
27 my $in = $mime->as_string;
28 $mime->body_set('');
29
30 my $err = 0;
31 my @output = qw(> /dev/null > /dev/null);
32
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) = ("", "");
38
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$/) {
49                                 $err = 1;
50                         }
51                 }
52         } else { # $train eq "ham"
53                 require PublicInbox::MDA;
54                 require PublicInbox::Filter;
55
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);
59
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;
65
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/) {
70                         $err = 1;
71                 }
72         }
73         if (!run([qw(spamc -L), $train], \$in, @output)) {
74                 $err = 1;
75         }
76 }
77 exit $err;