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);
66 sub _fill_all ($) { each_inbox($_[0], sub {}) }
68 sub _lookup_fill ($$$) {
69 my ($self, $cache, $key) = @_;
70 $self->{$cache}->{$key} // do {
72 $self->{$cache}->{$key};
77 my ($self, $recipient) = @_;
78 _lookup_fill($self, '-by_addr', lc($recipient));
82 my ($self, $list_id) = @_;
83 _lookup_fill($self, '-by_list_id', lc($list_id));
86 sub lookup_name ($$) {
87 my ($self, $name) = @_;
88 $self->{-by_name}->{$name} // _fill($self, "publicinbox.$name");
93 # may auto-vivify if config file is non-existent:
94 foreach my $section (@{$self->{-section_order}}) {
95 next if $section !~ m!\Apublicinbox\.([^/]+)\z!;
96 my $ibx = lookup_name($self, $1) or next;
101 sub lookup_newsgroup {
102 my ($self, $ng) = @_;
103 _lookup_fill($self, '-by_newsgroup', lc($ng));
107 my ($self, $name) = @_;
108 $self->{-limiters}->{$name} //= do {
109 require PublicInbox::Qspawn;
110 my $max = $self->{"publicinboxlimiter.$name.max"} || 1;
111 my $limiter = PublicInbox::Qspawn::Limiter->new($max);
112 $limiter->setup_rlimit($name, $self);
117 sub config_dir { $ENV{PI_DIR} // "$ENV{HOME}/.public-inbox" }
120 my $f = $ENV{PI_CONFIG};
121 return $f if defined $f;
122 config_dir() . '/config';
125 sub config_fh_parse ($$$) {
126 my ($fh, $rs, $fs) = @_;
128 my (%section_seen, @section_order);
130 while (defined(my $line = <$fh>)) {
132 my ($k, $v) = split($fs, $line, 2);
133 my ($section) = ($k =~ /\A(\S+)\.[^\.]+\z/);
134 unless (defined $section_seen{$section}) {
135 $section_seen{$section} = 1;
136 push @section_order, $section;
141 if (ref($cur) eq "ARRAY") {
144 $rv{$k} = [ $cur, $v ];
150 $rv{-section_order} = \@section_order;
155 sub git_config_dump {
157 return {} unless -e $file;
158 my @cmd = (qw/git config -z -l/, "--file=$file");
159 my $cmd = join(' ', @cmd);
160 my $fh = popen_rd(\@cmd) or die "popen_rd failed for $file: $!\n";
161 my $rv = config_fh_parse($fh, "\0", "\n");
162 close $fh or die "failed to close ($cmd) pipe: $?";
166 sub valid_inbox_name ($) {
169 # Similar rules found in git.git/remote.c::valid_remote_nick
170 # and git.git/refs.c::check_refname_component
171 # We don't reject /\.lock\z/, however, since we don't lock refs
172 if ($name eq '' || $name =~ /\@\{/ ||
173 $name =~ /\.\./ || $name =~ m![/:\?\[\]\^~\s\f[:cntrl:]\*]! ||
174 $name =~ /\A\./ || $name =~ /\.\z/) {
178 # Note: we allow URL-unfriendly characters; users may configure
179 # non-HTTP-accessible inboxes
183 # XXX needs testing for cgit compatibility
184 # cf. cgit/scan-tree.c::add_repo
185 sub cgit_repo_merge ($$$) {
186 my ($self, $base, $repo) = @_;
187 my $path = $repo->{dir};
188 if (defined(my $se = $self->{-cgit_strict_export})) {
189 return unless -e "$path/$se";
191 return if -e "$path/noweb";
192 # $repo = { url => 'foo.git', dir => '/path/to/foo.git' }
193 my $rel = $repo->{url};
194 unless (defined $rel) {
195 my $off = index($path, $base, 0);
199 $rel = substr($path, length($base) + 1);
202 $rel =~ s!/\.git\z!! or
205 $self->{-cgit_remove_suffix} and
206 $rel =~ s!/?\.git\z!!;
208 $self->{"coderepo.$rel.dir"} //= $path;
209 $self->{"coderepo.$rel.cgiturl"} //= $rel;
214 -d "$git_dir/objects" && -f "$git_dir/HEAD";
217 # XXX needs testing for cgit compatibility
218 sub scan_path_coderepo {
219 my ($self, $base, $path) = @_;
220 opendir(my $dh, $path) or do {
221 warn "error opening directory: $path\n";
225 if (is_git_dir($git_dir) || is_git_dir($git_dir .= '/.git')) {
226 my $repo = { dir => $git_dir };
227 cgit_repo_merge($self, $base, $repo);
230 while (defined(my $dn = readdir $dh)) {
231 next if $dn eq '.' || $dn eq '..';
232 if (index($dn, '.') == 0 && !$self->{-cgit_scan_hidden_path}) {
235 my $dir = "$path/$dn";
236 scan_path_coderepo($self, $base, $dir) if -d $dir;
240 sub scan_tree_coderepo ($$) {
241 my ($self, $path) = @_;
242 scan_path_coderepo($self, $path, $path);
245 sub scan_projects_coderepo ($$$) {
246 my ($self, $list, $path) = @_;
247 open my $fh, '<', $list or do {
248 warn "failed to open cgit projectlist=$list: $!\n";
253 scan_path_coderepo($self, $path, "$path/$_");
258 my ($self, $cgitrc, $nesting) = @_;
261 my %s = map { $_ => 1 } qw(/cgit.css /cgit.png
262 /favicon.ico /robots.txt);
263 $self->{-cgit_static} = \%s;
266 # same limit as cgit/configfile.c::parse_configfile
267 return if $nesting > 8;
269 open my $fh, '<', $cgitrc or do {
270 warn "failed to open cgitrc=$cgitrc: $!\n";
274 # FIXME: this doesn't support macro expansion via $VARS, yet
278 if (m!\Arepo\.url=(.+?)/*\z!) {
280 cgit_repo_merge($self, $repo->{dir}, $repo) if $repo;
281 $repo = { url => $nick };
282 } elsif (m!\Arepo\.path=(.+)\z!) {
286 warn "$_ without repo.url\n";
288 } elsif (m!\Ainclude=(.+)\z!) {
289 parse_cgitrc($self, $1, $nesting + 1);
290 } elsif (m!\A(scan-hidden-path|remove-suffix)=([0-9]+)\z!) {
291 my ($k, $v) = ($1, $2);
293 $self->{"-cgit_$k"} = $v;
294 } elsif (m!\A(project-list|strict-export)=(.+)\z!) {
295 my ($k, $v) = ($1, $2);
297 $self->{"-cgit_$k"} = $v;
298 } elsif (m!\Ascan-path=(.+)\z!) {
299 if (defined(my $list = $self->{-cgit_project_list})) {
300 scan_projects_coderepo($self, $list, $1);
302 scan_tree_coderepo($self, $1);
304 } elsif (m!\A(?:css|favicon|logo|repo\.logo)=(/.+)\z!) {
305 # absolute paths for static files via PublicInbox::Cgit
306 $self->{-cgit_static}->{$1} = 1;
309 cgit_repo_merge($self, $repo->{dir}, $repo) if $repo;
313 # Only git is supported at the moment, but SVN and Hg are possibilities
314 sub _fill_code_repo {
315 my ($self, $nick) = @_;
316 my $pfx = "coderepo.$nick";
318 # TODO: support gitweb and other repository viewers?
319 if (defined(my $cgitrc = delete $self->{-cgitrc_unparsed})) {
320 parse_cgitrc($self, $cgitrc, 0);
322 my $dir = $self->{"$pfx.dir"}; # aka "GIT_DIR"
323 unless (defined $dir) {
324 warn "$pfx.dir unset\n";
328 my $git = PublicInbox::Git->new($dir);
329 foreach my $t (qw(blob commit tree tag)) {
330 $git->{$t.'_url_format'} =
331 _array($self->{lc("$pfx.${t}UrlFormat")});
334 if (my $cgits = $self->{lc("$pfx.cgitUrl")}) {
335 $git->{cgit_url} = $cgits = _array($cgits);
337 # cgit supports "/blob/?id=%s", but it's only a plain-text
338 # display and requires an unabbreviated id=
339 foreach my $t (qw(blob commit tag)) {
340 $git->{$t.'_url_format'} //= map {
349 sub _git_config_bool ($) {
351 if ($val =~ /\A(?:false|no|off|[\-\+]?(?:0x)?0+)\z/i) {
353 } elsif ($val =~ /\A(?:true|yes|on|[\-\+]?(?:0x)?[0-9]+)\z/i) {
361 my ($self, $pfx) = @_;
364 foreach my $k (qw(inboxdir filter url newsgroup
365 infourl watch watchheader httpbackendmax
366 replyto feedmax nntpserver indexlevel)) {
367 my $v = $self->{"$pfx.$k"};
368 $ibx->{$k} = $v if defined $v;
371 # backwards compatibility:
372 $ibx->{inboxdir} //= $self->{"$pfx.mainrepo"};
374 foreach my $k (qw(obfuscate)) {
375 my $v = $self->{"$pfx.$k"};
377 if (defined(my $bval = _git_config_bool($v))) {
380 warn "Ignoring $pfx.$k=$v in config, not boolean\n";
383 # TODO: more arrays, we should support multi-value for
384 # more things to encourage decentralization
385 foreach my $k (qw(address altid nntpmirror coderepo hide listid)) {
386 if (defined(my $v = $self->{"$pfx.$k"})) {
387 $ibx->{$k} = _array($v);
391 return unless defined($ibx->{inboxdir});
393 $name =~ s/\Apublicinbox\.//;
395 if (!valid_inbox_name($name)) {
396 warn "invalid inbox name: '$name'\n";
400 $ibx->{name} = $name;
401 $ibx->{-pi_config} = $self;
402 $ibx = PublicInbox::Inbox->new($ibx);
403 foreach (@{$ibx->{address}}) {
404 my $lc_addr = lc($_);
405 $self->{-by_addr}->{$lc_addr} = $ibx;
406 $self->{-no_obfuscate}->{$lc_addr} = 1;
408 if (my $listids = $ibx->{listid}) {
409 foreach my $list_id (@$listids) {
410 $self->{-by_list_id}->{$list_id} = $ibx;
413 if (my $ng = $ibx->{newsgroup}) {
414 $self->{-by_newsgroup}->{$ng} = $ibx;
416 $self->{-by_name}->{$name} = $ibx;
417 if ($ibx->{obfuscate}) {
418 $ibx->{-no_obfuscate} = $self->{-no_obfuscate};
419 $ibx->{-no_obfuscate_re} = $self->{-no_obfuscate_re};
420 _fill_all($self); # noop to populate -no_obfuscate
423 if (my $ibx_code_repos = $ibx->{coderepo}) {
424 my $code_repos = $self->{-code_repos};
425 my $repo_objs = $ibx->{-repo_objs} = [];
426 foreach my $nick (@$ibx_code_repos) {
427 my @parts = split(m!/!, $nick);
429 $valid += valid_inbox_name($_) foreach (@parts);
430 $valid == scalar(@parts) or next;
432 my $repo = $code_repos->{$nick} //=
433 _fill_code_repo($self, $nick);
434 push @$repo_objs, $repo if $repo;