Factored out the connect/disconnect code, added settings for the host and the from address and made them be handled by a setup.configure handler, wrapped the call to Net::SMTP->new() in a warning-free block.

This commit is contained in:
ian%hixie.ch 2001-11-09 20:21:11 +00:00
Родитель 4d542c9a80
Коммит 197ee7192a
1 изменённых файлов: 72 добавлений и 5 удалений

Просмотреть файл

@ -39,6 +39,8 @@ sub provides {
my($service) = @_;
return ($service eq 'output.generic.email' or
$service eq 'protocol.smtp' or
$service eq 'setup.configure' or
$service eq 'dataSource.configuration.client' or
$class->SUPER::provides($service));
}
@ -46,7 +48,31 @@ sub init {
my $self = shift;
my($app) = @_;
$self->SUPER::init(@_);
$self->handle(Net::SMTP->new('localhost')); # XXX
eval {
$app->getService('dataSource.configuration')->getSettings($app, $self, 'protocol.smtp');
};
if ($@) {
$self->dump(9, "failed to get the SMTP configuration, not going to bother to connect: $@");
$self->handle(undef);
} else {
$self->open();
}
}
sub open {
my $self = shift;
{ local $^W = 0; # XXX shut up warnings in Net::SMTP
$self->handle(Net::SMTP->new($self->host)); }
if (not defined($self->handle)) {
$self->warn(4, 'Could not create the SMTP handle');
}
}
sub close {
my $self = shift;
if (defined($self->handle)) {
$self->handle->quit();
}
}
# output.generic.email
@ -54,7 +80,7 @@ sub output {
my $self = shift;
my($app, $session, $string) = @_;
$self->assert(defined($self->handle), 1, 'No SMTP handle, can\'t send mail');
$self->handle->mail('XXX@spam.hixie.ch');
$self->handle->mail($self->from);
$self->handle->to($session->getAddress('email'));
$self->handle->data($string);
}
@ -70,8 +96,49 @@ sub checkAddress {
sub DESTROY {
my $self = shift;
if (defined($self->handle)) {
$self->handle->quit();
}
$self->SUPER::DESTROY(@_);
}
# dataSource.configuration.client
sub settings {
return qw(host from);
}
# setup.configure
sub setupConfigure {
my $self = shift;
my($app) = @_;
$self->dump(9, 'about to protocol.smtp...');
$app->output->setupProgress('protocol.smtp');
$self->close();
my $value;
$value = $self->host;
if (not defined($value)) {
$value = 'localhost';
}
$value = $app->input->getArgument("protocol.smtp.host", $value);
if (not defined($value)) {
return 'protocol.smtp.host';
}
$self->host($value);
$value = $self->from;
if (defined($value)) {
# default to original value
$value = $app->input->getArgument("protocol.smtp.from", $value);
} else {
# no default for 'from' (don't use $USER@`hostname` because it's rarely correct)
$value = $app->input->getArgument("protocol.smtp.from");
}
if (not defined($value)) {
return 'protocol.smtp.from';
}
$self->from($value);
$self->open();
$app->getService('dataSource.configuration')->setSettings($app, $self, 'protocol.smtp');
$self->dump(9, 'done configuring protocol.smtp');
return;
}