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