+# spam is removed from all known inboxes since it is often Bcc:-ed
+if ($train eq 'spam' || ($train eq 'rm' && $opt{all})) {
+ $pi_cfg->each_inbox(sub {
+ my ($ibx) = @_;
+ $ibx = PublicInbox::InboxWritable->new($ibx);
+ my $im = $ibx->importer(0);
+ $im->remove($mime, $train);
+ $im->done;
+ });
+} else {
+ require PublicInbox::MDA;
+
+ # get all recipients
+ my %dests; # address => <PublicInbox::Inbox|0(false)>
+ for ($mime->header('Cc'), $mime->header('To')) {
+ foreach my $addr (PublicInbox::Address::emails($_)) {
+ $addr = lc($addr);
+ $dests{$addr} //= $pi_cfg->lookup($addr) // 0;
+ }
+ }
+
+ # 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
+ next if $seen{"$ibx"}++;
+ remove_or_add($ibx, $train, $mime, $addr);
+ }
+ my $dests = PublicInbox::MDA->inboxes_for_list_id($pi_cfg, $mime);
+ for my $ibx (@$dests) {
+ next if $seen{"$ibx"}++;
+ remove_or_add($ibx, $train, $mime, $ibx->{-primary_address});
+ }
+}
+