allow plugin configuration via the plugins config

store _hooks globally so they'll work from the transaction object too


git-svn-id: https://svn.perl.org/qpsmtpd/trunk@95 958fd67b-6ff1-0310-b445-bb7760255be9
This commit is contained in:
Ask Bjørn Hansen 2002-11-06 06:40:35 +00:00
parent 2001523033
commit 9d5610a80a

View File

@ -1,7 +1,7 @@
package Qpsmtpd; package Qpsmtpd;
use strict; use strict;
$Qpsmtpd::VERSION = "0.12"; $Qpsmtpd::VERSION = "0.20-dev";
sub TRACE_LEVEL { 6 } sub TRACE_LEVEL { 6 }
use Sys::Hostname; use Sys::Hostname;
@ -9,6 +9,8 @@ use Qpsmtpd::Constants;
sub version { $Qpsmtpd::VERSION }; sub version { $Qpsmtpd::VERSION };
$Qpsmtpd::_hooks = {};
sub log { sub log {
my ($self, $trace, @log) = @_; my ($self, $trace, @log) = @_;
warn join(" ", $$, @log), "\n" warn join(" ", $$, @log), "\n"
@ -31,13 +33,16 @@ sub config {
timeout => 1200, timeout => 1200,
); );
my ($rc, @config) = $self->run_hooks("config", $c);
@config = () unless $rc == OK;
if (wantarray) { if (wantarray) {
my @config = $self->get_qmail_config($c); @config = $self->get_qmail_config($c) unless @config;
@config = @{$defaults{$c}} if (!@config and $defaults{$c}); @config = @{$defaults{$c}} if (!@config and $defaults{$c});
return @config; return @config;
} }
else { else {
return ($self->get_qmail_config($c) || $defaults{$c}); return ($config[0] || $self->get_qmail_config($c) || $defaults{$c});
} }
} }
@ -73,7 +78,10 @@ sub load_plugins {
for my $plugin (@plugins) { for my $plugin (@plugins) {
$self->log(7, "Loading $plugin"); $self->log(7, "Loading $plugin");
my ($plugin, @args) = split /\s+/, $plugin;
my $plugin_name = $plugin; my $plugin_name = $plugin;
# Escape everything into valid perl identifiers # Escape everything into valid perl identifiers
$plugin_name =~ s/([^A-Za-z0-9_\/])/sprintf("_%2x",unpack("C",$1))/eg; $plugin_name =~ s/([^A-Za-z0-9_\/])/sprintf("_%2x",unpack("C",$1))/eg;
@ -120,18 +128,19 @@ sub load_plugins {
die "eval $@" if $@; die "eval $@" if $@;
my $plug = $package->new(qpsmtpd => $self); my $plug = $package->new(qpsmtpd => $self);
$plug->register($self); $plug->register($self, @args);
} }
} }
sub run_hooks { sub run_hooks {
my ($self, $hook) = (shift, shift); my ($self, $hook) = (shift, shift);
$self->{_hooks} = $Qpsmtpd::_hooks;
if ($self->{_hooks}->{$hook}) { if ($self->{_hooks}->{$hook}) {
my @r; my @r;
for my $code (@{$self->{_hooks}->{$hook}}) { for my $code (@{$self->{_hooks}->{$hook}}) {
$self->log(5, "running plugin ", $code->{name}); $self->log(5, "running plugin ", $code->{name});
eval { (@r) = &{$code->{code}}($self->transaction, @_); }; eval { (@r) = &{$code->{code}}($self->can('transaction') ? $self->transaction : {}, @_); };
$@ and $self->log(0, "FATAL PLUGIN ERROR: ", $@) and next; $@ and $self->log(0, "FATAL PLUGIN ERROR: ", $@) and next;
!defined $r[0] !defined $r[0]
and $self->log(1, "plugin ".$code->{name} and $self->log(1, "plugin ".$code->{name}
@ -139,9 +148,9 @@ sub run_hooks {
and next; and next;
last unless $r[0] == DECLINED; last unless $r[0] == DECLINED;
} }
$r[0] = DECLINED if not defined $r[0];
return @r; return @r;
} }
warn "Did not run any hooks ...";
return (0, ''); return (0, '');
} }
@ -151,7 +160,7 @@ sub _register_hook {
#my $plugin = shift; # see comment in Plugin.pm:register_hook #my $plugin = shift; # see comment in Plugin.pm:register_hook
$self->{_hooks} ||= {}; $self->{_hooks} = $Qpsmtpd::_hooks;
my $hooks = $self->{_hooks}; my $hooks = $self->{_hooks};
push @{$hooks->{$hook}}, $code; push @{$hooks->{$hook}}, $code;
} }