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 # the main "lei q SEARCH_TERMS" method
12 my ($self, @argv) = @_;
13 require PublicInbox::LeiXSearch;
14 require PublicInbox::LeiOverview;
15 PublicInbox::Config->json; # preload before forking
16 my $opt = $self->{opt};
17 my $lxs = $self->{lxs} = PublicInbox::LeiXSearch->new;
18 # any number of LeiXSearch || LeiSearch || Inbox
19 if ($opt->{'local'} //= 1) { # --local is enabled by default
20 my $sto = $self->_lei_store(1);
21 $lxs->prepare_external($sto->search);
24 # --external is enabled by default, but allow --no-external
25 if ($opt->{external} //= 1) {
26 my $cb = $lxs->can('prepare_external');
27 $self->_externals_each($cb, $lxs);
29 my $xj = $opt->{thread} ? $lxs->locals : ($lxs->remotes + 1);
30 my $ovv = PublicInbox::LeiOverview->new($self) or return;
31 $self->atfork_prepare_wq($lxs);
32 $lxs->wq_workers_start('lei_xsearch', $xj, $self->oldset);
33 delete $lxs->{-ipc_atfork_child_close};
34 if (my $l2m = $self->{l2m}) {
35 my $mj = 4; # TODO: configurable
36 $self->atfork_prepare_wq($l2m);
37 $l2m->wq_workers_start('lei2mail', $mj, $self->oldset);
38 delete $l2m->{-ipc_atfork_child_close};
41 # no forking workers after this
43 my %mset_opt = map { $_ => $opt->{$_} } qw(thread limit offset);
44 $mset_opt{asc} = $opt->{'reverse'} ? 1 : 0;
45 $mset_opt{qstr} = join(' ', map {;
46 # Consider spaces in argv to be for phrase search in Xapian.
47 # In other words, the users should need only care about
48 # normal shell quotes and not have to learn Xapian quoting.
49 /\s/ ? (s/\A(\w+:)// ? qq{$1"$_"} : qq{"$_"}) : $_
51 if (defined(my $sort = $opt->{'sort'})) {
52 if ($sort eq 'relevance') {
53 $mset_opt{relevance} = 1;
54 } elsif ($sort eq 'docid') {
55 $mset_opt{relevance} = $mset_opt{asc} ? -1 : -2;
56 } elsif ($sort =~ /\Areceived(?:-?[aA]t)?\z/) {
59 die "unrecognized --sort=$sort\n";
62 # descending docid order
63 $mset_opt{relevance} //= -2 if $opt->{thread};
64 $self->{mset_opt} = \%mset_opt;
65 $ovv->ovv_begin($self);
66 $lxs->do_query($self);
69 # Stuff we may pass through to curl (as of 7.64.0), see curl manpage for
70 # details, so most options which make sense for HTTP/HTTPS (including proxy
71 # support for Tor and other methods of getting past weird networks).
72 # Most of these are untested by us, some may not make sense for our use case
73 # and typos below are likely.
74 # n.b. some short options (-$NUMBER) are not supported since they conflict
75 # with other "lei q" switches.
76 # FIXME: Getopt::Long doesn't easily let us support support options with
77 # '.' in them (e.g. --http1.1)
79 abstract-unix-socket=s anyauth basic cacert=s capath=s
80 cert-status cert-type cert|E=s ciphers=s config|K=s@
81 connect-timeout=s connect-to=s cookie-jar|c=s cookie|b=s crlfile=s
82 digest disable dns-interface=s dns-ipv4-addr=s dns-ipv6-addr=s
83 dns-servers=s doh-url=s egd-file=s engine=s false-start
84 happy-eyeballs-timeout-ms=s haproxy-protocol header|H=s@
85 http2-prior-knowledge http2 insecure|k
86 interface=s ipv4 ipv6 junk-session-cookies
87 key-type=s key=s limit-rate=s local-port=s location-trusted location|L
88 max-redirs=i max-time=s negotiate netrc-file=s netrc-optional netrc
89 no-alpn no-buffer|N no-npn no-sessionid noproxy=s ntlm-wb ntlm
90 pass=s pinnedpubkey=s post301 post302 post303 preproxy=s
91 proxy-anyauth proxy-basic proxy-cacert=s proxy-capath=s
92 proxy-cert-type=s proxy-cert=s proxy-ciphers=s proxy-crlfile=s
93 proxy-digest proxy-header=s@ proxy-insecure
94 proxy-key-type=s proxy-key proxy-negotiate proxy-ntlm proxy-pass=s
95 proxy-pinnedpubkey=s proxy-service-name=s proxy-ssl-allow-beast
96 proxy-tls13-ciphers=s proxy-tlsauthtype=s proxy-tlspassword=s
97 proxy-tlsuser=s proxy-tlsv1 proxy-user|U=s proxy=s
98 proxytunnel=s pubkey=s random-file=s referer=s resolve=s
99 retry-connrefused retry-delay=s retry-max-time=s retry=i
100 sasl-ir service-name=s socks4=s socks4a=s socks5-basic
101 socks5-gssapi-service-name=s socks5-gssapi socks5-hostname=s socks5=s
102 speed-limit|Y speed-type|y ssl-allow-beast sslv2 sslv3
103 suppress-connect-headers tcp-fastopen tls-max=s
104 tls13-ciphers=s tlsauthtype=s tlspassword=s tlsuser=s
105 tlsv1 trace-ascii=s trace-time trace=s
106 unix-socket=s user-agent|A=s user|u=s