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 my $lss = PublicInbox::LeiSavedSearch->up($lei, $out) or return;
20 my $mset_opt = $lei->{mset_opt} = { relevance => -2 };
21 $mset_opt->{limit} = $lei->{opt}->{limit} // 10000;
22 my $q = $mset_opt->{q_raw} = $lss->{-cfg}->{'lei.q'} //
23 return $lei->fail("lei.q unset in $f");
24 my $lse = $lei->{lse} // die 'BUG: {lse} missing';
26 $mset_opt->{qstr} = $lse->query_argv_to_string($lse->git, $q);
28 $lse->query_approxidate($lse->git, $mset_opt->{qstr} = $q);
30 my $o = $lei->{opt}->{output} = $lss->{-cfg}->{'lei.q.output'} //
31 return $lei->fail("lei.q.output unset in $f");
32 ref($o) and return $lei->fail("multiple values of lei.q.output in $f");
33 if (defined(my $dd = $lss->{-cfg}->{'lei.q.dedupe'})) {
34 $lss->translate_dedupe($lei, $dd) or return;
35 $lei->{opt}->{dedupe} = $dd;
37 for my $k (qw(only include exclude)) {
38 my $v = $lss->{-cfg}->get_all("lei.q.$k") // next;
39 $lei->{opt}->{$k} = $v;
41 for my $k (qw(external local remote
42 import-remote import-before threads)) {
44 my $v = $lss->{-cfg}->{$c} // next;
45 ref($v) and return $lei->fail("multiple values of $c in $f");
46 $lei->{opt}->{$k} = $v;
48 $lei->{lss} = $lss; # for LeiOverview->new and query_remote_mboxrd
49 my $lxs = $lei->lxs_prepare or return;
50 $lei->ale->refresh_externals($lxs, $lei);
55 my ($lei, $out, $op_p) = @_;
57 if (defined($lei->{opt}->{mua})) { # single output
59 } else { # multiple outputs
60 $l = bless { %$lei }, ref($lei);
61 $l->{opt} = { %{$l->{opt}} }; # deep copy
62 delete $l->{opt}->{all};
63 delete $l->{sock}; # do not close
64 # make close($l->{1}) happy in lei->dclose
65 open my $fh, '>&', $l->{1} or
66 return $l->child_error(0, "dup: $!");
68 $l->qerr("# updating $out");
70 $l->{''} = $op_p; # daemon only ($l => $lei => script/lei)
71 eval { $l->dispatch('up', $out) };
72 $lei->child_error(0, $@) if $@ || $l->{failed}; # lei->fail()
75 sub redispatch_all ($$) {
76 my ($self, $lei) = @_;
77 # re-dispatch into our event loop w/o creating an extra fork-level
78 $lei->{fmsg} = PublicInbox::LeiFinmsg->new($lei);
79 my ($op_c, $op_p) = PublicInbox::PktOp->pair;
80 for my $o (@{$self->{local} // []}, @{$self->{remote} // []}) {
81 PublicInbox::DS::requeue(sub {
82 up1_redispatch($lei, $o, $op_p);
85 $lei->event_step_init;
86 $lei->pkt_ops($op_c->{ops} = { '' => [$lei->can('dclose'), $lei] });
90 my ($lei, @outs) = @_;
91 my $opt = $lei->{opt};
92 my $self = bless { -mail_sync => 1 }, __PACKAGE__;
93 if (defined(my $all = $opt->{all})) {
94 return $lei->fail("--all and @outs incompatible") if @outs;
95 defined($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");
108 } elsif ($lei->{lse}) {
109 scalar(@outs) == 1 or die "BUG: lse set w/ >1 out[@outs]";
110 return up1($lei, $outs[0]);
112 $self->{remote} = [ grep(/$REMOTE_RE/, @outs) ];
113 $self->{local} = [ grep(!/$REMOTE_RE/, @outs) ];
115 $lei->{lse} = $lei->_lei_store(1)->write_prepare($lei)->search;
116 ((@{$self->{local} // []} + @{$self->{remote} // []}) > 1 &&
117 defined($opt->{mua})) and return $lei->fail(<<EOM);
118 multiple outputs and --mua= are incompatible
120 if ($self->{remote}) { # setup lei->{auth}
121 $self->prepare_inputs($lei, $self->{remote}) or return;
123 if ($lei->{auth}) { # start auth worker
124 require PublicInbox::NetWriter;
125 bless $lei->{net}, 'PublicInbox::NetWriter';
126 $lei->{auth}->op_merge(my $ops = {}, $self, $lei);
127 (my $op_c, $ops) = $lei->workers_start($self, 1, $ops);
129 $lei->wait_wq_events($op_c, $ops);
130 # net_merge_all_done will fire when auth is done
132 redispatch_all($self, $lei); # see below
136 # called in top-level lei-daemon when LeiAuth is done
137 sub net_merge_all_done {
138 my ($self, $lei) = @_;
139 $lei->{net} = delete($self->{-net_new}) if $self->{-net_new};
141 redispatch_all($self, $lei);
145 my ($lei, @argv) = @_;
146 my $match_cb = $lei->complete_url_prepare(\@argv);
147 map { $match_cb->($_) } PublicInbox::LeiSavedSearch::list($lei);
151 *ipc_atfork_child = \&PublicInbox::LeiInput::input_only_atfork_child;