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 # handles lei <q|ls-query|rm-query|mv-query> commands
5 package PublicInbox::LeiQuery;
10 sub prep_ext { # externals_each callback
11 my ($lxs, $exclude, $loc) = @_;
12 $lxs->prepare_external($loc) unless $exclude->{$loc};
18 require PublicInbox::LeiAuth;
19 $self->{auth} = PublicInbox::LeiAuth->new
21 $self->{lxs}->do_query($self);
24 sub qstr_add { # PublicInbox::InputPipe::consume callback for --stdin
25 my ($self) = @_; # $_[1] = $rbuf
27 $_[1] eq '' and return eval {
28 my $lse = delete $self->{lse};
29 $lse->query_approxidate($lse->git,
30 $self->{mset_opt}->{qstr});
33 $self->{mset_opt}->{qstr} .= $_[1];
35 $self->fail("error reading stdin: $!");
39 # the main "lei q SEARCH_TERMS" method
41 my ($self, @argv) = @_;
42 require PublicInbox::LeiXSearch;
43 require PublicInbox::LeiOverview;
44 require PublicInbox::V2Writable;
45 PublicInbox::Config->json; # preload before forking
46 my $opt = $self->{opt};
47 # prepare any number of LeiXSearch || LeiSearch || Inbox || URL
48 my $lxs = $self->{lxs} = PublicInbox::LeiXSearch->new;
49 my @only = @{$opt->{only} // []};
50 # --local is enabled by default unless --only is used
51 # we'll allow "--only $LOCATION --local"
52 my $sto = $self->_lei_store(1);
53 my $lse = $sto->search;
54 if (($opt->{'import-remote'} //= 1) |
55 (($opt->{'import-before'} //= \1) ? 1 : 0)) {
56 $sto->write_prepare($self);
58 if ($opt->{'local'} //= scalar(@only) ? 0 : 1) {
59 $lxs->prepare_external($lse);
61 my $tmp = PublicInbox::LeiXSearch->new;
62 $tmp->prepare_external($lse);
63 $self->ale->refresh_externals($tmp);
67 my @loc = $self->get_externals($loc) or return;
68 $lxs->prepare_external($_) for @loc;
71 my (@ilocals, @iremotes);
72 for my $loc (@{$opt->{include} // []}) {
73 my @loc = $self->get_externals($loc) or return;
74 $lxs->prepare_external($_) for @loc;
75 @ilocals = @{$lxs->{locals} // []};
76 @iremotes = @{$lxs->{remotes} // []};
78 # --external is enabled by default, but allow --no-external
79 if ($opt->{external} //= 1) {
81 for my $loc (@{$opt->{exclude} // []}) {
82 my @l = $self->get_externals($loc, 1) or return;
85 my $ne = $self->externals_each(\&prep_ext, $lxs, \%x);
86 $opt->{remote} //= !($lxs->locals - $opt->{'local'});
87 if ($opt->{'local'}) {
88 $lxs->{remotes} = \@iremotes if !$opt->{remote};
90 $lxs->{locals} = \@ilocals;
94 unless ($lxs->locals || $lxs->remotes) {
95 return $self->fail('no local or remote inboxes to search');
97 $self->ale->refresh_externals($lxs);
98 my ($xj, $mj) = split(/,/, $opt->{jobs} // '');
99 if (defined($xj) && $xj ne '' && $xj !~ /\A[1-9][0-9]*\z/) {
100 return $self->fail("`$xj' search jobs must be >= 1");
102 $xj ||= $lxs->concurrency($opt); # allow: "--jobs ,$WRITER_ONLY"
103 my $nproc = $lxs->detect_nproc // 1; # don't memoize, schedtool(1) exists
104 $xj = $nproc if $xj > $nproc;
105 $lxs->{-wq_nr_workers} = $xj;
106 if (defined($mj) && $mj !~ /\A[1-9][0-9]*\z/) {
107 return $self->fail("`$mj' writer jobs must be >= 1");
109 PublicInbox::LeiOverview->new($self) or return;
110 $self->{l2m} and $self->{l2m}->{-wq_nr_workers} = $mj // do {
111 $mj = POSIX::lround($nproc * 3 / 4); # keep some CPU for git
115 my %mset_opt = map { $_ => $opt->{$_} } qw(threads limit offset);
116 $mset_opt{asc} = $opt->{'reverse'} ? 1 : 0;
117 $mset_opt{limit} //= 10000;
118 if (defined(my $sort = $opt->{'sort'})) {
119 if ($sort eq 'relevance') {
120 $mset_opt{relevance} = 1;
121 } elsif ($sort eq 'docid') {
122 $mset_opt{relevance} = $mset_opt{asc} ? -1 : -2;
123 } elsif ($sort =~ /\Areceived(?:-?[aA]t)?\z/) {
126 die "unrecognized --sort=$sort\n";
129 # descending docid order
130 $mset_opt{relevance} //= -2 if $opt->{threads};
131 $self->{mset_opt} = \%mset_opt;
134 return $self->fail(<<'') if @argv;
135 no query allowed on command-line with --stdin
137 require PublicInbox::InputPipe;
138 $self->{lse} = $lse; # for query_approxidate
139 PublicInbox::InputPipe::consume($self->{0}, \&qstr_add, $self);
142 $mset_opt{qstr} = $lse->query_argv_to_string($lse->git, \@argv);
146 # shell completion helper called by lei__complete
148 my ($self, @argv) = @_;
149 my $ext = qr/\A(?:-I|(?:--(?:include|exclude|only)))\z/;
152 if ($argv[-1] =~ $ext) {
153 my @c = $self->_complete_forget_external(@cur);
154 # try basename match:
155 if (scalar(@cur) == 1 && index($cur[0], '/') < 0) {
156 my $all = $self->externals_each;
158 for my $loc (keys %$all) {
159 my $bn = (split(m!/!, $loc))[-1];
163 $bn{$_} == 1 && /\A\Q$cur[0]/
168 unshift(@cur, pop @argv);
173 # Stuff we may pass through to curl (as of 7.64.0), see curl manpage for
174 # details, so most options which make sense for HTTP/HTTPS (including proxy
175 # support for Tor and other methods of getting past weird networks).
176 # Most of these are untested by us, some may not make sense for our use case
177 # and typos below are likely.
178 # n.b. some short options (-$NUMBER) are not supported since they conflict
179 # with other "lei q" switches.
180 # FIXME: Getopt::Long doesn't easily let us support support options with
181 # '.' in them (e.g. --http1.1)
182 # TODO: should we depend on "-c http.*" options for things which have
183 # analogues in git(1)? that would reduce likelyhood of conflicts with
184 # our other CLI options
185 # Note: some names are renamed to avoid potential conflicts,
186 # see %lei2mail in lib/PublicInbox/LeiCurl.pm
189 abstract-unix-socket=s anyauth basic cacert=s capath=s
190 cert-status cert-type cert=s ciphers=s
191 connect-timeout=s connect-to=s cookie-jar=s cookie=s crlfile=s
192 digest disable dns-interface=s dns-ipv4-addr=s dns-ipv6-addr=s
193 dns-servers=s doh-url=s egd-file=s engine=s false-start
194 happy-eyeballs-timeout-ms=s haproxy-protocol header=s@
195 http2-prior-knowledge http2 insecure
196 interface=s ipv4 ipv6 junk-session-cookies
197 key-type=s key=s limit-rate=s local-port=s location-trusted location
198 max-redirs=i max-time=s negotiate netrc-file=s netrc-optional netrc
199 no-alpn no-buffer no-npn no-sessionid noproxy=s ntlm-wb ntlm
200 pass=s pinnedpubkey=s post301 post302 post303 preproxy=s
201 proxy-anyauth proxy-basic proxy-cacert=s proxy-capath=s
202 proxy-cert-type=s proxy-cert=s proxy-ciphers=s proxy-crlfile=s
203 proxy-digest proxy-header=s@ proxy-insecure
204 proxy-key-type=s proxy-key proxy-negotiate proxy-ntlm proxy-pass=s
205 proxy-pinnedpubkey=s proxy-service-name=s proxy-ssl-allow-beast
206 proxy-tls13-ciphers=s proxy-tlsauthtype=s proxy-tlspassword=s
207 proxy-tlsuser=s proxy-tlsv1 proxy-user=s proxy=s
208 proxytunnel=s pubkey=s random-file=s referer=s resolve=s
209 retry-connrefused retry-delay=s retry-max-time=s retry=i
210 sasl-ir service-name=s socks4=s socks4a=s socks5-basic
211 socks5-gssapi-service-name=s socks5-gssapi socks5-hostname=s socks5=s
212 speed-limit speed-type ssl-allow-beast sslv2 sslv3
213 suppress-connect-headers tcp-fastopen tls-max=s
214 tls13-ciphers=s tlsauthtype=s tlspassword=s tlsuser=s
215 tlsv1 trace-ascii=s trace-time trace=s
216 unix-socket=s user-agent=s user=s