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