1 # Copyright (C) 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; # OverIdx
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 = $lss->{-cfg}->get_all('lei.q') //
30 die("lei.q unset in $f (out=$out)\n");
31 my $lse = $lei->{lse} // die 'BUG: {lse} missing';
32 my $rawstr = $lss->{-cfg}->{'lei.internal.rawstr'} //
33 (scalar(@$q) == 1 && substr($q->[0], -1) eq "\n");
36 die "$f: lei.q has multiple values (@$q) (out=$out)\n";
37 $lse->query_approxidate($lse->git, $mset_opt->{qstr} = $q->[0]);
39 $mset_opt->{qstr} = $lse->query_argv_to_string($lse->git, $q);
41 # n.b. only a few CLI args are accepted for "up", so //= usually sets
42 for my $k ($lss->ARRAY_FIELDS) {
43 my $v = $lss->{-cfg}->get_all("lei.q.$k") // next;
44 $lei->{opt}->{$k} //= $v;
47 # --no-(local|remote) CLI flags overrided saved `lei.q.only'
48 my $only = $lei->{opt}->{only};
49 @$only = map { $lei->get_externals($_) } @$only if $only;
50 if (scalar keys %no && $only) {
51 @$only = grep(!m!\Ahttps?://!i, @$only) if $no{remote};
52 @$only = grep(m!\Ahttps?://!i, @$only) if $no{'local'};
55 my $ex = $lei->canonicalize_excludes($cli_exclude);
56 @$only = grep { !$ex->{$_} } @$only if $only;
57 push @{$lei->{opt}->{exclude}}, @$cli_exclude;
59 delete $lei->{opt}->{only} if $no{external} || ($only && !@$only);
60 for my $k ($lss->BOOL_FIELDS, $lss->SINGLE_FIELDS) {
61 my $v = $lss->{-cfg}->get_1("lei.q.$k") // next;
62 $lei->{opt}->{$k} //= $v;
64 my $o = $lei->{opt}->{output} // '';
65 return die("lei.q.output unset in $f (out=$out)\n") if $o eq '';
66 $lss->translate_dedupe($lei) or return;
67 $lei->{lss} = $lss; # for LeiOverview->new and query_remote_mboxrd
68 my $lxs = $lei->lxs_prepare or return;
69 $lei->ale->refresh_externals($lxs, $lei);
73 sub redispatch_all ($$) {
74 my ($self, $lei) = @_;
75 my $upq = [ (@{$self->{o_local} // []}, @{$self->{o_remote} // []}) ];
76 return up1($lei, $upq->[0]) if @$upq == 1; # just one, may start MUA
78 PublicInbox::OverIdx::fork_ok($lei->{opt});
79 # FIXME: this is also used per-query, see lei->_start_query
80 my $j = $lei->{opt}->{jobs} || do {
81 my $n = $self->detect_nproc // 1;
84 $j = ($j =~ /\A([0-9]+)/) ? $1 + 0 : 1; # may be --jobs=$x,$m on CLI
85 # re-dispatch into our event loop w/o creating an extra fork-level
86 # $upq will be drained via DESTROY as each query finishes
87 $lei->{fmsg} = PublicInbox::LeiFinmsg->new($lei);
88 my ($op_c, $op_p) = PublicInbox::PktOp->pair;
89 # call lei->dclose when upq is done processing:
90 $op_c->{ops} = { '' => [ $lei->can('dclose'), $lei ] };
91 my @first_batch = splice(@$upq, 0, $j); # initial parallelism
93 $lei->{daemon_pid} = $$;
94 $lei->event_step_init; # wait for client disconnects
95 for my $out (@first_batch) {
96 PublicInbox::DS::requeue(
97 PublicInbox::LeiUp1::nxt($lei, $out, $op_p));
102 my ($self, $lei, $all) = @_;
103 my @outs = PublicInbox::LeiSavedSearch::list($lei);
104 if ($all eq 'local') {
105 $self->{o_local} = [ grep(!/$REMOTE_RE/, @outs) ];
106 } elsif ($all eq 'remote') {
107 $self->{o_remote} = [ grep(/$REMOTE_RE/, @outs) ];
108 } elsif ($all eq '') {
109 $self->{o_remote} = [ grep(/$REMOTE_RE/, @outs) ];
110 $self->{o_local} = [ grep(!/$REMOTE_RE/, @outs) ];
117 my ($lei, @outs) = @_;
118 my $opt = $lei->{opt};
119 my $self = bless { -mail_sync => 1 }, __PACKAGE__;
120 if (defined(my $all = $opt->{all})) {
121 return $lei->fail("--all and @outs incompatible") if @outs;
122 defined($opt->{mua}) and return
123 $lei->fail('--all and --mua= are incompatible');
124 filter_lss($self, $lei, $all) // return
125 $lei->fail("only --all=$all not understood");
126 } elsif ($lei->{lse}) { # redispatched
127 scalar(@outs) == 1 or die "BUG: lse set w/ >1 out[@outs]";
128 return up1($lei, $outs[0]);
130 $self->{o_remote} = [ grep(/$REMOTE_RE/, @outs) ];
131 $self->{o_local} = [ grep(!/$REMOTE_RE/, @outs) ];
133 $lei->{lse} = $lei->_lei_store(1)->write_prepare($lei)->search;
134 ((@{$self->{o_local} // []} + @{$self->{o_remote} // []}) > 1 &&
135 defined($opt->{mua})) and return $lei->fail(<<EOM);
136 multiple outputs and --mua= are incompatible
138 if ($self->{o_remote}) { # setup lei->{auth}
139 $self->prepare_inputs($lei, $self->{o_remote}) or return;
141 if ($lei->{auth}) { # start auth worker
142 require PublicInbox::NetWriter;
143 bless $lei->{net}, 'PublicInbox::NetWriter';
144 $lei->wq1_start($self);
145 # net_merge_all_done will fire when auth is done
147 redispatch_all($self, $lei); # see below
151 # called in top-level lei-daemon when LeiAuth is done
152 sub net_merge_all_done {
153 my ($self, $lei) = @_;
154 $lei->{net} = delete($self->{-net_new}) if $self->{-net_new};
156 eval { redispatch_all($self, $lei) };
157 $lei->child_error(0, "E: $@") if $@;
160 sub _complete_up { # lei__complete hook
161 my ($lei, @argv) = @_;
162 my $match_cb = $lei->complete_url_prepare(\@argv);
163 map { $match_cb->($_) } PublicInbox::LeiSavedSearch::list($lei);
166 sub _wq_done_wait { # dwaitpid callback
167 my ($arg, $pid) = @_;
168 my ($wq, $lei) = @$arg;
169 $lei->child_error($?, 'auth failure') if $?
173 *ipc_atfork_child = \&PublicInbox::LeiInput::input_only_atfork_child;
175 package PublicInbox::LeiUp1; # for redispatch_all
180 my ($lei, $out, $op_p) = @_;
181 bless { lei => $lei, out => $out, op_p => $op_p }, __PACKAGE__;
184 sub event_step { # runs via PublicInbox::DS::requeue
186 my $lei = $self->{lei}; # the original, from lei_up
187 my $l = bless { %$lei }, ref($lei); # per-output copy
188 delete($l->{sock}) or return; # client disconnected if {sock} is gone
189 $l->{opt} = { %{$l->{opt}} }; # deep copy
190 delete $l->{opt}->{all};
191 $l->qerr("# updating $self->{out}");
192 my $o = " (output: $self->{out})"; # add to all warnings
193 my $cb = $SIG{__WARN__} // \&CORE::warn;
194 local $SIG{__WARN__} = sub {
196 push(@m, $o) if !@m || $m[-1] !~ s/\n\z/$o\n/;
199 $l->{-up1} = $self; # for LeiUp1->DESTROY
200 delete @$l{qw(-socks -event_init_done)};
201 my ($op_c, $op_p) = PublicInbox::PktOp->pair;
202 $self->{unref_on_destroy} = $op_c->{sock}; # to cleanup $lei->{-socks}
203 $lei->pkt_ops($op_c->{ops} //= {}); # errors from $l -> script/lei
204 push @{$lei->{-socks}}, $op_c->{sock}; # script/lei signals to $l
205 $l->{sock} = $op_p->{op_p}; # receive signals from op_c->{sock}
206 $op_c = $op_p = undef;
208 eval { $l->dispatch('up', $self->{out}) };
209 $lei->child_error(0, $@) if $@ || $l->{failed}; # lei->fail()
214 my $lei = $self->{lei}; # the original, from lei_up
215 return if $lei->{daemon_pid} != $$;
216 my $sock = delete $self->{unref_on_destroy};
217 my $s = $lei->{-socks} // [];
218 @$s = grep { $_ != $sock } @$s;
219 my $out = shift(@{$lei->{-upq}}) or return;
220 PublicInbox::DS::requeue(nxt($lei, $out, $self->{op_p}));