diff --git a/lib/Qpsmtpd.pm b/lib/Qpsmtpd.pm index 50a17e6..4036f1a 100644 --- a/lib/Qpsmtpd.pm +++ b/lib/Qpsmtpd.pm @@ -43,7 +43,14 @@ sub version { $VERSION . ($git ? "/$git" : "") } sub TRACE_LEVEL { $TraceLevel }; # leave for plugin compatibility -sub hooks { $hooks; } +sub hooks { + my ($self, $hook) = @_; + if ($hook) { + if (!defined $hooks->{$hook}) { return wantarray ? () : []; }; + return wantarray ? @{$hooks->{$hook}} : $hooks->{$hook}; + }; + return $hooks; +} sub load_logging { my $self = shift; @@ -252,9 +259,7 @@ sub transaction { return {}; } # base class implements empty transaction sub run_hooks { my ($self, $hook) = (shift, shift); - if ($hooks->{$hook}) { - my @r; - my @local_hooks = @{$hooks->{$hook}}; + if (my @local_hooks = $self->hooks($hook)) { $self->{_continuation} = [$hook, [@_], @local_hooks]; return $self->run_continuation(); } @@ -265,7 +270,7 @@ sub run_hooks_no_respond { my ($self, $hook) = (shift, shift); if ($hooks->{$hook}) { my @r; - for my $code (@{$hooks->{$hook}}) { + for my $code ($self->hooks($hook)) { eval { (@r) = $code->{code}->($self, $self->transaction, @_); }; if ($@) { warn("FATAL PLUGIN ERROR [" . $code->{name} . "]: ", $@); diff --git a/t/qpsmtpd.t b/t/qpsmtpd.t index f4183b4..b18a6a5 100644 --- a/t/qpsmtpd.t +++ b/t/qpsmtpd.t @@ -19,10 +19,10 @@ BEGIN { my $qp = bless {}, 'Qpsmtpd'; ok($qp->version(), "version, " . $qp->version()); -is_deeply(Qpsmtpd::hooks(), {}, 'hooks, empty'); +__hooks_none(); ok(my ($smtpd, $conn) = Test::Qpsmtpd->new_conn(), "get new connection"); -ok(Qpsmtpd::hooks(), "hooks, populated"); +__hooks(); __temp_file(); __temp_dir(); @@ -40,6 +40,27 @@ __config(); done_testing(); +sub __hooks { + ok(Qpsmtpd::hooks(), "hooks, populated"); + my $r = $qp->hooks; + ok(%$r, "hooks, populated returns a hashref"); + + $r = $qp->hooks('connect'); + ok(@$r, "hooks, populated, connect"); + #warn Data::Dumper::Dumper($r); + + my @r = $qp->hooks('connect'); + ok(@r, "hooks, populated, connect, wants array"); +} + +sub __hooks_none { + is_deeply(Qpsmtpd::hooks(), {}, 'hooks, empty'); + is_deeply($qp->hooks, {}, 'hooks, empty'); + + my $r = $qp->hooks('connect'); + is_deeply($r, [], 'hooks, empty, specified'); +} + sub __log { my $warned = ''; local $SIG{__WARN__} = sub {