]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Config.pm
www: admin-configurable CSS via "publicinbox.css"
[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
34         if (my $no = delete $self->{'publicinbox.noobfuscate'}) {
35                 $no = [ $no ] if ref($no) ne 'ARRAY';
36                 my @domains;
37                 foreach my $n (@$no) {
38                         my @n = split(/\s+/, $n);
39                         foreach (@n) {
40                                 if (/\S+@\S+/) { # full address
41                                         $self->{-no_obfuscate}->{lc $_} = 1;
42                                 } else {
43                                         # allow "example.com" or "@example.com"
44                                         s/\A@//;
45                                         push @domains, quotemeta($_);
46                                 }
47                         }
48                 }
49                 my $nod = join('|', @domains);
50                 $self->{-no_obfuscate_re} = qr/(?:$nod)\z/i;
51         }
52         if (my $css = delete $self->{'publicinbox.css'}) {
53                 $self->{css} = _array($css);
54         }
55
56         $self;
57 }
58
59 sub lookup {
60         my ($self, $recipient) = @_;
61         my $addr = lc($recipient);
62         my $inbox = $self->{-by_addr}->{$addr};
63         return $inbox if $inbox;
64
65         my $pfx;
66
67         foreach my $k (keys %$self) {
68                 $k =~ m!\A(publicinbox\.[^/]+)\.address\z! or next;
69                 my $v = $self->{$k};
70                 if (ref($v) eq "ARRAY") {
71                         foreach my $alias (@$v) {
72                                 (lc($alias) eq $addr) or next;
73                                 $pfx = $1;
74                                 last;
75                         }
76                 } else {
77                         (lc($v) eq $addr) or next;
78                         $pfx = $1;
79                         last;
80                 }
81         }
82         defined $pfx or return;
83         _fill($self, $pfx);
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         my %seen;
94         foreach my $k (keys %$self) {
95                 $k =~ m!\Apublicinbox\.([^/]+)\.mainrepo\z! or next;
96                 next if $seen{$1};
97                 $seen{$1} = 1;
98                 my $ibx = lookup_name($self, $1) or next;
99                 $cb->($ibx);
100         }
101 }
102
103 sub lookup_newsgroup {
104         my ($self, $ng) = @_;
105         $ng = lc($ng);
106         my $rv = $self->{-by_newsgroup}->{$ng};
107         return $rv if $rv;
108
109         foreach my $k (keys %$self) {
110                 $k =~ m!\A(publicinbox\.[^/]+)\.newsgroup\z! or next;
111                 my $v = $self->{$k};
112                 my $pfx = $1;
113                 if ($v eq $ng) {
114                         $rv = _fill($self, $pfx);
115                         return $rv;
116                 }
117         }
118         undef;
119 }
120
121 sub limiter {
122         my ($self, $name) = @_;
123         $self->{-limiters}->{$name} ||= do {
124                 require PublicInbox::Qspawn;
125                 my $max = $self->{"publicinboxlimiter.$name.max"};
126                 PublicInbox::Qspawn::Limiter->new($max);
127         };
128 }
129
130 sub config_dir { $ENV{PI_DIR} || "$ENV{HOME}/.public-inbox" }
131
132 sub default_file {
133         my $f = $ENV{PI_CONFIG};
134         return $f if defined $f;
135         config_dir() . '/config';
136 }
137
138 sub git_config_dump {
139         my ($file) = @_;
140         my ($in, $out);
141         my @cmd = (qw/git config/, "--file=$file", '-l');
142         my $cmd = join(' ', @cmd);
143         my $fh = popen_rd(\@cmd) or die "popen_rd failed for $file: $!\n";
144         my %rv;
145         local $/ = "\n";
146         while (defined(my $line = <$fh>)) {
147                 chomp $line;
148                 my ($k, $v) = split(/=/, $line, 2);
149                 my $cur = $rv{$k};
150
151                 if (defined $cur) {
152                         if (ref($cur) eq "ARRAY") {
153                                 push @$cur, $v;
154                         } else {
155                                 $rv{$k} = [ $cur, $v ];
156                         }
157                 } else {
158                         $rv{$k} = $v;
159                 }
160         }
161         close $fh or die "failed to close ($cmd) pipe: $?";
162
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 # parse a code repo
184 # Only git is supported at the moment, but SVN and Hg are possibilities
185 sub _fill_code_repo {
186         my ($self, $nick) = @_;
187         my $pfx = "coderepo.$nick";
188
189         my $dir = $self->{"$pfx.dir"}; # aka "GIT_DIR"
190         unless (defined $dir) {
191                 warn "$pfx.repodir unset";
192                 return;
193         }
194
195         my $git = PublicInbox::Git->new($dir);
196         foreach my $t (qw(blob commit tree tag)) {
197                 $git->{$t.'_url_format'} =
198                                 _array($self->{lc("$pfx.${t}UrlFormat")});
199         }
200
201         if (my $cgits = $self->{lc("$pfx.cgitUrl")}) {
202                 $git->{cgit_url} = $cgits = _array($cgits);
203
204                 # cgit supports "/blob/?id=%s", but it's only a plain-text
205                 # display and requires an unabbreviated id=
206                 foreach my $t (qw(blob commit tag)) {
207                         $git->{$t.'_url_format'} ||= map {
208                                 "$_/$t/?id=%s"
209                         } @$cgits;
210                 }
211         }
212         # TODO: support gitweb and other repository viewers?
213         # TODO: parse cgitrc
214
215         $git;
216 }
217
218 sub _fill {
219         my ($self, $pfx) = @_;
220         my $rv = {};
221
222         foreach my $k (qw(mainrepo filter url newsgroup
223                         infourl watch watchheader httpbackendmax
224                         replyto feedmax nntpserver indexlevel)) {
225                 my $v = $self->{"$pfx.$k"};
226                 $rv->{$k} = $v if defined $v;
227         }
228         foreach my $k (qw(obfuscate)) {
229                 my $v = $self->{"$pfx.$k"};
230                 defined $v or next;
231                 if ($v =~ /\A(?:false|no|off|0)\z/) {
232                         $rv->{$k} = 0;
233                 } elsif ($v =~ /\A(?:true|yes|on|1)\z/) {
234                         $rv->{$k} = 1;
235                 } else {
236                         warn "Ignoring $pfx.$k=$v in config, not boolean\n";
237                 }
238         }
239         # TODO: more arrays, we should support multi-value for
240         # more things to encourage decentralization
241         foreach my $k (qw(address altid nntpmirror coderepo)) {
242                 if (defined(my $v = $self->{"$pfx.$k"})) {
243                         $rv->{$k} = _array($v);
244                 }
245         }
246
247         return unless $rv->{mainrepo};
248         my $name = $pfx;
249         $name =~ s/\Apublicinbox\.//;
250
251         if (!valid_inbox_name($name)) {
252                 warn "invalid inbox name: '$name'\n";
253                 return;
254         }
255
256         $rv->{name} = $name;
257         $rv->{-pi_config} = $self;
258         $rv = PublicInbox::Inbox->new($rv);
259         foreach (@{$rv->{address}}) {
260                 my $lc_addr = lc($_);
261                 $self->{-by_addr}->{$lc_addr} = $rv;
262                 $self->{-no_obfuscate}->{$lc_addr} = 1;
263         }
264         if (my $ng = $rv->{newsgroup}) {
265                 $self->{-by_newsgroup}->{$ng} = $rv;
266         }
267         $self->{-by_name}->{$name} = $rv;
268         if ($rv->{obfuscate}) {
269                 $rv->{-no_obfuscate} = $self->{-no_obfuscate};
270                 $rv->{-no_obfuscate_re} = $self->{-no_obfuscate_re};
271                 each_inbox($self, sub {}); # noop to populate -no_obfuscate
272         }
273
274         if (my $ibx_code_repos = $rv->{coderepo}) {
275                 my $code_repos = $self->{-code_repos};
276                 my $repo_objs = $rv->{-repo_objs} = [];
277                 foreach my $nick (@$ibx_code_repos) {
278                         valid_inbox_name($nick) or next;
279                         my $repo = $code_repos->{$nick} ||=
280                                                 _fill_code_repo($self, $nick);
281                         push @$repo_objs, $repo if $repo;
282                 }
283         }
284
285         $rv
286 }
287
288 1;