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
6 # Note: I hate camelCase; but git-config(1) uses it, but it's better
7 # than alllowercasewithoutunderscores, so use lc('configKey') where
8 # applicable for readability
10 package PublicInbox::Config;
13 require PublicInbox::Inbox;
14 use PublicInbox::Spawn qw(popen_rd);
16 sub _array ($) { ref($_[0]) eq 'ARRAY' ? $_[0] : [ $_[0] ] }
18 # returns key-value pairs of config directives in a hash
19 # if keys may be multi-value, the value is an array ref containing all values
21 my ($class, $file) = @_;
22 $file = default_file() unless defined($file);
23 $file = ref $file ? $file : git_config_dump($file);
24 my $self = bless $file, $class;
27 $self->{-by_addr} ||= {};
28 $self->{-by_name} ||= {};
29 $self->{-by_newsgroup} ||= {};
30 $self->{-no_obfuscate} ||= {};
31 $self->{-limiters} ||= {};
32 $self->{-code_repos} ||= {}; # nick => PublicInbox::Git object
34 if (my $no = delete $self->{'publicinbox.noobfuscate'}) {
37 foreach my $n (@$no) {
38 my @n = split(/\s+/, $n);
40 if (/\S+@\S+/) { # full address
41 $self->{-no_obfuscate}->{lc $_} = 1;
43 # allow "example.com" or "@example.com"
45 push @domains, quotemeta($_);
49 my $nod = join('|', @domains);
50 $self->{-no_obfuscate_re} = qr/(?:$nod)\z/i;
52 if (my $css = delete $self->{'publicinbox.css'}) {
53 $self->{css} = _array($css);
60 my ($self, $recipient) = @_;
61 my $addr = lc($recipient);
62 my $inbox = $self->{-by_addr}->{$addr};
63 return $inbox if $inbox;
67 foreach my $k (keys %$self) {
68 $k =~ m!\A(publicinbox\.[^/]+)\.address\z! or next;
70 if (ref($v) eq "ARRAY") {
71 foreach my $alias (@$v) {
72 (lc($alias) eq $addr) or next;
77 (lc($v) eq $addr) or next;
82 defined $pfx or return;
86 sub lookup_name ($$) {
87 my ($self, $name) = @_;
88 $self->{-by_name}->{$name} || _fill($self, "publicinbox.$name");
93 if (my $section_order = $self->{-section_order}) {
94 foreach my $section (@$section_order) {
95 next if $section !~ m!\Apublicinbox\.([^/]+)\z!;
96 $self->{"publicinbox.$1.mainrepo"} or next;
97 my $ibx = lookup_name($self, $1) or next;
102 foreach my $k (keys %$self) {
103 $k =~ m!\Apublicinbox\.([^/]+)\.mainrepo\z! or next;
106 my $ibx = lookup_name($self, $1) or next;
112 sub lookup_newsgroup {
113 my ($self, $ng) = @_;
115 my $rv = $self->{-by_newsgroup}->{$ng};
118 foreach my $k (keys %$self) {
119 $k =~ m!\A(publicinbox\.[^/]+)\.newsgroup\z! or next;
123 $rv = _fill($self, $pfx);
131 my ($self, $name) = @_;
132 $self->{-limiters}->{$name} ||= do {
133 require PublicInbox::Qspawn;
134 my $max = $self->{"publicinboxlimiter.$name.max"};
135 PublicInbox::Qspawn::Limiter->new($max);
139 sub config_dir { $ENV{PI_DIR} || "$ENV{HOME}/.public-inbox" }
142 my $f = $ENV{PI_CONFIG};
143 return $f if defined $f;
144 config_dir() . '/config';
147 sub git_config_dump {
149 my (%section_seen, @section_order);
150 return {} unless -e $file;
151 my @cmd = (qw/git config/, "--file=$file", '-l');
152 my $cmd = join(' ', @cmd);
153 my $fh = popen_rd(\@cmd) or die "popen_rd failed for $file: $!\n";
156 while (defined(my $line = <$fh>)) {
158 my ($k, $v) = split(/=/, $line, 2);
160 my ($section) = ($k =~ /\A(\S+)\.[^\.]+\z/);
161 unless (defined $section_seen{$section}) {
162 $section_seen{$section} = 1;
163 push @section_order, $section;
168 if (ref($cur) eq "ARRAY") {
171 $rv{$k} = [ $cur, $v ];
177 close $fh or die "failed to close ($cmd) pipe: $?";
178 $rv{-section_order} = \@section_order;
183 sub valid_inbox_name ($) {
186 # Similar rules found in git.git/remote.c::valid_remote_nick
187 # and git.git/refs.c::check_refname_component
188 # We don't reject /\.lock\z/, however, since we don't lock refs
189 if ($name eq '' || $name =~ /\@\{/ ||
190 $name =~ /\.\./ || $name =~ m![/:\?\[\]\^~\s\f[:cntrl:]\*]! ||
191 $name =~ /\A\./ || $name =~ /\.\z/) {
195 # Note: we allow URL-unfriendly characters; users may configure
196 # non-HTTP-accessible inboxes
200 sub cgit_repo_merge ($$) {
201 my ($self, $repo) = @_;
202 # $repo = { url => 'foo.git', path => '/path/to/foo.git' }
203 my $nick = $repo->{url};
204 $self->{"coderepo.$nick.dir"} ||= $repo->{path};
205 $self->{"coderepo.$nick.cgiturl"} ||= $nick;
209 my ($self, $cgitrc, $nesting) = @_;
211 # same limit as cgit/configfile.c::parse_configfile
212 return if $nesting > 8;
214 open my $fh, '<', $cgitrc or do {
215 warn "failed to open cgitrc=$cgitrc: $!\n";
219 # FIXME: this doesn't support macro expansion via $VARS, yet
223 if (m!\Arepo\.url=(.+?)/*\z!) {
225 cgit_repo_merge($self, $repo) if $repo;
226 $repo = { url => $nick };
227 } elsif (m!\Arepo\.path=(.+)\z!) {
231 warn "$_ without repo.url\n";
233 } elsif (m!\Ainclude=(.+)\z!) {
234 parse_cgitrc($self, $1, $nesting + 1);
237 cgit_repo_merge($self, $repo) if $repo;
241 # Only git is supported at the moment, but SVN and Hg are possibilities
242 sub _fill_code_repo {
243 my ($self, $nick) = @_;
244 my $pfx = "coderepo.$nick";
246 # TODO: support gitweb and other repository viewers?
247 if (defined(my $cgitrc = delete $self->{'publicinbox.cgitrc'})) {
248 parse_cgitrc($self, $cgitrc, 0);
250 my $dir = $self->{"$pfx.dir"}; # aka "GIT_DIR"
251 unless (defined $dir) {
252 warn "$pfx.dir unset";
256 my $git = PublicInbox::Git->new($dir);
257 foreach my $t (qw(blob commit tree tag)) {
258 $git->{$t.'_url_format'} =
259 _array($self->{lc("$pfx.${t}UrlFormat")});
262 if (my $cgits = $self->{lc("$pfx.cgitUrl")}) {
263 $git->{cgit_url} = $cgits = _array($cgits);
265 # cgit supports "/blob/?id=%s", but it's only a plain-text
266 # display and requires an unabbreviated id=
267 foreach my $t (qw(blob commit tag)) {
268 $git->{$t.'_url_format'} ||= map {
278 my ($self, $pfx) = @_;
281 foreach my $k (qw(mainrepo filter url newsgroup
282 infourl watch watchheader httpbackendmax
283 replyto feedmax nntpserver indexlevel)) {
284 my $v = $self->{"$pfx.$k"};
285 $rv->{$k} = $v if defined $v;
287 foreach my $k (qw(obfuscate)) {
288 my $v = $self->{"$pfx.$k"};
290 if ($v =~ /\A(?:false|no|off|0)\z/) {
292 } elsif ($v =~ /\A(?:true|yes|on|1)\z/) {
295 warn "Ignoring $pfx.$k=$v in config, not boolean\n";
298 # TODO: more arrays, we should support multi-value for
299 # more things to encourage decentralization
300 foreach my $k (qw(address altid nntpmirror coderepo)) {
301 if (defined(my $v = $self->{"$pfx.$k"})) {
302 $rv->{$k} = _array($v);
306 return unless $rv->{mainrepo};
308 $name =~ s/\Apublicinbox\.//;
310 if (!valid_inbox_name($name)) {
311 warn "invalid inbox name: '$name'\n";
316 $rv->{-pi_config} = $self;
317 $rv = PublicInbox::Inbox->new($rv);
318 foreach (@{$rv->{address}}) {
319 my $lc_addr = lc($_);
320 $self->{-by_addr}->{$lc_addr} = $rv;
321 $self->{-no_obfuscate}->{$lc_addr} = 1;
323 if (my $ng = $rv->{newsgroup}) {
324 $self->{-by_newsgroup}->{$ng} = $rv;
326 $self->{-by_name}->{$name} = $rv;
327 if ($rv->{obfuscate}) {
328 $rv->{-no_obfuscate} = $self->{-no_obfuscate};
329 $rv->{-no_obfuscate_re} = $self->{-no_obfuscate_re};
330 each_inbox($self, sub {}); # noop to populate -no_obfuscate
333 if (my $ibx_code_repos = $rv->{coderepo}) {
334 my $code_repos = $self->{-code_repos};
335 my $repo_objs = $rv->{-repo_objs} = [];
336 foreach my $nick (@$ibx_code_repos) {
337 my @parts = split(m!/!, $nick);
339 $valid += valid_inbox_name($_) foreach (@parts);
340 $valid == scalar(@parts) or next;
342 my $repo = $code_repos->{$nick} ||=
343 _fill_code_repo($self, $nick);
344 push @$repo_objs, $repo if $repo;