diff --git a/lib/Qpsmtpd.pm b/lib/Qpsmtpd.pm index a33a00e..b812b4d 100644 --- a/lib/Qpsmtpd.pm +++ b/lib/Qpsmtpd.pm @@ -10,8 +10,6 @@ sub TRACE_LEVEL { $LogLevel } sub version { $VERSION }; -$Qpsmtpd::_hooks = {}; - sub init_logger { my $self = shift; # Get the loglevel - we localise loglevel to zero while we do this @@ -116,6 +114,9 @@ sub _config_from_file { sub load_plugins { my $self = shift; + + $self->{hooks} ||= {}; + my @plugins = $self->config('plugins'); my ($name) = ($0 =~ m!(.*?)/([^/]+)$!); @@ -206,20 +207,24 @@ sub _load_plugins { eval $eval; die "eval $@" if $@; - my $plug = $package->new(qpsmtpd => $self); - $plug->register($self, @args); + my $plug = $package->new(); + $plug->_register($self, @args); } } +sub transaction { + return {}; # base class implements empty transaction +} + sub run_hooks { my ($self, $hook) = (shift, shift); - $self->{_hooks} = $Qpsmtpd::_hooks; - if ($self->{_hooks}->{$hook}) { + my $hooks = $self->{hooks}; + if ($hooks->{$hook}) { my @r; - for my $code (@{$self->{_hooks}->{$hook}}) { + for my $code (@{$hooks->{$hook}}) { $self->log(LOGINFO, "running plugin ", $code->{name}); - eval { (@r) = $code->{code}->($self, $self->can('transaction') ? $self->transaction : {}, @_); }; + eval { (@r) = $code->{code}->($self, $self->transaction, @_); }; $@ and $self->log(LOGCRIT, "FATAL PLUGIN ERROR: ", $@) and next; !defined $r[0] and $self->log(LOGERROR, "plugin ".$code->{name} @@ -245,10 +250,7 @@ sub _register_hook { my $self = shift; my ($hook, $code, $unshift) = @_; - #my $plugin = shift; # see comment in Plugin.pm:register_hook - - $self->{_hooks} = $Qpsmtpd::_hooks; - my $hooks = $self->{_hooks}; + my $hooks = $self->{hooks}; if ($unshift) { unshift @{$hooks->{$hook}}, $code; } diff --git a/lib/Qpsmtpd/Plugin.pm b/lib/Qpsmtpd/Plugin.pm index 597803b..3d26523 100644 --- a/lib/Qpsmtpd/Plugin.pm +++ b/lib/Qpsmtpd/Plugin.pm @@ -10,8 +10,7 @@ my %hooks = map { $_ => 1 } qw( sub new { my $proto = shift; my $class = ref($proto) || $proto; - my %args = @_; - bless ({ _qp => $args{qpsmtpd} }, $class); + bless ({}, $class); } sub register_hook { @@ -28,6 +27,13 @@ sub register_hook { ); } +sub _register { + my $self = shift; + my $qp = shift; + local $self->{_qp} = $qp; + $self->register($qp, @_); +} + sub qp { shift->{_qp}; } @@ -42,4 +48,8 @@ sub transaction { shift->qp->transaction; } +sub connection { + shift->qp->connection; +} + 1; diff --git a/lib/Qpsmtpd/SMTP.pm b/lib/Qpsmtpd/SMTP.pm index 0a24bc2..57d8c25 100644 --- a/lib/Qpsmtpd/SMTP.pm +++ b/lib/Qpsmtpd/SMTP.pm @@ -169,7 +169,7 @@ sub ehlo { # Check for possible AUTH mechanisms my %auth_mechanisms; -HOOK: foreach my $hook ( keys %{$self->{_hooks}} ) { +HOOK: foreach my $hook ( keys %{$self->{hooks}} ) { if ( $hook =~ m/^auth-?(.+)?$/ ) { if ( defined $1 ) { $auth_mechanisms{uc($1)} = 1;