]> Sergey Matveev's repositories - public-inbox.git/blob - public-inbox-learn
0c7b4199abeea2def7b452101ce83e4e24b9b7a1
[public-inbox.git] / public-inbox-learn
1 #!/usr/bin/perl -w
2 # Copyright (C) 2014-2015 all contributors <meta@public-inbox.org>
3 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
4 #
5 # Used for training spam (via SpamAssassin) and removing messages from a
6 # public-inbox
7 my $usage = "$0 (spam|ham) < /path/to/message";
8 use strict;
9 use warnings;
10 use PublicInbox::Config;
11 use Email::MIME;
12 use Email::Address;
13 use IPC::Run qw/run/;
14 my $train = shift or die "usage: $usage\n";
15 if ($train !~ /\A(?:ham|spam)\z/) {
16         die "`$train' not recognized.\nusage: $usage\n";
17 }
18
19 my $pi_config = PublicInbox::Config->new;
20 my $mime = Email::MIME->new(eval { local $/; <> });
21
22 # get all recipients
23 my %dests;
24 foreach my $h (qw(Cc To)) {
25         foreach my $recipient (Email::Address->parse($mime->header($h))) {
26                 $dests{lc($recipient->address)} = 1;
27         }
28 }
29
30 my ($name, $email, $date);
31
32 if ($train eq "ham") {
33         require PublicInbox::MDA;
34         require PublicInbox::Filter;
35         PublicInbox::Filter->run($mime);
36         ($name, $email, $date) = PublicInbox::MDA->author_info($mime);
37 }
38
39 my $in = $mime->as_string;
40 my $err = 0;
41 my @output = qw(> /dev/null > /dev/null);
42
43 # n.b. message may be cross-posted to multiple public-inboxes
44 foreach my $recipient (keys %dests) {
45         my $dst = $pi_config->lookup($recipient) or next;
46         my $git_dir = $dst->{mainrepo} or next;
47         my ($out, $err) = ("", "");
48
49         # We do not touch GIT_COMMITTER_* env here so we can track
50         # who trained the message.
51         # We will not touch GIT_AUTHOR_* when learning spam messages, either
52         if ($train eq "spam") {
53                 # This needs to be idempotent, as my inotify trainer
54                 # may train for each cross-posted message, and this
55                 # script already learns for every list in
56                 # ~/.public-inbox/config
57                 if (!run(["ssoma-rm", $git_dir], \$in, \$out, \$err)) {
58                         if ($err !~ /^git cat-file .+ failed: 32768$/) {
59                                 $err = 1;
60                         }
61                 }
62         } else { # $train eq "ham"
63                 # no checking for spam here, we assume the message has
64                 # been reviewed by a human at this point:
65                 PublicInbox::MDA->set_list_headers($mime, $dst);
66                 my $s  = $mime->as_string;
67
68                 local $ENV{GIT_AUTHOR_NAME} = $name;
69                 local $ENV{GIT_AUTHOR_EMAIL} = $email;
70                 local $ENV{GIT_AUTHOR_DATE} = $date;
71
72                 # Ham messages are trained when they're marked into
73                 # a SEEN state, so this is idempotent:
74                 run([PublicInbox::MDA->cmd, $git_dir], \$s, \$out, \$err);
75                 if ($err !~ /CONFLICT/) {
76                         $err = 1;
77                 }
78         }
79         if (!run([qw(spamc -L), $train], \$in, @output)) {
80                 $err = 1;
81         }
82
83         $err or eval {
84                 require PublicInbox::SearchIdx;
85                 PublicInbox::SearchIdx->new($git_dir, 2)->index_sync;
86         };
87 }
88
89 exit $err;