1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
package PostgreyTestClient;
use strict;
use IO::Socket::INET;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(request);
sub new
{
my ($class, $path) = @_;
defined $path or die "ERROR: must define path";
my $self = {
path => $path,
time => time,
};
return bless $self, $class;
}
sub advance_time
{
my ($self, $seconds) = @_;
$self->{time}+=$seconds;
}
sub request
{
my ($self, $attrs) = @_;
# creating a listening socket
my $socket = new IO::Socket::UNIX (
Type => SOCK_STREAM(),
Peer => $self->{path},
);
defined $socket or do {
warn "ERROR: can't create socket: $!\n";
return undef;
};
print $socket "request=smtpd_access_policy\n";
print $socket "policy_test_time=$self->{time}\n";
for my $key (keys %$attrs) {
print $socket "$key=$attrs->{$key}\n";
}
print $socket "\n";
my $reply = '';
while(my $line = readline($socket)) {
chomp($line);
last if $line eq '';
$reply .= $line;
}
$reply =~ /^action=(.*)/ or do {
warn "unexpected reply: $reply\n";
return undef;
};
return $1;
}
1;
|