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 use 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 sub fill_all ($) { each_inbox($_[0], \&noop) }
69 sub _lookup_fill ($$$) {
70 my ($self, $cache, $key) = @_;
71 $self->{$cache}->{$key} // do {
73 $self->{$cache}->{$key};
78 my ($self, $recipient) = @_;
79 _lookup_fill($self, '-by_addr', lc($recipient));
83 my ($self, $list_id) = @_;
84 _lookup_fill($self, '-by_list_id', lc($list_id));
87 sub lookup_name ($$) {
88 my ($self, $name) = @_;
89 $self->{-by_name}->{$name} // _fill($self, "publicinbox.$name");
93 my ($self, $cb, $arg) = @_;
94 # may auto-vivify if config file is non-existent:
95 foreach my $section (@{$self->{-section_order}}) {
96 next if $section !~ m!\Apublicinbox\.([^/]+)\z!;
97 my $ibx = lookup_name($self, $1) or next;
102 sub lookup_newsgroup {
103 my ($self, $ng) = @_;
104 _lookup_fill($self, '-by_newsgroup', lc($ng));
108 my ($self, $name) = @_;
109 $self->{-limiters}->{$name} //= do {
110 require PublicInbox::Qspawn;
111 my $max = $self->{"publicinboxlimiter.$name.max"} || 1;
112 my $limiter = PublicInbox::Qspawn::Limiter->new($max);
113 $limiter->setup_rlimit($name, $self);
118 sub config_dir { $ENV{PI_DIR} // "$ENV{HOME}/.public-inbox" }
121 my $f = $ENV{PI_CONFIG};
122 return $f if defined $f;
123 config_dir() . '/config';
126 sub config_fh_parse ($$$) {
127 my ($fh, $rs, $fs) = @_;
129 my (%section_seen, @section_order);
131 while (defined(my $line = <$fh>)) {
133 my ($k, $v) = split($fs, $line, 2);
134 my ($section) = ($k =~ /\A(\S+)\.[^\.]+\z/);
135 unless (defined $section_seen{$section}) {
136 $section_seen{$section} = 1;
137 push @section_order, $section;
142 if (ref($cur) eq "ARRAY") {
145 $rv{$k} = [ $cur, $v ];
151 $rv{-section_order} = \@section_order;
156 sub git_config_dump {
158 return {} unless -e $file;
159 my @cmd = (qw/git config -z -l/, "--file=$file");
160 my $cmd = join(' ', @cmd);
161 my $fh = popen_rd(\@cmd);
162 my $rv = config_fh_parse($fh, "\0", "\n");
163 close $fh or die "failed to close ($cmd) pipe: $?";
167 sub valid_inbox_name ($) {
170 # Similar rules found in git.git/remote.c::valid_remote_nick
171 # and git.git/refs.c::check_refname_component
172 # We don't reject /\.lock\z/, however, since we don't lock refs
173 if ($name eq '' || $name =~ /\@\{/ ||
174 $name =~ /\.\./ || $name =~ m![/:\?\[\]\^~\s\f[:cntrl:]\*]! ||
175 $name =~ /\A\./ || $name =~ /\.\z/) {
179 # Note: we allow URL-unfriendly characters; users may configure
180 # non-HTTP-accessible inboxes
184 # XXX needs testing for cgit compatibility
185 # cf. cgit/scan-tree.c::add_repo
186 sub cgit_repo_merge ($$$) {
187 my ($self, $base, $repo) = @_;
188 my $path = $repo->{dir};
189 if (defined(my $se = $self->{-cgit_strict_export})) {
190 return unless -e "$path/$se";
192 return if -e "$path/noweb";
193 # $repo = { url => 'foo.git', dir => '/path/to/foo.git' }
194 my $rel = $repo->{url};
195 unless (defined $rel) {
196 my $off = index($path, $base, 0);
200 $rel = substr($path, length($base) + 1);
203 $rel =~ s!/\.git\z!! or
206 $self->{-cgit_remove_suffix} and
207 $rel =~ s!/?\.git\z!!;
209 $self->{"coderepo.$rel.dir"} //= $path;
210 $self->{"coderepo.$rel.cgiturl"} //= $rel;
215 -d "$git_dir/objects" && -f "$git_dir/HEAD";
218 # XXX needs testing for cgit compatibility
219 sub scan_path_coderepo {
220 my ($self, $base, $path) = @_;
221 opendir(my $dh, $path) or do {
222 warn "error opening directory: $path\n";
226 if (is_git_dir($git_dir) || is_git_dir($git_dir .= '/.git')) {
227 my $repo = { dir => $git_dir };
228 cgit_repo_merge($self, $base, $repo);
231 while (defined(my $dn = readdir $dh)) {
232 next if $dn eq '.' || $dn eq '..';
233 if (index($dn, '.') == 0 && !$self->{-cgit_scan_hidden_path}) {
236 my $dir = "$path/$dn";
237 scan_path_coderepo($self, $base, $dir) if -d $dir;
241 sub scan_tree_coderepo ($$) {
242 my ($self, $path) = @_;
243 scan_path_coderepo($self, $path, $path);
246 sub scan_projects_coderepo ($$$) {
247 my ($self, $list, $path) = @_;
248 open my $fh, '<', $list or do {
249 warn "failed to open cgit projectlist=$list: $!\n";
254 scan_path_coderepo($self, $path, "$path/$_");
259 my ($self, $cgitrc, $nesting) = @_;
262 my %s = map { $_ => 1 } qw(/cgit.css /cgit.png
263 /favicon.ico /robots.txt);
264 $self->{-cgit_static} = \%s;
267 # same limit as cgit/configfile.c::parse_configfile
268 return if $nesting > 8;
270 open my $fh, '<', $cgitrc or do {
271 warn "failed to open cgitrc=$cgitrc: $!\n";
275 # FIXME: this doesn't support macro expansion via $VARS, yet
279 if (m!\Arepo\.url=(.+?)/*\z!) {
281 cgit_repo_merge($self, $repo->{dir}, $repo) if $repo;
282 $repo = { url => $nick };
283 } elsif (m!\Arepo\.path=(.+)\z!) {
287 warn "$_ without repo.url\n";
289 } elsif (m!\Ainclude=(.+)\z!) {
290 parse_cgitrc($self, $1, $nesting + 1);
291 } elsif (m!\A(scan-hidden-path|remove-suffix)=([0-9]+)\z!) {
292 my ($k, $v) = ($1, $2);
294 $self->{"-cgit_$k"} = $v;
295 } elsif (m!\A(project-list|strict-export)=(.+)\z!) {
296 my ($k, $v) = ($1, $2);
298 $self->{"-cgit_$k"} = $v;
299 } elsif (m!\Ascan-path=(.+)\z!) {
300 if (defined(my $list = $self->{-cgit_project_list})) {
301 scan_projects_coderepo($self, $list, $1);
303 scan_tree_coderepo($self, $1);
305 } elsif (m!\A(?:css|favicon|logo|repo\.logo)=(/.+)\z!) {
306 # absolute paths for static files via PublicInbox::Cgit
307 $self->{-cgit_static}->{$1} = 1;
310 cgit_repo_merge($self, $repo->{dir}, $repo) if $repo;
314 # Only git is supported at the moment, but SVN and Hg are possibilities
315 sub _fill_code_repo {
316 my ($self, $nick) = @_;
317 my $pfx = "coderepo.$nick";
319 # TODO: support gitweb and other repository viewers?
320 if (defined(my $cgitrc = delete $self->{-cgitrc_unparsed})) {
321 parse_cgitrc($self, $cgitrc, 0);
323 my $dir = $self->{"$pfx.dir"}; # aka "GIT_DIR"
324 unless (defined $dir) {
325 warn "$pfx.dir unset\n";
329 my $git = PublicInbox::Git->new($dir);
330 foreach my $t (qw(blob commit tree tag)) {
331 $git->{$t.'_url_format'} =
332 _array($self->{lc("$pfx.${t}UrlFormat")});
335 if (my $cgits = $self->{lc("$pfx.cgitUrl")}) {
336 $git->{cgit_url} = $cgits = _array($cgits);
338 # cgit supports "/blob/?id=%s", but it's only a plain-text
339 # display and requires an unabbreviated id=
340 foreach my $t (qw(blob commit tag)) {
341 $git->{$t.'_url_format'} //= map {
350 sub _git_config_bool ($) {
352 if ($val =~ /\A(?:false|no|off|[\-\+]?(?:0x)?0+)\z/i) {
354 } elsif ($val =~ /\A(?:true|yes|on|[\-\+]?(?:0x)?[0-9]+)\z/i) {
362 my ($self, $pfx) = @_;
365 foreach my $k (qw(inboxdir filter newsgroup
366 watch watchheader httpbackendmax
367 replyto feedmax nntpserver indexlevel)) {
368 my $v = $self->{"$pfx.$k"};
369 $ibx->{$k} = $v if defined $v;
372 # backwards compatibility:
373 $ibx->{inboxdir} //= $self->{"$pfx.mainrepo"};
375 foreach my $k (qw(obfuscate)) {
376 my $v = $self->{"$pfx.$k"};
378 if (defined(my $bval = _git_config_bool($v))) {
381 warn "Ignoring $pfx.$k=$v in config, not boolean\n";
384 # TODO: more arrays, we should support multi-value for
385 # more things to encourage decentralization
386 foreach my $k (qw(address altid nntpmirror coderepo hide listid url
388 if (defined(my $v = $self->{"$pfx.$k"})) {
389 $ibx->{$k} = _array($v);
393 return unless defined($ibx->{inboxdir});
395 $name =~ s/\Apublicinbox\.//;
397 if (!valid_inbox_name($name)) {
398 warn "invalid inbox name: '$name'\n";
402 $ibx->{name} = $name;
403 $ibx->{-pi_config} = $self;
404 $ibx = PublicInbox::Inbox->new($ibx);
405 foreach (@{$ibx->{address}}) {
406 my $lc_addr = lc($_);
407 $self->{-by_addr}->{$lc_addr} = $ibx;
408 $self->{-no_obfuscate}->{$lc_addr} = 1;
410 if (my $listids = $ibx->{listid}) {
411 foreach my $list_id (@$listids) {
412 $self->{-by_list_id}->{$list_id} = $ibx;
415 if (my $ng = $ibx->{newsgroup}) {
416 $self->{-by_newsgroup}->{$ng} = $ibx;
418 $self->{-by_name}->{$name} = $ibx;
419 if ($ibx->{obfuscate}) {
420 $ibx->{-no_obfuscate} = $self->{-no_obfuscate};
421 $ibx->{-no_obfuscate_re} = $self->{-no_obfuscate_re};
422 fill_all($self); # noop to populate -no_obfuscate
425 if (my $ibx_code_repos = $ibx->{coderepo}) {
426 my $code_repos = $self->{-code_repos};
427 my $repo_objs = $ibx->{-repo_objs} = [];
428 foreach my $nick (@$ibx_code_repos) {
429 my @parts = split(m!/!, $nick);
431 $valid += valid_inbox_name($_) foreach (@parts);
432 $valid == scalar(@parts) or next;
434 my $repo = $code_repos->{$nick} //=
435 _fill_code_repo($self, $nick);
436 push @$repo_objs, $repo if $repo;