]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-learn
learn: hoist out remove_or_add subroutine
[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|rm> </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 sub remove_or_add ($$$) {
43         my ($ibx, $train, $addr) = @_;
44
45         # We do not touch GIT_COMMITTER_* env here so we can track
46         # who trained the message.
47         $ibx->{name} = $ENV{GIT_COMMITTER_NAME} // $ibx->{name};
48         $ibx->{-primary_address} = $ENV{GIT_COMMITTER_EMAIL} // $addr;
49         $ibx = PublicInbox::InboxWritable->new($ibx);
50         my $im = $ibx->importer(0);
51
52         if ($train eq "rm") {
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                 $im->remove($mime, $train);
58         } elsif ($train eq "ham") {
59                 # no checking for spam here, we assume the message has
60                 # been reviewed by a human at this point:
61                 PublicInbox::MDA->set_list_headers($mime, $ibx);
62
63                 # Ham messages are trained when they're marked into
64                 # a SEEN state, so this is idempotent:
65                 $im->add($mime);
66         }
67         $im->done;
68 }
69
70 # spam is removed from all known inboxes since it is often Bcc:-ed
71 if ($train eq 'spam') {
72         $pi_config->each_inbox(sub {
73                 my ($ibx) = @_;
74                 $ibx = PublicInbox::InboxWritable->new($ibx);
75                 my $im = $ibx->importer(0);
76                 $im->remove($mime, 'spam');
77                 $im->done;
78         });
79 } else {
80         require PublicInbox::MDA if $train eq "ham";
81
82         # get all recipients
83         my %dests; # address => <PublicInbox::Inbox|0(false)>
84         for ($mime->header('Cc'), $mime->header('To')) {
85                 foreach my $addr (PublicInbox::Address::emails($_)) {
86                         $addr = lc($addr);
87                         $dests{$addr} //= $pi_config->lookup($addr) // 0;
88                 }
89         }
90
91         # n.b. message may be cross-posted to multiple public-inboxes
92         while (my ($addr, $ibx) = each %dests) {
93                 next unless ref($ibx); # $ibx may be 0
94                 remove_or_add($ibx, $train, $addr);
95         }
96 }
97
98 if ($err) {
99         warn $err;
100         exit 1;
101 }