+ config_dir() . '/config';
+}
+
+sub git_config_dump {
+ my ($file) = @_;
+ my (%section_seen, @section_order);
+ return {} unless -e $file;
+ my @cmd = (qw/git config/, "--file=$file", '-l');
+ my $cmd = join(' ', @cmd);
+ my $fh = popen_rd(\@cmd) or die "popen_rd failed for $file: $!\n";
+ my %rv;
+ local $/ = "\n";
+ while (defined(my $line = <$fh>)) {
+ chomp $line;
+ my ($k, $v) = split(/=/, $line, 2);
+
+ my ($section) = ($k =~ /\A(\S+)\.[^\.]+\z/);
+ unless (defined $section_seen{$section}) {
+ $section_seen{$section} = 1;
+ push @section_order, $section;
+ }
+
+ my $cur = $rv{$k};
+ if (defined $cur) {
+ if (ref($cur) eq "ARRAY") {
+ push @$cur, $v;
+ } else {
+ $rv{$k} = [ $cur, $v ];
+ }
+ } else {
+ $rv{$k} = $v;
+ }
+ }
+ close $fh or die "failed to close ($cmd) pipe: $?";
+ $rv{-section_order} = \@section_order;
+
+ \%rv;
+}
+
+sub valid_inbox_name ($) {
+ my ($name) = @_;
+
+ # Similar rules found in git.git/remote.c::valid_remote_nick
+ # and git.git/refs.c::check_refname_component
+ # We don't reject /\.lock\z/, however, since we don't lock refs
+ if ($name eq '' || $name =~ /\@\{/ ||
+ $name =~ /\.\./ || $name =~ m![/:\?\[\]\^~\s\f[:cntrl:]\*]! ||
+ $name =~ /\A\./ || $name =~ /\.\z/) {
+ return 0;
+ }
+
+ # Note: we allow URL-unfriendly characters; users may configure
+ # non-HTTP-accessible inboxes
+ 1;
+}
+
+# parse a code repo
+# Only git is supported at the moment, but SVN and Hg are possibilities
+sub _fill_code_repo {
+ my ($self, $nick) = @_;
+ my $pfx = "coderepo.$nick";
+
+ my $dir = $self->{"$pfx.dir"}; # aka "GIT_DIR"
+ unless (defined $dir) {
+ warn "$pfx.repodir unset";
+ return;
+ }
+
+ my $git = PublicInbox::Git->new($dir);
+ foreach my $t (qw(blob commit tree tag)) {
+ $git->{$t.'_url_format'} =
+ _array($self->{lc("$pfx.${t}UrlFormat")});
+ }
+
+ if (my $cgits = $self->{lc("$pfx.cgitUrl")}) {
+ $git->{cgit_url} = $cgits = _array($cgits);
+
+ # cgit supports "/blob/?id=%s", but it's only a plain-text
+ # display and requires an unabbreviated id=
+ foreach my $t (qw(blob commit tag)) {
+ $git->{$t.'_url_format'} ||= map {
+ "$_/$t/?id=%s"
+ } @$cgits;
+ }
+ }
+ # TODO: support gitweb and other repository viewers?
+ # TODO: parse cgitrc
+
+ $git;
+}
+
+sub _fill {
+ my ($self, $pfx) = @_;
+ my $rv = {};
+
+ foreach my $k (qw(mainrepo filter url newsgroup
+ infourl watch watchheader httpbackendmax
+ replyto feedmax nntpserver indexlevel)) {
+ my $v = $self->{"$pfx.$k"};
+ $rv->{$k} = $v if defined $v;
+ }
+ foreach my $k (qw(obfuscate)) {
+ my $v = $self->{"$pfx.$k"};
+ defined $v or next;
+ if ($v =~ /\A(?:false|no|off|0)\z/) {
+ $rv->{$k} = 0;
+ } elsif ($v =~ /\A(?:true|yes|on|1)\z/) {
+ $rv->{$k} = 1;
+ } else {
+ warn "Ignoring $pfx.$k=$v in config, not boolean\n";
+ }
+ }
+ # TODO: more arrays, we should support multi-value for
+ # more things to encourage decentralization
+ foreach my $k (qw(address altid nntpmirror coderepo)) {
+ if (defined(my $v = $self->{"$pfx.$k"})) {
+ $rv->{$k} = _array($v);
+ }
+ }
+
+ return unless $rv->{mainrepo};
+ my $name = $pfx;
+ $name =~ s/\Apublicinbox\.//;
+
+ if (!valid_inbox_name($name)) {
+ warn "invalid inbox name: '$name'\n";
+ return;
+ }
+
+ $rv->{name} = $name;
+ $rv->{-pi_config} = $self;
+ $rv = PublicInbox::Inbox->new($rv);
+ foreach (@{$rv->{address}}) {
+ my $lc_addr = lc($_);
+ $self->{-by_addr}->{$lc_addr} = $rv;
+ $self->{-no_obfuscate}->{$lc_addr} = 1;
+ }
+ if (my $ng = $rv->{newsgroup}) {
+ $self->{-by_newsgroup}->{$ng} = $rv;
+ }
+ $self->{-by_name}->{$name} = $rv;
+ if ($rv->{obfuscate}) {
+ $rv->{-no_obfuscate} = $self->{-no_obfuscate};
+ $rv->{-no_obfuscate_re} = $self->{-no_obfuscate_re};
+ each_inbox($self, sub {}); # noop to populate -no_obfuscate
+ }
+
+ if (my $ibx_code_repos = $rv->{coderepo}) {
+ my $code_repos = $self->{-code_repos};
+ my $repo_objs = $rv->{-repo_objs} = [];
+ foreach my $nick (@$ibx_code_repos) {
+ valid_inbox_name($nick) or next;
+ my $repo = $code_repos->{$nick} ||=
+ _fill_code_repo($self, $nick);
+ push @$repo_objs, $repo if $repo;
+ }
+ }
+
+ $rv