webgui_newsletter/sbin/newsletter-transport.pl
2010-10-28 15:10:34 +02:00

100 lines
2.7 KiB
Perl
Executable file

#!/data/wre/prereqs/bin/perl
BEGIN {
unshift @INC, qw(
/data/custom/webgui_newsletter/lib
/data/WebGUI/lib
);
}
use strict;
use warnings;
#use Mail::DeliveryStatus::BounceParser;
#use WebGUI::Mailing::Email;
use WebGUI::MailCommand;
use List::MoreUtils qw{ any };
use WebGUI::Config;
use Getopt::Long;
use 5.010;
my $NO_SUCH_USER = 67;
my $webguiRoot = '/data/WebGUI';
#---------------------------------------------------------------
# Startup
{
my ( $configFile, $command, $id, $sender, $senderIp ) = getCredentials();
if ( WebGUI::MailCommand::isValidCommand( $command ) ) {
my $session = openSession( $webguiRoot, $configFile );
no warnings 'once';
*{ WebGUI::Session::Env::getIp } = sub {
return $senderIp;
};
$session->log->warn( "IP is : [$senderIp][" .$session->env->getIp ."]");
WebGUI::MailCommand::processCommand( $session, $command, $id, $sender );
closeSession( $session );
}
else {
die "Not a valid command [$command].";
}
exit(0);
}
#-----------------------------------------------------------------------------
sub getCredentials {
my ( $domain, $user, $sender, $senderIp );
GetOptions(
'domain=s' => \$domain,
'user=s' => \$user,
'sender=s' => \$sender,
'senderIp=s'=> \$senderIp,
);
die "--domain parameter is required" unless $domain;
die "--user parameter is required" unless $user;
die "--sender parameter is required" unless $sender;
die "--senderIp parameter is required" unless $senderIp;
my $dispatch = WebGUI::Config->new( $webguiRoot, 'mailing_dispatch.config' )
|| die "Cannot open $webguiRoot/etc/mailing_dispatch.config";
my $configFile = $dispatch->get( $domain )
|| die "Received mail for domain [$domain] which is not configured!";
# Format is mailId-command
my ( $id, $command ) = $user =~ m{ ^ (.+) - ([^-]+) $ }ix;
die "Received mail addressed to [$user\@$domain] which contains no id" unless $id;
die "Received mail addressed to [$user\@$domain] which contains no command" unless $command;
return ( $configFile, $command, $id, $sender, $senderIp );
}
#-----------------------------------------------------------------------------
sub openSession {
my $webguiRoot = shift;
my $configFile = shift;
# Require WebGUI:Session rather than use it to save compilation of it when invalid commands are passed.
require WebGUI::Session;
my $session = WebGUI::Session->open( $webguiRoot, $configFile );
return $session;
}
#-----------------------------------------------------------------------------
sub closeSession {
my $session = shift;
$session->close;
}