]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Config.pm
config: remove redundant inboxdir check
[public-inbox.git] / lib / PublicInbox / Config.pm
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>
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         my $self;
24         if (ref($file) eq 'SCALAR') { # used by some tests
25                 open my $fh, '<', $file or die;  # PerlIO::scalar
26                 $self = config_fh_parse($fh, "\n", '=');
27         } else {
28                 $self = git_config_dump($file);
29         }
30         bless $self, $class;
31         # caches
32         $self->{-by_addr} = {};
33         $self->{-by_list_id} = {};
34         $self->{-by_name} = {};
35         $self->{-by_newsgroup} = {};
36         $self->{-no_obfuscate} = {};
37         $self->{-limiters} = {};
38         $self->{-code_repos} = {}; # nick => PublicInbox::Git object
39         $self->{-cgitrc_unparsed} = $self->{'publicinbox.cgitrc'};
40
41         if (my $no = delete $self->{'publicinbox.noobfuscate'}) {
42                 $no = _array($no);
43                 my @domains;
44                 foreach my $n (@$no) {
45                         my @n = split(/\s+/, $n);
46                         foreach (@n) {
47                                 if (/\S+@\S+/) { # full address
48                                         $self->{-no_obfuscate}->{lc $_} = 1;
49                                 } else {
50                                         # allow "example.com" or "@example.com"
51                                         s/\A@//;
52                                         push @domains, quotemeta($_);
53                                 }
54                         }
55                 }
56                 my $nod = join('|', @domains);
57                 $self->{-no_obfuscate_re} = qr/(?:$nod)\z/i;
58         }
59         if (my $css = delete $self->{'publicinbox.css'}) {
60                 $self->{css} = _array($css);
61         }
62
63         $self;
64 }
65
66 sub _fill_all ($) { each_inbox($_[0], sub {}) }
67
68 sub _lookup_fill ($$$) {
69         my ($self, $cache, $key) = @_;
70         $self->{$cache}->{$key} // do {
71                 _fill_all($self);
72                 $self->{$cache}->{$key};
73         }
74 }
75
76 sub lookup {
77         my ($self, $recipient) = @_;
78         _lookup_fill($self, '-by_addr', lc($recipient));
79 }
80
81 sub lookup_list_id {
82         my ($self, $list_id) = @_;
83         _lookup_fill($self, '-by_list_id', lc($list_id));
84 }
85
86 sub lookup_name ($$) {
87         my ($self, $name) = @_;
88         $self->{-by_name}->{$name} // _fill($self, "publicinbox.$name");
89 }
90
91 sub each_inbox {
92         my ($self, $cb) = @_;
93         # may auto-vivify if config file is non-existent:
94         foreach my $section (@{$self->{-section_order}}) {
95                 next if $section !~ m!\Apublicinbox\.([^/]+)\z!;
96                 my $ibx = lookup_name($self, $1) or next;
97                 $cb->($ibx);
98         }
99 }
100
101 sub lookup_newsgroup {
102         my ($self, $ng) = @_;
103         _lookup_fill($self, '-by_newsgroup', lc($ng));
104 }
105
106 sub limiter {
107         my ($self, $name) = @_;
108         $self->{-limiters}->{$name} //= do {
109                 require PublicInbox::Qspawn;
110                 my $max = $self->{"publicinboxlimiter.$name.max"} || 1;
111                 my $limiter = PublicInbox::Qspawn::Limiter->new($max);
112                 $limiter->setup_rlimit($name, $self);
113                 $limiter;
114         };
115 }
116
117 sub config_dir { $ENV{PI_DIR} // "$ENV{HOME}/.public-inbox" }
118
119 sub default_file {
120         my $f = $ENV{PI_CONFIG};
121         return $f if defined $f;
122         config_dir() . '/config';
123 }
124
125 sub config_fh_parse ($$$) {
126         my ($fh, $rs, $fs) = @_;
127         my %rv;
128         my (%section_seen, @section_order);
129         local $/ = $rs;
130         while (defined(my $line = <$fh>)) {
131                 chomp $line;
132                 my ($k, $v) = split($fs, $line, 2);
133                 my ($section) = ($k =~ /\A(\S+)\.[^\.]+\z/);
134                 unless (defined $section_seen{$section}) {
135                         $section_seen{$section} = 1;
136                         push @section_order, $section;
137                 }
138
139                 my $cur = $rv{$k};
140                 if (defined $cur) {
141                         if (ref($cur) eq "ARRAY") {
142                                 push @$cur, $v;
143                         } else {
144                                 $rv{$k} = [ $cur, $v ];
145                         }
146                 } else {
147                         $rv{$k} = $v;
148                 }
149         }
150         $rv{-section_order} = \@section_order;
151
152         \%rv;
153 }
154
155 sub git_config_dump {
156         my ($file) = @_;
157         return {} unless -e $file;
158         my @cmd = (qw/git config -z -l/, "--file=$file");
159         my $cmd = join(' ', @cmd);
160         my $fh = popen_rd(\@cmd) or die "popen_rd failed for $file: $!\n";
161         my $rv = config_fh_parse($fh, "\0", "\n");
162         close $fh or die "failed to close ($cmd) pipe: $?";
163         $rv;
164 }
165
166 sub valid_inbox_name ($) {
167         my ($name) = @_;
168
169         # Similar rules found in git.git/remote.c::valid_remote_nick
170         # and git.git/refs.c::check_refname_component
171         # We don't reject /\.lock\z/, however, since we don't lock refs
172         if ($name eq '' || $name =~ /\@\{/ ||
173             $name =~ /\.\./ || $name =~ m![/:\?\[\]\^~\s\f[:cntrl:]\*]! ||
174             $name =~ /\A\./ || $name =~ /\.\z/) {
175                 return 0;
176         }
177
178         # Note: we allow URL-unfriendly characters; users may configure
179         # non-HTTP-accessible inboxes
180         1;
181 }
182
183 # XXX needs testing for cgit compatibility
184 # cf. cgit/scan-tree.c::add_repo
185 sub cgit_repo_merge ($$$) {
186         my ($self, $base, $repo) = @_;
187         my $path = $repo->{dir};
188         if (defined(my $se = $self->{-cgit_strict_export})) {
189                 return unless -e "$path/$se";
190         }
191         return if -e "$path/noweb";
192         # $repo = { url => 'foo.git', dir => '/path/to/foo.git' }
193         my $rel = $repo->{url};
194         unless (defined $rel) {
195                 my $off = index($path, $base, 0);
196                 if ($off != 0) {
197                         $rel = $path;
198                 } else {
199                         $rel = substr($path, length($base) + 1);
200                 }
201
202                 $rel =~ s!/\.git\z!! or
203                         $rel =~ s!/+\z!!;
204
205                 $self->{-cgit_remove_suffix} and
206                         $rel =~ s!/?\.git\z!!;
207         }
208         $self->{"coderepo.$rel.dir"} //= $path;
209         $self->{"coderepo.$rel.cgiturl"} //= $rel;
210 }
211
212 sub is_git_dir ($) {
213         my ($git_dir) = @_;
214         -d "$git_dir/objects" && -f "$git_dir/HEAD";
215 }
216
217 # XXX needs testing for cgit compatibility
218 sub scan_path_coderepo {
219         my ($self, $base, $path) = @_;
220         opendir(my $dh, $path) or do {
221                 warn "error opening directory: $path\n";
222                 return
223         };
224         my $git_dir = $path;
225         if (is_git_dir($git_dir) || is_git_dir($git_dir .= '/.git')) {
226                 my $repo = { dir => $git_dir };
227                 cgit_repo_merge($self, $base, $repo);
228                 return;
229         }
230         while (defined(my $dn = readdir $dh)) {
231                 next if $dn eq '.' || $dn eq '..';
232                 if (index($dn, '.') == 0 && !$self->{-cgit_scan_hidden_path}) {
233                         next;
234                 }
235                 my $dir = "$path/$dn";
236                 scan_path_coderepo($self, $base, $dir) if -d $dir;
237         }
238 }
239
240 sub scan_tree_coderepo ($$) {
241         my ($self, $path) = @_;
242         scan_path_coderepo($self, $path, $path);
243 }
244
245 sub scan_projects_coderepo ($$$) {
246         my ($self, $list, $path) = @_;
247         open my $fh, '<', $list or do {
248                 warn "failed to open cgit projectlist=$list: $!\n";
249                 return;
250         };
251         foreach (<$fh>) {
252                 chomp;
253                 scan_path_coderepo($self, $path, "$path/$_");
254         }
255 }
256
257 sub parse_cgitrc {
258         my ($self, $cgitrc, $nesting) = @_;
259         if ($nesting == 0) {
260                 # defaults:
261                 my %s = map { $_ => 1 } qw(/cgit.css /cgit.png
262                                                 /favicon.ico /robots.txt);
263                 $self->{-cgit_static} = \%s;
264         }
265
266         # same limit as cgit/configfile.c::parse_configfile
267         return if $nesting > 8;
268
269         open my $fh, '<', $cgitrc or do {
270                 warn "failed to open cgitrc=$cgitrc: $!\n";
271                 return;
272         };
273
274         # FIXME: this doesn't support macro expansion via $VARS, yet
275         my $repo;
276         foreach (<$fh>) {
277                 chomp;
278                 if (m!\Arepo\.url=(.+?)/*\z!) {
279                         my $nick = $1;
280                         cgit_repo_merge($self, $repo->{dir}, $repo) if $repo;
281                         $repo = { url => $nick };
282                 } elsif (m!\Arepo\.path=(.+)\z!) {
283                         if (defined $repo) {
284                                 $repo->{dir} = $1;
285                         } else {
286                                 warn "$_ without repo.url\n";
287                         }
288                 } elsif (m!\Ainclude=(.+)\z!) {
289                         parse_cgitrc($self, $1, $nesting + 1);
290                 } elsif (m!\A(scan-hidden-path|remove-suffix)=([0-9]+)\z!) {
291                         my ($k, $v) = ($1, $2);
292                         $k =~ tr/-/_/;
293                         $self->{"-cgit_$k"} = $v;
294                 } elsif (m!\A(project-list|strict-export)=(.+)\z!) {
295                         my ($k, $v) = ($1, $2);
296                         $k =~ tr/-/_/;
297                         $self->{"-cgit_$k"} = $v;
298                 } elsif (m!\Ascan-path=(.+)\z!) {
299                         if (defined(my $list = $self->{-cgit_project_list})) {
300                                 scan_projects_coderepo($self, $list, $1);
301                         } else {
302                                 scan_tree_coderepo($self, $1);
303                         }
304                 } elsif (m!\A(?:css|favicon|logo|repo\.logo)=(/.+)\z!) {
305                         # absolute paths for static files via PublicInbox::Cgit
306                         $self->{-cgit_static}->{$1} = 1;
307                 }
308         }
309         cgit_repo_merge($self, $repo->{dir}, $repo) if $repo;
310 }
311
312 # parse a code repo
313 # Only git is supported at the moment, but SVN and Hg are possibilities
314 sub _fill_code_repo {
315         my ($self, $nick) = @_;
316         my $pfx = "coderepo.$nick";
317
318         # TODO: support gitweb and other repository viewers?
319         if (defined(my $cgitrc = delete $self->{-cgitrc_unparsed})) {
320                 parse_cgitrc($self, $cgitrc, 0);
321         }
322         my $dir = $self->{"$pfx.dir"}; # aka "GIT_DIR"
323         unless (defined $dir) {
324                 warn "$pfx.dir unset\n";
325                 return;
326         }
327
328         my $git = PublicInbox::Git->new($dir);
329         foreach my $t (qw(blob commit tree tag)) {
330                 $git->{$t.'_url_format'} =
331                                 _array($self->{lc("$pfx.${t}UrlFormat")});
332         }
333
334         if (my $cgits = $self->{lc("$pfx.cgitUrl")}) {
335                 $git->{cgit_url} = $cgits = _array($cgits);
336
337                 # cgit supports "/blob/?id=%s", but it's only a plain-text
338                 # display and requires an unabbreviated id=
339                 foreach my $t (qw(blob commit tag)) {
340                         $git->{$t.'_url_format'} //= map {
341                                 "$_/$t/?id=%s"
342                         } @$cgits;
343                 }
344         }
345
346         $git;
347 }
348
349 sub _git_config_bool ($) {
350         my ($val) = @_;
351         if ($val =~ /\A(?:false|no|off|[\-\+]?(?:0x)?0+)\z/i) {
352                 0;
353         } elsif ($val =~ /\A(?:true|yes|on|[\-\+]?(?:0x)?[0-9]+)\z/i) {
354                 1;
355         } else {
356                 undef;
357         }
358 }
359
360 sub _fill {
361         my ($self, $pfx) = @_;
362         my $ibx = {};
363
364         foreach my $k (qw(inboxdir filter url newsgroup
365                         infourl watch watchheader httpbackendmax
366                         replyto feedmax nntpserver indexlevel)) {
367                 my $v = $self->{"$pfx.$k"};
368                 $ibx->{$k} = $v if defined $v;
369         }
370
371         # backwards compatibility:
372         $ibx->{inboxdir} //= $self->{"$pfx.mainrepo"};
373
374         foreach my $k (qw(obfuscate)) {
375                 my $v = $self->{"$pfx.$k"};
376                 defined $v or next;
377                 if (defined(my $bval = _git_config_bool($v))) {
378                         $ibx->{$k} = $bval;
379                 } else {
380                         warn "Ignoring $pfx.$k=$v in config, not boolean\n";
381                 }
382         }
383         # TODO: more arrays, we should support multi-value for
384         # more things to encourage decentralization
385         foreach my $k (qw(address altid nntpmirror coderepo hide listid)) {
386                 if (defined(my $v = $self->{"$pfx.$k"})) {
387                         $ibx->{$k} = _array($v);
388                 }
389         }
390
391         return unless defined($ibx->{inboxdir});
392         my $name = $pfx;
393         $name =~ s/\Apublicinbox\.//;
394
395         if (!valid_inbox_name($name)) {
396                 warn "invalid inbox name: '$name'\n";
397                 return;
398         }
399
400         $ibx->{name} = $name;
401         $ibx->{-pi_config} = $self;
402         $ibx = PublicInbox::Inbox->new($ibx);
403         foreach (@{$ibx->{address}}) {
404                 my $lc_addr = lc($_);
405                 $self->{-by_addr}->{$lc_addr} = $ibx;
406                 $self->{-no_obfuscate}->{$lc_addr} = 1;
407         }
408         if (my $listids = $ibx->{listid}) {
409                 foreach my $list_id (@$listids) {
410                         $self->{-by_list_id}->{$list_id} = $ibx;
411                 }
412         }
413         if (my $ng = $ibx->{newsgroup}) {
414                 $self->{-by_newsgroup}->{$ng} = $ibx;
415         }
416         $self->{-by_name}->{$name} = $ibx;
417         if ($ibx->{obfuscate}) {
418                 $ibx->{-no_obfuscate} = $self->{-no_obfuscate};
419                 $ibx->{-no_obfuscate_re} = $self->{-no_obfuscate_re};
420                 _fill_all($self); # noop to populate -no_obfuscate
421         }
422
423         if (my $ibx_code_repos = $ibx->{coderepo}) {
424                 my $code_repos = $self->{-code_repos};
425                 my $repo_objs = $ibx->{-repo_objs} = [];
426                 foreach my $nick (@$ibx_code_repos) {
427                         my @parts = split(m!/!, $nick);
428                         my $valid = 0;
429                         $valid += valid_inbox_name($_) foreach (@parts);
430                         $valid == scalar(@parts) or next;
431
432                         my $repo = $code_repos->{$nick} //=
433                                                 _fill_code_repo($self, $nick);
434                         push @$repo_objs, $repo if $repo;
435                 }
436         }
437
438         $ibx
439 }
440
441 1;