]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-learn
38c83243f8971e63859fe4261b7ba4d521ff2335
[public-inbox.git] / script / 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 PublicInbox::Git;
12 use PublicInbox::Import;
13 use PublicInbox::MIME;
14 use Email::MIME::ContentType;
15 $Email::MIME::ContentType::STRICT_PARAMS = 0; # user input is imperfect
16 use PublicInbox::Address;
17 use PublicInbox::Spamcheck::Spamc;
18 my $train = shift or die "usage: $usage\n";
19 if ($train !~ /\A(?:ham|spam|rm)\z/) {
20         die "`$train' not recognized.\nusage: $usage\n";
21 }
22
23 my $spamc = PublicInbox::Spamcheck::Spamc->new;
24 my $pi_config = PublicInbox::Config->new;
25 my $err;
26 my $mime = PublicInbox::MIME->new(eval {
27         local $/;
28         my $data = scalar <STDIN>;
29         $data =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
30
31         if ($train ne 'rm') {
32                 eval {
33                         if ($train eq 'ham') {
34                                 $spamc->hamlearn(\$data);
35                         } elsif ($train eq 'spam') {
36                                 $spamc->spamlearn(\$data);
37                         }
38                         die "spamc failed with: $?\n" if $?;
39                 };
40                 $err = $@;
41         }
42         $data
43 });
44
45 # get all recipients
46 my %dests;
47 foreach my $h (qw(Cc To)) {
48         my $val = $mime->header($h) or next;
49         foreach my $email (PublicInbox::Address::emails($val)) {
50                 $dests{lc($email)} = 1;
51         }
52 }
53
54 require PublicInbox::MDA if $train eq "ham";
55
56 # n.b. message may be cross-posted to multiple public-inboxes
57 foreach my $recipient (keys %dests) {
58         my $dst = $pi_config->lookup($recipient) or next;
59         my $git_dir = $dst->{mainrepo} or next;
60         my $git = PublicInbox::Git->new($git_dir);
61         # We do not touch GIT_COMMITTER_* env here so we can track
62         # who trained the message.
63         my $name = $ENV{GIT_COMMITTER_NAME} || $dst->{name};
64         my $email = $ENV{GIT_COMMITTER_EMAIL} || $recipient;
65         my $im = PublicInbox::Import->new($git, $name, $email);
66
67         if ($train eq "spam" || $train eq "rm") {
68                 # This needs to be idempotent, as my inotify trainer
69                 # may train for each cross-posted message, and this
70                 # script already learns for every list in
71                 # ~/.public-inbox/config
72                 $im->remove($mime);
73         } else { # $train eq "ham"
74                 # no checking for spam here, we assume the message has
75                 # been reviewed by a human at this point:
76                 PublicInbox::MDA->set_list_headers($mime, $dst);
77
78                 # Ham messages are trained when they're marked into
79                 # a SEEN state, so this is idempotent:
80                 $im->add($mime);
81         }
82         $im->done;
83 }
84
85 if ($err) {
86         warn $err;
87         exit 1;
88 }