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 require PublicInbox::Inbox;
9 use PublicInbox::Spawn qw(popen_rd);
11 # returns key-value pairs of config directives in a hash
12 # if keys may be multi-value, the value is an array ref containing all values
14 my ($class, $file) = @_;
15 $file = default_file() unless defined($file);
16 $file = ref $file ? $file : git_config_dump($file);
17 my $self = bless $file, $class;
20 $self->{-by_addr} ||= {};
21 $self->{-by_name} ||= {};
22 $self->{-by_newsgroup} ||= {};
23 $self->{-limiters} ||= {};
28 my ($self, $recipient) = @_;
29 my $addr = lc($recipient);
30 my $inbox = $self->{-by_addr}->{$addr};
31 return $inbox if $inbox;
35 foreach my $k (keys %$self) {
36 $k =~ /\A(publicinbox\.[\w-]+)\.address\z/ or next;
38 if (ref($v) eq "ARRAY") {
39 foreach my $alias (@$v) {
40 (lc($alias) eq $addr) or next;
45 (lc($v) eq $addr) or next;
50 defined $pfx or return;
54 sub lookup_name ($$) {
55 my ($self, $name) = @_;
56 $self->{-by_name}->{$name} || _fill($self, "publicinbox.$name");
62 foreach my $k (keys %$self) {
63 $k =~ /\Apublicinbox\.([A-Z0-9a-z-]+)\.mainrepo\z/ or next;
66 my $ibx = lookup_name($self, $1) or next;
71 sub lookup_newsgroup {
74 my $rv = $self->{-by_newsgroup}->{$ng};
77 foreach my $k (keys %$self) {
78 $k =~ /\A(publicinbox\.[\w-]+)\.newsgroup\z/ or next;
82 $rv = _fill($self, $pfx);
90 my ($self, $name) = @_;
91 $self->{-limiters}->{$name} ||= do {
92 require PublicInbox::Qspawn;
94 # XXX "limiter.<name>.max" was a historical mistake
95 foreach my $pfx (qw(publicinboxlimiter limiter)) {
96 $max ||= $self->{"$pfx.$name.max"};
98 PublicInbox::Qspawn::Limiter->new($max);
103 my ($self, $inbox, $key) = @_;
105 $self->{"publicinbox.$inbox.$key"};
108 sub config_dir { $ENV{PI_DIR} || "$ENV{HOME}/.public-inbox" }
111 my $f = $ENV{PI_CONFIG};
112 return $f if defined $f;
113 config_dir() . '/config';
116 sub git_config_dump {
119 my @cmd = (qw/git config/, "--file=$file", '-l');
120 my $cmd = join(' ', @cmd);
121 my $fh = popen_rd(\@cmd) or die "popen_rd failed for $file: $!\n";
124 foreach my $line (<$fh>) {
126 my ($k, $v) = split(/=/, $line, 2);
130 if (ref($cur) eq "ARRAY") {
133 $rv{$k} = [ $cur, $v ];
139 close $fh or die "failed to close ($cmd) pipe: $?";
144 my ($self, $pfx) = @_;
147 foreach my $k (qw(mainrepo address filter url newsgroup
148 infourl watch watchheader httpbackendmax)) {
149 my $v = $self->{"$pfx.$k"};
150 $rv->{$k} = $v if defined $v;
153 # TODO: more arrays, we should support multi-value for
154 # more things to encourage decentralization
155 foreach my $k (qw(altid nntpmirror)) {
156 if (defined(my $v = $self->{"$pfx.$k"})) {
157 $rv->{$k} = ref($v) eq 'ARRAY' ? $v : [ $v ];
161 return unless $rv->{mainrepo};
163 $name =~ s/\Apublicinbox\.//;
165 $rv->{-pi_config} = $self;
166 $rv = PublicInbox::Inbox->new($rv);
167 my $v = $rv->{address};
168 if (ref($v) eq 'ARRAY') {
169 $self->{-by_addr}->{lc($_)} = $rv foreach @$v;
171 $self->{-by_addr}->{lc($v)} = $rv;
173 if (my $ng = $rv->{newsgroup}) {
174 $self->{-by_newsgroup}->{$ng} = $rv;
176 $self->{-by_name}->{$name} = $rv;