]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiCurl.pm
lei_curl: replace -K/--config with --curl-config
[public-inbox.git] / lib / PublicInbox / LeiCurl.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 # common option and torsocks(1) wrapping for curl(1)
5 package PublicInbox::LeiCurl;
6 use strict;
7 use v5.10.1;
8 use PublicInbox::Spawn qw(which);
9 use PublicInbox::Config;
10
11 my %lei2curl = (
12         'curl-config=s@' => 'config|K=s@',
13 );
14
15 # prepares a common command for curl(1) based on $lei command
16 sub new {
17         my ($cls, $lei, $curl) = @_;
18         $curl //= which('curl') // return $lei->fail('curl not found');
19         my $opt = $lei->{opt};
20         my @cmd = ($curl, qw(-Sf));
21         $cmd[-1] .= 's' if $opt->{quiet}; # already the default for "lei q"
22         $cmd[-1] .= 'v' if $opt->{verbose}; # we use ourselves, too
23         for my $o ($lei->curl_opt) {
24                 if (my $lei_spec = $lei2curl{$o}) {
25                         $o = $lei_spec;
26                 }
27                 $o =~ s/\|[a-z0-9]\b//i; # remove single char short option
28                 if ($o =~ s/=[is]@\z//) {
29                         my $ary = $opt->{$o} or next;
30                         push @cmd, map { ("--$o", $_) } @$ary;
31                 } elsif ($o =~ s/=[is]\z//) {
32                         my $val = $opt->{$o} // next;
33                         push @cmd, "--$o", $val;
34                 } elsif ($opt->{$o}) {
35                         push @cmd, "--$o";
36                 }
37         }
38         push @cmd, '-v' if $opt->{verbose}; # lei uses this itself
39         bless \@cmd, $cls;
40 }
41
42 sub torsocks { # useful for "git clone" and "git fetch", too
43         my ($self, $lei, $uri)= @_;
44         my $opt = $lei->{opt};
45         $opt->{torsocks} = 'false' if $opt->{'no-torsocks'};
46         my $torsocks = $opt->{torsocks} //= 'auto';
47         if ($torsocks eq 'auto' && substr($uri->host, -6) eq '.onion' &&
48                         (($lei->{env}->{LD_PRELOAD}//'') !~ /torsocks/)) {
49                 # "auto" continues anyways if torsocks is missing;
50                 # a proxy may be specified via CLI, curlrc,
51                 # environment variable, or even firewall rule
52                 [ ($lei->{torsocks} //= which('torsocks')) // () ]
53         } elsif (PublicInbox::Config::git_bool($torsocks)) {
54                 my $x = $lei->{torsocks} //= which('torsocks');
55                 $x or return $lei->fail(<<EOM);
56 --torsocks=yes specified but torsocks not found in PATH=$ENV{PATH}
57 EOM
58                 [ $x ];
59         } else { # the common case for current Internet :<
60                 [];
61         }
62 }
63
64 # completes the result of cmd() for $uri
65 sub for_uri {
66         my ($self, $lei, $uri) = @_;
67         my $pfx = torsocks($self, $lei, $uri) or return; # error
68         [ @$pfx, @$self, substr($uri->path, -3) eq '.gz' ? () : '--compressed',
69                 $uri->as_string ]
70 }
71
72 1;