Skip to content

Commit

Permalink
More control for isa_plugin() arguments
Browse files Browse the repository at this point in the history
When a plugin is wrapped with an isa_plugin() call, the plugin we're
inheriting from will receive the same arguments as the wrapper. This
patch allows to modify the argument list for the wrapped plugin, i.e.
 * delete arguments only valid for the wrapping plugin
 * add additional arguments
Modifying @_ directly only allows something like "for (@_) { s/a/b/g }",
not adding or deleting. Returning the new @_ will much likely break
existing plugins.
  • Loading branch information
vetinari committed Mar 14, 2009
1 parent 25b5cef commit b639002
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
14 changes: 11 additions & 3 deletions docs/plugins.pod
Expand Up @@ -176,10 +176,18 @@ All valid hooks are defined in F<lib/Qpsmtpd/Plugins.pm>, C<our @hooks>.
=head2 Inheritance

Inheriting methods from other plugins is an advanced topic. You can alter
arguments for the underlying plugin, prepare something for the I<real>
plugin or skip a hook with this. Instead of modifying C<@ISA>
arguments for the underlying plugin (see below), prepare something for the
C<real> plugin or skip a hook with this. Instead of modifying C<@ISA>
directly in your plugin, use the C<isa_plugin()> method from the
C<init()> subroutine.
C<init()> subroutine.

Altering arguments for the C<real> plugin is done by calling
C<$self-E<gt>set_isa_plugin_args(@args)> from C<init()>. If you do not
call this, the C<real> plugin will receive the same arguments as your
plugin (modulo changes done with C<for(@_) { tr/a-z/A-Z/; }> or similar,
see L<perlsub(1)>): C<init()> and C<register()> are called with the same
C<@_> as argument. Deleting or adding plugin arguments B<must> be done with
C<set_isa_plugin_args()>.

# rcpt_ok_child
sub init {
Expand Down
14 changes: 14 additions & 0 deletions lib/Qpsmtpd/Plugin.pm
Expand Up @@ -45,13 +45,27 @@ sub register_hook {
);
}

my @isa_plugin_args = ();
sub set_isa_plugin_args {
my $self = shift;
$self->{_isa_plugin_args} = 1;
@isa_plugin_args = @_;
}

sub _register {
my $self = shift;
my $qp = shift;
local $self->{_qp} = $qp;

@isa_plugin_args = ();
$self->init($qp, @_) if $self->can('init');
$self->{_isa_plugin_args}
and @_ = @isa_plugin_args;

$self->_register_standard_hooks($qp, @_);
$self->register($qp, @_) if $self->can('register');

delete $self->{_isa_plugin_args};
}

sub qp {
Expand Down

0 comments on commit b639002

Please sign in to comment.