App-Milter-Limit

 view release on metacpan or  search on metacpan

lib/App/Milter/Limit/Config.pm  view on Meta::CPAN

#
# This file is part of App-Milter-Limit
#
# This software is copyright (c) 2010 by Michael Schout.
#
# This is free software; you can redistribute it and/or modify it under
# the same terms as the Perl 5 programming language system itself.
#

package App::Milter::Limit::Config;
$App::Milter::Limit::Config::VERSION = '0.54';
# ABSTRACT: Milter Limit configuration object

use strict;
use warnings;

use base qw(Class::Singleton Class::Accessor);
use Config::Tiny;

__PACKAGE__->mk_accessors(qw(config));


sub _new_instance {
    my ($class, $config_file) = @_;

    my $config = Config::Tiny->read($config_file)
        or die "failed to read config file: ", Config::Tiny->errstr;

    # set defaults
    $config->{_}{name} ||= 'milter-limit';
    $config->{_}{state_dir} ||= '/var/run/milter-limit';

    my $self = $class->SUPER::_new_instance({config => $config});

    $self->init;

    return $self;
}

sub init {
    my $self = shift;

    no warnings 'uninitialized';

    my $conf = $self->global;
    if (my $user = $$conf{user}) {
        $$conf{user} = App::Milter::Limit::Util::get_uid($user);
    }

    if (my $group = $$conf{group}) {
        $$conf{group} = App::Milter::Limit::Util::get_gid($group);
    }
}


sub global {
    my $self = shift;
    $self->instance->config->{_};
}


sub section {
    my ($self, $name) = @_;
    $self->instance->config->{$name};
}


sub set_defaults {
    my ($self, $section, %defaults) = @_;

    $section = '_' if $section eq 'global';

    my $conf = $self->instance->config->{$section}
        or die "config section [$section] does not exist in the config file\n";

    for my $key (keys %defaults) {
        unless (defined $$conf{$key}) {
            $$conf{$key} = $defaults{$key};
        }
    }
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

App::Milter::Limit::Config - Milter Limit configuration object

=head1 VERSION

version 0.54

=head1 SYNOPSIS

 # pass config file name first time.
 my $conf = App::Milter::Limit::Config->instance('/etc/mail/milter-limit.conf');

 # after that, just call instance()
 $conf = App::Milter::Limit::Config->instance();

 # global config section
 my $global = $conf->global;
 my $limit = $global->{limit};

 # log section



( run in 2.270 seconds using v1.01-cache-2.11-cpan-ceb78f64989 )