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)
4 # Used throughout the project for reading configuration
5 package PublicInbox::Config;
8 use File::Path::Expand qw/expand_filename/;
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
13 my ($class, $file) = @_;
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";
22 foreach my $line (<$fh>) {
24 my ($k, $v) = split(/=/, $line, 2);
28 if (ref($cur) eq "ARRAY") {
31 $rv{$k} = [ $cur, $v ];
37 close $fh or die "failed to close ($cmd) pipe: $!\n";
38 $? and warn "$$ $cmd exited with: ($pid) $?\n";
43 my ($self, $recipient) = @_;
44 my $addr = lc($recipient);
47 foreach my $k (keys %$self) {
48 $k =~ /\A(publicinbox\.[A-Z0-9a-z-]+)\.address\z/ or next;
50 if (ref($v) eq "ARRAY") {
51 foreach my $alias (@$v) {
52 (lc($alias) eq $addr) or next;
57 (lc($v) eq $addr) or next;
63 defined $pfx or return;
66 foreach my $k (qw(mainrepo address filter)) {
67 my $v = $self->{"$pfx.$k"};
68 $rv{$k} = $v if defined $v;
71 $listname =~ s/\Apublicinbox\.//;
72 $rv{listname} = $listname;
74 $rv{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
79 my ($self, $listname, $key) = @_;
81 $self->{"publicinbox.$listname.$key"};
85 my $f = $ENV{PI_CONFIG};
86 return $f if defined $f;
87 my $pi_dir = $ENV{PI_DIR} || expand_filename('~/.public-inbox');