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