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_name} ||= {};
29 $self->{-by_newsgroup} ||= {};
30 $self->{-no_obfuscate} ||= {};
31 $self->{-limiters} ||= {};
32 $self->{-code_repos} ||= {}; # nick => PublicInbox::Git object
33 $self->{-cgitrc_unparsed} = $self->{'publicinbox.cgitrc'};
35 if (my $no = delete $self->{'publicinbox.noobfuscate'}) {
38 foreach my $n (@$no) {
39 my @n = split(/\s+/, $n);
41 if (/\S+@\S+/) { # full address
42 $self->{-no_obfuscate}->{lc $_} = 1;
44 # allow "example.com" or "@example.com"
46 push @domains, quotemeta($_);
50 my $nod = join('|', @domains);
51 $self->{-no_obfuscate_re} = qr/(?:$nod)\z/i;
53 if (my $css = delete $self->{'publicinbox.css'}) {
54 $self->{css} = _array($css);
61 my ($self, $recipient) = @_;
62 my $addr = lc($recipient);
63 my $ibx = $self->{-by_addr}->{$addr};
68 foreach my $k (keys %$self) {
69 $k =~ m!\A(publicinbox\.[^/]+)\.address\z! or next;
71 if (ref($v) eq "ARRAY") {
72 foreach my $alias (@$v) {
73 (lc($alias) eq $addr) or next;
78 (lc($v) eq $addr) or next;
83 defined $pfx or return;
87 sub lookup_name ($$) {
88 my ($self, $name) = @_;
89 $self->{-by_name}->{$name} || _fill($self, "publicinbox.$name");
94 if (my $section_order = $self->{-section_order}) {
95 foreach my $section (@$section_order) {
96 next if $section !~ m!\Apublicinbox\.([^/]+)\z!;
97 $self->{"publicinbox.$1.mainrepo"} or next;
98 my $ibx = lookup_name($self, $1) or next;
103 foreach my $k (keys %$self) {
104 $k =~ m!\Apublicinbox\.([^/]+)\.mainrepo\z! or next;
107 my $ibx = lookup_name($self, $1) or next;
113 sub lookup_newsgroup {
114 my ($self, $ng) = @_;
116 my $ibx = $self->{-by_newsgroup}->{$ng};
119 foreach my $k (keys %$self) {
120 $k =~ m!\A(publicinbox\.[^/]+)\.newsgroup\z! or next;
124 $ibx = _fill($self, $pfx);
132 my ($self, $name) = @_;
133 $self->{-limiters}->{$name} ||= do {
134 require PublicInbox::Qspawn;
135 my $max = $self->{"publicinboxlimiter.$name.max"} || 1;
136 my $limiter = PublicInbox::Qspawn::Limiter->new($max);
137 $limiter->setup_rlimit($name, $self);
142 sub config_dir { $ENV{PI_DIR} || "$ENV{HOME}/.public-inbox" }
145 my $f = $ENV{PI_CONFIG};
146 return $f if defined $f;
147 config_dir() . '/config';
150 sub git_config_dump {
152 my (%section_seen, @section_order);
153 return {} unless -e $file;
154 my @cmd = (qw/git config/, "--file=$file", '-l');
155 my $cmd = join(' ', @cmd);
156 my $fh = popen_rd(\@cmd) or die "popen_rd failed for $file: $!\n";
159 while (defined(my $line = <$fh>)) {
161 my ($k, $v) = split(/=/, $line, 2);
163 my ($section) = ($k =~ /\A(\S+)\.[^\.]+\z/);
164 unless (defined $section_seen{$section}) {
165 $section_seen{$section} = 1;
166 push @section_order, $section;
171 if (ref($cur) eq "ARRAY") {
174 $rv{$k} = [ $cur, $v ];
180 close $fh or die "failed to close ($cmd) pipe: $?";
181 $rv{-section_order} = \@section_order;
186 sub valid_inbox_name ($) {
189 # Similar rules found in git.git/remote.c::valid_remote_nick
190 # and git.git/refs.c::check_refname_component
191 # We don't reject /\.lock\z/, however, since we don't lock refs
192 if ($name eq '' || $name =~ /\@\{/ ||
193 $name =~ /\.\./ || $name =~ m![/:\?\[\]\^~\s\f[:cntrl:]\*]! ||
194 $name =~ /\A\./ || $name =~ /\.\z/) {
198 # Note: we allow URL-unfriendly characters; users may configure
199 # non-HTTP-accessible inboxes
203 # XXX needs testing for cgit compatibility
204 # cf. cgit/scan-tree.c::add_repo
205 sub cgit_repo_merge ($$$) {
206 my ($self, $base, $repo) = @_;
207 my $path = $repo->{dir};
208 if (defined(my $se = $self->{-cgit_strict_export})) {
209 return unless -e "$path/$se";
211 return if -e "$path/noweb";
212 # $repo = { url => 'foo.git', dir => '/path/to/foo.git' }
213 my $rel = $repo->{url};
214 unless (defined $rel) {
215 my $off = index($path, $base, 0);
219 $rel = substr($path, length($base) + 1);
222 $rel =~ s!/\.git\z!! or
225 $self->{-cgit_remove_suffix} and
226 $rel =~ s!/?\.git\z!!;
228 $self->{"coderepo.$rel.dir"} ||= $path;
229 $self->{"coderepo.$rel.cgiturl"} ||= $rel;
234 -d "$git_dir/objects" && -f "$git_dir/HEAD";
237 # XXX needs testing for cgit compatibility
238 sub scan_path_coderepo {
239 my ($self, $base, $path) = @_;
240 opendir(my $dh, $path) or do {
241 warn "error opening directory: $path\n";
245 if (is_git_dir($git_dir) || is_git_dir($git_dir .= '/.git')) {
246 my $repo = { dir => $git_dir };
247 cgit_repo_merge($self, $base, $repo);
250 while (defined(my $dn = readdir $dh)) {
251 next if $dn eq '.' || $dn eq '..';
252 if (index($dn, '.') == 0 && !$self->{-cgit_scan_hidden_path}) {
255 my $dir = "$path/$dn";
256 scan_path_coderepo($self, $base, $dir) if -d $dir;
260 sub scan_tree_coderepo ($$) {
261 my ($self, $path) = @_;
262 scan_path_coderepo($self, $path, $path);
265 sub scan_projects_coderepo ($$$) {
266 my ($self, $list, $path) = @_;
267 open my $fh, '<', $list or do {
268 warn "failed to open cgit projectlist=$list: $!\n";
273 scan_path_coderepo($self, $path, "$path/$_");
278 my ($self, $cgitrc, $nesting) = @_;
281 my %s = map { $_ => 1 } qw(/cgit.css /cgit.png
282 /favicon.ico /robots.txt);
283 $self->{-cgit_static} = \%s;
286 # same limit as cgit/configfile.c::parse_configfile
287 return if $nesting > 8;
289 open my $fh, '<', $cgitrc or do {
290 warn "failed to open cgitrc=$cgitrc: $!\n";
294 # FIXME: this doesn't support macro expansion via $VARS, yet
298 if (m!\Arepo\.url=(.+?)/*\z!) {
300 cgit_repo_merge($self, $repo->{dir}, $repo) if $repo;
301 $repo = { url => $nick };
302 } elsif (m!\Arepo\.path=(.+)\z!) {
306 warn "$_ without repo.url\n";
308 } elsif (m!\Ainclude=(.+)\z!) {
309 parse_cgitrc($self, $1, $nesting + 1);
310 } elsif (m!\A(scan-hidden-path|remove-suffix)=([0-9]+)\z!) {
311 my ($k, $v) = ($1, $2);
313 $self->{"-cgit_$k"} = $v;
314 } elsif (m!\A(project-list|strict-export)=(.+)\z!) {
315 my ($k, $v) = ($1, $2);
317 $self->{"-cgit_$k"} = $v;
318 } elsif (m!\Ascan-path=(.+)\z!) {
319 if (defined(my $list = $self->{-cgit_project_list})) {
320 scan_projects_coderepo($self, $list, $1);
322 scan_tree_coderepo($self, $1);
324 } elsif (m!\A(?:css|favicon|logo|repo\.logo)=(/.+)\z!) {
325 # absolute paths for static files via PublicInbox::Cgit
326 $self->{-cgit_static}->{$1} = 1;
329 cgit_repo_merge($self, $repo->{dir}, $repo) if $repo;
333 # Only git is supported at the moment, but SVN and Hg are possibilities
334 sub _fill_code_repo {
335 my ($self, $nick) = @_;
336 my $pfx = "coderepo.$nick";
338 # TODO: support gitweb and other repository viewers?
339 if (defined(my $cgitrc = delete $self->{-cgitrc_unparsed})) {
340 parse_cgitrc($self, $cgitrc, 0);
342 my $dir = $self->{"$pfx.dir"}; # aka "GIT_DIR"
343 unless (defined $dir) {
344 warn "$pfx.dir unset\n";
348 my $git = PublicInbox::Git->new($dir);
349 foreach my $t (qw(blob commit tree tag)) {
350 $git->{$t.'_url_format'} =
351 _array($self->{lc("$pfx.${t}UrlFormat")});
354 if (my $cgits = $self->{lc("$pfx.cgitUrl")}) {
355 $git->{cgit_url} = $cgits = _array($cgits);
357 # cgit supports "/blob/?id=%s", but it's only a plain-text
358 # display and requires an unabbreviated id=
359 foreach my $t (qw(blob commit tag)) {
360 $git->{$t.'_url_format'} ||= map {
369 sub _git_config_bool ($) {
371 if ($val =~ /\A(?:false|no|off|[\-\+]?(?:0x)?0+)\z/i) {
373 } elsif ($val =~ /\A(?:true|yes|on|[\-\+]?(?:0x)?[0-9]+)\z/i) {
381 my ($self, $pfx) = @_;
384 foreach my $k (qw(mainrepo filter url newsgroup
385 infourl watch watchheader httpbackendmax
386 replyto feedmax nntpserver indexlevel)) {
387 my $v = $self->{"$pfx.$k"};
388 $ibx->{$k} = $v if defined $v;
390 foreach my $k (qw(obfuscate)) {
391 my $v = $self->{"$pfx.$k"};
393 if (defined(my $bval = _git_config_bool($v))) {
396 warn "Ignoring $pfx.$k=$v in config, not boolean\n";
399 # TODO: more arrays, we should support multi-value for
400 # more things to encourage decentralization
401 foreach my $k (qw(address altid nntpmirror coderepo hide)) {
402 if (defined(my $v = $self->{"$pfx.$k"})) {
403 $ibx->{$k} = _array($v);
407 return unless $ibx->{mainrepo};
409 $name =~ s/\Apublicinbox\.//;
411 if (!valid_inbox_name($name)) {
412 warn "invalid inbox name: '$name'\n";
416 $ibx->{name} = $name;
417 $ibx->{-pi_config} = $self;
418 $ibx = PublicInbox::Inbox->new($ibx);
419 foreach (@{$ibx->{address}}) {
420 my $lc_addr = lc($_);
421 $self->{-by_addr}->{$lc_addr} = $ibx;
422 $self->{-no_obfuscate}->{$lc_addr} = 1;
424 if (my $ng = $ibx->{newsgroup}) {
425 $self->{-by_newsgroup}->{$ng} = $ibx;
427 $self->{-by_name}->{$name} = $ibx;
428 if ($ibx->{obfuscate}) {
429 $ibx->{-no_obfuscate} = $self->{-no_obfuscate};
430 $ibx->{-no_obfuscate_re} = $self->{-no_obfuscate_re};
431 each_inbox($self, sub {}); # noop to populate -no_obfuscate
434 if (my $ibx_code_repos = $ibx->{coderepo}) {
435 my $code_repos = $self->{-code_repos};
436 my $repo_objs = $ibx->{-repo_objs} = [];
437 foreach my $nick (@$ibx_code_repos) {
438 my @parts = split(m!/!, $nick);
440 $valid += valid_inbox_name($_) foreach (@parts);
441 $valid == scalar(@parts) or next;
443 my $repo = $code_repos->{$nick} ||=
444 _fill_code_repo($self, $nick);
445 push @$repo_objs, $repo if $repo;