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