]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Config.pm
a8c5105e636b50a2866860d686d7e4bb1eab16da
[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->{-by_newsgroup} ||= {};
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         $rv = _fill($self, "publicinbox.$name") or return;
59 }
60
61 sub lookup_newsgroup {
62         my ($self, $ng) = @_;
63         $ng = lc($ng);
64         my $rv = $self->{-by_newsgroup}->{$ng};
65         return $rv if $rv;
66
67         foreach my $k (keys %$self) {
68                 $k =~ /\A(publicinbox\.[\w-]+)\.newsgroup\z/ or next;
69                 my $v = $self->{$k};
70                 my $pfx = $1;
71                 if ($v eq $ng) {
72                         $rv = _fill($self, $pfx);
73                         return $rv;
74                 }
75         }
76         undef;
77 }
78
79 sub get {
80         my ($self, $inbox, $key) = @_;
81
82         $self->{"publicinbox.$inbox.$key"};
83 }
84
85 sub config_dir { $ENV{PI_DIR} || expand_filename('~/.public-inbox') }
86
87 sub default_file {
88         my $f = $ENV{PI_CONFIG};
89         return $f if defined $f;
90         config_dir() . '/config';
91 }
92
93 sub git_config_dump {
94         my ($file) = @_;
95         my ($in, $out);
96         my @cmd = (qw/git config/, "--file=$file", '-l');
97         my $cmd = join(' ', @cmd);
98         my $fh = popen_rd(\@cmd);
99         my %rv;
100         local $/ = "\n";
101         foreach my $line (<$fh>) {
102                 chomp $line;
103                 my ($k, $v) = split(/=/, $line, 2);
104                 my $cur = $rv{$k};
105
106                 if (defined $cur) {
107                         if (ref($cur) eq "ARRAY") {
108                                 push @$cur, $v;
109                         } else {
110                                 $rv{$k} = [ $cur, $v ];
111                         }
112                 } else {
113                         $rv{$k} = $v;
114                 }
115         }
116         close $fh or die "failed to close ($cmd) pipe: $?";
117         \%rv;
118 }
119
120 sub _fill {
121         my ($self, $pfx) = @_;
122         my $rv = {};
123
124         foreach my $k (qw(mainrepo address filter url newsgroup)) {
125                 my $v = $self->{"$pfx.$k"};
126                 $rv->{$k} = $v if defined $v;
127         }
128         return unless $rv->{mainrepo};
129         my $name = $pfx;
130         $name =~ s/\Apublicinbox\.//;
131         $rv->{name} = $name;
132         my $v = $rv->{address} ||= 'public-inbox@example.com';
133         my $p = $rv->{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
134         $rv->{domain} = ($p =~ /\@(\S+)\z/) ? $1 : 'localhost';
135         $rv = PublicInbox::Inbox->new($rv);
136         if (ref($v) eq 'ARRAY') {
137                 $self->{-by_addr}->{lc($_)} = $rv foreach @$v;
138         } else {
139                 $self->{-by_addr}->{lc($v)} = $rv;
140         }
141         if (my $ng = $rv->{newsgroup}) {
142                 $self->{-by_newsgroup}->{$ng} = $rv;
143         }
144         $self->{-by_name}->{$name} = $rv;
145 }
146
147 1;