]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiQuery.pm
10b8d6fae68339ab81df2ed1e3c268f204ac102e
[public-inbox.git] / lib / PublicInbox / LeiQuery.pm
1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # handles lei <q|ls-query|rm-query|mv-query> commands
5 package PublicInbox::LeiQuery;
6 use strict;
7 use v5.10.1;
8 use PublicInbox::DS qw(dwaitpid);
9
10 sub prep_ext { # externals_each callback
11         my ($lxs, $exclude, $loc) = @_;
12         $lxs->prepare_external($loc) unless $exclude->{$loc};
13 }
14
15 # the main "lei q SEARCH_TERMS" method
16 sub lei_q {
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);
31         }
32         if (@only) {
33                 for my $loc (@only) {
34                         my @loc = $self->get_externals($loc) or return;
35                         $lxs->prepare_external($_) for @loc;
36                 }
37         } else {
38                 for my $loc (@{$opt->{include} // []}) {
39                         my @loc = $self->get_externals($loc) or return;
40                         $lxs->prepare_external($_) for @loc;
41                 }
42                 # --external is enabled by default, but allow --no-external
43                 if ($opt->{external} //= 1) {
44                         my %x;
45                         for my $loc (@{$opt->{exclude} // []}) {
46                                 my @l = $self->get_externals($loc, 1) or return;
47                                 $x{$_} = 1 for @l;
48                         }
49                         my $ne = $self->externals_each(\&prep_ext, $lxs, \%x);
50                         $opt->{remote} //= !($lxs->locals - $opt->{'local'});
51                         if ($opt->{'local'}) {
52                                 delete($lxs->{remotes}) if !$opt->{remote};
53                         } else {
54                                 delete($lxs->{locals});
55                         }
56                 }
57         }
58         unless ($lxs->locals || $lxs->remotes) {
59                 return $self->fail('no local or remote inboxes to search');
60         }
61         my ($xj, $mj) = split(/,/, $opt->{jobs} // '');
62         if (defined($xj) && $xj ne '' && $xj !~ /\A[1-9][0-9]*\z/) {
63                 return $self->fail("`$xj' search jobs must be >= 1");
64         }
65         $xj ||= $lxs->concurrency($opt); # allow: "--jobs ,$WRITER_ONLY"
66         my $nproc = $lxs->detect_nproc; # don't memoize, schedtool(1) exists
67         $xj = $nproc if $xj > $nproc;
68         PublicInbox::LeiOverview->new($self) or return;
69         $self->atfork_prepare_wq($lxs);
70         $lxs->wq_workers_start('lei_xsearch', $xj, $self->oldset);
71         delete $lxs->{-ipc_atfork_child_close};
72         if (my $l2m = $self->{l2m}) {
73                 if (defined($mj) && $mj !~ /\A[1-9][0-9]*\z/) {
74                         return $self->fail("`$mj' writer jobs must be >= 1");
75                 }
76                 $mj //= $nproc;
77                 $self->atfork_prepare_wq($l2m);
78                 $l2m->wq_workers_start('lei2mail', $mj, $self->oldset);
79                 delete $l2m->{-ipc_atfork_child_close};
80         }
81
82         # no forking workers after this
83
84         my %mset_opt = map { $_ => $opt->{$_} } qw(thread limit offset);
85         $mset_opt{asc} = $opt->{'reverse'} ? 1 : 0;
86         $mset_opt{limit} //= 10000;
87         $mset_opt{qstr} = join(' ', map {;
88                 # Consider spaces in argv to be for phrase search in Xapian.
89                 # In other words, the users should need only care about
90                 # normal shell quotes and not have to learn Xapian quoting.
91                 /\s/ ? (s/\A(\w+:)// ? qq{$1"$_"} : qq{"$_"}) : $_
92         } @argv);
93         if (defined(my $sort = $opt->{'sort'})) {
94                 if ($sort eq 'relevance') {
95                         $mset_opt{relevance} = 1;
96                 } elsif ($sort eq 'docid') {
97                         $mset_opt{relevance} = $mset_opt{asc} ? -1 : -2;
98                 } elsif ($sort =~ /\Areceived(?:-?[aA]t)?\z/) {
99                         # the default
100                 } else {
101                         die "unrecognized --sort=$sort\n";
102                 }
103         }
104         # descending docid order
105         $mset_opt{relevance} //= -2 if $opt->{thread};
106         $self->{mset_opt} = \%mset_opt;
107         $self->{ovv}->ovv_begin($self);
108         $lxs->do_query($self);
109 }
110
111 # shell completion helper called by lei__complete
112 sub _complete_q {
113         my ($self, @argv) = @_;
114         my $ext = qr/\A(?:-I|(?:--(?:include|exclude|only)))\z/;
115         # $argv[-1] =~ $ext and return $self->_complete_forget_external;
116         my @cur;
117         while (@argv) {
118                 if ($argv[-1] =~ $ext) {
119                         my @c = $self->_complete_forget_external(@cur);
120                         return @c if @c;
121                 }
122                 unshift(@cur, pop @argv);
123         }
124         ();
125 }
126
127 # Stuff we may pass through to curl (as of 7.64.0), see curl manpage for
128 # details, so most options which make sense for HTTP/HTTPS (including proxy
129 # support for Tor and other methods of getting past weird networks).
130 # Most of these are untested by us, some may not make sense for our use case
131 # and typos below are likely.
132 # n.b. some short options (-$NUMBER) are not supported since they conflict
133 # with other "lei q" switches.
134 # FIXME: Getopt::Long doesn't easily let us support support options with
135 # '.' in them (e.g. --http1.1)
136 sub curl_opt { qw(
137         abstract-unix-socket=s anyauth basic cacert=s capath=s
138         cert-status cert-type cert|E=s ciphers=s config|K=s@
139         connect-timeout=s connect-to=s cookie-jar|c=s cookie|b=s crlfile=s
140         digest disable dns-interface=s dns-ipv4-addr=s dns-ipv6-addr=s
141         dns-servers=s doh-url=s egd-file=s engine=s false-start
142         happy-eyeballs-timeout-ms=s haproxy-protocol header|H=s@
143         http2-prior-knowledge http2 insecure|k
144         interface=s ipv4 ipv6 junk-session-cookies
145         key-type=s key=s limit-rate=s local-port=s location-trusted location|L
146         max-redirs=i max-time=s negotiate netrc-file=s netrc-optional netrc
147         no-alpn no-buffer|N no-npn no-sessionid noproxy=s ntlm-wb ntlm
148         pass=s pinnedpubkey=s post301 post302 post303 preproxy=s
149         proxy-anyauth proxy-basic proxy-cacert=s proxy-capath=s
150         proxy-cert-type=s proxy-cert=s proxy-ciphers=s proxy-crlfile=s
151         proxy-digest proxy-header=s@ proxy-insecure
152         proxy-key-type=s proxy-key proxy-negotiate proxy-ntlm proxy-pass=s
153         proxy-pinnedpubkey=s proxy-service-name=s proxy-ssl-allow-beast
154         proxy-tls13-ciphers=s proxy-tlsauthtype=s proxy-tlspassword=s
155         proxy-tlsuser=s proxy-tlsv1 proxy-user|U=s proxy=s
156         proxytunnel=s pubkey=s random-file=s referer=s resolve=s
157         retry-connrefused retry-delay=s retry-max-time=s retry=i
158         sasl-ir service-name=s socks4=s socks4a=s socks5-basic
159         socks5-gssapi-service-name=s socks5-gssapi socks5-hostname=s socks5=s
160         speed-limit|Y speed-type|y ssl-allow-beast sslv2 sslv3
161         suppress-connect-headers tcp-fastopen tls-max=s
162         tls13-ciphers=s tlsauthtype=s tlspassword=s tlsuser=s
163         tlsv1 trace-ascii=s trace-time trace=s
164         unix-socket=s user-agent|A=s user|u=s
165 )
166 }
167
168 1;