]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiTag.pm
lei rm|tag: drop redundant mbox+net callbacks
[public-inbox.git] / lib / PublicInbox / LeiTag.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 # handles "lei tag" command
5 package PublicInbox::LeiTag;
6 use strict;
7 use v5.10.1;
8 use parent qw(PublicInbox::IPC PublicInbox::LeiInput);
9 use PublicInbox::InboxWritable qw(eml_from_path);
10
11 sub input_eml_cb { # used by PublicInbox::LeiInput::input_fh
12         my ($self, $eml) = @_;
13         if (my $xoids = $self->{lse}->xoids_for($eml) // # tries LeiMailSync
14                         $self->{lei}->{ale}->xoids_for($eml)) {
15                 $self->{lei}->{sto}->wq_do('update_xvmd', $xoids, $eml,
16                                                 $self->{vmd_mod});
17         } else {
18                 ++$self->{unimported};
19         }
20 }
21
22 sub pmdir_cb { # called via wq_io_do from LeiPmdir->each_mdir_fn
23         my ($self, $f) = @_;
24         my $eml = eml_from_path($f) or return;
25         input_eml_cb($self, $eml);
26 }
27
28 sub lei_tag { # the "lei tag" method
29         my ($lei, @argv) = @_;
30         my $sto = $lei->_lei_store(1);
31         $sto->write_prepare($lei);
32         my $self = bless {}, __PACKAGE__;
33         $lei->ale; # refresh and prepare
34         my $vmd_mod = $self->vmd_mod_extract(\@argv);
35         return $lei->fail(join("\n", @{$vmd_mod->{err}})) if $vmd_mod->{err};
36         $self->{vmd_mod} = $vmd_mod; # before LeiPmdir->new in prepare_inputs
37         $self->prepare_inputs($lei, \@argv) or return;
38         grep(defined, @$vmd_mod{qw(+kw +L -L -kw)}) or
39                 return $lei->fail('no keywords or labels specified');
40         my $ops = {};
41         $lei->{auth}->op_merge($ops, $self, $lei) if $lei->{auth};
42         (my $op_c, $ops) = $lei->workers_start($self, 1, $ops);
43         $lei->{wq1} = $self;
44         $lei->{-err_type} = 'non-fatal';
45         net_merge_all_done($self) unless $lei->{auth};
46         $lei->wait_wq_events($op_c, $ops);
47 }
48
49 sub note_unimported {
50         my ($self) = @_;
51         my $n = $self->{unimported} or return;
52         $self->{lei}->{pkt_op_p}->pkt_do('incr', 'unimported', $n);
53 }
54
55 sub ipc_atfork_child {
56         my ($self) = @_;
57         PublicInbox::LeiInput::input_only_atfork_child($self);
58         $self->{lse} = $self->{lei}->{sto}->search;
59         # this goes out-of-scope at worker process exit:
60         PublicInbox::OnDestroy->new($$, \&note_unimported, $self);
61 }
62
63 # Workaround bash word-splitting s to ['kw', ':', 'keyword' ...]
64 # Maybe there's a better way to go about this in
65 # contrib/completion/lei-completion.bash
66 sub _complete_tag_common ($) {
67         my ($argv) = @_;
68         # Workaround bash word-splitting URLs to ['https', ':', '//' ...]
69         # Maybe there's a better way to go about this in
70         # contrib/completion/lei-completion.bash
71         my $re = '';
72         my $cur = pop(@$argv) // '';
73         if (@$argv) {
74                 my @x = @$argv;
75                 if ($cur eq ':' && @x) {
76                         push @x, $cur;
77                         $cur = '';
78                 }
79                 while (@x > 2 && $x[0] !~ /\A[+\-](?:kw|L)\z/ &&
80                                         $x[1] ne ':') {
81                         shift @x;
82                 }
83                 if (@x >= 2) { # qw(kw : $KEYWORD) or qw(kw :)
84                         $re = join('', @x);
85                 } else { # just return everything and hope for the best
86                         $re = join('', @$argv);
87                 }
88                 $re = quotemeta($re);
89         }
90         ($cur, $re);
91 }
92
93 # FIXME: same problems as _complete_forget_external and similar
94 sub _complete_tag {
95         my ($self, @argv) = @_;
96         require PublicInbox::LeiImport;
97         my @in = PublicInbox::LeiImport::_complete_import(@_);
98         my @L = eval { $self->_lei_store->search->all_terms('L') };
99         my @kwL = ((map { ("+kw:$_", "-kw:$_") } @PublicInbox::LeiInput::KW),
100                 (map { ("+L:$_", "-L:$_") } @L));
101         my ($cur, $re) = _complete_tag_common(\@argv);
102         my @m = map {
103                 # only return the part specified on the CLI
104                 # don't duplicate if already 100% completed
105                 /\A$re(\Q$cur\E.*)/ ? ($cur eq $1 ? () : $1) : ();
106         } grep(/$re\Q$cur/, @kwL);
107         (@in, (@m ? @m : @kwL));
108 }
109
110 no warnings 'once'; # the following works even when LeiAuth is lazy-loaded
111 *net_merge_all_done = \&PublicInbox::LeiInput::input_only_net_merge_all_done;
112
113 1;