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