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