about summary refs log tree commit
diff options
context:
space:
mode:
authorEric Wong <e@yhbt.net>2020-06-27 10:03:58 +0000
committerEric Wong <e@yhbt.net>2020-06-28 22:29:39 +0000
commit6968c91707d51f6a60d476cc2f5d65a660146813 (patch)
tree4f8d1309f9b9b0c50de80b17ca869d8bb5499e13
parentab58bcb9909967503bc4ed810f2053b8ed6efb2e (diff)
downloadpublic-inbox-6968c91707d51f6a60d476cc2f5d65a660146813.tar.gz
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.
-rw-r--r--lib/PublicInbox/GitCredential.pm15
-rw-r--r--lib/PublicInbox/WatchMaildir.pm15
2 files changed, 25 insertions, 5 deletions
diff --git a/lib/PublicInbox/GitCredential.pm b/lib/PublicInbox/GitCredential.pm
index 826e7a55..c6da6a09 100644
--- a/lib/PublicInbox/GitCredential.pm
+++ b/lib/PublicInbox/GitCredential.pm
@@ -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');
diff --git a/lib/PublicInbox/WatchMaildir.pm b/lib/PublicInbox/WatchMaildir.pm
index b89462de..ad514ec1 100644
--- a/lib/PublicInbox/WatchMaildir.pm
+++ b/lib/PublicInbox/WatchMaildir.pm
@@ -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 ....
         };