]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Config.pm
config: support cgit scan-path and scan-hidden-path
[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"} || 1;
136                 my $limiter = PublicInbox::Qspawn::Limiter->new($max);
137                 $limiter->setup_rlimit($name, $self);
138                 $limiter;
139         };
140 }
141
142 sub config_dir { $ENV{PI_DIR} || "$ENV{HOME}/.public-inbox" }
143
144 sub default_file {
145         my $f = $ENV{PI_CONFIG};
146         return $f if defined $f;
147         config_dir() . '/config';
148 }
149
150 sub git_config_dump {
151         my ($file) = @_;
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";
157         my %rv;
158         local $/ = "\n";
159         while (defined(my $line = <$fh>)) {
160                 chomp $line;
161                 my ($k, $v) = split(/=/, $line, 2);
162
163                 my ($section) = ($k =~ /\A(\S+)\.[^\.]+\z/);
164                 unless (defined $section_seen{$section}) {
165                         $section_seen{$section} = 1;
166                         push @section_order, $section;
167                 }
168
169                 my $cur = $rv{$k};
170                 if (defined $cur) {
171                         if (ref($cur) eq "ARRAY") {
172                                 push @$cur, $v;
173                         } else {
174                                 $rv{$k} = [ $cur, $v ];
175                         }
176                 } else {
177                         $rv{$k} = $v;
178                 }
179         }
180         close $fh or die "failed to close ($cmd) pipe: $?";
181         $rv{-section_order} = \@section_order;
182
183         \%rv;
184 }
185
186 sub valid_inbox_name ($) {
187         my ($name) = @_;
188
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/) {
195                 return 0;
196         }
197
198         # Note: we allow URL-unfriendly characters; users may configure
199         # non-HTTP-accessible inboxes
200         1;
201 }
202
203 sub cgit_repo_merge ($$) {
204         my ($self, $repo) = @_;
205         # $repo = { url => 'foo.git', path => '/path/to/foo.git' }
206         my $nick = $repo->{url};
207         $self->{"coderepo.$nick.dir"} ||= $repo->{path};
208         $self->{"coderepo.$nick.cgiturl"} ||= $nick;
209 }
210
211 sub is_git_dir ($) {
212         my ($git_dir) = @_;
213         -d "$git_dir/objects" && -f "$git_dir/HEAD";
214 }
215
216 sub scan_path_coderepo {
217         my ($self, $base, $path) = @_;
218         opendir my $dh, $path or return;
219         while (defined(my $dn = readdir $dh)) {
220                 next if $dn eq '.' || $dn eq '..';
221                 if (index($dn, '.') == 0 && !$self->{-cgit_scan_hidden_path}) {
222                         next;
223                 }
224                 my $nick = $base eq '' ? $dn : "$base/$dn";
225                 my $git_dir = "$path/$dn";
226                 if (is_git_dir($git_dir)) {
227                         my $repo = { url => $nick, path => $git_dir };
228                         cgit_repo_merge($self, $repo);
229                 } elsif (-d $git_dir) {
230                         scan_path_coderepo($self, $nick, $git_dir);
231                 }
232         }
233 }
234
235 sub parse_cgitrc {
236         my ($self, $cgitrc, $nesting) = @_;
237
238         # same limit as cgit/configfile.c::parse_configfile
239         return if $nesting > 8;
240
241         open my $fh, '<', $cgitrc or do {
242                 warn "failed to open cgitrc=$cgitrc: $!\n";
243                 return;
244         };
245
246         # FIXME: this doesn't support macro expansion via $VARS, yet
247         my $repo;
248         foreach (<$fh>) {
249                 chomp;
250                 if (m!\Arepo\.url=(.+?)/*\z!) {
251                         my $nick = $1;
252                         cgit_repo_merge($self, $repo) if $repo;
253                         $repo = { url => $nick };
254                 } elsif (m!\Arepo\.path=(.+)\z!) {
255                         if (defined $repo) {
256                                 $repo->{path} = $1;
257                         } else {
258                                 warn "$_ without repo.url\n";
259                         }
260                 } elsif (m!\Ainclude=(.+)\z!) {
261                         parse_cgitrc($self, $1, $nesting + 1);
262                 } elsif (m!\Ascan-hidden-path=(\d+)\z!) {
263                         $self->{-cgit_scan_hidden_path} = $1;
264                 } elsif (m!\Ascan-path=(.+)\z!) {
265                         scan_path_coderepo($self, '', $1);
266                 }
267         }
268         cgit_repo_merge($self, $repo) if $repo;
269 }
270
271 # parse a code repo
272 # Only git is supported at the moment, but SVN and Hg are possibilities
273 sub _fill_code_repo {
274         my ($self, $nick) = @_;
275         my $pfx = "coderepo.$nick";
276
277         # TODO: support gitweb and other repository viewers?
278         if (defined(my $cgitrc = delete $self->{-cgitrc_unparsed})) {
279                 parse_cgitrc($self, $cgitrc, 0);
280         }
281         my $dir = $self->{"$pfx.dir"}; # aka "GIT_DIR"
282         unless (defined $dir) {
283                 warn "$pfx.dir unset";
284                 return;
285         }
286
287         my $git = PublicInbox::Git->new($dir);
288         foreach my $t (qw(blob commit tree tag)) {
289                 $git->{$t.'_url_format'} =
290                                 _array($self->{lc("$pfx.${t}UrlFormat")});
291         }
292
293         if (my $cgits = $self->{lc("$pfx.cgitUrl")}) {
294                 $git->{cgit_url} = $cgits = _array($cgits);
295
296                 # cgit supports "/blob/?id=%s", but it's only a plain-text
297                 # display and requires an unabbreviated id=
298                 foreach my $t (qw(blob commit tag)) {
299                         $git->{$t.'_url_format'} ||= map {
300                                 "$_/$t/?id=%s"
301                         } @$cgits;
302                 }
303         }
304
305         $git;
306 }
307
308 sub _fill {
309         my ($self, $pfx) = @_;
310         my $rv = {};
311
312         foreach my $k (qw(mainrepo filter url newsgroup
313                         infourl watch watchheader httpbackendmax
314                         replyto feedmax nntpserver indexlevel)) {
315                 my $v = $self->{"$pfx.$k"};
316                 $rv->{$k} = $v if defined $v;
317         }
318         foreach my $k (qw(obfuscate)) {
319                 my $v = $self->{"$pfx.$k"};
320                 defined $v or next;
321                 if ($v =~ /\A(?:false|no|off|0)\z/) {
322                         $rv->{$k} = 0;
323                 } elsif ($v =~ /\A(?:true|yes|on|1)\z/) {
324                         $rv->{$k} = 1;
325                 } else {
326                         warn "Ignoring $pfx.$k=$v in config, not boolean\n";
327                 }
328         }
329         # TODO: more arrays, we should support multi-value for
330         # more things to encourage decentralization
331         foreach my $k (qw(address altid nntpmirror coderepo)) {
332                 if (defined(my $v = $self->{"$pfx.$k"})) {
333                         $rv->{$k} = _array($v);
334                 }
335         }
336
337         return unless $rv->{mainrepo};
338         my $name = $pfx;
339         $name =~ s/\Apublicinbox\.//;
340
341         if (!valid_inbox_name($name)) {
342                 warn "invalid inbox name: '$name'\n";
343                 return;
344         }
345
346         $rv->{name} = $name;
347         $rv->{-pi_config} = $self;
348         $rv = PublicInbox::Inbox->new($rv);
349         foreach (@{$rv->{address}}) {
350                 my $lc_addr = lc($_);
351                 $self->{-by_addr}->{$lc_addr} = $rv;
352                 $self->{-no_obfuscate}->{$lc_addr} = 1;
353         }
354         if (my $ng = $rv->{newsgroup}) {
355                 $self->{-by_newsgroup}->{$ng} = $rv;
356         }
357         $self->{-by_name}->{$name} = $rv;
358         if ($rv->{obfuscate}) {
359                 $rv->{-no_obfuscate} = $self->{-no_obfuscate};
360                 $rv->{-no_obfuscate_re} = $self->{-no_obfuscate_re};
361                 each_inbox($self, sub {}); # noop to populate -no_obfuscate
362         }
363
364         if (my $ibx_code_repos = $rv->{coderepo}) {
365                 my $code_repos = $self->{-code_repos};
366                 my $repo_objs = $rv->{-repo_objs} = [];
367                 foreach my $nick (@$ibx_code_repos) {
368                         my @parts = split(m!/!, $nick);
369                         my $valid = 0;
370                         $valid += valid_inbox_name($_) foreach (@parts);
371                         $valid == scalar(@parts) or next;
372
373                         my $repo = $code_repos->{$nick} ||=
374                                                 _fill_code_repo($self, $nick);
375                         push @$repo_objs, $repo if $repo;
376                 }
377         }
378
379         $rv
380 }
381
382 1;