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};
15 sub _start_query { # used by "lei q" and "lei up"
17 require PublicInbox::LeiOverview;
18 PublicInbox::LeiOverview->new($self) or return;
19 my $opt = $self->{opt};
20 my ($xj, $mj) = split(/,/, $opt->{jobs} // '');
21 if (defined($xj) && $xj ne '' && $xj !~ /\A[1-9][0-9]*\z/) {
22 return $self->fail("`$xj' search jobs must be >= 1");
24 my $lxs = $self->{lxs};
25 $xj ||= $lxs->concurrency($opt); # allow: "--jobs ,$WRITER_ONLY"
26 my $nproc = $lxs->detect_nproc // 1; # don't memoize, schedtool(1) exists
27 $xj = $nproc if $xj > $nproc;
28 $lxs->{-wq_nr_workers} = $xj;
29 if (defined($mj) && $mj !~ /\A[1-9][0-9]*\z/) {
30 return $self->fail("`$mj' writer jobs must be >= 1");
32 my $l2m = $self->{l2m};
33 if ($l2m && ($opt->{'import-remote'} //= 1) |
34 # we use \1 (a ref) to distinguish between
35 # user-supplied and default value
36 (($opt->{'import-before'} //= \1) ? 1 : 0)) {
37 $self->_lei_store(1)->write_prepare($self);
39 $l2m and $l2m->{-wq_nr_workers} = $mj // do {
40 $mj = POSIX::lround($nproc * 3 / 4); # keep some CPU for git
44 # descending docid order is cheapest, MUA controls sorting order
45 $self->{mset_opt}->{relevance} //= -2 if $l2m || $opt->{threads};
47 require PublicInbox::LeiAuth;
48 $self->{auth} = PublicInbox::LeiAuth->new
50 $lxs->do_query($self);
53 sub qstr_add { # PublicInbox::InputPipe::consume callback for --stdin
54 my ($self) = @_; # $_[1] = $rbuf
56 $_[1] eq '' and return eval {
57 if (my $dfd = $self->{3}) {
58 chdir($dfd) or return $self->fail("fchdir: $!");
60 $self->{mset_opt}->{q_raw} = $self->{mset_opt}->{qstr};
61 $self->{lse}->query_approxidate($self->{lse}->git,
62 $self->{mset_opt}->{qstr});
65 $self->{mset_opt}->{qstr} .= $_[1];
67 $self->fail("error reading stdin: $!");
73 require PublicInbox::LeiXSearch;
74 # prepare any number of LeiXSearch || LeiSearch || Inbox || URL
75 my $lxs = $self->{lxs} = PublicInbox::LeiXSearch->new;
76 my $opt = $self->{opt};
77 my @only = @{$opt->{only} // []};
78 # --local is enabled by default unless --only is used
79 # we'll allow "--only $LOCATION --local"
80 my $sto = $self->_lei_store(1); # FIXME: should not create
81 $self->{lse} = $sto->search;
82 if ($opt->{'local'} //= scalar(@only) ? 0 : 1) {
83 $lxs->prepare_external($self->{lse});
87 my @loc = $self->get_externals($loc) or return;
88 $lxs->prepare_external($_) for @loc;
91 my (@ilocals, @iremotes);
92 for my $loc (@{$opt->{include} // []}) {
93 my @loc = $self->get_externals($loc) or return;
94 $lxs->prepare_external($_) for @loc;
95 @ilocals = @{$lxs->{locals} // []};
96 @iremotes = @{$lxs->{remotes} // []};
98 # --external is enabled by default, but allow --no-external
99 if ($opt->{external} //= 1) {
101 for my $loc (@{$opt->{exclude} // []}) {
102 my @l = $self->get_externals($loc, 1) or return;
105 my $ne = $self->externals_each(\&prep_ext, $lxs, \%x);
106 $opt->{remote} //= !($lxs->locals - $opt->{'local'});
107 if ($opt->{'local'}) {
108 $lxs->{remotes} = \@iremotes if !$opt->{remote};
110 $lxs->{locals} = \@ilocals;
114 ($lxs->locals || $lxs->remotes) ? ($self->{lxs} = $lxs) :
115 $self->fail('no local or remote inboxes to search');
118 # the main "lei q SEARCH_TERMS" method
120 my ($self, @argv) = @_;
121 PublicInbox::Config->json; # preload before forking
122 my $lxs = lxs_prepare($self) or return;
123 $self->ale->refresh_externals($lxs);
124 my $opt = $self->{opt};
125 my %mset_opt = map { $_ => $opt->{$_} } qw(threads limit offset);
126 $mset_opt{asc} = $opt->{'reverse'} ? 1 : 0;
127 $mset_opt{limit} //= 10000;
128 if (defined(my $sort = $opt->{'sort'})) {
129 if ($sort eq 'relevance') {
130 $mset_opt{relevance} = 1;
131 } elsif ($sort eq 'docid') {
132 $mset_opt{relevance} = $mset_opt{asc} ? -1 : -2;
133 } elsif ($sort =~ /\Areceived(?:-?[aA]t)?\z/) {
136 die "unrecognized --sort=$sort\n";
139 $self->{mset_opt} = \%mset_opt;
142 return $self->fail(<<'') if @argv;
143 no query allowed on command-line with --stdin
145 require PublicInbox::InputPipe;
146 PublicInbox::InputPipe::consume($self->{0}, \&qstr_add, $self);
149 $mset_opt{q_raw} = [ @argv ]; # copy
151 $self->{lse}->query_argv_to_string($self->{lse}->git, \@argv);
155 # shell completion helper called by lei__complete
157 my ($self, @argv) = @_;
158 my $ext = qr/\A(?:-I|(?:--(?:include|exclude|only)))\z/;
161 if ($argv[-1] =~ $ext) {
162 my @c = $self->_complete_forget_external(@cur);
163 # try basename match:
164 if (scalar(@cur) == 1 && index($cur[0], '/') < 0) {
165 my $all = $self->externals_each;
167 for my $loc (keys %$all) {
168 my $bn = (split(m!/!, $loc))[-1];
172 $bn{$_} == 1 && /\A\Q$cur[0]/
177 unshift(@cur, pop @argv);
182 # Stuff we may pass through to curl (as of 7.64.0), see curl manpage for
183 # details, so most options which make sense for HTTP/HTTPS (including proxy
184 # support for Tor and other methods of getting past weird networks).
185 # Most of these are untested by us, some may not make sense for our use case
186 # and typos below are likely.
187 # n.b. some short options (-$NUMBER) are not supported since they conflict
188 # with other "lei q" switches.
189 # FIXME: Getopt::Long doesn't easily let us support support options with
190 # '.' in them (e.g. --http1.1)
191 # TODO: should we depend on "-c http.*" options for things which have
192 # analogues in git(1)? that would reduce likelyhood of conflicts with
193 # our other CLI options
194 # Note: some names are renamed to avoid potential conflicts,
195 # see %lei2mail in lib/PublicInbox/LeiCurl.pm
198 abstract-unix-socket=s anyauth basic cacert=s capath=s
199 cert-status cert-type cert=s ciphers=s
200 connect-timeout=s connect-to=s cookie-jar=s cookie=s crlfile=s
201 digest disable dns-interface=s dns-ipv4-addr=s dns-ipv6-addr=s
202 dns-servers=s doh-url=s egd-file=s engine=s false-start
203 happy-eyeballs-timeout-ms=s haproxy-protocol header=s@
204 http2-prior-knowledge http2 insecure
205 interface=s ipv4 ipv6 junk-session-cookies
206 key-type=s key=s limit-rate=s local-port=s location-trusted location
207 max-redirs=i max-time=s negotiate netrc-file=s netrc-optional netrc
208 no-alpn no-buffer no-npn no-sessionid noproxy=s ntlm-wb ntlm
209 pass=s pinnedpubkey=s post301 post302 post303 preproxy=s
210 proxy-anyauth proxy-basic proxy-cacert=s proxy-capath=s
211 proxy-cert-type=s proxy-cert=s proxy-ciphers=s proxy-crlfile=s
212 proxy-digest proxy-header=s@ proxy-insecure
213 proxy-key-type=s proxy-key proxy-negotiate proxy-ntlm proxy-pass=s
214 proxy-pinnedpubkey=s proxy-service-name=s proxy-ssl-allow-beast
215 proxy-tls13-ciphers=s proxy-tlsauthtype=s proxy-tlspassword=s
216 proxy-tlsuser=s proxy-tlsv1 proxy-user=s proxy=s
217 proxytunnel=s pubkey=s random-file=s referer=s resolve=s
218 retry-connrefused retry-delay=s retry-max-time=s retry=i
219 sasl-ir service-name=s socks4=s socks4a=s socks5-basic
220 socks5-gssapi-service-name=s socks5-gssapi socks5-hostname=s socks5=s
221 speed-limit speed-type ssl-allow-beast sslv2 sslv3
222 suppress-connect-headers tcp-fastopen tls-max=s
223 tls13-ciphers=s tlsauthtype=s tlspassword=s tlsuser=s
224 tlsv1 trace-ascii=s trace-time trace=s
225 unix-socket=s user-agent=s user=s