]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-learn
5cd08d490bf0273bce85ce4d148bdb09035ad288
[public-inbox.git] / script / public-inbox-learn
1 #!/usr/bin/perl -w
2 # Copyright (C) 2014-2020 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|rm> </path/to/message";
8 use strict;
9 use warnings;
10 use PublicInbox::Config;
11 use PublicInbox::InboxWritable;
12 use PublicInbox::Eml;
13 use PublicInbox::Address;
14 use PublicInbox::Spamcheck::Spamc;
15 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
16 my %opt = (all => 0);
17 GetOptions(\%opt, 'all') or die "bad command-line args\n";
18
19 my $train = shift or die "usage: $usage\n";
20 if ($train !~ /\A(?:ham|spam|rm)\z/) {
21         die "`$train' not recognized.\nusage: $usage\n";
22 }
23 die "--all only works with `rm'\n" if $opt{all} && $train ne 'rm';
24
25 my $spamc = PublicInbox::Spamcheck::Spamc->new;
26 my $pi_config = PublicInbox::Config->new;
27 my $err;
28 my $mime = PublicInbox::Eml->new(do{
29         local $/;
30         my $data = <STDIN>;
31         $data =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
32
33         if ($train ne 'rm') {
34                 eval {
35                         if ($train eq 'ham') {
36                                 $spamc->hamlearn(\$data);
37                         } elsif ($train eq 'spam') {
38                                 $spamc->spamlearn(\$data);
39                         }
40                         die "spamc failed with: $?\n" if $?;
41                 };
42                 $err = $@;
43         }
44         \$data
45 });
46
47 sub remove_or_add ($$$$) {
48         my ($ibx, $train, $mime, $addr) = @_;
49
50         # We do not touch GIT_COMMITTER_* env here so we can track
51         # who trained the message.
52         $ibx->{name} = $ENV{GIT_COMMITTER_NAME} // $ibx->{name};
53         $ibx->{-primary_address} = $ENV{GIT_COMMITTER_EMAIL} // $addr;
54         $ibx = PublicInbox::InboxWritable->new($ibx);
55         my $im = $ibx->importer(0);
56
57         if ($train eq "rm") {
58                 # This needs to be idempotent, as my inotify trainer
59                 # may train for each cross-posted message, and this
60                 # script already learns for every list in
61                 # ~/.public-inbox/config
62                 $im->remove($mime, $train);
63         } elsif ($train eq "ham") {
64                 # no checking for spam here, we assume the message has
65                 # been reviewed by a human at this point:
66                 PublicInbox::MDA->set_list_headers($mime, $ibx);
67
68                 # Ham messages are trained when they're marked into
69                 # a SEEN state, so this is idempotent:
70                 $im->add($mime);
71         }
72         $im->done;
73 }
74
75 # spam is removed from all known inboxes since it is often Bcc:-ed
76 if ($train eq 'spam' || ($train eq 'rm' && $opt{all})) {
77         $pi_config->each_inbox(sub {
78                 my ($ibx) = @_;
79                 $ibx = PublicInbox::InboxWritable->new($ibx);
80                 my $im = $ibx->importer(0);
81                 $im->remove($mime, $train);
82                 $im->done;
83         });
84 } else {
85         require PublicInbox::MDA;
86
87         # get all recipients
88         my %dests; # address => <PublicInbox::Inbox|0(false)>
89         for ($mime->header('Cc'), $mime->header('To')) {
90                 foreach my $addr (PublicInbox::Address::emails($_)) {
91                         $addr = lc($addr);
92                         $dests{$addr} //= $pi_config->lookup($addr) // 0;
93                 }
94         }
95
96         # n.b. message may be cross-posted to multiple public-inboxes
97         my %seen;
98         while (my ($addr, $ibx) = each %dests) {
99                 next unless ref($ibx); # $ibx may be 0
100                 next if $seen{"$ibx"}++;
101                 remove_or_add($ibx, $train, $mime, $addr);
102         }
103         my $dests = PublicInbox::MDA->inboxes_for_list_id($pi_config, $mime);
104         for my $ibx (@$dests) {
105                 next if $seen{"$ibx"}++;
106                 remove_or_add($ibx, $train, $mime, $ibx->{-primary_address});
107         }
108 }
109
110 if ($err) {
111         warn $err;
112         exit 1;
113 }