]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Config.pm
config: introduce each_inbox for iteration
[public-inbox.git] / lib / PublicInbox / Config.pm
1 # Copyright (C) 2014-2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 #
4 # Used throughout the project for reading configuration
5 package PublicInbox::Config;
6 use strict;
7 use warnings;
8 require PublicInbox::Inbox;
9 use PublicInbox::Spawn qw(popen_rd);
10
11 # returns key-value pairs of config directives in a hash
12 # if keys may be multi-value, the value is an array ref containing all values
13 sub new {
14         my ($class, $file) = @_;
15         $file = default_file() unless defined($file);
16         $file = ref $file ? $file : git_config_dump($file);
17         my $self = bless $file, $class;
18
19         # caches
20         $self->{-by_addr} ||= {};
21         $self->{-by_name} ||= {};
22         $self->{-by_newsgroup} ||= {};
23         $self;
24 }
25
26 sub lookup {
27         my ($self, $recipient) = @_;
28         my $addr = lc($recipient);
29         my $inbox = $self->{-by_addr}->{$addr};
30         return $inbox if $inbox;
31
32         my $pfx;
33
34         foreach my $k (keys %$self) {
35                 $k =~ /\A(publicinbox\.[\w-]+)\.address\z/ or next;
36                 my $v = $self->{$k};
37                 if (ref($v) eq "ARRAY") {
38                         foreach my $alias (@$v) {
39                                 (lc($alias) eq $addr) or next;
40                                 $pfx = $1;
41                                 last;
42                         }
43                 } else {
44                         (lc($v) eq $addr) or next;
45                         $pfx = $1;
46                         last;
47                 }
48         }
49         defined $pfx or return;
50         _fill($self, $pfx);
51 }
52
53 sub lookup_name ($$) {
54         my ($self, $name) = @_;
55         $self->{-by_name}->{$name} || _fill($self, "publicinbox.$name");
56 }
57
58 sub each_inbox {
59         my ($self, $cb) = @_;
60         my %seen;
61         foreach my $k (keys %$self) {
62                 $k =~ /\Apublicinbox\.([A-Z0-9a-z-]+)\.mainrepo\z/ or next;
63                 next if $seen{$1};
64                 $seen{$1} = 1;
65                 my $ibx = lookup_name($self, $1) or next;
66                 $cb->($ibx);
67         }
68 }
69
70 sub lookup_newsgroup {
71         my ($self, $ng) = @_;
72         $ng = lc($ng);
73         my $rv = $self->{-by_newsgroup}->{$ng};
74         return $rv if $rv;
75
76         foreach my $k (keys %$self) {
77                 $k =~ /\A(publicinbox\.[\w-]+)\.newsgroup\z/ or next;
78                 my $v = $self->{$k};
79                 my $pfx = $1;
80                 if ($v eq $ng) {
81                         $rv = _fill($self, $pfx);
82                         return $rv;
83                 }
84         }
85         undef;
86 }
87
88 sub get {
89         my ($self, $inbox, $key) = @_;
90
91         $self->{"publicinbox.$inbox.$key"};
92 }
93
94 sub config_dir { $ENV{PI_DIR} || "$ENV{HOME}/.public-inbox" }
95
96 sub default_file {
97         my $f = $ENV{PI_CONFIG};
98         return $f if defined $f;
99         config_dir() . '/config';
100 }
101
102 sub git_config_dump {
103         my ($file) = @_;
104         my ($in, $out);
105         my @cmd = (qw/git config/, "--file=$file", '-l');
106         my $cmd = join(' ', @cmd);
107         my $fh = popen_rd(\@cmd) or die "popen_rd failed for $file: $!\n";
108         my %rv;
109         local $/ = "\n";
110         foreach my $line (<$fh>) {
111                 chomp $line;
112                 my ($k, $v) = split(/=/, $line, 2);
113                 my $cur = $rv{$k};
114
115                 if (defined $cur) {
116                         if (ref($cur) eq "ARRAY") {
117                                 push @$cur, $v;
118                         } else {
119                                 $rv{$k} = [ $cur, $v ];
120                         }
121                 } else {
122                         $rv{$k} = $v;
123                 }
124         }
125         close $fh or die "failed to close ($cmd) pipe: $?";
126         \%rv;
127 }
128
129 sub _fill {
130         my ($self, $pfx) = @_;
131         my $rv = {};
132
133         foreach my $k (qw(mainrepo address filter url newsgroup
134                         watch watchheader)) {
135                 my $v = $self->{"$pfx.$k"};
136                 $rv->{$k} = $v if defined $v;
137         }
138         return unless $rv->{mainrepo};
139         my $name = $pfx;
140         $name =~ s/\Apublicinbox\.//;
141         $rv->{name} = $name;
142         $rv = PublicInbox::Inbox->new($rv);
143         my $v = $rv->{address};
144         if (ref($v) eq 'ARRAY') {
145                 $self->{-by_addr}->{lc($_)} = $rv foreach @$v;
146         } else {
147                 $self->{-by_addr}->{lc($v)} = $rv;
148         }
149         if (my $ng = $rv->{newsgroup}) {
150                 $self->{-by_newsgroup}->{$ng} = $rv;
151         }
152         $self->{-by_name}->{$name} = $rv;
153 }
154
155 1;