]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Config.pm
config: inbox name checking matches git.git more closely
[public-inbox.git] / lib / PublicInbox / Config.pm
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>
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 =~ m!\A(publicinbox\.[^/]+)\.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 =~ m!\Apublicinbox\.([^/]+)\.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 =~ m!\A(publicinbox\.[^/]+)\.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 valid_inbox_name ($) {
156         my ($name) = @_;
157
158         # Similar rules found in git.git/remote.c::valid_remote_nick
159         # and git.git/refs.c::check_refname_component
160         # We don't reject /\.lock\z/, however, since we don't lock refs
161         if ($name eq '' || $name =~ /\@\{/ ||
162             $name =~ /\.\./ || $name =~ m![/:\?\[\]\^~\s\f[:cntrl:]\*]! ||
163             $name =~ /\A\./ || $name =~ /\.\z/) {
164                 return 0;
165         }
166
167         # Note: we allow URL-unfriendly characters; users may configure
168         # non-HTTP-accessible inboxes
169         1;
170 }
171
172 sub _fill {
173         my ($self, $pfx) = @_;
174         my $rv = {};
175
176         foreach my $k (qw(mainrepo filter url newsgroup
177                         infourl watch watchheader httpbackendmax
178                         replyto feedmax nntpserver indexlevel)) {
179                 my $v = $self->{"$pfx.$k"};
180                 $rv->{$k} = $v if defined $v;
181         }
182         foreach my $k (qw(obfuscate)) {
183                 my $v = $self->{"$pfx.$k"};
184                 defined $v or next;
185                 if ($v =~ /\A(?:false|no|off|0)\z/) {
186                         $rv->{$k} = 0;
187                 } elsif ($v =~ /\A(?:true|yes|on|1)\z/) {
188                         $rv->{$k} = 1;
189                 } else {
190                         warn "Ignoring $pfx.$k=$v in config, not boolean\n";
191                 }
192         }
193         # TODO: more arrays, we should support multi-value for
194         # more things to encourage decentralization
195         foreach my $k (qw(address altid nntpmirror)) {
196                 if (defined(my $v = $self->{"$pfx.$k"})) {
197                         $rv->{$k} = ref($v) eq 'ARRAY' ? $v : [ $v ];
198                 }
199         }
200
201         return unless $rv->{mainrepo};
202         my $name = $pfx;
203         $name =~ s/\Apublicinbox\.//;
204
205         if (!valid_inbox_name($name)) {
206                 warn "invalid inbox name: '$name'\n";
207                 return;
208         }
209
210         $rv->{name} = $name;
211         $rv->{-pi_config} = $self;
212         $rv = PublicInbox::Inbox->new($rv);
213         foreach (@{$rv->{address}}) {
214                 my $lc_addr = lc($_);
215                 $self->{-by_addr}->{$lc_addr} = $rv;
216                 $self->{-no_obfuscate}->{$lc_addr} = 1;
217         }
218         if (my $ng = $rv->{newsgroup}) {
219                 $self->{-by_newsgroup}->{$ng} = $rv;
220         }
221         $self->{-by_name}->{$name} = $rv;
222         if ($rv->{obfuscate}) {
223                 $rv->{-no_obfuscate} = $self->{-no_obfuscate};
224                 $rv->{-no_obfuscate_re} = $self->{-no_obfuscate_re};
225                 each_inbox($self, sub {}); # noop to populate -no_obfuscate
226         }
227         $rv
228 }
229
230 1;