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>
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{
44 $data =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
48 if ($train eq 'ham') {
49 $spamc->hamlearn(\$data);
50 } elsif ($train eq 'spam') {
51 $spamc->spamlearn(\$data);
53 die "spamc failed with: $?\n" if $?;
60 sub remove_or_add ($$$$) {
61 my ($ibx, $train, $mime, $addr) = @_;
63 # We do not touch GIT_COMMITTER_* env here so we can track
64 # who trained the message.
65 $ibx->{name} = $ENV{GIT_COMMITTER_NAME} // $ibx->{name};
66 $ibx->{-primary_address} = $ENV{GIT_COMMITTER_EMAIL} // $addr;
67 $ibx = PublicInbox::InboxWritable->new($ibx);
68 my $im = $ibx->importer(0);
71 # This needs to be idempotent, as my inotify trainer
72 # may train for each cross-posted message, and this
73 # script already learns for every list in
74 # ~/.public-inbox/config
75 $im->remove($mime, $train);
76 } elsif ($train eq "ham") {
77 # no checking for spam here, we assume the message has
78 # been reviewed by a human at this point:
79 PublicInbox::MDA->set_list_headers($mime, $ibx);
81 # Ham messages are trained when they're marked into
82 # a SEEN state, so this is idempotent:
88 # spam is removed from all known inboxes since it is often Bcc:-ed
89 if ($train eq 'spam' || ($train eq 'rm' && $opt{all})) {
90 $pi_cfg->each_inbox(sub {
92 $ibx = PublicInbox::InboxWritable->new($ibx);
93 my $im = $ibx->importer(0);
94 $im->remove($mime, $train);
98 require PublicInbox::MDA;
101 my %dests; # address => <PublicInbox::Inbox|0(false)>
102 for ($mime->header('Cc'), $mime->header('To')) {
103 foreach my $addr (PublicInbox::Address::emails($_)) {
105 $dests{$addr} //= $pi_cfg->lookup($addr) // 0;
109 # n.b. message may be cross-posted to multiple public-inboxes
111 while (my ($addr, $ibx) = each %dests) {
112 next unless ref($ibx); # $ibx may be 0
113 next if $seen{"$ibx"}++;
114 remove_or_add($ibx, $train, $mime, $addr);
116 my $dests = PublicInbox::MDA->inboxes_for_list_id($pi_cfg, $mime);
117 for my $ibx (@$dests) {
118 next if $seen{"$ibx"}++;
119 remove_or_add($ibx, $train, $mime, $ibx->{-primary_address});