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