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 # "lei up" - updates the result of "lei q --save"
5 package PublicInbox::LeiUp;
8 # n.b. we use LeiInput to setup IMAP auth
9 use parent qw(PublicInbox::IPC PublicInbox::LeiInput);
10 use PublicInbox::LeiSavedSearch;
12 use PublicInbox::PktOp;
13 use PublicInbox::LeiFinmsg;
14 my $REMOTE_RE = qr!\A(?:imap|http)s?://!i; # http(s) will be for JMAP
18 # precedence note for CLI switches between lei q and up:
19 # `lei q --only' > `lei q --no-(remote|local|external)'
20 # `lei up --no-(remote|local|external)' > `lei.q.only' in saved search
22 my $v = $lei->{opt}->{$_}; # set by CLI
23 (defined($v) && !$v) ? ($_ => 1) : ();
24 } qw(remote local external);
25 my $cli_exclude = delete $lei->{opt}->{exclude};
26 my $lss = PublicInbox::LeiSavedSearch->up($lei, $out) or return;
28 my $mset_opt = $lei->{mset_opt} = { relevance => -2 };
29 my $q = $mset_opt->{q_raw} = $lss->{-cfg}->{'lei.q'} //
30 die("lei.q unset in $f (out=$out)\n");
31 my $lse = $lei->{lse} // die 'BUG: {lse} missing';
33 $mset_opt->{qstr} = $lse->query_argv_to_string($lse->git, $q);
35 $lse->query_approxidate($lse->git, $mset_opt->{qstr} = $q);
37 # n.b. only a few CLI args are accepted for "up", so //= usually sets
38 for my $k ($lss->ARRAY_FIELDS) {
39 my $v = $lss->{-cfg}->get_all("lei.q.$k") // next;
40 $lei->{opt}->{$k} //= $v;
43 # --no-(local|remote) CLI flags overrided saved `lei.q.only'
44 my $only = $lei->{opt}->{only};
45 @$only = map { $lei->get_externals($_) } @$only if $only;
46 if (scalar keys %no && $only) {
47 @$only = grep(!m!\Ahttps?://!i, @$only) if $no{remote};
48 @$only = grep(m!\Ahttps?://!i, @$only) if $no{'local'};
51 my $ex = $lei->canonicalize_excludes($cli_exclude);
52 @$only = grep { !$ex->{$_} } @$only if $only;
53 push @{$lei->{opt}->{exclude}}, @$cli_exclude;
55 delete $lei->{opt}->{only} if $no{external} || ($only && !@$only);
56 for my $k ($lss->BOOL_FIELDS, $lss->SINGLE_FIELDS) {
57 my $v = $lss->{-cfg}->get_1("lei.q.$k") // next;
58 $lei->{opt}->{$k} //= $v;
60 my $o = $lei->{opt}->{output} // '';
61 return die("lei.q.output unset in $f (out=$out)\n") if $o eq '';
62 $lss->translate_dedupe($lei) or return;
63 $lei->{lss} = $lss; # for LeiOverview->new and query_remote_mboxrd
64 my $lxs = $lei->lxs_prepare or return;
65 $lei->ale->refresh_externals($lxs, $lei);
69 sub redispatch_all ($$) {
70 my ($self, $lei) = @_;
71 my $upq = [ (@{$self->{o_local} // []}, @{$self->{o_remote} // []}) ];
72 return up1($lei, $upq->[0]) if @$upq == 1; # just one, may start MUA
74 # FIXME: this is also used per-query, see lei->_start_query
75 my $j = $lei->{opt}->{jobs} || do {
76 my $n = $self->detect_nproc // 1;
79 $j = ($j =~ /\A([0-9]+)/) ? $1 + 0 : 1; # may be --jobs=$x,$m on CLI
80 # re-dispatch into our event loop w/o creating an extra fork-level
81 # $upq will be drained via DESTROY as each query finishes
82 $lei->{fmsg} = PublicInbox::LeiFinmsg->new($lei);
83 my ($op_c, $op_p) = PublicInbox::PktOp->pair;
84 # call lei->dclose when upq is done processing:
85 $op_c->{ops} = { '' => [ $lei->can('dclose'), $lei ] };
86 my @first_batch = splice(@$upq, 0, $j); # initial parallelism
88 $lei->{daemon_pid} = $$;
89 $lei->event_step_init; # wait for client disconnects
90 for my $out (@first_batch) {
91 PublicInbox::DS::requeue(
92 PublicInbox::LeiUp1::nxt($lei, $out, $op_p));
97 my ($lei, @outs) = @_;
98 my $opt = $lei->{opt};
99 my $self = bless { -mail_sync => 1 }, __PACKAGE__;
100 if (defined(my $all = $opt->{all})) {
101 return $lei->fail("--all and @outs incompatible") if @outs;
102 defined($opt->{mua}) and return
103 $lei->fail('--all and --mua= are incompatible');
104 @outs = PublicInbox::LeiSavedSearch::list($lei);
105 if ($all eq 'local') {
106 $self->{o_local} = [ grep(!/$REMOTE_RE/, @outs) ];
107 } elsif ($all eq 'remote') {
108 $self->{o_remote} = [ grep(/$REMOTE_RE/, @outs) ];
109 } elsif ($all eq '') {
110 $self->{o_remote} = [ grep(/$REMOTE_RE/, @outs) ];
111 $self->{o_local} = [ grep(!/$REMOTE_RE/, @outs) ];
113 $lei->fail("only --all=$all not understood");
115 } elsif ($lei->{lse}) { # redispatched
116 scalar(@outs) == 1 or die "BUG: lse set w/ >1 out[@outs]";
117 return up1($lei, $outs[0]);
119 $self->{o_remote} = [ grep(/$REMOTE_RE/, @outs) ];
120 $self->{o_local} = [ grep(!/$REMOTE_RE/, @outs) ];
122 $lei->{lse} = $lei->_lei_store(1)->write_prepare($lei)->search;
123 ((@{$self->{o_local} // []} + @{$self->{o_remote} // []}) > 1 &&
124 defined($opt->{mua})) and return $lei->fail(<<EOM);
125 multiple outputs and --mua= are incompatible
127 if ($self->{o_remote}) { # setup lei->{auth}
128 $self->prepare_inputs($lei, $self->{o_remote}) or return;
130 if ($lei->{auth}) { # start auth worker
131 require PublicInbox::NetWriter;
132 bless $lei->{net}, 'PublicInbox::NetWriter';
133 $lei->{auth}->op_merge(my $ops = {}, $self, $lei);
134 (my $op_c, $ops) = $lei->workers_start($self, 1, $ops);
136 $lei->wait_wq_events($op_c, $ops);
137 # net_merge_all_done will fire when auth is done
139 redispatch_all($self, $lei); # see below
143 # called in top-level lei-daemon when LeiAuth is done
144 sub net_merge_all_done {
145 my ($self, $lei) = @_;
146 $lei->{net} = delete($self->{-net_new}) if $self->{-net_new};
148 eval { redispatch_all($self, $lei) };
149 $lei->child_error(0, "E: $@") if $@;
152 sub _complete_up { # lei__complete hook
153 my ($lei, @argv) = @_;
154 my $match_cb = $lei->complete_url_prepare(\@argv);
155 map { $match_cb->($_) } PublicInbox::LeiSavedSearch::list($lei);
158 sub _wq_done_wait { # dwaitpid callback
159 my ($arg, $pid) = @_;
160 my ($wq, $lei) = @$arg;
161 $lei->child_error($?, 'auth failure') if $?
165 *ipc_atfork_child = \&PublicInbox::LeiInput::input_only_atfork_child;
167 package PublicInbox::LeiUp1; # for redispatch_all
172 my ($lei, $out, $op_p) = @_;
173 bless { lei => $lei, out => $out, op_p => $op_p }, __PACKAGE__;
176 sub event_step { # runs via PublicInbox::DS::requeue
178 my $lei = $self->{lei}; # the original, from lei_up
179 my $l = bless { %$lei }, ref($lei); # per-output copy
180 delete($l->{sock}) or return; # client disconnected if {sock} is gone
181 $l->{opt} = { %{$l->{opt}} }; # deep copy
182 delete $l->{opt}->{all};
183 $l->qerr("# updating $self->{out}");
184 my $o = " (output: $self->{out})"; # add to all warnings
185 my $cb = $SIG{__WARN__} // \&CORE::warn;
186 local $SIG{__WARN__} = sub {
188 push(@m, $o) if !@m || $m[-1] !~ s/\n\z/$o\n/;
191 $l->{-up1} = $self; # for LeiUp1->DESTROY
192 delete @$l{qw(-socks -event_init_done)};
193 my ($op_c, $op_p) = PublicInbox::PktOp->pair;
194 $self->{unref_on_destroy} = $op_c->{sock}; # to cleanup $lei->{-socks}
195 $lei->pkt_ops($op_c->{ops} //= {}); # errors from $l -> script/lei
196 push @{$lei->{-socks}}, $op_c->{sock}; # script/lei signals to $l
197 $l->{sock} = $op_p->{op_p}; # receive signals from op_c->{sock}
198 $op_c = $op_p = undef;
200 eval { $l->dispatch('up', $self->{out}) };
201 $lei->child_error(0, $@) if $@ || $l->{failed}; # lei->fail()
206 my $lei = $self->{lei}; # the original, from lei_up
207 return if $lei->{daemon_pid} != $$;
208 my $sock = delete $self->{unref_on_destroy};
209 my $s = $lei->{-socks} // [];
210 @$s = grep { $_ != $sock } @$s;
211 my $out = shift(@{$lei->{-upq}}) or return;
212 PublicInbox::DS::requeue(nxt($lei, $out, $self->{op_p}));