// plugin_planned_shutdown.sma
// Version 1.1
//
// [BR5DY]
// thedude@br5dy.com
//
// Modified to fit idea
// Timing parts taken from plugin_method_time.sma by [geist.method] gideonv2@hotmail.com
// Shutdown parts taken from plugin_sank_shutdown2.sma by Kleen13
// Thanks to both of them!
// 
//
//---------------------------------------------------------------------------
//
//Sets a time for the server to shutdown with a shutdown sequence and notice to users.
//The shutdown time will be kept unless an admin deletes it.  So if you set it to shutdown at
//2:00 AM, it will shutdown at 2:00 AM everyday until you delete the shutdown time.
//
//===========================================================================================
//•Make sure that read/write access is allowed and vault file is set
//	- vault file should be: addons/adminmod/config/vault.ini
//	- if that file doesn't exit, make a blank document and save it as that
//
//•Access to all of these commands is granted by a user with access to the admin_rcon command
//	- can be changed in the script, but not recommended
//
//•ST is short for shutdown time - it makes it easier to remember
//===========================================================================================
//
//
//*COMMANDS:
//
// ----------------------------------------------------------------------------
// admin_st_status displays current shutdown time
// ----------------------------------------------------------------------------
//
// ----------------------------------------------------------------------------
// admin_st_set sets the shutdown time with these parameters:
// admin_st_set <hour> <minute>
// 
//	<hour> must be in 24 hour military time format, between 00-23 - 2 digits
// 	<minute> must be an actual minute, between 00-59 - 2 digits
// ----------------------------------------------------------------------------
//
// ----------------------------------------------------------------------------
// admin_st_delete deletes the shutdown time
// ----------------------------------------------------------------------------
//
// ----------------------------------------------------------------------------
// admin_st_now activates the shutdown sequence right then (given 30 second sequence)
// ----------------------------------------------------------------------------
//
// ----------------------------------------------------------------------------
// admin_st_abort aborts a shutdown process ONLY if the countdown has already started
// ----------------------------------------------------------------------------
//
//
//Good luck, have fun, and enjoy!
//
//[BR5DY]
//
//
 
#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
 
new STRING_VERSION[MAX_DATA_LENGTH] = "2.50.0";
new ST_Hour[MAX_DATA_LENGTH];
new ST_Minute[MAX_DATA_LENGTH];
new ST_AbortVar[MAX_DATA_LENGTH];
 
public strnsplit(a[],b[],n,m) {
 
	new i;
 
	for(i=n;i<=m;i++) 
		a[i-n]=b[i];
 
	return 1;
}
 
public admin_st_status()
{
	new Text[MAX_DATA_LENGTH];
	get_st();
 
	selfmessage("----------------------------");
	snprintf(Text, MAX_TEXT_LENGTH, "Shutdown Scheduled For: %s:%s ", ST_Hour, ST_Minute);
	selfmessage(Text);
	selfmessage("----------------------------");
 
	return PLUGIN_HANDLED;
}
 
public admin_st_set(HLCommand,HLData,HLUserName,UserIndex)
{
	new H[MAX_NUMBER_LENGTH];
	new M[MAX_NUMBER_LENGTH];
	new Command[MAX_COMMAND_LENGTH];
	new Data[MAX_DATA_LENGTH];
	new User[MAX_NAME_LENGTH];
	new TempN = 0;
	convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	convert_string(HLUserName,User,MAX_NAME_LENGTH);
 
	strgsep(Data, " ", "^"", H, MAX_NUMBER_LENGTH, M, MAX_NUMBER_LENGTH);
 
	TempN = strtonum(H);
	if ( (strlen(H)==0) || (strlen(H)==1) || (strlen(H) > 2) || (TempN > 23) || (TempN < 0) ) { 
		selfmessage( "Shtdown Time Error: Hour Required <00-23>");
		selfmessage( "Format: admin_st_set <hour> <minute>");
		return PLUGIN_HANDLED;
	}
	TempN = strtonum(M);
	if ( (strlen(M)==0) || (strlen(M)==1) || (strlen(H) > 2) || (TempN > 59) || (TempN < 0) ) {
		selfmessage( "Shutdown Time Error: Minute Required <00-59>");
		selfmessage( "Format: admin_st_set <hour> <minute>");
		return PLUGIN_HANDLED;
	}
 
	set_vaultdata("ST_HOUR", H);
	set_vaultdata("ST_MINUTE", M);
	selfmessage( "Shutdown Time Set.");
	return PLUGIN_HANDLED;
}
 
public admin_st_delete()
{
	set_vaultdata("ST_HOUR", "Unset");
	set_vaultdata("ST_MINUTE", "Unset");
	set_vaultdata("ST_R", "Unset");
	selfmessage( "Shutdown Time Deleted.");
	return PLUGIN_HANDLED;
}
 
public get_st()
{
	get_vaultdata("ST_Hour", ST_Hour, MAX_DATA_LENGTH);
	if(strlen(ST_Hour)==0) {
		set_vaultdata("ST_HOUR", "Unset");
		get_vaultdata("ST_HOUR", ST_Hour, MAX_DATA_LENGTH);
	}
	get_vaultdata("ST_Minute", ST_Minute, MAX_DATA_LENGTH);
	if(strlen(ST_Minute)==0) {
		set_vaultdata("ST_MINUTE", "Unset");
		get_vaultdata("ST_MINUTE", ST_Minute, MAX_DATA_LENGTH);
	}
	return PLUGIN_CONTINUE;
}
 
public st_check(Timer,Repeat,HLUser,HLParam) 
{
	new Holder[MAX_DATA_LENGTH];
	new Holder2[MAX_DATA_LENGTH];
 
	servertime(Holder, MAX_DATA_LENGTH, "%H");	
	servertime(Holder2, MAX_DATA_LENGTH, "%M");
 
	get_st();
	if(streq(Holder, ST_Hour)==1) {
		if(streq(Holder2, ST_Minute)==1) {
			//shutdown sequence
			admin_st_now();
		} 
	} 
	return PLUGIN_CONTINUE;
} 
 
public plugin_init() 
{
	plugin_registerinfo("Planned Shutdown Plugin", "Shuts down the server at a certain time.", STRING_VERSION);
	plugin_registercmd("say", "HandleSay", ACCESS_ALL);
	plugin_registercmd("admin_st_status", "admin_st_status", ACCESS_RCON, "admin_st_status: Shows the server's shutdown time.");
	plugin_registercmd("admin_st_set", "admin_st_set", ACCESS_RCON, "admin_st_set: Sets the server's shutdown time.");
	plugin_registercmd("admin_st_delete", "admin_st_delete", ACCESS_RCON, "admin_st_delete: Deletes the server's shutdown time.");
	plugin_registercmd("admin_st_now", "admin_st_now", ACCESS_RCON, "admin_st_now: Activates the shutdown sequence prematurely (30 second sequence)");
	plugin_registercmd("admin_st_abort", "admin_st_abort", ACCESS_RCON, "admin_st_abort: Deactivates the shutdown sequence");
	set_timer("st_check",60,99999);
	return PLUGIN_CONTINUE;
}
 
public admin_st_now()
{
	set_timer("kleen_shutdown", 1, 31);
	plugin_exec("admin_execall", "speak automatic shut down activated");
	plugin_exec("admin_ssay", "Thank you for playing...");
	plugin_exec("admin_ssay", "The server is shutting down...Please rejoin later.");
	return PLUGIN_HANDLED;
}
 
public admin_st_abort()
{
	set_vaultdata("ST_AbortVar", "1");
	return PLUGIN_HANDLED;
}
public kleen_shutdown(Timer, Repeat, HLUser, HLParam)
{
 
	if(get_vaultdata("ST_AbortVar", ST_AbortVar, MAX_DATA_LENGTH) == 0)
	{
		set_vaultdata("ST_AbortVar", "0");
		kill_timer(Timer);
		plugin_exec("admin_execall", "speak shut down deactivated");
		plugin_exec("admin_ssay", "The shutdown sequence has been deactivated.");
		return PLUGIN_HANDLED;
	}
 
	new Text[MAX_TEXT_LENGTH];
 
	snprintf(Text, MAX_TEXT_LENGTH, "Shutting down server in..^n %d", Repeat-1);
	plugin_exec("admin_csay", Text);
 
	if( Repeat-1 == 4 )
	{
		plugin_exec("admin_execall", "speak Goodbye");
	}
 
	if (Repeat-1 == 0)
	{
		kill_timer(Timer);
        	exec("quit");
	}
 
	return PLUGIN_HANDLED;
}