]> Sergey Matveev's repositories - public-inbox.git/blobdiff - script/public-inbox-learn
replace most uses of PublicInbox::MIME with Eml
[public-inbox.git] / script / public-inbox-learn
old mode 100755 (executable)
new mode 100644 (file)
index 56739f8..a33d813
@@ -1,5 +1,5 @@
 #!/usr/bin/perl -w
-# Copyright (C) 2014-2019 all contributors <meta@public-inbox.org>
+# Copyright (C) 2014-2020 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 #
 # Used for training spam (via SpamAssassin) and removing messages from a
@@ -9,7 +9,7 @@ use strict;
 use warnings;
 use PublicInbox::Config;
 use PublicInbox::InboxWritable;
-use PublicInbox::MIME;
+use PublicInbox::Eml;
 use PublicInbox::Address;
 use PublicInbox::Spamcheck::Spamc;
 my $train = shift or die "usage: $usage\n";
@@ -20,9 +20,9 @@ if ($train !~ /\A(?:ham|spam|rm)\z/) {
 my $spamc = PublicInbox::Spamcheck::Spamc->new;
 my $pi_config = PublicInbox::Config->new;
 my $err;
-my $mime = PublicInbox::MIME->new(eval {
+my $mime = PublicInbox::Eml->new(do{
        local $/;
-       my $data = scalar <STDIN>;
+       my $data = <STDIN>;
        $data =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
 
        if ($train ne 'rm') {
@@ -36,11 +36,11 @@ my $mime = PublicInbox::MIME->new(eval {
                };
                $err = $@;
        }
-       $data
+       \$data
 });
 
-sub remove_or_add ($$$) {
-       my ($ibx, $train, $addr) = @_;
+sub remove_or_add ($$$$) {
+       my ($ibx, $train, $mime, $addr) = @_;
 
        # We do not touch GIT_COMMITTER_* env here so we can track
        # who trained the message.
@@ -77,7 +77,7 @@ if ($train eq 'spam') {
                $im->done;
        });
 } else {
-       require PublicInbox::MDA if $train eq "ham";
+       require PublicInbox::MDA;
 
        # get all recipients
        my %dests; # address => <PublicInbox::Inbox|0(false)>
@@ -89,9 +89,16 @@ if ($train eq 'spam') {
        }
 
        # n.b. message may be cross-posted to multiple public-inboxes
+       my %seen;
        while (my ($addr, $ibx) = each %dests) {
                next unless ref($ibx); # $ibx may be 0
-               remove_or_add($ibx, $train, $addr);
+               next if $seen{"$ibx"}++;
+               remove_or_add($ibx, $train, $mime, $addr);
+       }
+       my $dests = PublicInbox::MDA->inboxes_for_list_id($pi_config, $mime);
+       for my $ibx (@$dests) {
+               next if !$seen{"$ibx"}++;
+               remove_or_add($ibx, $train, $mime, $ibx->{-primary_address});
        }
 }