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