]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiUp.pm
lei q: improve --limit behavior and progress
[public-inbox.git] / lib / PublicInbox / LeiUp.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 # "lei up" - updates the result of "lei q --save"
5 package PublicInbox::LeiUp;
6 use strict;
7 use v5.10.1;
8 # n.b. we use LeiInput to setup IMAP auth
9 use parent qw(PublicInbox::IPC PublicInbox::LeiInput);
10 use PublicInbox::LeiSavedSearch;
11 use PublicInbox::DS;
12 use PublicInbox::PktOp;
13 use PublicInbox::LeiFinmsg;
14 my $REMOTE_RE = qr!\A(?:imap|http)s?://!i; # http(s) will be for JMAP
15
16 sub up1 ($$) {
17         my ($lei, $out) = @_;
18         my $lss = PublicInbox::LeiSavedSearch->up($lei, $out) or return;
19         my $f = $lss->{'-f'};
20         my $mset_opt = $lei->{mset_opt} = { relevance => -2 };
21         my $q = $mset_opt->{q_raw} = $lss->{-cfg}->{'lei.q'} //
22                                 return $lei->fail("lei.q unset in $f");
23         my $lse = $lei->{lse} // die 'BUG: {lse} missing';
24         if (ref($q)) {
25                 $mset_opt->{qstr} = $lse->query_argv_to_string($lse->git, $q);
26         } else {
27                 $lse->query_approxidate($lse->git, $mset_opt->{qstr} = $q);
28         }
29         # n.b. only a few CLI args are accepted for "up", so //= usually sets
30         for my $k ($lss->ARRAY_FIELDS) {
31                 my $v = $lss->{-cfg}->get_all("lei.q.$k") // next;
32                 $lei->{opt}->{$k} //= $v;
33         }
34         for my $k ($lss->BOOL_FIELDS, $lss->SINGLE_FIELDS) {
35                 my $v = $lss->{-cfg}->get_1('lei.q', $k) // next;
36                 $lei->{opt}->{$k} //= $v;
37         }
38         my $o = $lei->{opt}->{output} // '';
39         return $lei->fail("lei.q.output unset in $f") if $o eq '';
40         $lss->translate_dedupe($lei) or return;
41         $lei->{lss} = $lss; # for LeiOverview->new and query_remote_mboxrd
42         my $lxs = $lei->lxs_prepare or return;
43         $lei->ale->refresh_externals($lxs, $lei);
44         $lei->_start_query;
45 }
46
47 sub up1_redispatch {
48         my ($lei, $out, $op_p) = @_;
49         my $l;
50         if (defined($lei->{opt}->{mua})) { # single output
51                 $l = $lei;
52         } else { # multiple outputs
53                 $l = bless { %$lei }, ref($lei);
54                 $l->{opt} = { %{$l->{opt}} }; # deep copy
55                 delete $l->{opt}->{all};
56                 delete $l->{sock}; # do not close
57                 # make close($l->{1}) happy in lei->dclose
58                 open my $fh, '>&', $l->{1} or
59                         return $l->child_error(0, "dup: $!");
60                 $l->{1} = $fh;
61                 $l->qerr("# updating $out");
62         }
63         $l->{''} = $op_p; # daemon only ($l => $lei => script/lei)
64         eval { $l->dispatch('up', $out) };
65         $lei->child_error(0, $@) if $@ || $l->{failed}; # lei->fail()
66 }
67
68 sub redispatch_all ($$) {
69         my ($self, $lei) = @_;
70         # re-dispatch into our event loop w/o creating an extra fork-level
71         $lei->{fmsg} = PublicInbox::LeiFinmsg->new($lei);
72         my ($op_c, $op_p) = PublicInbox::PktOp->pair;
73         for my $o (@{$self->{local} // []}, @{$self->{remote} // []}) {
74                 PublicInbox::DS::requeue(sub {
75                         up1_redispatch($lei, $o, $op_p);
76                 });
77         }
78         $lei->event_step_init;
79         $lei->pkt_ops($op_c->{ops} = { '' => [$lei->can('dclose'), $lei] });
80 }
81
82 sub lei_up {
83         my ($lei, @outs) = @_;
84         my $opt = $lei->{opt};
85         my $self = bless { -mail_sync => 1 }, __PACKAGE__;
86         if (defined(my $all = $opt->{all})) {
87                 return $lei->fail("--all and @outs incompatible") if @outs;
88                 defined($opt->{mua}) and return
89                         $lei->fail('--all and --mua= are incompatible');
90                 @outs = PublicInbox::LeiSavedSearch::list($lei);
91                 if ($all eq 'local') {
92                         $self->{local} = [ grep(!/$REMOTE_RE/, @outs) ];
93                 } elsif ($all eq 'remote') {
94                         $self->{remote} = [ grep(/$REMOTE_RE/, @outs) ];
95                 } elsif ($all eq '') {
96                         $self->{remote} = [ grep(/$REMOTE_RE/, @outs) ];
97                         $self->{local} = [ grep(!/$REMOTE_RE/, @outs) ];
98                 } else {
99                         $lei->fail("only --all=$all not understood");
100                 }
101         } elsif ($lei->{lse}) {
102                 scalar(@outs) == 1 or die "BUG: lse set w/ >1 out[@outs]";
103                 return up1($lei, $outs[0]);
104         } else {
105                 $self->{remote} = [ grep(/$REMOTE_RE/, @outs) ];
106                 $self->{local} = [ grep(!/$REMOTE_RE/, @outs) ];
107         }
108         $lei->{lse} = $lei->_lei_store(1)->write_prepare($lei)->search;
109         ((@{$self->{local} // []} + @{$self->{remote} // []}) > 1 &&
110                 defined($opt->{mua})) and return $lei->fail(<<EOM);
111 multiple outputs and --mua= are incompatible
112 EOM
113         if ($self->{remote}) { # setup lei->{auth}
114                 $self->prepare_inputs($lei, $self->{remote}) or return;
115         }
116         if ($lei->{auth}) { # start auth worker
117                 require PublicInbox::NetWriter;
118                 bless $lei->{net}, 'PublicInbox::NetWriter';
119                 $lei->{auth}->op_merge(my $ops = {}, $self, $lei);
120                 (my $op_c, $ops) = $lei->workers_start($self, 1, $ops);
121                 $lei->{wq1} = $self;
122                 $lei->wait_wq_events($op_c, $ops);
123                 # net_merge_all_done will fire when auth is done
124         } else {
125                 redispatch_all($self, $lei); # see below
126         }
127 }
128
129 # called in top-level lei-daemon when LeiAuth is done
130 sub net_merge_all_done {
131         my ($self, $lei) = @_;
132         $lei->{net} = delete($self->{-net_new}) if $self->{-net_new};
133         $self->wq_close(1);
134         redispatch_all($self, $lei);
135 }
136
137 sub _complete_up {
138         my ($lei, @argv) = @_;
139         my $match_cb = $lei->complete_url_prepare(\@argv);
140         map { $match_cb->($_) } PublicInbox::LeiSavedSearch::list($lei);
141 }
142
143 no warnings 'once';
144 *ipc_atfork_child = \&PublicInbox::LeiInput::input_only_atfork_child;
145
146 1;