| Current Path : /home/hotlineuser/HotlineManagement/ |
| Current File : //home/hotlineuser/HotlineManagement/hmc |
#!/usr/bin/perl
use strict;
use warnings;
use UI::Dialog;
use Term::ReadKey;
use Term::ANSIScreen qw(cls);
use Proc::ProcessTable;
use POSIX qw(strftime);
use Number::Bytes::Human qw(format_bytes parse_bytes);
# Commands
my $FileEditor = "/bin/nano";
my $InitDName = "/home/hotlineuser/bin/hotline";
my $HOTLINEDIR = "/home/hotlineuser/HotlineFiles";
my $BackupCommand = "/home/hotlineuser/HotlineBackup/hotlinebackup.pl -snapshot";
my $PagerCommand = "/usr/bin/less";
my $UpdateCommand = "/usr/bin/git pull";
my $HMCDir = "/home/hotlineuser/HotlineManagement";
my $DownloadEmail = "$HMCDir/DownloadedFiles";
my $BackupToolDir = "/home/hotlineuser/HotlineBackup";
my $StartupDir = "/home/hotlineuser/HotlineStartup";
my $DEBUG_MODE = "off";
# Data Files
my $AgreementTXT = "$HOTLINEDIR/Agreement.txt";
my $ConfigYaml = "$HOTLINEDIR/config.yaml";
my $MessageBoardTXT = "$HOTLINEDIR/MessageBoard.txt";
###################################################
# No changes below here
###################################################
my $MySettings = "$ENV{'HOME'}/.hmcmcrc";
my $HMC_ver = "1.3";
my $Record = "false"; # Are results saved?
my $TempDir = "/tmp";
my $RobotName = "";
my $BotVersion = "";
my $UserName = "";
my $ServerStatus = "foo";
sub PrintDebugCommand
{
if ($DEBUG_MODE eq "off")
{
return;
}
my $PassedString = shift;
print "About to run:\n$PassedString\n";
print "Press Enter To Run This:";
my $entered = <STDIN>;
}
my $d = new UI::Dialog ( backtitle => "Hotline Management Console v$HMC_ver", height => 20, width => 65, listheight => 5,
order => [ 'ascii', 'cdialog', 'xdialog' ]);
my $windowtitle = "Welcome to the Hotline Management Console!";
my $enjoyedtitle = "We hope you enjoyed HMC!";
my $introtext =
"This is the Hotline Management Console, a utility for Hotline operators
to manage their servers from a text UI rather than the command line.";
$d->msgbox( title => $windowtitle, text => $introtext );
if (($d->state() eq "ESC") || ($d->state() eq "CANCEL"))
{
exit 0;
}
# Check for config file
if (-f $MySettings)
{
# Read in settings
open (my $FH, "<", $MySettings) or die "Could not read default file '$MySettings' $!";
while (<$FH>)
{
chop();
my ($Command, $Setting) = split(/=/, $_);
if ($Command eq "fileeditor")
{
$FileEditor = $Setting;
}
if ($Command eq "initdname")
{
$InitDName = $Setting;
}
if ($Command eq "hotlinedir")
{
$HOTLINEDIR = $Setting;
}
if ($Command eq "backupcommand")
{
$BackupCommand = $Setting;
}
if ($Command eq "pagercommand")
{
$PagerCommand = $Setting;
}
if ($Command eq "agreementtxt")
{
$AgreementTXT = $Setting;
}
if ($Command eq "messageboardtxt")
{
$MessageBoardTXT = $Setting;
}
if ($Command eq "configyaml")
{
$ConfigYaml = $Setting;
}
}
close($FH);
}
else
{
# Store defaults
open (my $FH, ">", $MySettings) or die "Could not create default file '$MySettings' $!";
print $FH "fileeditor=/bin/nano\n";
print $FH "initdname=/home/hotlineuser/bin/hotline\n";
print $FH "hotlinedir=/home/hotlineuser/HotlineFiles\n";
print $FH "backupcommand=/home/hotlineuser/HotlineBackup/hotlinebackup.pl\n";
print $FH "pagercommand=/usr/bin/less\n";
print $FH "configyaml=$HOTLINEDIR/config.yaml\n";
print $FH "agreementtxt=$HOTLINEDIR/Agreement.txt\n";
print $FH "messageboardtxt=$HOTLINEDIR/MessageBoard.txt\n";
close($FH);
}
my $menuselection = "";
sub CheckServerStatus
{
my $running=`ps ax|grep mobius-hotline-server|grep -v grep`;
if ($running ne "")
{
$ServerStatus = "Running";
}
else
{
$ServerStatus = "Stopped";
}
}
sub UpdateToolsMenu
{
my $WantRespawn="ON";
CheckServerStatus();
if (-f "$HOTLINEDIR/nostart")
{
$WantRespawn="OFF";
}
$menuselection = $d->menu( title => "Update Tools Menu", text => "Server is $ServerStatus and respawn is $WantRespawn - Select one:",
list => [ '1', 'Update HMC',
'2', 'Update Backup',
'3', 'Update Startup',
'q', 'Main Menu' ] );
}
sub DoUpdate
{
my $DesiredDir = $_[0];
# Update desired utility
chdir ($DesiredDir);
PrintDebugCommand("Running $UpdateCommand in $DesiredDir\n");
system("$UpdateCommand");
print "Press Enter To Continue";
my $entered = <STDIN>;
}
sub UpdateTools
{
while (-1)
{
UpdateToolsMenu();
if (($menuselection eq "CANCEL") || ($menuselection eq "ESC") || ($menuselection eq "") || ($menuselection eq "q") || ($menuselection eq "Q"))
{
return;
}
elsif ($menuselection eq "1")
{
# Update HMC
DoUpdate($HMCDir);
}
elsif ($menuselection eq "2")
{
# Update BackupToolDir
DoUpdate($BackupToolDir);
}
elsif ($menuselection eq "3")
{
# Update Startup
DoUpdate($StartupDir);
}
}
}
sub MainMenu
{
my $WantRespawn="ON";
CheckServerStatus();
if (-f "$HOTLINEDIR/nostart")
{
$WantRespawn="OFF";
}
$menuselection = $d->menu( title => "Main Menu", text => "Server is $ServerStatus and respawn is $WantRespawn - Select one:",
list => [ '1', 'Start Server',
'2', 'Stop Server',
'3', 'Server Console',
'4', 'Turn Off Respawn',
'5', 'Turn Respawn On',
'6', 'Edit config.yaml',
'7', 'Edit Agreement.txt',
'8', 'Edit MessageBoard',
'9', 'Run Backup',
'10', 'Run eMail Notice',
'11', 'Edit Settings',
'12', 'Restart Hotline',
'13', 'Update Tools',
'q', 'Quit HMC' ] );
}
while (-1)
{
MainMenu();
if (($menuselection eq "CANCEL") || ($menuselection eq "ESC") || ($menuselection eq "") || ($menuselection eq "q") || ($menuselection eq "Q"))
{
$d->msgbox( title => $enjoyedtitle, text => "Thanks for using HMC..." );
exit 0;
}
if ($menuselection eq "1")
{
system("$InitDName start");
}
elsif ($menuselection eq "2")
{
if ($d->yesno( text => "Confirm stopping the server", text => "Are you sure you want to shut down the server?" ))
{
system("$InitDName stop");
sleep(5);
system("killall mobius-hotline-server");
}
}
elsif ($menuselection eq "3")
{
$d->msgbox( text => "To exit the Hotline console and return to HMC press CTRL-A CTRL-D" );
system("screen -r Hotline");
}
elsif ($menuselection eq "4")
{
# Turn off respawn
system("touch $HOTLINEDIR/nostart");
}
elsif ($menuselection eq "5")
{
# Turn respawn back on
if (-f "$HOTLINEDIR/nostart")
{
unlink("$HOTLINEDIR/nostart");
}
}
elsif ($menuselection eq "6")
{
# Edit default.cfg file
system("$FileEditor $ConfigYaml");
}
elsif ($menuselection eq "7")
{
# Edit Agreement.txt file
system("$FileEditor $AgreementTXT");
}
elsif ($menuselection eq "8")
{
# Edit MessageBoard.txt file
system("$FileEditor $MessageBoardTXT");
}
elsif ($menuselection eq "9")
{
# Run a backup
system("clear");
system("$BackupCommand");
print "Press Enter To Continue";
my $entered = <STDIN>;
}
elsif ($menuselection eq "10")
{
# Edit settings file
system("clear");
system("$DownloadEmail");
print "Press Enter To Continue";
my $entered = <STDIN>;
}
elsif ($menuselection eq "11")
{
# Run the download settings email
system("$FileEditor $MySettings");
}
elsif ($menuselection eq "12")
{
# Reset server process
print "Killing Hotline Server Process\n";
system("killall mobius-hotline-server");
print "Process killed...";
sleep(5);
}
elsif ($menuselection eq "13")
{
# Update tools menu
UpdateTools();
}
}
exit 0;