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