]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Config.pm
localize $/ in more places to avoid potential problems
[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 base qw/Exporter/;
9 our @EXPORT_OK = qw/try_cat/;
10 require PublicInbox::Inbox;
11 use File::Path::Expand qw/expand_filename/;
12
13 # returns key-value pairs of config directives in a hash
14 # if keys may be multi-value, the value is an array ref containing all values
15 sub new {
16         my ($class, $file) = @_;
17         $file = default_file() unless defined($file);
18         $file = ref $file ? $file : git_config_dump($file);
19         my $self = bless $file, $class;
20
21         # caches
22         $self->{-by_addr} ||= {};
23         $self->{-by_name} ||= {};
24         $self;
25 }
26
27 sub lookup {
28         my ($self, $recipient) = @_;
29         my $addr = lc($recipient);
30         my $inbox = $self->{-by_addr}->{$addr};
31         return $inbox if $inbox;
32
33         my $pfx;
34
35         foreach my $k (keys %$self) {
36                 $k =~ /\A(publicinbox\.[\w-]+)\.address\z/ or next;
37                 my $v = $self->{$k};
38                 if (ref($v) eq "ARRAY") {
39                         foreach my $alias (@$v) {
40                                 (lc($alias) eq $addr) or next;
41                                 $pfx = $1;
42                                 last;
43                         }
44                 } else {
45                         (lc($v) eq $addr) or next;
46                         $pfx = $1;
47                         last;
48                 }
49         }
50         defined $pfx or return;
51         _fill($self, $pfx);
52 }
53
54 sub lookup_name {
55         my ($self, $name) = @_;
56         my $rv = $self->{-by_name}->{$name};
57         return $rv if $rv;
58         $self->{-by_name}->{$name} = _fill($self, "publicinbox.$name");
59 }
60
61 sub get {
62         my ($self, $inbox, $key) = @_;
63
64         $self->{"publicinbox.$inbox.$key"};
65 }
66
67 sub config_dir { $ENV{PI_DIR} || expand_filename('~/.public-inbox') }
68
69 sub default_file {
70         my $f = $ENV{PI_CONFIG};
71         return $f if defined $f;
72         config_dir() . '/config';
73 }
74
75 sub git_config_dump {
76         my ($file) = @_;
77         my ($in, $out);
78         my @cmd = (qw/git config/, "--file=$file", '-l');
79         my $cmd = join(' ', @cmd);
80         my $pid = open(my $fh, '-|', @cmd);
81         defined $pid or die "$cmd failed: $!";
82         my %rv;
83         local $/ = "\n";
84         foreach my $line (<$fh>) {
85                 chomp $line;
86                 my ($k, $v) = split(/=/, $line, 2);
87                 my $cur = $rv{$k};
88
89                 if (defined $cur) {
90                         if (ref($cur) eq "ARRAY") {
91                                 push @$cur, $v;
92                         } else {
93                                 $rv{$k} = [ $cur, $v ];
94                         }
95                 } else {
96                         $rv{$k} = $v;
97                 }
98         }
99         close $fh or die "failed to close ($cmd) pipe: $!";
100         $? and warn "$$ $cmd exited with: ($pid) $?";
101         \%rv;
102 }
103
104 sub try_cat {
105         my ($path) = @_;
106         my $rv;
107         if (open(my $fh, '<', $path)) {
108                 local $/;
109                 $rv = <$fh>;
110         }
111         $rv;
112 }
113
114 sub _fill {
115         my ($self, $pfx) = @_;
116         my $rv = {};
117
118         foreach my $k (qw(mainrepo address filter url)) {
119                 my $v = $self->{"$pfx.$k"};
120                 $rv->{$k} = $v if defined $v;
121         }
122         my $inbox = $pfx;
123         $inbox =~ s/\Apublicinbox\.//;
124         $rv->{name} = $inbox;
125         my $v = $rv->{address} ||= 'public-inbox@example.com';
126         $rv->{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
127         $rv = PublicInbox::Inbox->new($rv);
128         if (ref($v) eq 'ARRAY') {
129                 $self->{-by_addr}->{lc($_)} = $rv foreach @$v;
130         } else {
131                 $self->{-by_addr}->{lc($v)} = $rv;
132         }
133         $rv;
134 }
135
136
137 1;