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