]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiUp.pm
abb05d46eca5252637a31163f72c6c116a6229a7
[public-inbox.git] / lib / PublicInbox / LeiUp.pm
1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # "lei up" - updates the result of "lei q --save"
5 package PublicInbox::LeiUp;
6 use strict;
7 use v5.10.1;
8 # n.b. we use LeiInput to setup IMAP auth
9 use parent qw(PublicInbox::IPC PublicInbox::LeiInput);
10 use PublicInbox::LeiSavedSearch;
11 use PublicInbox::DS;
12 use PublicInbox::PktOp;
13 use PublicInbox::LeiFinmsg;
14 my $REMOTE_RE = qr!\A(?:imap|http)s?://!i; # http(s) will be for JMAP
15
16 sub up1 ($$) {
17         my ($lei, $out) = @_;
18         my $lss = PublicInbox::LeiSavedSearch->up($lei, $out) or return;
19         my $f = $lss->{'-f'};
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';
25         if (ref($q)) {
26                 $mset_opt->{qstr} = $lse->query_argv_to_string($lse->git, $q);
27         } else {
28                 $lse->query_approxidate($lse->git, $mset_opt->{qstr} = $q);
29         }
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;
36         }
37         for my $k (qw(only include exclude)) {
38                 my $v = $lss->{-cfg}->get_all("lei.q.$k") // next;
39                 $lei->{opt}->{$k} = $v;
40         }
41         for my $k (qw(external local remote
42                         import-remote import-before threads)) {
43                 my $c = "lei.q.$k";
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;
47         }
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);
51         $lei->_start_query;
52 }
53
54 sub up1_redispatch {
55         my ($lei, $out, $op_p) = @_;
56         my $l;
57         if (defined($lei->{opt}->{mua})) { # single output
58                 $l = $lei;
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: $!");
67                 $l->{1} = $fh;
68                 $l->qerr("# updating $out");
69         }
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()
73 }
74
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);
83                 });
84         }
85         $lei->event_step_init;
86         $lei->pkt_ops($op_c->{ops} = { '' => [$lei->can('dclose'), $lei] });
87 }
88
89 sub lei_up {
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) ];
105                 } else {
106                         $lei->fail("only --all=$all not understood");
107                 }
108         } elsif ($lei->{lse}) {
109                 scalar(@outs) == 1 or die "BUG: lse set w/ >1 out[@outs]";
110                 return up1($lei, $outs[0]);
111         } else {
112                 $self->{remote} = [ grep(/$REMOTE_RE/, @outs) ];
113                 $self->{local} = [ grep(!/$REMOTE_RE/, @outs) ];
114         }
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
119 EOM
120         if ($self->{remote}) { # setup lei->{auth}
121                 $self->prepare_inputs($lei, $self->{remote}) or return;
122         }
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);
128                 $lei->{wq1} = $self;
129                 $lei->wait_wq_events($op_c, $ops);
130                 # net_merge_all_done will fire when auth is done
131         } else {
132                 redispatch_all($self, $lei); # see below
133         }
134 }
135
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};
140         $self->wq_close(1);
141         redispatch_all($self, $lei);
142 }
143
144 sub _complete_up {
145         my ($lei, @argv) = @_;
146         my $match_cb = $lei->complete_url_prepare(\@argv);
147         map { $match_cb->($_) } PublicInbox::LeiSavedSearch::list($lei);
148 }
149
150 no warnings 'once';
151 *ipc_atfork_child = \&PublicInbox::LeiInput::input_only_atfork_child;
152
153 1;