2002-07-06 09:18:48 +02:00
|
|
|
package Qpsmtpd::Plugin;
|
|
|
|
use strict;
|
|
|
|
|
2004-09-19 20:49:05 +02:00
|
|
|
our %hooks = map { $_ => 1 } qw(
|
2004-07-16 09:27:26 +02:00
|
|
|
config queue data data_post quit rcpt mail ehlo helo
|
2004-06-29 23:45:35 +02:00
|
|
|
auth auth-plain auth-login auth-cram-md5
|
2004-03-05 13:46:24 +01:00
|
|
|
connect reset_transaction unrecognized_command disconnect
|
2004-09-19 20:49:05 +02:00
|
|
|
deny
|
2004-03-05 13:46:24 +01:00
|
|
|
);
|
|
|
|
|
2002-07-06 09:18:48 +02:00
|
|
|
sub new {
|
|
|
|
my $proto = shift;
|
|
|
|
my $class = ref($proto) || $proto;
|
2004-08-31 03:58:57 +02:00
|
|
|
bless ({}, $class);
|
2002-07-06 09:18:48 +02:00
|
|
|
}
|
|
|
|
|
2002-07-08 04:30:11 +02:00
|
|
|
sub register_hook {
|
2004-06-11 22:01:17 +02:00
|
|
|
my ($plugin, $hook, $method, $unshift) = @_;
|
2004-03-05 13:46:24 +01:00
|
|
|
|
|
|
|
die $plugin->plugin_name . " : Invalid hook: $hook" unless $hooks{$hook};
|
|
|
|
|
2002-07-08 04:30:11 +02:00
|
|
|
# I can't quite decide if it's better to parse this code ref or if
|
|
|
|
# we should pass the plugin object and method name ... hmn.
|
2003-11-02 12:13:08 +01:00
|
|
|
$plugin->qp->_register_hook($hook, { code => sub { local $plugin->{_qp} = shift; $plugin->$method(@_) },
|
2004-06-11 22:01:17 +02:00
|
|
|
name => $plugin->plugin_name,
|
|
|
|
},
|
|
|
|
$unshift,
|
2002-09-24 12:56:35 +02:00
|
|
|
);
|
2002-07-08 04:30:11 +02:00
|
|
|
}
|
2002-07-06 09:18:48 +02:00
|
|
|
|
2004-08-31 03:58:57 +02:00
|
|
|
sub _register {
|
|
|
|
my $self = shift;
|
|
|
|
my $qp = shift;
|
|
|
|
local $self->{_qp} = $qp;
|
|
|
|
$self->register($qp, @_);
|
|
|
|
}
|
|
|
|
|
2002-07-08 04:30:11 +02:00
|
|
|
sub qp {
|
|
|
|
shift->{_qp};
|
|
|
|
}
|
2002-07-06 09:18:48 +02:00
|
|
|
|
2002-07-08 04:30:11 +02:00
|
|
|
sub log {
|
2002-09-08 12:00:02 +02:00
|
|
|
my $self = shift;
|
|
|
|
$self->qp->log(shift, $self->plugin_name . " plugin: " . shift, @_);
|
2002-07-06 09:18:48 +02:00
|
|
|
}
|
|
|
|
|
2002-07-08 04:30:11 +02:00
|
|
|
sub transaction {
|
|
|
|
# not sure if this will work in a non-forking or a threaded daemon
|
|
|
|
shift->qp->transaction;
|
|
|
|
}
|
2002-07-06 09:18:48 +02:00
|
|
|
|
2004-08-31 03:58:57 +02:00
|
|
|
sub connection {
|
|
|
|
shift->qp->connection;
|
|
|
|
}
|
|
|
|
|
2005-03-08 23:52:23 +01:00
|
|
|
sub config {
|
|
|
|
shift->qp->config(@_);
|
|
|
|
}
|
|
|
|
|
2005-02-22 03:47:39 +01:00
|
|
|
sub spool_dir {
|
|
|
|
shift->qp->spool_dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub temp_file {
|
|
|
|
my $self = shift;
|
|
|
|
my $tempfile = $self->qp->temp_file;
|
|
|
|
push @{$self->qp->transaction->{_temp_files}}, $tempfile;
|
|
|
|
return $tempfile;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub temp_dir {
|
|
|
|
my $self = shift;
|
|
|
|
my $tempdir = $self->qp->temp_dir();
|
|
|
|
push @{$self->qp->transaction->{_temp_dirs}}, $tempdir;
|
|
|
|
return $tempdir;
|
|
|
|
}
|
|
|
|
|
2004-09-08 07:14:10 +02:00
|
|
|
# plugin inheritance:
|
|
|
|
# usage:
|
|
|
|
# sub register {
|
|
|
|
# my $self = shift;
|
|
|
|
# $self->isa_plugin("rhsbl");
|
|
|
|
# $self->SUPER::register(@_);
|
|
|
|
# }
|
|
|
|
sub isa_plugin {
|
|
|
|
my ($self, $parent) = @_;
|
|
|
|
my ($currentPackage) = caller;
|
|
|
|
my $newPackage = $currentPackage."::_isa_";
|
|
|
|
|
|
|
|
return if defined &{"${newPackage}::register"};
|
|
|
|
|
|
|
|
Qpsmtpd::_compile($self->plugin_name . "_isa",
|
|
|
|
$newPackage,
|
|
|
|
"plugins/$parent"); # assumes Cwd is qpsmtpd root
|
2004-09-07 07:07:20 +02:00
|
|
|
|
|
|
|
no strict 'refs';
|
2004-09-08 07:14:10 +02:00
|
|
|
push @{"${currentPackage}::ISA"}, $newPackage;
|
2004-09-07 07:07:20 +02:00
|
|
|
}
|
|
|
|
|
2004-11-18 20:47:10 +01:00
|
|
|
sub compile {
|
|
|
|
my ($class, $plugin, $package, $file, $test_mode) = @_;
|
|
|
|
|
|
|
|
my $sub;
|
|
|
|
open F, $file or die "could not open $file: $!";
|
|
|
|
{
|
|
|
|
local $/ = undef;
|
|
|
|
$sub = <F>;
|
|
|
|
}
|
|
|
|
close F;
|
|
|
|
|
2005-05-09 15:43:40 +02:00
|
|
|
my $line = "\n#line 0 $file\n";
|
2004-11-18 20:47:10 +01:00
|
|
|
|
|
|
|
if ($test_mode) {
|
|
|
|
if (open(F, "t/plugin_tests/$plugin")) {
|
|
|
|
local $/ = undef;
|
|
|
|
$sub .= "#line 1 t/plugin_tests/$plugin\n";
|
|
|
|
$sub .= <F>;
|
|
|
|
close F;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
my $eval = join(
|
|
|
|
"\n",
|
|
|
|
"package $package;",
|
|
|
|
'use Qpsmtpd::Constants;',
|
|
|
|
"require Qpsmtpd::Plugin;",
|
|
|
|
'use vars qw(@ISA);',
|
|
|
|
'@ISA = qw(Qpsmtpd::Plugin);',
|
|
|
|
($test_mode ? 'use Test::More;' : ''),
|
|
|
|
"sub plugin_name { qq[$plugin] }",
|
|
|
|
$line,
|
|
|
|
$sub,
|
|
|
|
"\n", # last line comment without newline?
|
|
|
|
);
|
|
|
|
|
|
|
|
#warn "eval: $eval";
|
|
|
|
|
|
|
|
$eval =~ m/(.*)/s;
|
|
|
|
$eval = $1;
|
|
|
|
|
|
|
|
eval $eval;
|
|
|
|
die "eval $@" if $@;
|
|
|
|
}
|
|
|
|
|
2002-07-06 09:18:48 +02:00
|
|
|
1;
|