]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Config.pm
drop dependency on File::Path::Expand
[public-inbox.git] / lib / PublicInbox / Config.pm
index 3f3707ecc1470efda6a32787b6a053de365b5c36..4651861d215229fe6e53d5ca7f9886d395d3a2fe 100644 (file)
@@ -5,19 +5,21 @@
 package PublicInbox::Config;
 use strict;
 use warnings;
-use base qw/Exporter/;
-our @EXPORT_OK = qw/try_cat/;
 require PublicInbox::Inbox;
-use File::Path::Expand qw/expand_filename/;
+use PublicInbox::Spawn qw(popen_rd);
 
 # returns key-value pairs of config directives in a hash
 # if keys may be multi-value, the value is an array ref containing all values
 sub new {
        my ($class, $file) = @_;
        $file = default_file() unless defined($file);
-       my $self = bless git_config_dump($file), $class;
-       $self->{-by_addr} = {};
-       $self->{-by_name} = {};
+       $file = ref $file ? $file : git_config_dump($file);
+       my $self = bless $file, $class;
+
+       # caches
+       $self->{-by_addr} ||= {};
+       $self->{-by_name} ||= {};
+       $self->{-by_newsgroup} ||= {};
        $self;
 }
 
@@ -52,7 +54,25 @@ sub lookup_name {
        my ($self, $name) = @_;
        my $rv = $self->{-by_name}->{$name};
        return $rv if $rv;
-       $self->{-by_name}->{$name} = _fill($self, "publicinbox.$name");
+       $rv = _fill($self, "publicinbox.$name") or return;
+}
+
+sub lookup_newsgroup {
+       my ($self, $ng) = @_;
+       $ng = lc($ng);
+       my $rv = $self->{-by_newsgroup}->{$ng};
+       return $rv if $rv;
+
+       foreach my $k (keys %$self) {
+               $k =~ /\A(publicinbox\.[\w-]+)\.newsgroup\z/ or next;
+               my $v = $self->{$k};
+               my $pfx = $1;
+               if ($v eq $ng) {
+                       $rv = _fill($self, $pfx);
+                       return $rv;
+               }
+       }
+       undef;
 }
 
 sub get {
@@ -61,7 +81,7 @@ sub get {
        $self->{"publicinbox.$inbox.$key"};
 }
 
-sub config_dir { $ENV{PI_DIR} || expand_filename('~/.public-inbox') }
+sub config_dir { $ENV{PI_DIR} || "$ENV{HOME}/.public-inbox" }
 
 sub default_file {
        my $f = $ENV{PI_CONFIG};
@@ -74,9 +94,9 @@ sub git_config_dump {
        my ($in, $out);
        my @cmd = (qw/git config/, "--file=$file", '-l');
        my $cmd = join(' ', @cmd);
-       my $pid = open(my $fh, '-|', @cmd);
-       defined $pid or die "$cmd failed: $!";
+       my $fh = popen_rd(\@cmd);
        my %rv;
+       local $/ = "\n";
        foreach my $line (<$fh>) {
                chomp $line;
                my ($k, $v) = split(/=/, $line, 2);
@@ -92,42 +112,35 @@ sub git_config_dump {
                        $rv{$k} = $v;
                }
        }
-       close $fh or die "failed to close ($cmd) pipe: $!";
-       $? and warn "$$ $cmd exited with: ($pid) $?";
+       close $fh or die "failed to close ($cmd) pipe: $?";
        \%rv;
 }
 
-sub try_cat {
-       my ($path) = @_;
-       my $rv;
-       if (open(my $fh, '<', $path)) {
-               local $/;
-               $rv = <$fh>;
-       }
-       $rv;
-}
-
 sub _fill {
        my ($self, $pfx) = @_;
        my $rv = {};
 
-       foreach my $k (qw(mainrepo address filter url)) {
+       foreach my $k (qw(mainrepo address filter url newsgroup)) {
                my $v = $self->{"$pfx.$k"};
                $rv->{$k} = $v if defined $v;
        }
-       my $inbox = $pfx;
-       $inbox =~ s/\Apublicinbox\.//;
-       $rv->{name} = $inbox;
+       return unless $rv->{mainrepo};
+       my $name = $pfx;
+       $name =~ s/\Apublicinbox\.//;
+       $rv->{name} = $name;
        my $v = $rv->{address} ||= 'public-inbox@example.com';
-       $rv->{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
+       my $p = $rv->{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
+       $rv->{domain} = ($p =~ /\@(\S+)\z/) ? $1 : 'localhost';
        $rv = PublicInbox::Inbox->new($rv);
        if (ref($v) eq 'ARRAY') {
                $self->{-by_addr}->{lc($_)} = $rv foreach @$v;
        } else {
                $self->{-by_addr}->{lc($v)} = $rv;
        }
-       $rv;
+       if (my $ng = $rv->{newsgroup}) {
+               $self->{-by_newsgroup}->{$ng} = $rv;
+       }
+       $self->{-by_name}->{$name} = $rv;
 }
 
-
 1;