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