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