1 # Copyright (C) 2014-2018 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
34 if (my $no = delete $self->{'publicinbox.noobfuscate'}) {
35 $no = [ $no ] if ref($no) ne 'ARRAY';
37 foreach my $n (@$no) {
38 my @n = split(/\s+/, $n);
40 if (/\S+@\S+/) { # full address
41 $self->{-no_obfuscate}->{lc $_} = 1;
43 # allow "example.com" or "@example.com"
45 push @domains, quotemeta($_);
49 my $nod = join('|', @domains);
50 $self->{-no_obfuscate_re} = qr/(?:$nod)\z/i;
57 my ($self, $recipient) = @_;
58 my $addr = lc($recipient);
59 my $inbox = $self->{-by_addr}->{$addr};
60 return $inbox if $inbox;
64 foreach my $k (keys %$self) {
65 $k =~ m!\A(publicinbox\.[^/]+)\.address\z! or next;
67 if (ref($v) eq "ARRAY") {
68 foreach my $alias (@$v) {
69 (lc($alias) eq $addr) or next;
74 (lc($v) eq $addr) or next;
79 defined $pfx or return;
83 sub lookup_name ($$) {
84 my ($self, $name) = @_;
85 $self->{-by_name}->{$name} || _fill($self, "publicinbox.$name");
91 foreach my $k (keys %$self) {
92 $k =~ m!\Apublicinbox\.([^/]+)\.mainrepo\z! or next;
95 my $ibx = lookup_name($self, $1) or next;
100 sub lookup_newsgroup {
101 my ($self, $ng) = @_;
103 my $rv = $self->{-by_newsgroup}->{$ng};
106 foreach my $k (keys %$self) {
107 $k =~ m!\A(publicinbox\.[^/]+)\.newsgroup\z! or next;
111 $rv = _fill($self, $pfx);
119 my ($self, $name) = @_;
120 $self->{-limiters}->{$name} ||= do {
121 require PublicInbox::Qspawn;
122 my $max = $self->{"publicinboxlimiter.$name.max"};
123 PublicInbox::Qspawn::Limiter->new($max);
127 sub config_dir { $ENV{PI_DIR} || "$ENV{HOME}/.public-inbox" }
130 my $f = $ENV{PI_CONFIG};
131 return $f if defined $f;
132 config_dir() . '/config';
135 sub git_config_dump {
138 my @cmd = (qw/git config/, "--file=$file", '-l');
139 my $cmd = join(' ', @cmd);
140 my $fh = popen_rd(\@cmd) or die "popen_rd failed for $file: $!\n";
143 while (defined(my $line = <$fh>)) {
145 my ($k, $v) = split(/=/, $line, 2);
149 if (ref($cur) eq "ARRAY") {
152 $rv{$k} = [ $cur, $v ];
158 close $fh or die "failed to close ($cmd) pipe: $?";
163 sub valid_inbox_name ($) {
166 # Similar rules found in git.git/remote.c::valid_remote_nick
167 # and git.git/refs.c::check_refname_component
168 # We don't reject /\.lock\z/, however, since we don't lock refs
169 if ($name eq '' || $name =~ /\@\{/ ||
170 $name =~ /\.\./ || $name =~ m![/:\?\[\]\^~\s\f[:cntrl:]\*]! ||
171 $name =~ /\A\./ || $name =~ /\.\z/) {
175 # Note: we allow URL-unfriendly characters; users may configure
176 # non-HTTP-accessible inboxes
181 # Only git is supported at the moment, but SVN and Hg are possibilities
182 sub _fill_code_repo {
183 my ($self, $nick) = @_;
184 my $pfx = "coderepo.$nick";
186 my $dir = $self->{"$pfx.dir"}; # aka "GIT_DIR"
187 unless (defined $dir) {
188 warn "$pfx.repodir unset";
192 my $git = PublicInbox::Git->new($dir);
193 foreach my $t (qw(blob commit tree tag)) {
194 $git->{$t.'_url_format'} =
195 _array($self->{lc("$pfx.${t}UrlFormat")});
198 if (my $cgits = $self->{lc("$pfx.cgitUrl")}) {
199 $git->{cgit_url} = $cgits = _array($cgits);
201 # cgit supports "/blob/?id=%s", but it's only a plain-text
202 # display and requires an unabbreviated id=
203 foreach my $t (qw(blob commit tag)) {
204 $git->{$t.'_url_format'} ||= map {
209 # TODO: support gitweb and other repository viewers?
216 my ($self, $pfx) = @_;
219 foreach my $k (qw(mainrepo filter url newsgroup
220 infourl watch watchheader httpbackendmax
221 replyto feedmax nntpserver indexlevel)) {
222 my $v = $self->{"$pfx.$k"};
223 $rv->{$k} = $v if defined $v;
225 foreach my $k (qw(obfuscate)) {
226 my $v = $self->{"$pfx.$k"};
228 if ($v =~ /\A(?:false|no|off|0)\z/) {
230 } elsif ($v =~ /\A(?:true|yes|on|1)\z/) {
233 warn "Ignoring $pfx.$k=$v in config, not boolean\n";
236 # TODO: more arrays, we should support multi-value for
237 # more things to encourage decentralization
238 foreach my $k (qw(address altid nntpmirror coderepo)) {
239 if (defined(my $v = $self->{"$pfx.$k"})) {
240 $rv->{$k} = _array($v);
244 return unless $rv->{mainrepo};
246 $name =~ s/\Apublicinbox\.//;
248 if (!valid_inbox_name($name)) {
249 warn "invalid inbox name: '$name'\n";
254 $rv->{-pi_config} = $self;
255 $rv = PublicInbox::Inbox->new($rv);
256 foreach (@{$rv->{address}}) {
257 my $lc_addr = lc($_);
258 $self->{-by_addr}->{$lc_addr} = $rv;
259 $self->{-no_obfuscate}->{$lc_addr} = 1;
261 if (my $ng = $rv->{newsgroup}) {
262 $self->{-by_newsgroup}->{$ng} = $rv;
264 $self->{-by_name}->{$name} = $rv;
265 if ($rv->{obfuscate}) {
266 $rv->{-no_obfuscate} = $self->{-no_obfuscate};
267 $rv->{-no_obfuscate_re} = $self->{-no_obfuscate_re};
268 each_inbox($self, sub {}); # noop to populate -no_obfuscate
271 if (my $ibx_code_repos = $rv->{coderepo}) {
272 my $code_repos = $self->{-code_repos};
273 my $repo_objs = $rv->{-repo_objs} = [];
274 foreach my $nick (@$ibx_code_repos) {
275 valid_inbox_name($nick) or next;
276 my $repo = $code_repos->{$nick} ||=
277 _fill_code_repo($self, $nick);
278 push @$repo_objs, $repo if $repo;