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;
15 my $REMOTE_RE = qr!\A(?:imap|http)s?://!i; # http(s) will be for JMAP
19 my $lss = PublicInbox::LeiSavedSearch->up($lei, $out) or return;
21 my $mset_opt = $lei->{mset_opt} = { relevance => -2 };
22 $mset_opt->{limit} = $lei->{opt}->{limit} // 10000;
23 my $q = $mset_opt->{q_raw} = $lss->{-cfg}->{'lei.q'} //
24 return $lei->fail("lei.q unset in $f");
25 my $lse = $lei->{lse} // die 'BUG: {lse} missing';
27 $mset_opt->{qstr} = $lse->query_argv_to_string($lse->git, $q);
29 $lse->query_approxidate($lse->git, $mset_opt->{qstr} = $q);
31 my $o = $lei->{opt}->{output} = $lss->{-cfg}->{'lei.q.output'} //
32 return $lei->fail("lei.q.output unset in $f");
33 ref($o) and return $lei->fail("multiple values of lei.q.output in $f");
34 if (defined(my $dd = $lss->{-cfg}->{'lei.q.dedupe'})) {
35 $lss->translate_dedupe($lei, $dd) or return;
36 $lei->{opt}->{dedupe} = $dd;
38 for my $k (qw(only include exclude)) {
39 my $v = $lss->{-cfg}->get_all("lei.q.$k") // next;
40 $lei->{opt}->{$k} = $v;
42 for my $k (qw(external local remote
43 import-remote import-before threads)) {
45 my $v = $lss->{-cfg}->{$c} // next;
46 ref($v) and return $lei->fail("multiple values of $c in $f");
47 $lei->{opt}->{$k} = $v;
49 $lei->{lss} = $lss; # for LeiOverview->new
50 my $lxs = $lei->lxs_prepare or return;
51 $lei->ale->refresh_externals($lxs, $lei);
56 my ($lei, $out, $op_p) = @_;
57 my $l = bless { %$lei }, ref($lei);
58 $l->{opt} = { %{$l->{opt}} }; # deep copy
59 delete $l->{sock}; # do not close
60 $l->{''} = $op_p; # daemon only ($l => $lei => script/lei)
62 # make close($l->{1}) happy in lei->dclose
63 open my $fh, '>&', $l->{1} or return $l->child_error(0, "dup: $!");
64 local $PublicInbox::LEI::current_lei = $l;
65 local %ENV = %{$l->{env}};
68 $l->qerr("# updating $out");
71 $lei->child_error(0, $@) if $@ || $l->{failed}; # lei->fail()
74 sub redispatch_all ($$) {
75 my ($self, $lei) = @_;
76 # re-dispatch into our event loop w/o creating an extra fork-level
77 $lei->{fmsg} = PublicInbox::LeiFinmsg->new($lei);
78 my ($op_c, $op_p) = PublicInbox::PktOp->pair;
79 for my $o (@{$self->{local} // []}, @{$self->{remote} // []}) {
80 PublicInbox::DS::requeue(sub {
81 up1_redispatch($lei, $o, $op_p);
84 $lei->event_step_init;
85 $lei->pkt_ops($op_c->{ops} = { '' => [$lei->can('dclose'), $lei] });
89 my ($lei, @outs) = @_;
90 my $opt = $lei->{opt};
91 my $self = bless { -mail_sync => 1 }, __PACKAGE__;
92 $lei->{lse} = $lei->_lei_store(1)->write_prepare($lei)->search;
93 if (defined(my $all = $opt->{all})) {
94 return $lei->fail("--all and @outs incompatible") if @outs;
95 length($opt->{mua}//'') and return
96 $lei->fail('--all and --mua= are incompatible');
97 @outs = PublicInbox::LeiSavedSearch::list($lei);
98 if ($all eq 'local') {
99 $self->{local} = [ grep(!/$REMOTE_RE/, @outs) ];
100 } elsif ($all eq 'remote') {
101 $self->{remote} = [ grep(/$REMOTE_RE/, @outs) ];
102 } elsif ($all eq '') {
103 $self->{remote} = [ grep(/$REMOTE_RE/, @outs) ];
104 $self->{local} = [ grep(!/$REMOTE_RE/, @outs) ];
106 $lei->fail("only --all=$all not understood");
109 $self->{remote} = [ grep(/$REMOTE_RE/, @outs) ];
110 $self->{local} = [ grep(!/$REMOTE_RE/, @outs) ];
112 ((@{$self->{local} // []} + @{$self->{remote} // []}) > 1 &&
113 length($opt->{mua} // '')) and return $lei->fail(<<EOM);
114 multiple outputs and --mua= are incompatible
116 if ($self->{remote}) { # setup lei->{auth}
117 $self->prepare_inputs($lei, $self->{remote}) or return;
119 if ($lei->{auth}) { # start auth worker
120 require PublicInbox::NetWriter;
121 bless $lei->{net}, 'PublicInbox::NetWriter';
122 $lei->{auth}->op_merge(my $ops = {}, $self, $lei);
123 (my $op_c, $ops) = $lei->workers_start($self, 1, $ops);
125 $lei->wait_wq_events($op_c, $ops);
126 # net_merge_all_done will fire when auth is done
128 redispatch_all($self, $lei); # see below
132 # called in top-level lei-daemon when LeiAuth is done
133 sub net_merge_all_done {
134 my ($self, $lei) = @_;
135 $lei->{net} = delete($self->{-net_new}) if $self->{-net_new};
137 redispatch_all($self, $lei);
141 my ($lei, @argv) = @_;
142 my $match_cb = $lei->complete_url_prepare(\@argv);
143 map { $match_cb->($_) } PublicInbox::LeiSavedSearch::list($lei);
147 *ipc_atfork_child = \&PublicInbox::LeiInput::input_only_atfork_child;