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;
8 use PublicInbox::DS qw(dwaitpid);
10 sub prep_ext { # externals_each callback
11 my ($lxs, $exclude, $loc) = @_;
12 $lxs->prepare_external($loc) unless $exclude->{$loc};
15 # the main "lei q SEARCH_TERMS" method
17 my ($self, @argv) = @_;
18 require PublicInbox::LeiXSearch;
19 require PublicInbox::LeiOverview;
20 require PublicInbox::V2Writable;
21 PublicInbox::Config->json; # preload before forking
22 my $opt = $self->{opt};
23 # prepare any number of LeiXSearch || LeiSearch || Inbox || URL
24 my $lxs = $self->{lxs} = PublicInbox::LeiXSearch->new;
25 my @only = @{$opt->{only} // []};
26 # --local is enabled by default unless --only is used
27 # we'll allow "--only $LOCATION --local"
28 if ($opt->{'local'} //= scalar(@only) ? 0 : 1) {
29 my $sto = $self->_lei_store(1);
30 $lxs->prepare_external($sto->search);
34 $lxs->prepare_external($self->ext_canonicalize($loc));
37 for my $loc (@{$opt->{include} // []}) {
38 $lxs->prepare_external($self->ext_canonicalize($loc));
40 # --external is enabled by default, but allow --no-external
41 if ($opt->{external} //= 1) {
43 ($self->ext_canonicalize($_), 1)
44 } @{$self->{exclude} // []};
45 my $ne = $self->externals_each(\&prep_ext, $lxs, \%x);
46 $opt->{remote} //= !($lxs->locals - $opt->{'local'});
47 if ($opt->{'local'}) {
48 delete($lxs->{remotes}) if !$opt->{remote};
50 delete($lxs->{locals});
54 unless ($lxs->locals || $lxs->remotes) {
55 return $self->fail('no local or remote inboxes to search');
57 my ($xj, $mj) = split(/,/, $opt->{jobs} // '');
58 if (defined($xj) && $xj ne '' && $xj !~ /\A[1-9][0-9]*\z/) {
59 return $self->fail("`$xj' search jobs must be >= 1");
61 $xj ||= $lxs->concurrency($opt); # allow: "--jobs ,$WRITER_ONLY"
62 my $nproc = $lxs->detect_nproc; # don't memoize, schedtool(1) exists
63 $xj = $nproc if $xj > $nproc;
64 PublicInbox::LeiOverview->new($self) or return;
65 $self->atfork_prepare_wq($lxs);
66 $lxs->wq_workers_start('lei_xsearch', $xj, $self->oldset);
67 delete $lxs->{-ipc_atfork_child_close};
68 if (my $l2m = $self->{l2m}) {
69 if (defined($mj) && $mj !~ /\A[1-9][0-9]*\z/) {
70 return $self->fail("`$mj' writer jobs must be >= 1");
73 $self->atfork_prepare_wq($l2m);
74 $l2m->wq_workers_start('lei2mail', $mj, $self->oldset);
75 delete $l2m->{-ipc_atfork_child_close};
78 # no forking workers after this
80 my %mset_opt = map { $_ => $opt->{$_} } qw(thread limit offset);
81 $mset_opt{asc} = $opt->{'reverse'} ? 1 : 0;
82 $mset_opt{limit} //= 10000;
83 $mset_opt{qstr} = join(' ', map {;
84 # Consider spaces in argv to be for phrase search in Xapian.
85 # In other words, the users should need only care about
86 # normal shell quotes and not have to learn Xapian quoting.
87 /\s/ ? (s/\A(\w+:)// ? qq{$1"$_"} : qq{"$_"}) : $_
89 if (defined(my $sort = $opt->{'sort'})) {
90 if ($sort eq 'relevance') {
91 $mset_opt{relevance} = 1;
92 } elsif ($sort eq 'docid') {
93 $mset_opt{relevance} = $mset_opt{asc} ? -1 : -2;
94 } elsif ($sort =~ /\Areceived(?:-?[aA]t)?\z/) {
97 die "unrecognized --sort=$sort\n";
100 # descending docid order
101 $mset_opt{relevance} //= -2 if $opt->{thread};
102 $self->{mset_opt} = \%mset_opt;
103 $self->{ovv}->ovv_begin($self);
104 $lxs->do_query($self);
107 # shell completion helper called by lei__complete
109 my ($self, @argv) = @_;
110 my $ext = qr/\A(?:-I|(?:--(?:include|exclude|only)))\z/;
111 # $argv[-1] =~ $ext and return $self->_complete_forget_external;
114 if ($argv[-1] =~ $ext) {
115 my @c = $self->_complete_forget_external(@cur);
118 unshift(@cur, pop @argv);
123 # Stuff we may pass through to curl (as of 7.64.0), see curl manpage for
124 # details, so most options which make sense for HTTP/HTTPS (including proxy
125 # support for Tor and other methods of getting past weird networks).
126 # Most of these are untested by us, some may not make sense for our use case
127 # and typos below are likely.
128 # n.b. some short options (-$NUMBER) are not supported since they conflict
129 # with other "lei q" switches.
130 # FIXME: Getopt::Long doesn't easily let us support support options with
131 # '.' in them (e.g. --http1.1)
133 abstract-unix-socket=s anyauth basic cacert=s capath=s
134 cert-status cert-type cert|E=s ciphers=s config|K=s@
135 connect-timeout=s connect-to=s cookie-jar|c=s cookie|b=s crlfile=s
136 digest disable dns-interface=s dns-ipv4-addr=s dns-ipv6-addr=s
137 dns-servers=s doh-url=s egd-file=s engine=s false-start
138 happy-eyeballs-timeout-ms=s haproxy-protocol header|H=s@
139 http2-prior-knowledge http2 insecure|k
140 interface=s ipv4 ipv6 junk-session-cookies
141 key-type=s key=s limit-rate=s local-port=s location-trusted location|L
142 max-redirs=i max-time=s negotiate netrc-file=s netrc-optional netrc
143 no-alpn no-buffer|N no-npn no-sessionid noproxy=s ntlm-wb ntlm
144 pass=s pinnedpubkey=s post301 post302 post303 preproxy=s
145 proxy-anyauth proxy-basic proxy-cacert=s proxy-capath=s
146 proxy-cert-type=s proxy-cert=s proxy-ciphers=s proxy-crlfile=s
147 proxy-digest proxy-header=s@ proxy-insecure
148 proxy-key-type=s proxy-key proxy-negotiate proxy-ntlm proxy-pass=s
149 proxy-pinnedpubkey=s proxy-service-name=s proxy-ssl-allow-beast
150 proxy-tls13-ciphers=s proxy-tlsauthtype=s proxy-tlspassword=s
151 proxy-tlsuser=s proxy-tlsv1 proxy-user|U=s proxy=s
152 proxytunnel=s pubkey=s random-file=s referer=s resolve=s
153 retry-connrefused retry-delay=s retry-max-time=s retry=i
154 sasl-ir service-name=s socks4=s socks4a=s socks5-basic
155 socks5-gssapi-service-name=s socks5-gssapi socks5-hostname=s socks5=s
156 speed-limit|Y speed-type|y ssl-allow-beast sslv2 sslv3
157 suppress-connect-headers tcp-fastopen tls-max=s
158 tls13-ciphers=s tlsauthtype=s tlspassword=s tlsuser=s
159 tlsv1 trace-ascii=s trace-time trace=s
160 unix-socket=s user-agent|A=s user|u=s