1 # Copyright (C) 2014-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <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->{-no_obfuscate} ||= {};
24 $self->{-limiters} ||= {};
26 if (my $no = delete $self->{'publicinbox.noobfuscate'}) {
27 $no = [ $no ] if ref($no) ne 'ARRAY';
29 foreach my $n (@$no) {
30 my @n = split(/\s+/, $n);
32 if (/\S+@\S+/) { # full address
33 $self->{-no_obfuscate}->{lc $_} = 1;
35 # allow "example.com" or "@example.com"
37 push @domains, quotemeta($_);
41 my $nod = join('|', @domains);
42 $self->{-no_obfuscate_re} = qr/(?:$nod)\z/i;
49 my ($self, $recipient) = @_;
50 my $addr = lc($recipient);
51 my $inbox = $self->{-by_addr}->{$addr};
52 return $inbox if $inbox;
56 foreach my $k (keys %$self) {
57 $k =~ /\A(publicinbox\.[\w-]+)\.address\z/ or next;
59 if (ref($v) eq "ARRAY") {
60 foreach my $alias (@$v) {
61 (lc($alias) eq $addr) or next;
66 (lc($v) eq $addr) or next;
71 defined $pfx or return;
75 sub lookup_name ($$) {
76 my ($self, $name) = @_;
77 $self->{-by_name}->{$name} || _fill($self, "publicinbox.$name");
83 foreach my $k (keys %$self) {
84 $k =~ /\Apublicinbox\.([A-Z0-9a-z-]+)\.mainrepo\z/ or next;
87 my $ibx = lookup_name($self, $1) or next;
92 sub lookup_newsgroup {
95 my $rv = $self->{-by_newsgroup}->{$ng};
98 foreach my $k (keys %$self) {
99 $k =~ /\A(publicinbox\.[\w-]+)\.newsgroup\z/ or next;
103 $rv = _fill($self, $pfx);
111 my ($self, $name) = @_;
112 $self->{-limiters}->{$name} ||= do {
113 require PublicInbox::Qspawn;
114 my $max = $self->{"publicinboxlimiter.$name.max"};
115 PublicInbox::Qspawn::Limiter->new($max);
119 sub config_dir { $ENV{PI_DIR} || "$ENV{HOME}/.public-inbox" }
122 my $f = $ENV{PI_CONFIG};
123 return $f if defined $f;
124 config_dir() . '/config';
127 sub git_config_dump {
130 my @cmd = (qw/git config/, "--file=$file", '-l');
131 my $cmd = join(' ', @cmd);
132 my $fh = popen_rd(\@cmd) or die "popen_rd failed for $file: $!\n";
135 while (defined(my $line = <$fh>)) {
137 my ($k, $v) = split(/=/, $line, 2);
141 if (ref($cur) eq "ARRAY") {
144 $rv{$k} = [ $cur, $v ];
150 close $fh or die "failed to close ($cmd) pipe: $?";
156 my ($self, $pfx) = @_;
159 foreach my $k (qw(mainrepo filter url newsgroup
160 infourl watch watchheader httpbackendmax
161 replyto feedmax nntpserver indexlevel)) {
162 my $v = $self->{"$pfx.$k"};
163 $rv->{$k} = $v if defined $v;
165 foreach my $k (qw(obfuscate)) {
166 my $v = $self->{"$pfx.$k"};
168 if ($v =~ /\A(?:false|no|off|0)\z/) {
170 } elsif ($v =~ /\A(?:true|yes|on|1)\z/) {
173 warn "Ignoring $pfx.$k=$v in config, not boolean\n";
176 # TODO: more arrays, we should support multi-value for
177 # more things to encourage decentralization
178 foreach my $k (qw(address altid nntpmirror)) {
179 if (defined(my $v = $self->{"$pfx.$k"})) {
180 $rv->{$k} = ref($v) eq 'ARRAY' ? $v : [ $v ];
184 return unless $rv->{mainrepo};
186 $name =~ s/\Apublicinbox\.//;
188 $rv->{-pi_config} = $self;
189 $rv = PublicInbox::Inbox->new($rv);
190 foreach (@{$rv->{address}}) {
191 my $lc_addr = lc($_);
192 $self->{-by_addr}->{$lc_addr} = $rv;
193 $self->{-no_obfuscate}->{$lc_addr} = 1;
195 if (my $ng = $rv->{newsgroup}) {
196 $self->{-by_newsgroup}->{$ng} = $rv;
198 $self->{-by_name}->{$name} = $rv;
199 if ($rv->{obfuscate}) {
200 $rv->{-no_obfuscate} = $self->{-no_obfuscate};
201 $rv->{-no_obfuscate_re} = $self->{-no_obfuscate_re};
202 each_inbox($self, sub {}); # noop to populate -no_obfuscate