App-Milter-Limit
view release on metacpan or search on metacpan
lib/App/Milter/Limit.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;
$App::Milter::Limit::VERSION = '0.54';
# ABSTRACT: Sendmail Milter that limits message rate by sender
use strict;
use warnings;
use base qw(Class::Accessor Class::Singleton);
use Carp;
use App::Milter::Limit::Config;
use App::Milter::Limit::Log;
use App::Milter::Limit::Util;
use Sendmail::PMilter 0.98 ':all';
use Sys::Syslog ();
__PACKAGE__->mk_accessors(qw(driver milter));
sub _new_instance {
my ($class, $driver) = @_;
croak "usage: new(driver)" unless defined $driver;
my $self = $class->SUPER::_new_instance();
$self->init($driver);
return $self;
}
sub init {
my ($self, $driver) = @_;
$self->_init_log;
$self->_init_statedir;
$self->milter(new Sendmail::PMilter);
$self->_init_driver($driver);
}
# initialize logging
sub _init_log {
my $self = shift;
my $conf = $self->config->section('log');
$$conf{identity} ||= 'milter-limit';
$$conf{facility} ||= 'mail';
Sys::Syslog::openlog($$conf{identity}, $$conf{options}, $$conf{facility});
info("syslog initialized");
$SIG{__WARN__} = sub {
Sys::Syslog::syslog('warning', "warning: ".join('', @_));
};
$SIG{__DIE__} = sub {
Sys::Syslog::syslog('crit', "fatal: ".join('',@_));
die @_;
};
}
# initialize the configured state dir.
# default: /var/run/milter-limit
sub _init_statedir {
my $self = shift;
my $conf = $self->config->global;
App::Milter::Limit::Util::make_path($$conf{state_dir});
}
sub _init_driver {
my ($self, $driver) = @_;
my $driver_class = "App::Milter::Limit::Plugin::$driver";
eval "require $driver_class";
if ($@) {
die "failed to load $driver_class: $@\n";
}
debug("loaded driver $driver");
( run in 1.778 second using v1.01-cache-2.11-cpan-2398b32b56e )