]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiRm.pm
lei rm: new command to remove messages from index
[public-inbox.git] / lib / PublicInbox / LeiRm.pm
1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # implements the "lei rm" command, you can point this at
5 # an entire spam mailbox or read a message from stdin
6 package PublicInbox::LeiRm;
7 use strict;
8 use v5.10.1;
9 use parent qw(PublicInbox::IPC PublicInbox::LeiInput);
10
11 sub input_eml_cb { # used by PublicInbox::LeiInput::input_fh
12         my ($self, $eml) = @_;
13         $self->{lei}->{sto}->ipc_do('remove_eml', $eml);
14 }
15
16 sub input_mbox_cb { # MboxReader callback
17         my ($eml, $self) = @_;
18         input_eml_cb($self, $eml);
19 }
20
21 sub input_net_cb { # callback for ->imap_each, ->nntp_each
22         my (undef, undef, $kw, $eml, $self) = @_; # @_[0,1]: url + uid ignored
23         input_eml_cb($self, $eml);
24 }
25
26 sub input_maildir_cb {
27         my (undef, $kw, $eml, $self) = @_; # $_[0] $filename ignored
28         input_eml_cb($self, $eml);
29 }
30
31 sub lei_rm {
32         my ($lei, @inputs) = @_;
33         $lei->_lei_store(1)->write_prepare($lei);
34         $lei->{opt}->{stdin} = 1 if !@inputs;
35         $lei->{opt}->{'in-format'} //= 'eml';
36         my $self = bless { -wq_nr_workers => 1 }, __PACKAGE__;
37         $self->prepare_inputs($lei, \@inputs) or return;
38         my ($op_c, $ops) = $lei->workers_start($self, 1);
39         $lei->{wq1} = $self;
40         $lei->{-err_type} = 'non-fatal';
41         net_merge_all_done($self) unless $lei->{auth};
42         $op_c->op_wait_event($ops);
43 }
44
45 no warnings 'once';
46 *ipc_atfork_child = \&PublicInbox::LeiInput::input_only_atfork_child;
47 *net_merge_all_done = \&PublicInbox::LeiInput::input_only_net_merge_all_done;
48 *net_merge_all = \&PublicInbox::LeiAuth::net_merge_all;
49
50 1;