Merge pull request #108 from msimerson/hooks

Q::hooks(), accept a hook name argument
This commit is contained in:
Jared Johnson 2014-09-16 23:38:21 -05:00
commit d029c89113
2 changed files with 33 additions and 7 deletions

View File

@ -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} . "]: ", $@);

View File

@ -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 {