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