]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Config.pm
317d290a4c36954c0688609dac91694832526d25
[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 require PublicInbox::Inbox;
9 use PublicInbox::Spawn qw(popen_rd);
10 use File::Path::Expand qw/expand_filename/;
11
12 # returns key-value pairs of config directives in a hash
13 # if keys may be multi-value, the value is an array ref containing all values
14 sub new {
15         my ($class, $file) = @_;
16         $file = default_file() unless defined($file);
17         $file = ref $file ? $file : git_config_dump($file);
18         my $self = bless $file, $class;
19
20         # caches
21         $self->{-by_addr} ||= {};
22         $self->{-by_name} ||= {};
23         $self;
24 }
25
26 sub lookup {
27         my ($self, $recipient) = @_;
28         my $addr = lc($recipient);
29         my $inbox = $self->{-by_addr}->{$addr};
30         return $inbox if $inbox;
31
32         my $pfx;
33
34         foreach my $k (keys %$self) {
35                 $k =~ /\A(publicinbox\.[\w-]+)\.address\z/ or next;
36                 my $v = $self->{$k};
37                 if (ref($v) eq "ARRAY") {
38                         foreach my $alias (@$v) {
39                                 (lc($alias) eq $addr) or next;
40                                 $pfx = $1;
41                                 last;
42                         }
43                 } else {
44                         (lc($v) eq $addr) or next;
45                         $pfx = $1;
46                         last;
47                 }
48         }
49         defined $pfx or return;
50         _fill($self, $pfx);
51 }
52
53 sub lookup_name {
54         my ($self, $name) = @_;
55         my $rv = $self->{-by_name}->{$name};
56         return $rv if $rv;
57         $rv = _fill($self, "publicinbox.$name") or return;
58         $self->{-by_name}->{$name} = $rv;
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 $fh = popen_rd(\@cmd);
81         my %rv;
82         local $/ = "\n";
83         foreach my $line (<$fh>) {
84                 chomp $line;
85                 my ($k, $v) = split(/=/, $line, 2);
86                 my $cur = $rv{$k};
87
88                 if (defined $cur) {
89                         if (ref($cur) eq "ARRAY") {
90                                 push @$cur, $v;
91                         } else {
92                                 $rv{$k} = [ $cur, $v ];
93                         }
94                 } else {
95                         $rv{$k} = $v;
96                 }
97         }
98         close $fh or die "failed to close ($cmd) pipe: $?";
99         \%rv;
100 }
101
102 sub _fill {
103         my ($self, $pfx) = @_;
104         my $rv = {};
105
106         foreach my $k (qw(mainrepo address filter url)) {
107                 my $v = $self->{"$pfx.$k"};
108                 $rv->{$k} = $v if defined $v;
109         }
110         return unless $rv->{mainrepo};
111         my $inbox = $pfx;
112         $inbox =~ s/\Apublicinbox\.//;
113         $rv->{name} = $inbox;
114         my $v = $rv->{address} ||= 'public-inbox@example.com';
115         $rv->{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
116         $rv = PublicInbox::Inbox->new($rv);
117         if (ref($v) eq 'ARRAY') {
118                 $self->{-by_addr}->{lc($_)} = $rv foreach @$v;
119         } else {
120                 $self->{-by_addr}->{lc($v)} = $rv;
121         }
122         $rv;
123 }
124
125
126 1;