use strict;
use Net::FTP;
package FB::myFTP;
my @servers = ( "151.1.152.11", "151.1.152.15");
my $myftpuser;
my $myftpcode;
my $retries=20;
my $ftp;
my $connection;
my $mode=1; ## 1 is ascii 2 is binary
my $ftplog="";
sub new
{
my $self = {};
bless($self);
my $class = shift;
return $self;
}
sub DESTROY
{
my $self = shift;
$self->logoff();
}
sub getAuthorization
{
my $self = shift;
my ($s) = @_;
my $ftpauth;
$ftpauth = FB::Authorize->new($s);
if (! $ftpauth)
{
return undef;
}
$myftpuser = $ftpauth->getUser();
$myftpcode = $ftpauth->getPasswd();
}
sub setFtpLog
{
#####################################################
# Purpose: Method to set ftp log class
#####################################################
# Accept Parameters
#
my $self = shift;
my ($r) = @_;
$ftplog=$r;
return "OK";
}
sub setRetries
{
#####################################################
# Purpose: Method to set retries
#####################################################
# Accept Parameters
#
my $self = shift;
my ($r) = @_;
$retries=$r;
return "OK";
}
sub setModeBinary
{
#####################################################
# Purpose: Method to set Mode Binary
#####################################################
# Accept Parameters
#
my $self = shift;
$mode=2;
return "OK";
}
sub setModeAscii
{
#####################################################
# Purpose: Method to set Mode Ascii
#####################################################
# Accept Parameters
#
my $self = shift;
$mode=1;
return "OK";
}
sub setServers
{
#####################################################
# Purpose: Method to set servers
#####################################################
# Accept Parameters
#
my $self = shift;
my (@r) = @_;
@servers=@r;
return "OK";
}
sub login
{
my $self = shift;
$self->getAuthorization($connection);
$ftp->login($myftpuser,$myftpcode)|| return ("Error Logging On: $!");
if ($mode == 1)
{
$ftp->ascii || return ("Error setting ascii mode: $!");
}
else
{
$ftp->binary || return ("Error setting binary mode: $!");
}
return "OK";
}
sub logoff
{
my $rc;
my $self = shift;
if ($ftp)
{
$rc=$ftp->quit;
}
undef $ftp;
}
sub connect
{
my $self = shift;
my $tryAgain = 1;
while($retries)
{
foreach my $s (@servers)
{
if ($ftp = Net::FTP->new($s))
{
$connection = $s; ## save the server success connect
return "OK";
}
}
$retries--;
sleep 120;
}
return "Connection error to Server";
}
sub setDirectory
{
#####################################################
# Purpose: Method to set directory
#####################################################
# Accept Parameters
#
my $self = shift;
my ($dir) = @_;
my $rc;
if (!$ftp)
{
$rc=$self->connect();
$rc=$self->login();
}
$ftp->cwd($dir)|| return("Failed to cd to $dir: $!");
return "OK";
}
sub MakeDirectory
{
#####################################################
# Purpose: Method to make directory
#####################################################
# Accept Parameters
#
my $self = shift;
my ($dir) = @_;
my $rc;
if (!$ftp)
{
$rc=$self->connect();
$rc=$self->login();
}
$ftp->mkdir($dir)|| return("Failed to make $dir: $!");
return "OK";
}
sub readDirectory
{
#####################################################
# Purpose: Method to list directory
#####################################################
# Accept Parameters
#
my $self = shift;
my ($dir) = @_;
my $rc;
my @list;
my $name;
my @rlist;
if (!$ftp)
{
$rc=$self->connect();
$rc=$self->login();
}
if ($dir)
{
@list=$ftp->dir($dir)|| return("Failed to list directory: $!");
}
else
{
@list=$ftp->dir|| return("Failed to list directory: $!");
}
return @{$list[0]};
}
sub sendFile
{
#####################################################
# Purpose: Method to send the file
#####################################################
# Accept Parameters
#
my $self = shift;
my ($fi, $fo) = @_;
my $rc;
if (!$ftp)
{
$rc=$self->connect();
$rc=$self->login();
}
if ($fo)
{
$ftp->put($fi, $fo)|| return("Failed to send $fi FTP: $!");
}
else
{
$ftp->put($fi)|| return("Failed to send $fi FTP: $!");
}
return "OK";
}
sub getFile
{
#####################################################
# Purpose: Method to get a file
#####################################################
# Accept Parameters
#
my $self = shift;
my ($fi, $fo) = @_;
my $rc;
if (!$ftp)
{
$rc=$self->connect();
$rc=$self->login();
}
if ($fo)
{
$ftp->get($fi, $fo)|| return("Failed to get $fi FTP: $!");
}
else
{
$ftp->get($fi)|| return("Failed to get $fi FTP: $!");
}
return "OK";
}
return 1;