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);
23 $file = ref $file ? $file : git_config_dump($file);
24 my $self = bless $file, $class;
27 $self->{-by_addr} ||= {};
28 $self->{-by_list_id} ||= {};
29 $self->{-by_name} ||= {};
30 $self->{-by_newsgroup} ||= {};
31 $self->{-no_obfuscate} ||= {};
32 $self->{-limiters} ||= {};
33 $self->{-code_repos} ||= {}; # nick => PublicInbox::Git object
34 $self->{-cgitrc_unparsed} = $self->{'publicinbox.cgitrc'};
36 if (my $no = delete $self->{'publicinbox.noobfuscate'}) {
39 foreach my $n (@$no) {
40 my @n = split(/\s+/, $n);
42 if (/\S+@\S+/) { # full address
43 $self->{-no_obfuscate}->{lc $_} = 1;
45 # allow "example.com" or "@example.com"
47 push @domains, quotemeta($_);
51 my $nod = join('|', @domains);
52 $self->{-no_obfuscate_re} = qr/(?:$nod)\z/i;
54 if (my $css = delete $self->{'publicinbox.css'}) {
55 $self->{css} = _array($css);
62 my ($self, $recipient) = @_;
63 my $addr = lc($recipient);
64 my $ibx = $self->{-by_addr}->{$addr};
69 foreach my $k (keys %$self) {
70 $k =~ m!\A(publicinbox\.[^/]+)\.address\z! or next;
72 if (ref($v) eq "ARRAY") {
73 foreach my $alias (@$v) {
74 (lc($alias) eq $addr) or next;
79 (lc($v) eq $addr) or next;
84 defined $pfx or return;
89 my ($self, $list_id) = @_;
90 $list_id = lc($list_id);
91 my $ibx = $self->{-by_list_id}->{$list_id};
96 foreach my $k (keys %$self) {
97 $k =~ /\A(publicinbox\.[\w-]+)\.listid\z/ or next;
99 if (ref($v) eq "ARRAY") {
100 foreach my $alias (@$v) {
101 (lc($alias) eq $list_id) or next;
106 (lc($v) eq $list_id) or next;
111 defined $pfx or return;
115 sub lookup_name ($$) {
116 my ($self, $name) = @_;
117 $self->{-by_name}->{$name} || _fill($self, "publicinbox.$name");
121 my ($self, $cb) = @_;
122 if (my $section_order = $self->{-section_order}) {
123 foreach my $section (@$section_order) {
124 next if $section !~ m!\Apublicinbox\.([^/]+)\z!;
125 $self->{"publicinbox.$1.mainrepo"} or next;
126 my $ibx = lookup_name($self, $1) or next;
131 foreach my $k (keys %$self) {
132 $k =~ m!\Apublicinbox\.([^/]+)\.mainrepo\z! or next;
135 my $ibx = lookup_name($self, $1) or next;
141 sub lookup_newsgroup {
142 my ($self, $ng) = @_;
144 my $ibx = $self->{-by_newsgroup}->{$ng};
147 foreach my $k (keys %$self) {
148 $k =~ m!\A(publicinbox\.[^/]+)\.newsgroup\z! or next;
152 $ibx = _fill($self, $pfx);
160 my ($self, $name) = @_;
161 $self->{-limiters}->{$name} ||= do {
162 require PublicInbox::Qspawn;
163 my $max = $self->{"publicinboxlimiter.$name.max"} || 1;
164 my $limiter = PublicInbox::Qspawn::Limiter->new($max);
165 $limiter->setup_rlimit($name, $self);
170 sub config_dir { $ENV{PI_DIR} || "$ENV{HOME}/.public-inbox" }
173 my $f = $ENV{PI_CONFIG};
174 return $f if defined $f;
175 config_dir() . '/config';
178 sub git_config_dump {
180 my (%section_seen, @section_order);
181 return {} unless -e $file;
182 my @cmd = (qw/git config -z -l/, "--file=$file");
183 my $cmd = join(' ', @cmd);
184 my $fh = popen_rd(\@cmd) or die "popen_rd failed for $file: $!\n";
187 while (defined(my $line = <$fh>)) {
189 my ($k, $v) = split(/\n/, $line, 2);
191 my ($section) = ($k =~ /\A(\S+)\.[^\.]+\z/);
192 unless (defined $section_seen{$section}) {
193 $section_seen{$section} = 1;
194 push @section_order, $section;
199 if (ref($cur) eq "ARRAY") {
202 $rv{$k} = [ $cur, $v ];
208 close $fh or die "failed to close ($cmd) pipe: $?";
209 $rv{-section_order} = \@section_order;
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;