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