]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Config.pm
allow admins to configure non-obfuscated addresses/domains
[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
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
13 sub new {
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;
18
19         # caches
20         $self->{-by_addr} ||= {};
21         $self->{-by_name} ||= {};
22         $self->{-by_newsgroup} ||= {};
23         $self->{-no_obfuscate} ||= {};
24         $self->{-limiters} ||= {};
25
26         if (my $no = delete $self->{'publicinbox.noobfuscate'}) {
27                 $no = [ $no ] if ref($no) ne 'ARRAY';
28                 my @domains;
29                 foreach my $n (@$no) {
30                         my @n = split(/\s+/, $n);
31                         foreach (@n) {
32                                 if (/\S+@\S+/) { # full address
33                                         $self->{-no_obfuscate}->{lc $_} = 1;
34                                 } else {
35                                         # allow "example.com" or "@example.com"
36                                         s/\A@//;
37                                         push @domains, quotemeta($_);
38                                 }
39                         }
40                 }
41                 my $nod = join('|', @domains);
42                 $self->{-no_obfuscate_re} = qr/(?:$nod)\z/i;
43         }
44
45         $self;
46 }
47
48 sub lookup {
49         my ($self, $recipient) = @_;
50         my $addr = lc($recipient);
51         my $inbox = $self->{-by_addr}->{$addr};
52         return $inbox if $inbox;
53
54         my $pfx;
55
56         foreach my $k (keys %$self) {
57                 $k =~ /\A(publicinbox\.[\w-]+)\.address\z/ or next;
58                 my $v = $self->{$k};
59                 if (ref($v) eq "ARRAY") {
60                         foreach my $alias (@$v) {
61                                 (lc($alias) eq $addr) or next;
62                                 $pfx = $1;
63                                 last;
64                         }
65                 } else {
66                         (lc($v) eq $addr) or next;
67                         $pfx = $1;
68                         last;
69                 }
70         }
71         defined $pfx or return;
72         _fill($self, $pfx);
73 }
74
75 sub lookup_name ($$) {
76         my ($self, $name) = @_;
77         $self->{-by_name}->{$name} || _fill($self, "publicinbox.$name");
78 }
79
80 sub each_inbox {
81         my ($self, $cb) = @_;
82         my %seen;
83         foreach my $k (keys %$self) {
84                 $k =~ /\Apublicinbox\.([A-Z0-9a-z-]+)\.mainrepo\z/ or next;
85                 next if $seen{$1};
86                 $seen{$1} = 1;
87                 my $ibx = lookup_name($self, $1) or next;
88                 $cb->($ibx);
89         }
90 }
91
92 sub lookup_newsgroup {
93         my ($self, $ng) = @_;
94         $ng = lc($ng);
95         my $rv = $self->{-by_newsgroup}->{$ng};
96         return $rv if $rv;
97
98         foreach my $k (keys %$self) {
99                 $k =~ /\A(publicinbox\.[\w-]+)\.newsgroup\z/ or next;
100                 my $v = $self->{$k};
101                 my $pfx = $1;
102                 if ($v eq $ng) {
103                         $rv = _fill($self, $pfx);
104                         return $rv;
105                 }
106         }
107         undef;
108 }
109
110 sub limiter {
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);
116         };
117 }
118
119 sub config_dir { $ENV{PI_DIR} || "$ENV{HOME}/.public-inbox" }
120
121 sub default_file {
122         my $f = $ENV{PI_CONFIG};
123         return $f if defined $f;
124         config_dir() . '/config';
125 }
126
127 sub git_config_dump {
128         my ($file) = @_;
129         my ($in, $out);
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";
133         my %rv;
134         local $/ = "\n";
135         while (defined(my $line = <$fh>)) {
136                 chomp $line;
137                 my ($k, $v) = split(/=/, $line, 2);
138                 my $cur = $rv{$k};
139
140                 if (defined $cur) {
141                         if (ref($cur) eq "ARRAY") {
142                                 push @$cur, $v;
143                         } else {
144                                 $rv{$k} = [ $cur, $v ];
145                         }
146                 } else {
147                         $rv{$k} = $v;
148                 }
149         }
150         close $fh or die "failed to close ($cmd) pipe: $?";
151
152         \%rv;
153 }
154
155 sub _fill {
156         my ($self, $pfx) = @_;
157         my $rv = {};
158
159         foreach my $k (qw(mainrepo filter url newsgroup
160                         infourl watch watchheader httpbackendmax
161                         replyto feedmax nntpserver)) {
162                 my $v = $self->{"$pfx.$k"};
163                 $rv->{$k} = $v if defined $v;
164         }
165         foreach my $k (qw(obfuscate)) {
166                 my $v = $self->{"$pfx.$k"};
167                 defined $v or next;
168                 if ($v =~ /\A(?:false|no|off|0)\z/) {
169                         $rv->{$k} = 0;
170                 } elsif ($v =~ /\A(?:true|yes|on|1)\z/) {
171                         $rv->{$k} = 1;
172                 } else {
173                         warn "Ignoring $pfx.$k=$v in config, not boolean\n";
174                 }
175         }
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 ];
181                 }
182         }
183
184         return unless $rv->{mainrepo};
185         my $name = $pfx;
186         $name =~ s/\Apublicinbox\.//;
187         $rv->{name} = $name;
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;
194         }
195         if (my $ng = $rv->{newsgroup}) {
196                 $self->{-by_newsgroup}->{$ng} = $rv;
197         }
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
203         }
204         $rv
205 }
206
207 1;