]> Sergey Matveev's repositories - public-inbox.git/commitdiff
watch: support ~/.netrc via Net::Netrc
authorEric Wong <e@yhbt.net>
Sat, 27 Jun 2020 10:03:58 +0000 (10:03 +0000)
committerEric Wong <e@yhbt.net>
Sun, 28 Jun 2020 22:29:39 +0000 (22:29 +0000)
While git-credential-netrc exists in git.git contrib/, it may
not be widely known or installed.  Net::Netrc is already a
standard part of most (if not all) Perl installations, so use it
directly if available.

lib/PublicInbox/GitCredential.pm
lib/PublicInbox/WatchMaildir.pm

index 826e7a55e8b854fe6a1f2bfd63f696430d78b791..c6da6a090caad0b415c425e3e5e694c63963f60d 100644 (file)
@@ -25,6 +25,21 @@ sub run ($$) {
        close $out_r or die "`git credential $op' failed: \$!=$! \$?=$?\n";
 }
 
+sub check_netrc ($) {
+       my ($self) = @_;
+
+       # part of the standard library, but distributions may split it out
+       eval { require Net::Netrc };
+       if ($@) {
+               warn "W: Net::Netrc missing: $@\n";
+               return;
+       }
+       if (my $x = Net::Netrc->lookup($self->{host}, $self->{username})) {
+               $self->{username} //= $x->login;
+               $self->{password} = $x->password;
+       }
+}
+
 sub fill {
        my ($self) = @_;
        my $out_r = run($self, 'fill');
index b89462de679e31158f03d79921057ef0cc1fbc8b..ad514ec1e80b4bfff5d08b59b65fdacf72ac74d7 100644 (file)
@@ -317,11 +317,12 @@ sub mic_for ($$$) { # mic = Mail::IMAPClient
                password => $uri->password,
        }, 'PublicInbox::GitCredential';
        my $common = $mic_args->{uri_section($uri)} // {};
+       # IMAPClient and Net::Netrc both mishandles `0', so we pass `127.0.0.1'
        my $host = $cred->{host};
+       $host = '127.0.0.1' if $host eq '0';
        my $mic_arg = {
                Port => $uri->port,
-               # IMAPClient mishandles `0', so we pass `127.0.0.1'
-               Server => $host eq '0' ? '127.0.0.1' : $host,
+               Server => $host,
                Ssl => $uri->scheme eq 'imaps',
                Keepalive => 1, # SO_KEEPALIVE
                %$common, # may set Starttls, Compress, Debug ....
@@ -343,6 +344,7 @@ sub mic_for ($$$) { # mic = Mail::IMAPClient
                $cred = undef;
        }
        if ($cred) {
+               $cred->check_netrc unless defined $cred->{password};
                $cred->fill; # may prompt user here
                $mic->User($mic_arg->{User} = $cred->{username});
                $mic->Password($mic_arg->{Password} = $cred->{password});
@@ -768,6 +770,9 @@ sub nn_for ($$$) { # nn = Net::NNTP
        my $uri = uri_new($url);
        my $sec = uri_section($uri);
        my $nntp_opt = $self->{nntp_opt}->{$sec} //= {};
+       my $host = $uri->host;
+       # Net::NNTP and Net::Netrc both mishandle `0', so we pass `127.0.0.1'
+       $host = '127.0.0.1' if $host eq '0';
        my $cred;
        my ($u, $p);
        if (defined(my $ui = $uri->userinfo)) {
@@ -775,16 +780,16 @@ sub nn_for ($$$) { # nn = Net::NNTP
                $cred = bless {
                        url => $sec,
                        protocol => uri_scheme($uri),
-                       host => $uri->host,
+                       host => $host,
                }, 'PublicInbox::GitCredential';
                ($u, $p) = split(/:/, $ui, 2);
                ($cred->{username}, $cred->{password}) = ($u, $p);
+               $cred->check_netrc unless defined $p;
        }
        my $common = $nn_args->{$sec} // {};
        my $nn_arg = {
                Port => $uri->port,
-               # Net::NNTP mishandles `0', so we pass `127.0.0.1'
-               Host => $uri->host eq '0' ? '127.0.0.1' : $uri->host,
+               Host => $host,
                SSL => $uri->secure, # snews == nntps
                %$common, # may Debug ....
        };