| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # | 
| 2 |  |  |  |  |  |  | # This file is part of App-Milter-Limit | 
| 3 |  |  |  |  |  |  | # | 
| 4 |  |  |  |  |  |  | # This software is copyright (c) 2010 by Michael Schout. | 
| 5 |  |  |  |  |  |  | # | 
| 6 |  |  |  |  |  |  | # This is free software; you can redistribute it and/or modify it under | 
| 7 |  |  |  |  |  |  | # the same terms as the Perl 5 programming language system itself. | 
| 8 |  |  |  |  |  |  | # | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | package App::Milter::Limit::Config; | 
| 11 |  |  |  |  |  |  | $App::Milter::Limit::Config::VERSION = '0.53'; | 
| 12 |  |  |  |  |  |  | # ABSTRACT: Milter Limit configuration object | 
| 13 |  |  |  |  |  |  |  | 
| 14 | 5 |  |  | 5 |  | 31 | use strict; | 
|  | 5 |  |  |  |  | 8 |  | 
|  | 5 |  |  |  |  | 123 |  | 
| 15 | 5 |  |  | 5 |  | 24 | use warnings; | 
|  | 5 |  |  |  |  | 8 |  | 
|  | 5 |  |  |  |  | 127 |  | 
| 16 |  |  |  |  |  |  |  | 
| 17 | 5 |  |  | 5 |  | 24 | use base qw(Class::Singleton Class::Accessor); | 
|  | 5 |  |  |  |  | 11 |  | 
|  | 5 |  |  |  |  | 364 |  | 
| 18 | 5 |  |  | 5 |  | 1330 | use Config::Tiny; | 
|  | 5 |  |  |  |  | 4315 |  | 
|  | 5 |  |  |  |  | 579 |  | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | __PACKAGE__->mk_accessors(qw(config)); | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | sub _new_instance { | 
| 24 | 4 |  |  | 4 |  | 60354 | my ($class, $config_file) = @_; | 
| 25 |  |  |  |  |  |  |  | 
| 26 | 4 | 50 |  |  |  | 50 | my $config = Config::Tiny->read($config_file) | 
| 27 |  |  |  |  |  |  | or die "failed to read config file: ", Config::Tiny->errstr; | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | # set defaults | 
| 30 | 4 |  | 50 |  |  | 1184 | $config->{_}{name} ||= 'milter-limit'; | 
| 31 | 4 |  | 50 |  |  | 22 | $config->{_}{state_dir} ||= '/var/run/milter-limit'; | 
| 32 |  |  |  |  |  |  |  | 
| 33 | 4 |  |  |  |  | 46 | my $self = $class->SUPER::_new_instance({config => $config}); | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 4 |  |  |  |  | 110 | $self->init; | 
| 36 |  |  |  |  |  |  |  | 
| 37 | 4 |  |  |  |  | 18 | return $self; | 
| 38 |  |  |  |  |  |  | } | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | sub init { | 
| 41 | 4 |  |  | 4 | 0 | 16 | my $self = shift; | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 5 |  |  | 5 |  | 30 | no warnings 'uninitialized'; | 
|  | 5 |  |  |  |  | 11 |  | 
|  | 5 |  |  |  |  | 1196 |  | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 4 |  |  |  |  | 18 | my $conf = $self->global; | 
| 46 | 4 | 50 |  |  |  | 174 | if (my $user = $$conf{user}) { | 
| 47 | 0 |  |  |  |  | 0 | $$conf{user} = App::Milter::Limit::Util::get_uid($user); | 
| 48 |  |  |  |  |  |  | } | 
| 49 |  |  |  |  |  |  |  | 
| 50 | 4 | 50 |  |  |  | 30 | if (my $group = $$conf{group}) { | 
| 51 | 0 |  |  |  |  | 0 | $$conf{group} = App::Milter::Limit::Util::get_gid($group); | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | sub global { | 
| 57 | 18 |  |  | 18 | 1 | 216 | my $self = shift; | 
| 58 | 18 |  |  |  |  | 71 | $self->instance->config->{_}; | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | sub section { | 
| 63 | 4 |  |  | 4 | 1 | 50 | my ($self, $name) = @_; | 
| 64 | 4 |  |  |  |  | 50 | $self->instance->config->{$name}; | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | sub set_defaults { | 
| 69 | 0 |  |  | 0 | 1 |  | my ($self, $section, %defaults) = @_; | 
| 70 |  |  |  |  |  |  |  | 
| 71 | 0 | 0 |  |  |  |  | $section = '_' if $section eq 'global'; | 
| 72 |  |  |  |  |  |  |  | 
| 73 | 0 | 0 |  |  |  |  | my $conf = $self->instance->config->{$section} | 
| 74 |  |  |  |  |  |  | or die "config section [$section] does not exist in the config file\n"; | 
| 75 |  |  |  |  |  |  |  | 
| 76 | 0 |  |  |  |  |  | for my $key (keys %defaults) { | 
| 77 | 0 | 0 |  |  |  |  | unless (defined $$conf{$key}) { | 
| 78 | 0 |  |  |  |  |  | $$conf{$key} = $defaults{$key}; | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | 1; | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | __END__ |