| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::Milter::Limit::Config; | 
| 2 |  |  |  |  |  |  | $App::Milter::Limit::Config::VERSION = '0.52'; | 
| 3 |  |  |  |  |  |  | # ABSTRACT: Milter Limit configuration object | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 1 |  |  | 1 |  | 4 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 29 |  | 
| 6 | 1 |  |  | 1 |  | 4 | use base qw(Class::Singleton Class::Accessor); | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 86 |  | 
| 7 | 1 |  |  | 1 |  | 699 | use Config::Tiny; | 
|  | 1 |  |  |  |  | 826 |  | 
|  | 1 |  |  |  |  | 361 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | __PACKAGE__->mk_accessors(qw(config)); | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | sub _new_instance { | 
| 13 | 0 |  |  | 0 |  |  | my ($class, $config_file) = @_; | 
| 14 |  |  |  |  |  |  |  | 
| 15 | 0 | 0 |  |  |  |  | my $config = Config::Tiny->read($config_file) | 
| 16 |  |  |  |  |  |  | or die "failed to read config file: ", Config::Tiny->errstr; | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | # set defaults | 
| 19 | 0 |  | 0 |  |  |  | $config->{_}{name} ||= 'milter-limit'; | 
| 20 | 0 |  | 0 |  |  |  | $config->{_}{state_dir} ||= '/var/run/milter-limit'; | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 0 |  |  |  |  |  | my $self = $class->SUPER::_new_instance({config => $config}); | 
| 23 |  |  |  |  |  |  |  | 
| 24 | 0 |  |  |  |  |  | $self->init; | 
| 25 |  |  |  |  |  |  |  | 
| 26 | 0 |  |  |  |  |  | return $self; | 
| 27 |  |  |  |  |  |  | } | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | sub init { | 
| 30 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 0 |  |  |  |  |  | my $conf = $self->global; | 
| 33 | 0 | 0 |  |  |  |  | if (my $user = $$conf{user}) { | 
| 34 | 0 |  |  |  |  |  | $$conf{user} = App::Milter::Limit::Util::get_uid($user); | 
| 35 |  |  |  |  |  |  | } | 
| 36 |  |  |  |  |  |  |  | 
| 37 | 0 | 0 |  |  |  |  | if (my $group = $$conf{group}) { | 
| 38 | 0 |  |  |  |  |  | $$conf{group} = App::Milter::Limit::Util::get_gid($group); | 
| 39 |  |  |  |  |  |  | } | 
| 40 |  |  |  |  |  |  | } | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | sub global { | 
| 44 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 45 | 0 |  |  |  |  |  | $self->instance->config->{_}; | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | sub section { | 
| 50 | 0 |  |  | 0 | 1 |  | my ($self, $name) = @_; | 
| 51 | 0 |  |  |  |  |  | $self->instance->config->{$name}; | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | sub set_defaults { | 
| 56 | 0 |  |  | 0 | 1 |  | my ($self, $section, %defaults) = @_; | 
| 57 |  |  |  |  |  |  |  | 
| 58 | 0 | 0 |  |  |  |  | $section = '_' if $section eq 'global'; | 
| 59 |  |  |  |  |  |  |  | 
| 60 | 0 | 0 |  |  |  |  | my $conf = $self->instance->config->{$section} | 
| 61 |  |  |  |  |  |  | or die "config section [$section] does not exist in the config file\n"; | 
| 62 |  |  |  |  |  |  |  | 
| 63 | 0 |  |  |  |  |  | for my $key (keys %defaults) { | 
| 64 | 0 | 0 |  |  |  |  | unless (defined $$conf{$key}) { | 
| 65 | 0 |  |  |  |  |  | $$conf{$key} = $defaults{$key}; | 
| 66 |  |  |  |  |  |  | } | 
| 67 |  |  |  |  |  |  | } | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | 1; | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | __END__ |