Qpsmtpd: style changes in hook_responder

This commit is contained in:
Matt Simerson 2014-09-16 17:54:26 -07:00
parent 9cd8b165a0
commit 0499317912
2 changed files with 42 additions and 58 deletions

View File

@ -30,7 +30,7 @@ sub _restart {
} }
} }
sub version { $VERSION . ($git ? "/$git" : "") } sub version { $VERSION . ($git ? "/$git" : '') }
sub git_version { sub git_version {
return if !-e '.git'; return if !-e '.git';
@ -204,7 +204,7 @@ sub _load_plugin {
(/+) # directory (/+) # directory
(\d?) # package's first character (\d?) # package's first character
}[ }[
"::" . (length $2 ? sprintf("_%2x",unpack("C",$2)) : "") "::" . (length $2 ? sprintf("_%2x",unpack("C",$2)) : '')
]egx; ]egx;
my $package = "Qpsmtpd::Plugin::$plugin_name"; my $package = "Qpsmtpd::Plugin::$plugin_name";
@ -288,44 +288,43 @@ sub run_hooks_no_respond {
sub run_continuation { sub run_continuation {
my $self = shift; my $self = shift;
die "No continuation in progress" unless $self->{_continuation}; die "No continuation in progress" if !$self->{_continuation};
my $todo = $self->{_continuation}; my $todo = $self->{_continuation};
$self->{_continuation} = undef; $self->{_continuation} = undef;
my $hook = shift @$todo || die "No hook in the continuation"; my $hook = shift @$todo or die "No hook in the continuation";
my $args = shift @$todo || die "No hook args in the continuation"; my $args = shift @$todo or die "No hook args in the continuation";
my @r; my @r;
while (@$todo) { while (@$todo) {
my $code = shift @$todo; my $code = shift @$todo;
my $name = $code->{name};
$self->varlog(LOGDEBUG, $hook, $code->{name}); $self->varlog(LOGDEBUG, $hook, $name);
my $tran = $self->transaction; my $tran = $self->transaction;
eval { (@r) = $code->{code}->($self, $tran, @$args); }; eval { (@r) = $code->{code}->($self, $tran, @$args); };
if ($@) { if ($@) {
$self->log(LOGCRIT, "FATAL PLUGIN ERROR [" . $code->{name} . "]: ", $self->log(LOGCRIT, "FATAL PLUGIN ERROR [$name]: ", $@);
$@);
next; next;
} }
!defined $r[0] my $log_msg = "Plugin $name, hook $hook returned ";
and $self->log(LOGERROR, if (!defined $r[0]) {
"plugin " $self->log(LOGERROR, $log_msg . "undef!");
. $code->{name} next;
. " running the $hook hook returned undef!" }
)
and next;
# note this is wrong as $tran is always true in the # note this is wrong as $tran is always true in the current code...
# current code...
if ($tran) { if ($tran) {
my $tnotes = $tran->notes($code->{name}); my $tnotes = $tran->notes($name);
$tnotes->{"hook_$hook"}->{'return'} = $r[0] if (!defined $tnotes || ref $tnotes eq 'HASH') {
if (!defined $tnotes || ref $tnotes eq "HASH"); $tnotes->{"hook_$hook"}{return} = $r[0];
};
} }
else { else {
my $cnotes = $self->connection->notes($code->{name}); my $cnotes = $self->connection->notes($name);
$cnotes->{"hook_$hook"}->{'return'} = $r[0] if (!defined $cnotes || ref $cnotes eq 'HASH') {
if (!defined $cnotes || ref $cnotes eq "HASH"); $cnotes->{"hook_$hook"}{return} = $r[0];
};
} }
if ( $r[0] == DENY if ( $r[0] == DENY
@ -333,37 +332,27 @@ sub run_continuation {
or $r[0] == DENY_DISCONNECT or $r[0] == DENY_DISCONNECT
or $r[0] == DENYSOFT_DISCONNECT) or $r[0] == DENYSOFT_DISCONNECT)
{ {
$r[1] = "" if not defined $r[1]; $r[1] = '' if !defined $r[1];
$self->log(LOGDEBUG, $self->log(LOGDEBUG, $log_msg . return_code($r[0]) . ", $r[1]");
"Plugin " if ($hook ne 'deny') {
. $code->{name} $self->run_hooks_no_respond("deny", $name, $r[0], $r[1])
. ", hook $hook returned " };
. return_code($r[0])
. ", $r[1]"
);
$self->run_hooks_no_respond("deny", $code->{name}, $r[0], $r[1])
unless ($hook eq "deny");
} }
else { else {
$r[1] = "" if not defined $r[1]; $r[1] = '' if not defined $r[1];
$self->log(LOGDEBUG, $self->log(LOGDEBUG, $log_msg . return_code($r[0]) . ", $r[1]");
"Plugin " $self->run_hooks_no_respond("ok", $name, $r[0], $r[1]) if $hook ne "ok";
. $code->{name}
. ", hook $hook returned "
. return_code($r[0])
. ", $r[1]"
);
$self->run_hooks_no_respond("ok", $code->{name}, $r[0], $r[1])
unless ($hook eq "ok");
} }
last unless $r[0] == DECLINED; last if $r[0] != DECLINED;
} }
$r[0] = DECLINED if not defined $r[0]; $r[0] = DECLINED if ! defined $r[0];
# hook_*_parse() may return a CODE ref.. # hook_*_parse() may return a CODE ref..
# ... which breaks when splitting as string: # ... which breaks when splitting as string:
@r = map { split /\n/ } @r unless (ref($r[1]) eq "CODE"); if ('CODE' ne ref $r[1]) {
@r = map { split /\n/ } @r;
};
return $self->hook_responder($hook, \@r, $args); return $self->hook_responder($hook, \@r, $args);
} }
@ -450,17 +439,17 @@ sub size_threshold {
sub authenticated { sub authenticated {
my $self = shift; my $self = shift;
return (defined $self->{_auth} ? $self->{_auth} : ""); return (defined $self->{_auth} ? $self->{_auth} : '');
} }
sub auth_user { sub auth_user {
my $self = shift; my $self = shift;
return (defined $self->{_auth_user} ? $self->{_auth_user} : ""); return (defined $self->{_auth_user} ? $self->{_auth_user} : '');
} }
sub auth_mechanism { sub auth_mechanism {
my $self = shift; my $self = shift;
return (defined $self->{_auth_mechanism} ? $self->{_auth_mechanism} : ""); return (defined $self->{_auth_mechanism} ? $self->{_auth_mechanism} : '');
} }
sub address { sub address {

View File

@ -26,6 +26,7 @@ __hooks();
__register_hook(); __register_hook();
__hook_responder(); __hook_responder();
#done_testing() and exit;
__temp_file(); __temp_file();
__temp_dir(); __temp_dir();
@ -65,24 +66,18 @@ sub __hooks_none {
} }
sub __hook_responder { sub __hook_responder {
# my ($self, $hook, $msg, $args) = @_; my ($code, $msg) = $qp->hook_responder('test-hook', ['test code','test mesg']);
my ($code, $msg) = $qp->hook_responder('test-hook', ['test code','test mesg'], ['test-arg']);
is($code, 'test code', "hook_responder, code"); is($code, 'test code', "hook_responder, code");
is($msg, 'test mesg', "hook_responder, test msg"); is($msg, 'test mesg', "hook_responder, test msg");
($code, $msg) = $smtpd->hook_responder('connect', ['test code','test mesg'], ['test-arg']); ($code, $msg) = $smtpd->hook_responder('connect', ['test code','test mesg']);
is($code->[0], 220, "hook_responder, code"); is($code->[0], 220, "hook_responder, code");
ok($code->[1] =~ /ESMTP qpsmtpd/, "hook_responder, message: ". $code->[1]); ok($code->[1] =~ /ESMTP qpsmtpd/, "hook_responder, message: ". $code->[1]);
my $rej_msg = 'Your father smells of elderberries'; my $rej_msg = 'Your father smells of elderberries';
#($smtpd, $conn) = Test::Qpsmtpd->new_conn();
($code, $msg) = $smtpd->hook_responder('connect', [DENY, $rej_msg]); ($code, $msg) = $smtpd->hook_responder('connect', [DENY, $rej_msg]);
# warn Data::Dumper::Dumper($code); is($code, undef, "hook_responder, disconnected yields undef code");
# warn Data::Dumper::Dumper($msg);
# is($code, undef, "hook_responder, disconnected yields undef code");
is($msg, undef, "hook_responder, disconnected yields undef msg"); is($msg, undef, "hook_responder, disconnected yields undef msg");
#warn Data::Dumper::Dumper($msg);
} }
sub __register_hook { sub __register_hook {