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>
5 # Used for training spam (via SpamAssassin) and removing messages from a
8 usage: public-inbox-learn [OPTIONS] [spam|ham|rm] </path/to/RFC2822_message
10 required action argument:
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
18 --all scan all inboxes on `rm'
20 See public-inbox-learn(1) man page for full documentation.
23 use PublicInbox::Config;
24 use PublicInbox::InboxWritable;
26 use PublicInbox::Address;
27 use PublicInbox::Spamcheck::Spamc;
28 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
30 GetOptions(\%opt, qw(all help|h)) or die $help;
32 my $train = shift or die $help;
33 if ($train !~ /\A(?:ham|spam|rm)\z/) {
34 die "`$train' not recognized.\n$help";
36 die "--all only works with `rm'\n" if $opt{all} && $train ne 'rm';
38 my $spamc = PublicInbox::Spamcheck::Spamc->new;
39 my $pi_cfg = PublicInbox::Config->new;
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;
47 if ($train eq 'ham') {
48 $spamc->hamlearn(\$data);
49 } elsif ($train eq 'spam') {
50 $spamc->spamlearn(\$data);
52 die "spamc failed with: $?\n" if $?;
59 sub remove_or_add ($$$$) {
60 my ($ibx, $train, $mime, $addr) = @_;
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);
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);
80 # Ham messages are trained when they're marked into
81 # a SEEN state, so this is idempotent:
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 {
91 $ibx = PublicInbox::InboxWritable->new($ibx);
92 my $im = $ibx->importer(0);
93 $im->remove($mime, $train);
97 require PublicInbox::MDA;
100 my %dests; # address => <PublicInbox::Inbox|0(false)>
101 for ($mime->header('Cc'), $mime->header('To')) {
102 foreach my $addr (PublicInbox::Address::emails($_)) {
104 $dests{$addr} //= $pi_cfg->lookup($addr) // 0;
108 # n.b. message may be cross-posted to multiple public-inboxes
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);
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});