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);
63 my @loc = $self->get_externals($loc) or return;
64 $lxs->prepare_external($_) for @loc;
67 my (@ilocals, @iremotes);
68 for my $loc (@{$opt->{include} // []}) {
69 my @loc = $self->get_externals($loc) or return;
70 $lxs->prepare_external($_) for @loc;
71 @ilocals = @{$lxs->{locals} // []};
72 @iremotes = @{$lxs->{remotes} // []};
74 # --external is enabled by default, but allow --no-external
75 if ($opt->{external} //= 1) {
77 for my $loc (@{$opt->{exclude} // []}) {
78 my @l = $self->get_externals($loc, 1) or return;
81 my $ne = $self->externals_each(\&prep_ext, $lxs, \%x);
82 $opt->{remote} //= !($lxs->locals - $opt->{'local'});
83 if ($opt->{'local'}) {
84 $lxs->{remotes} = \@iremotes if !$opt->{remote};
86 $lxs->{locals} = \@ilocals;
90 unless ($lxs->locals || $lxs->remotes) {
91 return $self->fail('no local or remote inboxes to search');
93 my ($xj, $mj) = split(/,/, $opt->{jobs} // '');
94 if (defined($xj) && $xj ne '' && $xj !~ /\A[1-9][0-9]*\z/) {
95 return $self->fail("`$xj' search jobs must be >= 1");
97 $xj ||= $lxs->concurrency($opt); # allow: "--jobs ,$WRITER_ONLY"
98 my $nproc = $lxs->detect_nproc // 1; # don't memoize, schedtool(1) exists
99 $xj = $nproc if $xj > $nproc;
100 $lxs->{-wq_nr_workers} = $xj;
101 if (defined($mj) && $mj !~ /\A[1-9][0-9]*\z/) {
102 return $self->fail("`$mj' writer jobs must be >= 1");
104 PublicInbox::LeiOverview->new($self) or return;
105 $self->{l2m} and $self->{l2m}->{-wq_nr_workers} = $mj // do {
106 $mj = POSIX::lround($nproc * 3 / 4); # keep some CPU for git
110 my %mset_opt = map { $_ => $opt->{$_} } qw(threads limit offset);
111 $mset_opt{asc} = $opt->{'reverse'} ? 1 : 0;
112 $mset_opt{limit} //= 10000;
113 if (defined(my $sort = $opt->{'sort'})) {
114 if ($sort eq 'relevance') {
115 $mset_opt{relevance} = 1;
116 } elsif ($sort eq 'docid') {
117 $mset_opt{relevance} = $mset_opt{asc} ? -1 : -2;
118 } elsif ($sort =~ /\Areceived(?:-?[aA]t)?\z/) {
121 die "unrecognized --sort=$sort\n";
124 # descending docid order
125 $mset_opt{relevance} //= -2 if $opt->{threads};
126 $self->{mset_opt} = \%mset_opt;
129 return $self->fail(<<'') if @argv;
130 no query allowed on command-line with --stdin
132 require PublicInbox::InputPipe;
133 $self->{lse} = $lse; # for query_approxidate
134 PublicInbox::InputPipe::consume($self->{0}, \&qstr_add, $self);
137 $mset_opt{qstr} = $lse->query_argv_to_string($lse->git, \@argv);
141 # shell completion helper called by lei__complete
143 my ($self, @argv) = @_;
144 my $ext = qr/\A(?:-I|(?:--(?:include|exclude|only)))\z/;
147 if ($argv[-1] =~ $ext) {
148 my @c = $self->_complete_forget_external(@cur);
149 # try basename match:
150 if (scalar(@cur) == 1 && index($cur[0], '/') < 0) {
151 my $all = $self->externals_each;
153 for my $loc (keys %$all) {
154 my $bn = (split(m!/!, $loc))[-1];
158 $bn{$_} == 1 && /\A\Q$cur[0]/
163 unshift(@cur, pop @argv);
168 # Stuff we may pass through to curl (as of 7.64.0), see curl manpage for
169 # details, so most options which make sense for HTTP/HTTPS (including proxy
170 # support for Tor and other methods of getting past weird networks).
171 # Most of these are untested by us, some may not make sense for our use case
172 # and typos below are likely.
173 # n.b. some short options (-$NUMBER) are not supported since they conflict
174 # with other "lei q" switches.
175 # FIXME: Getopt::Long doesn't easily let us support support options with
176 # '.' in them (e.g. --http1.1)
177 # TODO: should we depend on "-c http.*" options for things which have
178 # analogues in git(1)? that would reduce likelyhood of conflicts with
179 # our other CLI options
180 # Note: some names are renamed to avoid potential conflicts,
181 # see %lei2mail in lib/PublicInbox/LeiCurl.pm
184 abstract-unix-socket=s anyauth basic cacert=s capath=s
185 cert-status cert-type cert=s ciphers=s
186 connect-timeout=s connect-to=s cookie-jar=s cookie=s crlfile=s
187 digest disable dns-interface=s dns-ipv4-addr=s dns-ipv6-addr=s
188 dns-servers=s doh-url=s egd-file=s engine=s false-start
189 happy-eyeballs-timeout-ms=s haproxy-protocol header=s@
190 http2-prior-knowledge http2 insecure
191 interface=s ipv4 ipv6 junk-session-cookies
192 key-type=s key=s limit-rate=s local-port=s location-trusted location
193 max-redirs=i max-time=s negotiate netrc-file=s netrc-optional netrc
194 no-alpn no-buffer no-npn no-sessionid noproxy=s ntlm-wb ntlm
195 pass=s pinnedpubkey=s post301 post302 post303 preproxy=s
196 proxy-anyauth proxy-basic proxy-cacert=s proxy-capath=s
197 proxy-cert-type=s proxy-cert=s proxy-ciphers=s proxy-crlfile=s
198 proxy-digest proxy-header=s@ proxy-insecure
199 proxy-key-type=s proxy-key proxy-negotiate proxy-ntlm proxy-pass=s
200 proxy-pinnedpubkey=s proxy-service-name=s proxy-ssl-allow-beast
201 proxy-tls13-ciphers=s proxy-tlsauthtype=s proxy-tlspassword=s
202 proxy-tlsuser=s proxy-tlsv1 proxy-user=s proxy=s
203 proxytunnel=s pubkey=s random-file=s referer=s resolve=s
204 retry-connrefused retry-delay=s retry-max-time=s retry=i
205 sasl-ir service-name=s socks4=s socks4a=s socks5-basic
206 socks5-gssapi-service-name=s socks5-gssapi socks5-hostname=s socks5=s
207 speed-limit speed-type ssl-allow-beast sslv2 sslv3
208 suppress-connect-headers tcp-fastopen tls-max=s
209 tls13-ciphers=s tlsauthtype=s tlspassword=s tlsuser=s
210 tlsv1 trace-ascii=s trace-time trace=s
211 unix-socket=s user-agent=s user=s