]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiQuery.pm
lei_query: trim curl options
[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
9 sub prep_ext { # externals_each callback
10         my ($lxs, $exclude, $loc) = @_;
11         $lxs->prepare_external($loc) unless $exclude->{$loc};
12 }
13
14 sub qstr_add { # for --stdin
15         my ($self) = @_; # $_[1] = $rbuf
16         if (defined($_[1])) {
17                 return eval { $self->{lxs}->do_query($self) } if $_[1] eq '';
18                 $self->{mset_opt}->{qstr} .= $_[1];
19         } else {
20                 $self->fail("error reading stdin: $!");
21         }
22 }
23
24 # the main "lei q SEARCH_TERMS" method
25 sub lei_q {
26         my ($self, @argv) = @_;
27         require PublicInbox::LeiXSearch;
28         require PublicInbox::LeiOverview;
29         require PublicInbox::V2Writable;
30         PublicInbox::Config->json; # preload before forking
31         my $opt = $self->{opt};
32         # prepare any number of LeiXSearch || LeiSearch || Inbox || URL
33         my $lxs = $self->{lxs} = PublicInbox::LeiXSearch->new;
34         my @only = @{$opt->{only} // []};
35         # --local is enabled by default unless --only is used
36         # we'll allow "--only $LOCATION --local"
37         if ($opt->{'local'} //= scalar(@only) ? 0 : 1) {
38                 my $sto = $self->_lei_store(1);
39                 $lxs->prepare_external($sto->search);
40         }
41         if (@only) {
42                 for my $loc (@only) {
43                         my @loc = $self->get_externals($loc) or return;
44                         $lxs->prepare_external($_) for @loc;
45                 }
46         } else {
47                 for my $loc (@{$opt->{include} // []}) {
48                         my @loc = $self->get_externals($loc) or return;
49                         $lxs->prepare_external($_) for @loc;
50                 }
51                 # --external is enabled by default, but allow --no-external
52                 if ($opt->{external} //= 1) {
53                         my %x;
54                         for my $loc (@{$opt->{exclude} // []}) {
55                                 my @l = $self->get_externals($loc, 1) or return;
56                                 $x{$_} = 1 for @l;
57                         }
58                         my $ne = $self->externals_each(\&prep_ext, $lxs, \%x);
59                         $opt->{remote} //= !($lxs->locals - $opt->{'local'});
60                         if ($opt->{'local'}) {
61                                 delete($lxs->{remotes}) if !$opt->{remote};
62                         } else {
63                                 delete($lxs->{locals});
64                         }
65                 }
66         }
67         unless ($lxs->locals || $lxs->remotes) {
68                 return $self->fail('no local or remote inboxes to search');
69         }
70         my ($xj, $mj) = split(/,/, $opt->{jobs} // '');
71         if (defined($xj) && $xj ne '' && $xj !~ /\A[1-9][0-9]*\z/) {
72                 return $self->fail("`$xj' search jobs must be >= 1");
73         }
74         $xj ||= $lxs->concurrency($opt); # allow: "--jobs ,$WRITER_ONLY"
75         my $nproc = $lxs->detect_nproc; # don't memoize, schedtool(1) exists
76         $xj = $nproc if $xj > $nproc;
77         $lxs->{jobs} = $xj;
78         if (defined($mj) && $mj !~ /\A[1-9][0-9]*\z/) {
79                 return $self->fail("`$mj' writer jobs must be >= 1");
80         }
81         $self->{l2m}->{jobs} = ($mj // $nproc) if $self->{l2m};
82         PublicInbox::LeiOverview->new($self) or return;
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         if (defined(my $sort = $opt->{'sort'})) {
88                 if ($sort eq 'relevance') {
89                         $mset_opt{relevance} = 1;
90                 } elsif ($sort eq 'docid') {
91                         $mset_opt{relevance} = $mset_opt{asc} ? -1 : -2;
92                 } elsif ($sort =~ /\Areceived(?:-?[aA]t)?\z/) {
93                         # the default
94                 } else {
95                         die "unrecognized --sort=$sort\n";
96                 }
97         }
98         # descending docid order
99         $mset_opt{relevance} //= -2 if $opt->{thread};
100         $self->{mset_opt} = \%mset_opt;
101
102         if ($opt->{stdin}) {
103                 return $self->fail(<<'') if @argv;
104 no query allowed on command-line with --stdin
105
106                 require PublicInbox::InputPipe;
107                 PublicInbox::InputPipe::consume($self->{0}, \&qstr_add, $self);
108                 return;
109         }
110         # Consider spaces in argv to be for phrase search in Xapian.
111         # In other words, the users should need only care about
112         # normal shell quotes and not have to learn Xapian quoting.
113         $mset_opt{qstr} = join(' ', map {;
114                 /\s/ ? (s/\A(\w+:)// ? qq{$1"$_"} : qq{"$_"}) : $_
115         } @argv);
116         $lxs->do_query($self);
117 }
118
119 # shell completion helper called by lei__complete
120 sub _complete_q {
121         my ($self, @argv) = @_;
122         my $ext = qr/\A(?:-I|(?:--(?:include|exclude|only)))\z/;
123         my @cur;
124         while (@argv) {
125                 if ($argv[-1] =~ $ext) {
126                         my @c = $self->_complete_forget_external(@cur);
127                         # try basename match:
128                         if (scalar(@cur) == 1 && index($cur[0], '/') < 0) {
129                                 my $all = $self->externals_each;
130                                 my %bn;
131                                 for my $loc (keys %$all) {
132                                         my $bn = (split(m!/!, $loc))[-1];
133                                         ++$bn{$bn};
134                                 }
135                                 push @c, grep {
136                                         $bn{$_} == 1 && /\A\Q$cur[0]/
137                                 } keys %bn;
138                         }
139                         return @c if @c;
140                 }
141                 unshift(@cur, pop @argv);
142         }
143         ();
144 }
145
146 # Stuff we may pass through to curl (as of 7.64.0), see curl manpage for
147 # details, so most options which make sense for HTTP/HTTPS (including proxy
148 # support for Tor and other methods of getting past weird networks).
149 # Most of these are untested by us, some may not make sense for our use case
150 # and typos below are likely.
151 # n.b. some short options (-$NUMBER) are not supported since they conflict
152 # with other "lei q" switches.
153 # FIXME: Getopt::Long doesn't easily let us support support options with
154 # '.' in them (e.g. --http1.1)
155 # TODO: should we depend on "-c http.*" options for things which have
156 # analogues in git(1)? that would reduce likelyhood of conflicts with
157 # our other CLI options
158 sub curl_opt { qw(
159         abstract-unix-socket=s anyauth basic cacert=s capath=s
160         cert-status cert-type cert=s ciphers=s config|K=s@
161         connect-timeout=s connect-to=s cookie-jar=s cookie=s crlfile=s
162         digest disable dns-interface=s dns-ipv4-addr=s dns-ipv6-addr=s
163         dns-servers=s doh-url=s egd-file=s engine=s false-start
164         happy-eyeballs-timeout-ms=s haproxy-protocol header|H=s@
165         http2-prior-knowledge http2 insecure
166         interface=s ipv4 ipv6 junk-session-cookies
167         key-type=s key=s limit-rate=s local-port=s location-trusted location
168         max-redirs=i max-time=s negotiate netrc-file=s netrc-optional netrc
169         no-alpn no-buffer no-npn no-sessionid noproxy=s ntlm-wb ntlm
170         pass=s pinnedpubkey=s post301 post302 post303 preproxy=s
171         proxy-anyauth proxy-basic proxy-cacert=s proxy-capath=s
172         proxy-cert-type=s proxy-cert=s proxy-ciphers=s proxy-crlfile=s
173         proxy-digest proxy-header=s@ proxy-insecure
174         proxy-key-type=s proxy-key proxy-negotiate proxy-ntlm proxy-pass=s
175         proxy-pinnedpubkey=s proxy-service-name=s proxy-ssl-allow-beast
176         proxy-tls13-ciphers=s proxy-tlsauthtype=s proxy-tlspassword=s
177         proxy-tlsuser=s proxy-tlsv1 proxy-user|U=s proxy=s
178         proxytunnel=s pubkey=s random-file=s referer=s resolve=s
179         retry-connrefused retry-delay=s retry-max-time=s retry=i
180         sasl-ir service-name=s socks4=s socks4a=s socks5-basic
181         socks5-gssapi-service-name=s socks5-gssapi socks5-hostname=s socks5=s
182         speed-limit speed-type ssl-allow-beast sslv2 sslv3
183         suppress-connect-headers tcp-fastopen tls-max=s
184         tls13-ciphers=s tlsauthtype=s tlspassword=s tlsuser=s
185         tlsv1 trace-ascii=s trace-time trace=s
186         unix-socket=s user-agent|A=s user|u=s
187 )
188 }
189
190 1;