]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Config.pm
update copyright headers and email addresses
[public-inbox.git] / lib / PublicInbox / Config.pm
1 # Copyright (C) 2014-2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 package PublicInbox::Config;
4 use strict;
5 use warnings;
6 use File::Path::Expand qw/expand_filename/;
7
8 # returns key-value pairs of config directives in a hash
9 # if keys may be multi-value, the value is an array ref containing all values
10 sub new {
11         my ($class, $file) = @_;
12         my ($in, $out);
13
14         $file = default_file() unless defined($file);
15         my @cmd = (qw/git config/, "--file=$file", '-l');
16         my $cmd = join(' ', @cmd);
17         my $pid = open(my $fh, '-|', @cmd);
18         defined $pid or die "$cmd failed: $!\n";
19         my %rv;
20         foreach my $line (<$fh>) {
21                 chomp $line;
22                 my ($k, $v) = split(/=/, $line, 2);
23                 my $cur = $rv{$k};
24
25                 if (defined $cur) {
26                         if (ref($cur) eq "ARRAY") {
27                                 push @$cur, $v;
28                         } else {
29                                 $rv{$k} = [ $cur, $v ];
30                         }
31                 } else {
32                         $rv{$k} = $v;
33                 }
34         }
35         close $fh or die "failed to close ($cmd) pipe: $!\n";
36         $? and warn "$$ $cmd exited with: ($pid) $?\n";
37         bless \%rv, $class;
38 }
39
40 sub lookup {
41         my ($self, $recipient) = @_;
42         my $addr = lc($recipient);
43         my $pfx;
44
45         foreach my $k (keys %$self) {
46                 $k =~ /\A(publicinbox\.[A-Z0-9a-z-]+)\.address\z/ or next;
47                 my $v = $self->{$k};
48                 if (ref($v) eq "ARRAY") {
49                         foreach my $alias (@$v) {
50                                 (lc($alias) eq $addr) or next;
51                                 $pfx = $1;
52                                 last;
53                         }
54                 } else {
55                         (lc($v) eq $addr) or next;
56                         $pfx = $1;
57                         last;
58                 }
59         }
60
61         defined $pfx or return;
62
63         my %rv;
64         foreach my $k (qw(mainrepo address)) {
65                 my $v = $self->{"$pfx.$k"};
66                 $rv{$k} = $v if defined $v;
67         }
68         my $listname = $pfx;
69         $listname =~ s/\Apublicinbox\.//;
70         $rv{listname} = $listname;
71         my $v = $rv{address};
72         $rv{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
73         \%rv;
74 }
75
76 sub get {
77         my ($self, $listname, $key) = @_;
78
79         $self->{"publicinbox.$listname.$key"};
80 }
81
82 sub default_file {
83         my $f = $ENV{PI_CONFIG};
84         return $f if defined $f;
85         my $pi_dir = $ENV{PI_DIR} || expand_filename('~/.public-inbox');
86         "$pi_dir/config";
87 }
88
89 1;