]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Config.pm
www: wire up cgit as a 404 handler if cgitrc is configured
[public-inbox.git] / lib / PublicInbox / Config.pm
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>
3 #
4 # Used throughout the project for reading configuration
5 #
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
9
10 package PublicInbox::Config;
11 use strict;
12 use warnings;
13 require PublicInbox::Inbox;
14 use PublicInbox::Spawn qw(popen_rd);
15
16 sub _array ($) { ref($_[0]) eq 'ARRAY' ? $_[0] : [ $_[0] ] }
17
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
20 sub new {
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;
25
26         # caches
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'};
34
35         if (my $no = delete $self->{'publicinbox.noobfuscate'}) {
36                 $no = _array($no);
37                 my @domains;
38                 foreach my $n (@$no) {
39                         my @n = split(/\s+/, $n);
40                         foreach (@n) {
41                                 if (/\S+@\S+/) { # full address
42                                         $self->{-no_obfuscate}->{lc $_} = 1;
43                                 } else {
44                                         # allow "example.com" or "@example.com"
45                                         s/\A@//;
46                                         push @domains, quotemeta($_);
47                                 }
48                         }
49                 }
50                 my $nod = join('|', @domains);
51                 $self->{-no_obfuscate_re} = qr/(?:$nod)\z/i;
52         }
53         if (my $css = delete $self->{'publicinbox.css'}) {
54                 $self->{css} = _array($css);
55         }
56
57         $self;
58 }
59
60 sub lookup {
61         my ($self, $recipient) = @_;
62         my $addr = lc($recipient);
63         my $inbox = $self->{-by_addr}->{$addr};
64         return $inbox if $inbox;
65
66         my $pfx;
67
68         foreach my $k (keys %$self) {
69                 $k =~ m!\A(publicinbox\.[^/]+)\.address\z! or next;
70                 my $v = $self->{$k};
71                 if (ref($v) eq "ARRAY") {
72                         foreach my $alias (@$v) {
73                                 (lc($alias) eq $addr) or next;
74                                 $pfx = $1;
75                                 last;
76                         }
77                 } else {
78                         (lc($v) eq $addr) or next;
79                         $pfx = $1;
80                         last;
81                 }
82         }
83         defined $pfx or return;
84         _fill($self, $pfx);
85 }
86
87 sub lookup_name ($$) {
88         my ($self, $name) = @_;
89         $self->{-by_name}->{$name} || _fill($self, "publicinbox.$name");
90 }
91
92 sub each_inbox {
93         my ($self, $cb) = @_;
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;
99                         $cb->($ibx);
100                 }
101         } else {
102                 my %seen;
103                 foreach my $k (keys %$self) {
104                         $k =~ m!\Apublicinbox\.([^/]+)\.mainrepo\z! or next;
105                         next if $seen{$1};
106                         $seen{$1} = 1;
107                         my $ibx = lookup_name($self, $1) or next;
108                         $cb->($ibx);
109                 }
110         }
111 }
112
113 sub lookup_newsgroup {
114         my ($self, $ng) = @_;
115         $ng = lc($ng);
116         my $rv = $self->{-by_newsgroup}->{$ng};
117         return $rv if $rv;
118
119         foreach my $k (keys %$self) {
120                 $k =~ m!\A(publicinbox\.[^/]+)\.newsgroup\z! or next;
121                 my $v = $self->{$k};
122                 my $pfx = $1;
123                 if ($v eq $ng) {
124                         $rv = _fill($self, $pfx);
125                         return $rv;
126                 }
127         }
128         undef;
129 }
130
131 sub limiter {
132         my ($self, $name) = @_;
133         $self->{-limiters}->{$name} ||= do {
134                 require PublicInbox::Qspawn;
135                 my $max = $self->{"publicinboxlimiter.$name.max"};
136                 PublicInbox::Qspawn::Limiter->new($max);
137         };
138 }
139
140 sub config_dir { $ENV{PI_DIR} || "$ENV{HOME}/.public-inbox" }
141
142 sub default_file {
143         my $f = $ENV{PI_CONFIG};
144         return $f if defined $f;
145         config_dir() . '/config';
146 }
147
148 sub git_config_dump {
149         my ($file) = @_;
150         my (%section_seen, @section_order);
151         return {} unless -e $file;
152         my @cmd = (qw/git config/, "--file=$file", '-l');
153         my $cmd = join(' ', @cmd);
154         my $fh = popen_rd(\@cmd) or die "popen_rd failed for $file: $!\n";
155         my %rv;
156         local $/ = "\n";
157         while (defined(my $line = <$fh>)) {
158                 chomp $line;
159                 my ($k, $v) = split(/=/, $line, 2);
160
161                 my ($section) = ($k =~ /\A(\S+)\.[^\.]+\z/);
162                 unless (defined $section_seen{$section}) {
163                         $section_seen{$section} = 1;
164                         push @section_order, $section;
165                 }
166
167                 my $cur = $rv{$k};
168                 if (defined $cur) {
169                         if (ref($cur) eq "ARRAY") {
170                                 push @$cur, $v;
171                         } else {
172                                 $rv{$k} = [ $cur, $v ];
173                         }
174                 } else {
175                         $rv{$k} = $v;
176                 }
177         }
178         close $fh or die "failed to close ($cmd) pipe: $?";
179         $rv{-section_order} = \@section_order;
180
181         \%rv;
182 }
183
184 sub valid_inbox_name ($) {
185         my ($name) = @_;
186
187         # Similar rules found in git.git/remote.c::valid_remote_nick
188         # and git.git/refs.c::check_refname_component
189         # We don't reject /\.lock\z/, however, since we don't lock refs
190         if ($name eq '' || $name =~ /\@\{/ ||
191             $name =~ /\.\./ || $name =~ m![/:\?\[\]\^~\s\f[:cntrl:]\*]! ||
192             $name =~ /\A\./ || $name =~ /\.\z/) {
193                 return 0;
194         }
195
196         # Note: we allow URL-unfriendly characters; users may configure
197         # non-HTTP-accessible inboxes
198         1;
199 }
200
201 sub cgit_repo_merge ($$) {
202         my ($self, $repo) = @_;
203         # $repo = { url => 'foo.git', path => '/path/to/foo.git' }
204         my $nick = $repo->{url};
205         $self->{"coderepo.$nick.dir"} ||= $repo->{path};
206         $self->{"coderepo.$nick.cgiturl"} ||= $nick;
207 }
208
209 sub parse_cgitrc {
210         my ($self, $cgitrc, $nesting) = @_;
211
212         # same limit as cgit/configfile.c::parse_configfile
213         return if $nesting > 8;
214
215         open my $fh, '<', $cgitrc or do {
216                 warn "failed to open cgitrc=$cgitrc: $!\n";
217                 return;
218         };
219
220         # FIXME: this doesn't support macro expansion via $VARS, yet
221         my $repo;
222         foreach (<$fh>) {
223                 chomp;
224                 if (m!\Arepo\.url=(.+?)/*\z!) {
225                         my $nick = $1;
226                         cgit_repo_merge($self, $repo) if $repo;
227                         $repo = { url => $nick };
228                 } elsif (m!\Arepo\.path=(.+)\z!) {
229                         if (defined $repo) {
230                                 $repo->{path} = $1;
231                         } else {
232                                 warn "$_ without repo.url\n";
233                         }
234                 } elsif (m!\Ainclude=(.+)\z!) {
235                         parse_cgitrc($self, $1, $nesting + 1);
236                 }
237         }
238         cgit_repo_merge($self, $repo) if $repo;
239 }
240
241 # parse a code repo
242 # Only git is supported at the moment, but SVN and Hg are possibilities
243 sub _fill_code_repo {
244         my ($self, $nick) = @_;
245         my $pfx = "coderepo.$nick";
246
247         # TODO: support gitweb and other repository viewers?
248         if (defined(my $cgitrc = delete $self->{-cgitrc_unparsed})) {
249                 parse_cgitrc($self, $cgitrc, 0);
250         }
251         my $dir = $self->{"$pfx.dir"}; # aka "GIT_DIR"
252         unless (defined $dir) {
253                 warn "$pfx.dir unset";
254                 return;
255         }
256
257         my $git = PublicInbox::Git->new($dir);
258         foreach my $t (qw(blob commit tree tag)) {
259                 $git->{$t.'_url_format'} =
260                                 _array($self->{lc("$pfx.${t}UrlFormat")});
261         }
262
263         if (my $cgits = $self->{lc("$pfx.cgitUrl")}) {
264                 $git->{cgit_url} = $cgits = _array($cgits);
265
266                 # cgit supports "/blob/?id=%s", but it's only a plain-text
267                 # display and requires an unabbreviated id=
268                 foreach my $t (qw(blob commit tag)) {
269                         $git->{$t.'_url_format'} ||= map {
270                                 "$_/$t/?id=%s"
271                         } @$cgits;
272                 }
273         }
274
275         $git;
276 }
277
278 sub _fill {
279         my ($self, $pfx) = @_;
280         my $rv = {};
281
282         foreach my $k (qw(mainrepo filter url newsgroup
283                         infourl watch watchheader httpbackendmax
284                         replyto feedmax nntpserver indexlevel)) {
285                 my $v = $self->{"$pfx.$k"};
286                 $rv->{$k} = $v if defined $v;
287         }
288         foreach my $k (qw(obfuscate)) {
289                 my $v = $self->{"$pfx.$k"};
290                 defined $v or next;
291                 if ($v =~ /\A(?:false|no|off|0)\z/) {
292                         $rv->{$k} = 0;
293                 } elsif ($v =~ /\A(?:true|yes|on|1)\z/) {
294                         $rv->{$k} = 1;
295                 } else {
296                         warn "Ignoring $pfx.$k=$v in config, not boolean\n";
297                 }
298         }
299         # TODO: more arrays, we should support multi-value for
300         # more things to encourage decentralization
301         foreach my $k (qw(address altid nntpmirror coderepo)) {
302                 if (defined(my $v = $self->{"$pfx.$k"})) {
303                         $rv->{$k} = _array($v);
304                 }
305         }
306
307         return unless $rv->{mainrepo};
308         my $name = $pfx;
309         $name =~ s/\Apublicinbox\.//;
310
311         if (!valid_inbox_name($name)) {
312                 warn "invalid inbox name: '$name'\n";
313                 return;
314         }
315
316         $rv->{name} = $name;
317         $rv->{-pi_config} = $self;
318         $rv = PublicInbox::Inbox->new($rv);
319         foreach (@{$rv->{address}}) {
320                 my $lc_addr = lc($_);
321                 $self->{-by_addr}->{$lc_addr} = $rv;
322                 $self->{-no_obfuscate}->{$lc_addr} = 1;
323         }
324         if (my $ng = $rv->{newsgroup}) {
325                 $self->{-by_newsgroup}->{$ng} = $rv;
326         }
327         $self->{-by_name}->{$name} = $rv;
328         if ($rv->{obfuscate}) {
329                 $rv->{-no_obfuscate} = $self->{-no_obfuscate};
330                 $rv->{-no_obfuscate_re} = $self->{-no_obfuscate_re};
331                 each_inbox($self, sub {}); # noop to populate -no_obfuscate
332         }
333
334         if (my $ibx_code_repos = $rv->{coderepo}) {
335                 my $code_repos = $self->{-code_repos};
336                 my $repo_objs = $rv->{-repo_objs} = [];
337                 foreach my $nick (@$ibx_code_repos) {
338                         my @parts = split(m!/!, $nick);
339                         my $valid = 0;
340                         $valid += valid_inbox_name($_) foreach (@parts);
341                         $valid == scalar(@parts) or next;
342
343                         my $repo = $code_repos->{$nick} ||=
344                                                 _fill_code_repo($self, $nick);
345                         push @$repo_objs, $repo if $repo;
346                 }
347         }
348
349         $rv
350 }
351
352 1;