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);
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,
20 sub input_mbox_cb { input_eml_cb($_[1], $_[0]) }
22 sub net_merge_complete { # callback used by LeiAuth
24 $self->wq_io_do('process_inputs');
28 sub input_maildir_cb { # maildir_each_eml cb
29 my ($f, $kw, $eml, $self) = @_;
30 input_eml_cb($self, $eml);
33 sub input_net_cb { # imap_each, nntp_each cb
34 my ($url, $uid, $kw, $eml, $self) = @_;
35 input_eml_cb($self, $eml);
38 sub lei_tag { # the "lei tag" method
39 my ($lei, @argv) = @_;
40 my $sto = $lei->_lei_store(1);
41 $sto->write_prepare($lei);
42 my $self = bless { missing => 0 }, __PACKAGE__;
43 $lei->ale; # refresh and prepare
44 my $vmd_mod = $self->vmd_mod_extract(\@argv);
45 return $lei->fail(join("\n", @{$vmd_mod->{err}})) if $vmd_mod->{err};
46 $self->prepare_inputs($lei, \@argv) or return;
47 grep(defined, @$vmd_mod{qw(+kw +L -L -kw)}) or
48 return $lei->fail('no keywords or labels specified');
50 $lei->{auth}->op_merge($ops, $self) if $lei->{auth};
51 $self->{vmd_mod} = $vmd_mod;
52 my $j = $self->{-wq_nr_workers} = 1; # locked for now
53 (my $op_c, $ops) = $lei->workers_start($self, 'lei-tag', $j, $ops);
55 $lei->{-err_type} = 'non-fatal';
56 net_merge_complete($self) unless $lei->{auth};
57 $op_c->op_wait_event($ops);
62 my $n = $self->{missing} or return;
63 $self->{lei}->child_error(1 << 8, "$n missed messages");
66 sub ipc_atfork_child {
68 PublicInbox::LeiInput::input_only_atfork_child($self);
69 # this goes out-of-scope at worker process exit:
70 PublicInbox::OnDestroy->new($$, \¬e_missing, $self);
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 ($) {
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
82 my $cur = pop(@$argv) // '';
85 if ($cur eq ':' && @x) {
89 while (@x > 2 && $x[0] !~ /\A[+\-](?:kw|L)\z/ &&
93 if (@x >= 2) { # qw(kw : $KEYWORD) or qw(kw :)
95 } else { # just return everything and hope for the best
96 $re = join('', @$argv);
103 # FIXME: same problems as _complete_forget_external and similar
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);
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);
118 no warnings 'once'; # the following works even when LeiAuth is lazy-loaded
119 *net_merge_all = \&PublicInbox::LeiAuth::net_merge_all;