]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Config.pm
config: ignore missing config files
[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 = _array($no);
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         if (my $section_order = $self->{-section_order}) {
94                 foreach my $section (@$section_order) {
95                         next if $section !~ m!\Apublicinbox\.([^/]+)\z!;
96                         $self->{"publicinbox.$1.mainrepo"} or next;
97                         my $ibx = lookup_name($self, $1) or next;
98                         $cb->($ibx);
99                 }
100         } else {
101                 my %seen;
102                 foreach my $k (keys %$self) {
103                         $k =~ m!\Apublicinbox\.([^/]+)\.mainrepo\z! or next;
104                         next if $seen{$1};
105                         $seen{$1} = 1;
106                         my $ibx = lookup_name($self, $1) or next;
107                         $cb->($ibx);
108                 }
109         }
110 }
111
112 sub lookup_newsgroup {
113         my ($self, $ng) = @_;
114         $ng = lc($ng);
115         my $rv = $self->{-by_newsgroup}->{$ng};
116         return $rv if $rv;
117
118         foreach my $k (keys %$self) {
119                 $k =~ m!\A(publicinbox\.[^/]+)\.newsgroup\z! or next;
120                 my $v = $self->{$k};
121                 my $pfx = $1;
122                 if ($v eq $ng) {
123                         $rv = _fill($self, $pfx);
124                         return $rv;
125                 }
126         }
127         undef;
128 }
129
130 sub limiter {
131         my ($self, $name) = @_;
132         $self->{-limiters}->{$name} ||= do {
133                 require PublicInbox::Qspawn;
134                 my $max = $self->{"publicinboxlimiter.$name.max"};
135                 PublicInbox::Qspawn::Limiter->new($max);
136         };
137 }
138
139 sub config_dir { $ENV{PI_DIR} || "$ENV{HOME}/.public-inbox" }
140
141 sub default_file {
142         my $f = $ENV{PI_CONFIG};
143         return $f if defined $f;
144         config_dir() . '/config';
145 }
146
147 sub git_config_dump {
148         my ($file) = @_;
149         my (%section_seen, @section_order);
150         return {} unless -e $file;
151         my @cmd = (qw/git config/, "--file=$file", '-l');
152         my $cmd = join(' ', @cmd);
153         my $fh = popen_rd(\@cmd) or die "popen_rd failed for $file: $!\n";
154         my %rv;
155         local $/ = "\n";
156         while (defined(my $line = <$fh>)) {
157                 chomp $line;
158                 my ($k, $v) = split(/=/, $line, 2);
159
160                 my ($section) = ($k =~ /\A(\S+)\.[^\.]+\z/);
161                 unless (defined $section_seen{$section}) {
162                         $section_seen{$section} = 1;
163                         push @section_order, $section;
164                 }
165
166                 my $cur = $rv{$k};
167                 if (defined $cur) {
168                         if (ref($cur) eq "ARRAY") {
169                                 push @$cur, $v;
170                         } else {
171                                 $rv{$k} = [ $cur, $v ];
172                         }
173                 } else {
174                         $rv{$k} = $v;
175                 }
176         }
177         close $fh or die "failed to close ($cmd) pipe: $?";
178         $rv{-section_order} = \@section_order;
179
180         \%rv;
181 }
182
183 sub valid_inbox_name ($) {
184         my ($name) = @_;
185
186         # Similar rules found in git.git/remote.c::valid_remote_nick
187         # and git.git/refs.c::check_refname_component
188         # We don't reject /\.lock\z/, however, since we don't lock refs
189         if ($name eq '' || $name =~ /\@\{/ ||
190             $name =~ /\.\./ || $name =~ m![/:\?\[\]\^~\s\f[:cntrl:]\*]! ||
191             $name =~ /\A\./ || $name =~ /\.\z/) {
192                 return 0;
193         }
194
195         # Note: we allow URL-unfriendly characters; users may configure
196         # non-HTTP-accessible inboxes
197         1;
198 }
199
200 # parse a code repo
201 # Only git is supported at the moment, but SVN and Hg are possibilities
202 sub _fill_code_repo {
203         my ($self, $nick) = @_;
204         my $pfx = "coderepo.$nick";
205
206         my $dir = $self->{"$pfx.dir"}; # aka "GIT_DIR"
207         unless (defined $dir) {
208                 warn "$pfx.repodir unset";
209                 return;
210         }
211
212         my $git = PublicInbox::Git->new($dir);
213         foreach my $t (qw(blob commit tree tag)) {
214                 $git->{$t.'_url_format'} =
215                                 _array($self->{lc("$pfx.${t}UrlFormat")});
216         }
217
218         if (my $cgits = $self->{lc("$pfx.cgitUrl")}) {
219                 $git->{cgit_url} = $cgits = _array($cgits);
220
221                 # cgit supports "/blob/?id=%s", but it's only a plain-text
222                 # display and requires an unabbreviated id=
223                 foreach my $t (qw(blob commit tag)) {
224                         $git->{$t.'_url_format'} ||= map {
225                                 "$_/$t/?id=%s"
226                         } @$cgits;
227                 }
228         }
229         # TODO: support gitweb and other repository viewers?
230         # TODO: parse cgitrc
231
232         $git;
233 }
234
235 sub _fill {
236         my ($self, $pfx) = @_;
237         my $rv = {};
238
239         foreach my $k (qw(mainrepo filter url newsgroup
240                         infourl watch watchheader httpbackendmax
241                         replyto feedmax nntpserver indexlevel)) {
242                 my $v = $self->{"$pfx.$k"};
243                 $rv->{$k} = $v if defined $v;
244         }
245         foreach my $k (qw(obfuscate)) {
246                 my $v = $self->{"$pfx.$k"};
247                 defined $v or next;
248                 if ($v =~ /\A(?:false|no|off|0)\z/) {
249                         $rv->{$k} = 0;
250                 } elsif ($v =~ /\A(?:true|yes|on|1)\z/) {
251                         $rv->{$k} = 1;
252                 } else {
253                         warn "Ignoring $pfx.$k=$v in config, not boolean\n";
254                 }
255         }
256         # TODO: more arrays, we should support multi-value for
257         # more things to encourage decentralization
258         foreach my $k (qw(address altid nntpmirror coderepo)) {
259                 if (defined(my $v = $self->{"$pfx.$k"})) {
260                         $rv->{$k} = _array($v);
261                 }
262         }
263
264         return unless $rv->{mainrepo};
265         my $name = $pfx;
266         $name =~ s/\Apublicinbox\.//;
267
268         if (!valid_inbox_name($name)) {
269                 warn "invalid inbox name: '$name'\n";
270                 return;
271         }
272
273         $rv->{name} = $name;
274         $rv->{-pi_config} = $self;
275         $rv = PublicInbox::Inbox->new($rv);
276         foreach (@{$rv->{address}}) {
277                 my $lc_addr = lc($_);
278                 $self->{-by_addr}->{$lc_addr} = $rv;
279                 $self->{-no_obfuscate}->{$lc_addr} = 1;
280         }
281         if (my $ng = $rv->{newsgroup}) {
282                 $self->{-by_newsgroup}->{$ng} = $rv;
283         }
284         $self->{-by_name}->{$name} = $rv;
285         if ($rv->{obfuscate}) {
286                 $rv->{-no_obfuscate} = $self->{-no_obfuscate};
287                 $rv->{-no_obfuscate_re} = $self->{-no_obfuscate_re};
288                 each_inbox($self, sub {}); # noop to populate -no_obfuscate
289         }
290
291         if (my $ibx_code_repos = $rv->{coderepo}) {
292                 my $code_repos = $self->{-code_repos};
293                 my $repo_objs = $rv->{-repo_objs} = [];
294                 foreach my $nick (@$ibx_code_repos) {
295                         valid_inbox_name($nick) or next;
296                         my $repo = $code_repos->{$nick} ||=
297                                                 _fill_code_repo($self, $nick);
298                         push @$repo_objs, $repo if $repo;
299                 }
300         }
301
302         $rv
303 }
304
305 1;