1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # handles "lei tag" command
5 package PublicInbox::LeiTag;
8 use parent qw(PublicInbox::IPC PublicInbox::LeiInput);
9 use PublicInbox::InboxWritable qw(eml_from_path);
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,
18 ++$self->{unimported};
22 sub pmdir_cb { # called via wq_io_do from LeiPmdir->each_mdir_fn
24 my $eml = eml_from_path($f) or return;
25 input_eml_cb($self, $eml);
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 $lei->{-err_type} = 'non-fatal';
41 $lei->wq1_start($self);
46 my $n = $self->{unimported} or return;
47 $self->{lei}->{pkt_op_p}->pkt_do('incr', 'unimported', $n);
50 sub ipc_atfork_child {
52 PublicInbox::LeiInput::input_only_atfork_child($self);
53 $self->{lse} = $self->{lei}->{sto}->search;
54 # this goes out-of-scope at worker process exit:
55 PublicInbox::OnDestroy->new($$, \¬e_unimported, $self);
58 # Workaround bash word-splitting s to ['kw', ':', 'keyword' ...]
59 # Maybe there's a better way to go about this in
60 # contrib/completion/lei-completion.bash
61 sub _complete_tag_common ($) {
63 # Workaround bash word-splitting URLs to ['https', ':', '//' ...]
64 # Maybe there's a better way to go about this in
65 # contrib/completion/lei-completion.bash
67 my $cur = pop(@$argv) // '';
70 if ($cur eq ':' && @x) {
74 while (@x > 2 && $x[0] !~ /\A[+\-](?:kw|L)\z/ &&
78 if (@x >= 2) { # qw(kw : $KEYWORD) or qw(kw :)
80 } else { # just return everything and hope for the best
81 $re = join('', @$argv);
88 # FIXME: same problems as _complete_forget_external and similar
90 my ($self, @argv) = @_;
91 require PublicInbox::LeiImport;
92 my @in = PublicInbox::LeiImport::_complete_import(@_);
93 my @L = eval { $self->_lei_store->search->all_terms('L') };
94 my @kwL = ((map { ("+kw:$_", "-kw:$_") } @PublicInbox::LeiInput::KW),
95 (map { ("+L:$_", "-L:$_") } @L));
96 my ($cur, $re) = _complete_tag_common(\@argv);
98 # only return the part specified on the CLI
99 # don't duplicate if already 100% completed
100 /\A$re(\Q$cur\E.*)/ ? ($cur eq $1 ? () : $1) : ();
101 } grep(/$re\Q$cur/, @kwL);
102 (@in, (@m ? @m : @kwL));
105 no warnings 'once'; # the following works even when LeiAuth is lazy-loaded
106 *net_merge_all_done = \&PublicInbox::LeiInput::input_only_net_merge_all_done;