]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiUp.pm
lei up: fix --mua with single output
[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 use PublicInbox::LEI;
15 my $REMOTE_RE = qr!\A(?:imap|http)s?://!i; # http(s) will be for JMAP
16
17 sub up1 ($$) {
18         my ($lei, $out) = @_;
19         my $lss = PublicInbox::LeiSavedSearch->up($lei, $out) or return;
20         my $f = $lss->{'-f'};
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';
26         if (ref($q)) {
27                 $mset_opt->{qstr} = $lse->query_argv_to_string($lse->git, $q);
28         } else {
29                 $lse->query_approxidate($lse->git, $mset_opt->{qstr} = $q);
30         }
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;
37         }
38         for my $k (qw(only include exclude)) {
39                 my $v = $lss->{-cfg}->get_all("lei.q.$k") // next;
40                 $lei->{opt}->{$k} = $v;
41         }
42         for my $k (qw(external local remote
43                         import-remote import-before threads)) {
44                 my $c = "lei.q.$k";
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;
48         }
49         $lei->{lss} = $lss; # for LeiOverview->new
50         my $lxs = $lei->lxs_prepare or return;
51         $lei->ale->refresh_externals($lxs, $lei);
52         $lei->_start_query;
53 }
54
55 sub up1_redispatch {
56         my ($lei, $out, $op_p) = @_;
57         my $l;
58         if (defined($lei->{opt}->{mua})) { # single output
59                 $l = $lei;
60         } else { # multiple outputs
61                 $l = bless { %$lei }, ref($lei);
62                 $l->{opt} = { %{$l->{opt}} }; # deep copy
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         }
69         local $PublicInbox::LEI::current_lei = $l;
70         local %ENV = %{$l->{env}};
71         $l->{''} = $op_p; # daemon only ($l => $lei => script/lei)
72         eval {
73                 $l->qerr("# updating $out");
74                 up1($l, $out);
75         };
76         $lei->child_error(0, $@) if $@ || $l->{failed}; # lei->fail()
77 }
78
79 sub redispatch_all ($$) {
80         my ($self, $lei) = @_;
81         # re-dispatch into our event loop w/o creating an extra fork-level
82         $lei->{fmsg} = PublicInbox::LeiFinmsg->new($lei);
83         my ($op_c, $op_p) = PublicInbox::PktOp->pair;
84         for my $o (@{$self->{local} // []}, @{$self->{remote} // []}) {
85                 PublicInbox::DS::requeue(sub {
86                         up1_redispatch($lei, $o, $op_p);
87                 });
88         }
89         $lei->event_step_init;
90         $lei->pkt_ops($op_c->{ops} = { '' => [$lei->can('dclose'), $lei] });
91 }
92
93 sub lei_up {
94         my ($lei, @outs) = @_;
95         my $opt = $lei->{opt};
96         my $self = bless { -mail_sync => 1 }, __PACKAGE__;
97         $lei->{lse} = $lei->_lei_store(1)->write_prepare($lei)->search;
98         if (defined(my $all = $opt->{all})) {
99                 return $lei->fail("--all and @outs incompatible") if @outs;
100                 defined($opt->{mua}) and return
101                         $lei->fail('--all and --mua= are incompatible');
102                 @outs = PublicInbox::LeiSavedSearch::list($lei);
103                 if ($all eq 'local') {
104                         $self->{local} = [ grep(!/$REMOTE_RE/, @outs) ];
105                 } elsif ($all eq 'remote') {
106                         $self->{remote} = [ grep(/$REMOTE_RE/, @outs) ];
107                 } elsif ($all eq '') {
108                         $self->{remote} = [ grep(/$REMOTE_RE/, @outs) ];
109                         $self->{local} = [ grep(!/$REMOTE_RE/, @outs) ];
110                 } else {
111                         $lei->fail("only --all=$all not understood");
112                 }
113         } else {
114                 $self->{remote} = [ grep(/$REMOTE_RE/, @outs) ];
115                 $self->{local} = [ grep(!/$REMOTE_RE/, @outs) ];
116         }
117         ((@{$self->{local} // []} + @{$self->{remote} // []}) > 1 &&
118                 defined($opt->{mua})) and return $lei->fail(<<EOM);
119 multiple outputs and --mua= are incompatible
120 EOM
121         if ($self->{remote}) { # setup lei->{auth}
122                 $self->prepare_inputs($lei, $self->{remote}) or return;
123         }
124         if ($lei->{auth}) { # start auth worker
125                 require PublicInbox::NetWriter;
126                 bless $lei->{net}, 'PublicInbox::NetWriter';
127                 $lei->{auth}->op_merge(my $ops = {}, $self, $lei);
128                 (my $op_c, $ops) = $lei->workers_start($self, 1, $ops);
129                 $lei->{wq1} = $self;
130                 $lei->wait_wq_events($op_c, $ops);
131                 # net_merge_all_done will fire when auth is done
132         } else {
133                 redispatch_all($self, $lei); # see below
134         }
135 }
136
137 # called in top-level lei-daemon when LeiAuth is done
138 sub net_merge_all_done {
139         my ($self, $lei) = @_;
140         $lei->{net} = delete($self->{-net_new}) if $self->{-net_new};
141         $self->wq_close(1);
142         redispatch_all($self, $lei);
143 }
144
145 sub _complete_up {
146         my ($lei, @argv) = @_;
147         my $match_cb = $lei->complete_url_prepare(\@argv);
148         map { $match_cb->($_) } PublicInbox::LeiSavedSearch::list($lei);
149 }
150
151 no warnings 'once';
152 *ipc_atfork_child = \&PublicInbox::LeiInput::input_only_atfork_child;
153
154 1;