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