1 # Copyright (C) 2014-2019 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);
24 if (ref($file) eq 'SCALAR') { # used by some tests
25 open my $fh, '<', $file or die; # PerlIO::scalar
26 $self = config_fh_parse($fh, "\n", '=');
28 $self = git_config_dump($file);
32 $self->{-by_addr} ||= {};
33 $self->{-by_list_id} ||= {};
34 $self->{-by_name} ||= {};
35 $self->{-by_newsgroup} ||= {};
36 $self->{-no_obfuscate} ||= {};
37 $self->{-limiters} ||= {};
38 $self->{-code_repos} ||= {}; # nick => PublicInbox::Git object
39 $self->{-cgitrc_unparsed} = $self->{'publicinbox.cgitrc'};
41 if (my $no = delete $self->{'publicinbox.noobfuscate'}) {
44 foreach my $n (@$no) {
45 my @n = split(/\s+/, $n);
47 if (/\S+@\S+/) { # full address
48 $self->{-no_obfuscate}->{lc $_} = 1;
50 # allow "example.com" or "@example.com"
52 push @domains, quotemeta($_);
56 my $nod = join('|', @domains);
57 $self->{-no_obfuscate_re} = qr/(?:$nod)\z/i;
59 if (my $css = delete $self->{'publicinbox.css'}) {
60 $self->{css} = _array($css);
67 my ($self, $recipient) = @_;
68 my $addr = lc($recipient);
69 my $ibx = $self->{-by_addr}->{$addr};
74 foreach my $k (keys %$self) {
75 $k =~ m!\A(publicinbox\.[^/]+)\.address\z! or next;
77 if (ref($v) eq "ARRAY") {
78 foreach my $alias (@$v) {
79 (lc($alias) eq $addr) or next;
84 (lc($v) eq $addr) or next;
89 defined $pfx or return;
94 my ($self, $list_id) = @_;
95 $list_id = lc($list_id);
96 my $ibx = $self->{-by_list_id}->{$list_id};
101 foreach my $k (keys %$self) {
102 $k =~ /\A(publicinbox\.[\w-]+)\.listid\z/ or next;
104 if (ref($v) eq "ARRAY") {
105 foreach my $alias (@$v) {
106 (lc($alias) eq $list_id) or next;
111 (lc($v) eq $list_id) or next;
116 defined $pfx or return;
120 sub lookup_name ($$) {
121 my ($self, $name) = @_;
122 $self->{-by_name}->{$name} || _fill($self, "publicinbox.$name");
126 my ($self, $cb) = @_;
127 # may auto-vivify if config file is non-existent:
128 foreach my $section (@{$self->{-section_order}}) {
129 next if $section !~ m!\Apublicinbox\.([^/]+)\z!;
130 $self->{"publicinbox.$1.mainrepo"} or next;
131 my $ibx = lookup_name($self, $1) or next;
136 sub lookup_newsgroup {
137 my ($self, $ng) = @_;
139 my $ibx = $self->{-by_newsgroup}->{$ng};
142 foreach my $k (keys %$self) {
143 $k =~ m!\A(publicinbox\.[^/]+)\.newsgroup\z! or next;
147 $ibx = _fill($self, $pfx);
155 my ($self, $name) = @_;
156 $self->{-limiters}->{$name} ||= do {
157 require PublicInbox::Qspawn;
158 my $max = $self->{"publicinboxlimiter.$name.max"} || 1;
159 my $limiter = PublicInbox::Qspawn::Limiter->new($max);
160 $limiter->setup_rlimit($name, $self);
165 sub config_dir { $ENV{PI_DIR} || "$ENV{HOME}/.public-inbox" }
168 my $f = $ENV{PI_CONFIG};
169 return $f if defined $f;
170 config_dir() . '/config';
173 sub config_fh_parse ($$$) {
174 my ($fh, $rs, $fs) = @_;
176 my (%section_seen, @section_order);
178 while (defined(my $line = <$fh>)) {
180 my ($k, $v) = split($fs, $line, 2);
181 my ($section) = ($k =~ /\A(\S+)\.[^\.]+\z/);
182 unless (defined $section_seen{$section}) {
183 $section_seen{$section} = 1;
184 push @section_order, $section;
189 if (ref($cur) eq "ARRAY") {
192 $rv{$k} = [ $cur, $v ];
198 $rv{-section_order} = \@section_order;
203 sub git_config_dump {
205 return {} unless -e $file;
206 my @cmd = (qw/git config -z -l/, "--file=$file");
207 my $cmd = join(' ', @cmd);
208 my $fh = popen_rd(\@cmd) or die "popen_rd failed for $file: $!\n";
209 my $rv = config_fh_parse($fh, "\0", "\n");
210 close $fh or die "failed to close ($cmd) pipe: $?";
214 sub valid_inbox_name ($) {
217 # Similar rules found in git.git/remote.c::valid_remote_nick
218 # and git.git/refs.c::check_refname_component
219 # We don't reject /\.lock\z/, however, since we don't lock refs
220 if ($name eq '' || $name =~ /\@\{/ ||
221 $name =~ /\.\./ || $name =~ m![/:\?\[\]\^~\s\f[:cntrl:]\*]! ||
222 $name =~ /\A\./ || $name =~ /\.\z/) {
226 # Note: we allow URL-unfriendly characters; users may configure
227 # non-HTTP-accessible inboxes
231 # XXX needs testing for cgit compatibility
232 # cf. cgit/scan-tree.c::add_repo
233 sub cgit_repo_merge ($$$) {
234 my ($self, $base, $repo) = @_;
235 my $path = $repo->{dir};
236 if (defined(my $se = $self->{-cgit_strict_export})) {
237 return unless -e "$path/$se";
239 return if -e "$path/noweb";
240 # $repo = { url => 'foo.git', dir => '/path/to/foo.git' }
241 my $rel = $repo->{url};
242 unless (defined $rel) {
243 my $off = index($path, $base, 0);
247 $rel = substr($path, length($base) + 1);
250 $rel =~ s!/\.git\z!! or
253 $self->{-cgit_remove_suffix} and
254 $rel =~ s!/?\.git\z!!;
256 $self->{"coderepo.$rel.dir"} ||= $path;
257 $self->{"coderepo.$rel.cgiturl"} ||= $rel;
262 -d "$git_dir/objects" && -f "$git_dir/HEAD";
265 # XXX needs testing for cgit compatibility
266 sub scan_path_coderepo {
267 my ($self, $base, $path) = @_;
268 opendir(my $dh, $path) or do {
269 warn "error opening directory: $path\n";
273 if (is_git_dir($git_dir) || is_git_dir($git_dir .= '/.git')) {
274 my $repo = { dir => $git_dir };
275 cgit_repo_merge($self, $base, $repo);
278 while (defined(my $dn = readdir $dh)) {
279 next if $dn eq '.' || $dn eq '..';
280 if (index($dn, '.') == 0 && !$self->{-cgit_scan_hidden_path}) {
283 my $dir = "$path/$dn";
284 scan_path_coderepo($self, $base, $dir) if -d $dir;
288 sub scan_tree_coderepo ($$) {
289 my ($self, $path) = @_;
290 scan_path_coderepo($self, $path, $path);
293 sub scan_projects_coderepo ($$$) {
294 my ($self, $list, $path) = @_;
295 open my $fh, '<', $list or do {
296 warn "failed to open cgit projectlist=$list: $!\n";
301 scan_path_coderepo($self, $path, "$path/$_");
306 my ($self, $cgitrc, $nesting) = @_;
309 my %s = map { $_ => 1 } qw(/cgit.css /cgit.png
310 /favicon.ico /robots.txt);
311 $self->{-cgit_static} = \%s;
314 # same limit as cgit/configfile.c::parse_configfile
315 return if $nesting > 8;
317 open my $fh, '<', $cgitrc or do {
318 warn "failed to open cgitrc=$cgitrc: $!\n";
322 # FIXME: this doesn't support macro expansion via $VARS, yet
326 if (m!\Arepo\.url=(.+?)/*\z!) {
328 cgit_repo_merge($self, $repo->{dir}, $repo) if $repo;
329 $repo = { url => $nick };
330 } elsif (m!\Arepo\.path=(.+)\z!) {
334 warn "$_ without repo.url\n";
336 } elsif (m!\Ainclude=(.+)\z!) {
337 parse_cgitrc($self, $1, $nesting + 1);
338 } elsif (m!\A(scan-hidden-path|remove-suffix)=([0-9]+)\z!) {
339 my ($k, $v) = ($1, $2);
341 $self->{"-cgit_$k"} = $v;
342 } elsif (m!\A(project-list|strict-export)=(.+)\z!) {
343 my ($k, $v) = ($1, $2);
345 $self->{"-cgit_$k"} = $v;
346 } elsif (m!\Ascan-path=(.+)\z!) {
347 if (defined(my $list = $self->{-cgit_project_list})) {
348 scan_projects_coderepo($self, $list, $1);
350 scan_tree_coderepo($self, $1);
352 } elsif (m!\A(?:css|favicon|logo|repo\.logo)=(/.+)\z!) {
353 # absolute paths for static files via PublicInbox::Cgit
354 $self->{-cgit_static}->{$1} = 1;
357 cgit_repo_merge($self, $repo->{dir}, $repo) if $repo;
361 # Only git is supported at the moment, but SVN and Hg are possibilities
362 sub _fill_code_repo {
363 my ($self, $nick) = @_;
364 my $pfx = "coderepo.$nick";
366 # TODO: support gitweb and other repository viewers?
367 if (defined(my $cgitrc = delete $self->{-cgitrc_unparsed})) {
368 parse_cgitrc($self, $cgitrc, 0);
370 my $dir = $self->{"$pfx.dir"}; # aka "GIT_DIR"
371 unless (defined $dir) {
372 warn "$pfx.dir unset\n";
376 my $git = PublicInbox::Git->new($dir);
377 foreach my $t (qw(blob commit tree tag)) {
378 $git->{$t.'_url_format'} =
379 _array($self->{lc("$pfx.${t}UrlFormat")});
382 if (my $cgits = $self->{lc("$pfx.cgitUrl")}) {
383 $git->{cgit_url} = $cgits = _array($cgits);
385 # cgit supports "/blob/?id=%s", but it's only a plain-text
386 # display and requires an unabbreviated id=
387 foreach my $t (qw(blob commit tag)) {
388 $git->{$t.'_url_format'} ||= map {
397 sub _git_config_bool ($) {
399 if ($val =~ /\A(?:false|no|off|[\-\+]?(?:0x)?0+)\z/i) {
401 } elsif ($val =~ /\A(?:true|yes|on|[\-\+]?(?:0x)?[0-9]+)\z/i) {
409 my ($self, $pfx) = @_;
412 foreach my $k (qw(mainrepo filter url newsgroup
413 infourl watch watchheader httpbackendmax
414 replyto feedmax nntpserver indexlevel)) {
415 my $v = $self->{"$pfx.$k"};
416 $ibx->{$k} = $v if defined $v;
418 foreach my $k (qw(obfuscate)) {
419 my $v = $self->{"$pfx.$k"};
421 if (defined(my $bval = _git_config_bool($v))) {
424 warn "Ignoring $pfx.$k=$v in config, not boolean\n";
427 # TODO: more arrays, we should support multi-value for
428 # more things to encourage decentralization
429 foreach my $k (qw(address altid nntpmirror coderepo hide listid)) {
430 if (defined(my $v = $self->{"$pfx.$k"})) {
431 $ibx->{$k} = _array($v);
435 return unless $ibx->{mainrepo};
437 $name =~ s/\Apublicinbox\.//;
439 if (!valid_inbox_name($name)) {
440 warn "invalid inbox name: '$name'\n";
444 $ibx->{name} = $name;
445 $ibx->{-pi_config} = $self;
446 $ibx = PublicInbox::Inbox->new($ibx);
447 foreach (@{$ibx->{address}}) {
448 my $lc_addr = lc($_);
449 $self->{-by_addr}->{$lc_addr} = $ibx;
450 $self->{-no_obfuscate}->{$lc_addr} = 1;
452 if (my $listids = $ibx->{listid}) {
453 foreach my $list_id (@$listids) {
454 $self->{-by_list_id}->{$list_id} = $ibx;
457 if (my $ng = $ibx->{newsgroup}) {
458 $self->{-by_newsgroup}->{$ng} = $ibx;
460 $self->{-by_name}->{$name} = $ibx;
461 if ($ibx->{obfuscate}) {
462 $ibx->{-no_obfuscate} = $self->{-no_obfuscate};
463 $ibx->{-no_obfuscate_re} = $self->{-no_obfuscate_re};
464 each_inbox($self, sub {}); # noop to populate -no_obfuscate
467 if (my $ibx_code_repos = $ibx->{coderepo}) {
468 my $code_repos = $self->{-code_repos};
469 my $repo_objs = $ibx->{-repo_objs} = [];
470 foreach my $nick (@$ibx_code_repos) {
471 my @parts = split(m!/!, $nick);
473 $valid += valid_inbox_name($_) foreach (@parts);
474 $valid == scalar(@parts) or next;
476 my $repo = $code_repos->{$nick} ||=
477 _fill_code_repo($self, $nick);
478 push @$repo_objs, $repo if $repo;