From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 31BB81F94D for ; Sat, 27 Jun 2020 10:04:06 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 32/34] watch: support ~/.netrc via Net::Netrc Date: Sat, 27 Jun 2020 10:03:58 +0000 Message-Id: <20200627100400.9871-33-e@yhbt.net> In-Reply-To: <20200627100400.9871-1-e@yhbt.net> References: <20200627100400.9871-1-e@yhbt.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: 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 | 15 +++++++++++++++ lib/PublicInbox/WatchMaildir.pm | 15 ++++++++++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/lib/PublicInbox/GitCredential.pm b/lib/PublicInbox/GitCredential.pm index 826e7a55e8b..c6da6a090ca 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 19f894d4315..377548a2ad5 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 .... };