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;
9 sub prep_ext { # externals_each callback
10 my ($lxs, $exclude, $loc) = @_;
11 $lxs->prepare_external($loc) unless $exclude->{$loc};
16 if (my $nwr = $self->{nwr}) {
17 require PublicInbox::LeiAuth;
18 my $auth = $self->{auth} = PublicInbox::LeiAuth->new($nwr);
19 my $lxs = $self->{lxs};
20 $auth->auth_start($self, $lxs->can('do_query'), $lxs, $self);
22 $self->{lxs}->do_query($self);
26 sub qstr_add { # PublicInbox::InputPipe::consume callback for --stdin
27 my ($self) = @_; # $_[1] = $rbuf
29 $_[1] eq '' and return eval {
30 my $lse = delete $self->{lse};
31 $lse->query_approxidate($lse->git,
32 $self->{mset_opt}->{qstr});
35 $self->{mset_opt}->{qstr} .= $_[1];
37 $self->fail("error reading stdin: $!");
41 # the main "lei q SEARCH_TERMS" method
43 my ($self, @argv) = @_;
44 require PublicInbox::LeiXSearch;
45 require PublicInbox::LeiOverview;
46 require PublicInbox::V2Writable;
47 PublicInbox::Config->json; # preload before forking
48 my $opt = $self->{opt};
49 # prepare any number of LeiXSearch || LeiSearch || Inbox || URL
50 my $lxs = $self->{lxs} = PublicInbox::LeiXSearch->new;
51 my @only = @{$opt->{only} // []};
52 # --local is enabled by default unless --only is used
53 # we'll allow "--only $LOCATION --local"
54 my $sto = $self->_lei_store(1);
55 my $lse = $sto->search;
56 if ($opt->{'local'} //= scalar(@only) ? 0 : 1) {
57 $lxs->prepare_external($lse);
61 my @loc = $self->get_externals($loc) or return;
62 $lxs->prepare_external($_) for @loc;
65 for my $loc (@{$opt->{include} // []}) {
66 my @loc = $self->get_externals($loc) or return;
67 $lxs->prepare_external($_) for @loc;
69 # --external is enabled by default, but allow --no-external
70 if ($opt->{external} //= 1) {
72 for my $loc (@{$opt->{exclude} // []}) {
73 my @l = $self->get_externals($loc, 1) or return;
76 my $ne = $self->externals_each(\&prep_ext, $lxs, \%x);
77 $opt->{remote} //= !($lxs->locals - $opt->{'local'});
78 if ($opt->{'local'}) {
79 delete($lxs->{remotes}) if !$opt->{remote};
81 delete($lxs->{locals});
85 unless ($lxs->locals || $lxs->remotes) {
86 return $self->fail('no local or remote inboxes to search');
88 my ($xj, $mj) = split(/,/, $opt->{jobs} // '');
89 if (defined($xj) && $xj ne '' && $xj !~ /\A[1-9][0-9]*\z/) {
90 return $self->fail("`$xj' search jobs must be >= 1");
92 $xj ||= $lxs->concurrency($opt); # allow: "--jobs ,$WRITER_ONLY"
93 my $nproc = $lxs->detect_nproc; # don't memoize, schedtool(1) exists
94 $xj = $nproc if $xj > $nproc;
96 if (defined($mj) && $mj !~ /\A[1-9][0-9]*\z/) {
97 return $self->fail("`$mj' writer jobs must be >= 1");
99 $self->{l2m}->{jobs} = ($mj // $nproc) if $self->{l2m};
100 PublicInbox::LeiOverview->new($self) or return;
102 my %mset_opt = map { $_ => $opt->{$_} } qw(threads limit offset);
103 $mset_opt{asc} = $opt->{'reverse'} ? 1 : 0;
104 $mset_opt{limit} //= 10000;
105 if (defined(my $sort = $opt->{'sort'})) {
106 if ($sort eq 'relevance') {
107 $mset_opt{relevance} = 1;
108 } elsif ($sort eq 'docid') {
109 $mset_opt{relevance} = $mset_opt{asc} ? -1 : -2;
110 } elsif ($sort =~ /\Areceived(?:-?[aA]t)?\z/) {
113 die "unrecognized --sort=$sort\n";
116 # descending docid order
117 $mset_opt{relevance} //= -2 if $opt->{threads};
118 $self->{mset_opt} = \%mset_opt;
121 return $self->fail(<<'') if @argv;
122 no query allowed on command-line with --stdin
124 require PublicInbox::InputPipe;
125 $self->{lse} = $lse; # for query_approxidate
126 PublicInbox::InputPipe::consume($self->{0}, \&qstr_add, $self);
129 $mset_opt{qstr} = $lse->query_argv_to_string($lse->git, \@argv);
133 # shell completion helper called by lei__complete
135 my ($self, @argv) = @_;
136 my $ext = qr/\A(?:-I|(?:--(?:include|exclude|only)))\z/;
139 if ($argv[-1] =~ $ext) {
140 my @c = $self->_complete_forget_external(@cur);
141 # try basename match:
142 if (scalar(@cur) == 1 && index($cur[0], '/') < 0) {
143 my $all = $self->externals_each;
145 for my $loc (keys %$all) {
146 my $bn = (split(m!/!, $loc))[-1];
150 $bn{$_} == 1 && /\A\Q$cur[0]/
155 unshift(@cur, pop @argv);
160 # Stuff we may pass through to curl (as of 7.64.0), see curl manpage for
161 # details, so most options which make sense for HTTP/HTTPS (including proxy
162 # support for Tor and other methods of getting past weird networks).
163 # Most of these are untested by us, some may not make sense for our use case
164 # and typos below are likely.
165 # n.b. some short options (-$NUMBER) are not supported since they conflict
166 # with other "lei q" switches.
167 # FIXME: Getopt::Long doesn't easily let us support support options with
168 # '.' in them (e.g. --http1.1)
169 # TODO: should we depend on "-c http.*" options for things which have
170 # analogues in git(1)? that would reduce likelyhood of conflicts with
171 # our other CLI options
172 # Note: some names are renamed to avoid potential conflicts,
173 # see %lei2mail in lib/PublicInbox/LeiCurl.pm
176 abstract-unix-socket=s anyauth basic cacert=s capath=s
177 cert-status cert-type cert=s ciphers=s
178 connect-timeout=s connect-to=s cookie-jar=s cookie=s crlfile=s
179 digest disable dns-interface=s dns-ipv4-addr=s dns-ipv6-addr=s
180 dns-servers=s doh-url=s egd-file=s engine=s false-start
181 happy-eyeballs-timeout-ms=s haproxy-protocol header=s@
182 http2-prior-knowledge http2 insecure
183 interface=s ipv4 ipv6 junk-session-cookies
184 key-type=s key=s limit-rate=s local-port=s location-trusted location
185 max-redirs=i max-time=s negotiate netrc-file=s netrc-optional netrc
186 no-alpn no-buffer no-npn no-sessionid noproxy=s ntlm-wb ntlm
187 pass=s pinnedpubkey=s post301 post302 post303 preproxy=s
188 proxy-anyauth proxy-basic proxy-cacert=s proxy-capath=s
189 proxy-cert-type=s proxy-cert=s proxy-ciphers=s proxy-crlfile=s
190 proxy-digest proxy-header=s@ proxy-insecure
191 proxy-key-type=s proxy-key proxy-negotiate proxy-ntlm proxy-pass=s
192 proxy-pinnedpubkey=s proxy-service-name=s proxy-ssl-allow-beast
193 proxy-tls13-ciphers=s proxy-tlsauthtype=s proxy-tlspassword=s
194 proxy-tlsuser=s proxy-tlsv1 proxy-user=s proxy=s
195 proxytunnel=s pubkey=s random-file=s referer=s resolve=s
196 retry-connrefused retry-delay=s retry-max-time=s retry=i
197 sasl-ir service-name=s socks4=s socks4a=s socks5-basic
198 socks5-gssapi-service-name=s socks5-gssapi socks5-hostname=s socks5=s
199 speed-limit speed-type ssl-allow-beast sslv2 sslv3
200 suppress-connect-headers tcp-fastopen tls-max=s
201 tls13-ciphers=s tlsauthtype=s tlspassword=s tlsuser=s
202 tlsv1 trace-ascii=s trace-time trace=s
203 unix-socket=s user-agent=s user=s