/* $ID plugin_sharp_redirect.sma by: Sharpsniper ID$ */
 
/* This plugin will automaticly ReDirect players to another server of your choice, just edit the config file
 * Included and set your ReDirected server, this is good if you and your friend are both running a server
 * And yours is full, then send the players to your friends and everyone wins :) and everyone gets to play
 * I like this one better than the old one, because this one has a config file and it redirects everyone unless
 * The server isnt full */
 
#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
 
#define ACCESS_EDIT_REDIRECT 8192 /* Access level to edit Re-Direct settings */
 
// Allow Editing of the ReDirect Settings?
// 1  = Allows Editing
// 0 = Disallows editing
new Allow_Edit = 1;
 
// Redirect Server
// Enter the server IP adress here: 
new ReDirectAdress[MAX_DATA_LENGTH] = "142.59.38.29";
 
// Only Redirect if server is full?
// 1 = Redirects ONLY if the server is full
// 0 = Redirects everyone no matter what
new Full_Redirect = 1;
 
new STRING_VERSION[MAX_DATA_LENGTH] = "2.50.0";
new ConfigFile[]="redirect.cfg";
new LogFile[]="redirected people.txt";
 
public LoadConfigFile() {
 
	new ChangedAccess = 0;
	new ConfigFileSize;
	new LineBuffer[MAX_TEXT_LENGTH];	
	new Data[MAX_TEXT_LENGTH];	
	new NullText[MAX_TEXT_LENGTH];	
 
	if (getvar("file_access_read") == 0) {
		ChangedAccess = 1;
		exec("file_access_read 1");
	}
 
	if (fileexists(ConfigFile) == 0) {
		return PLUGIN_HANDLED;
	}
 
	ConfigFileSize = filesize(ConfigFile);
 
	for (new LineNum = 0; LineNum < ConfigFileSize; LineNum++) {
		readfile(ConfigFile, LineBuffer, LineNum, MAX_TEXT_LENGTH);
		if ((strncmp(LineBuffer, "//", 2) != 0) && (strlen(LineBuffer) != 0)) {
 
			Allow_Edit = CheckVariable("Allow_Edit ", LineBuffer, Allow_Edit);
			Full_Redirect = CheckVariable("Full_Redirect ", LineBuffer, Full_Redirect);
 
			if (strncmp(LineBuffer, "ReDirectAdress ", strlen("ReDirectAdress ")) == 0) {
				strbreak(LineBuffer, NullText, Data, MAX_DATA_LENGTH);
				strcpy(ReDirectAdress, Data, MAX_DATA_LENGTH);
			}
		}
	}
 
	if (ChangedAccess == 1) {
		exec("file_access_read 0");
	}
 
	log("Config File Loaded.");
 
	return PLUGIN_HANDLED;
}
 
public CheckVariable( VarName [ ], LineBuffer [ ], CurrentValue ) {
 
	new VarValue = CurrentValue;	
	if (strncmp(LineBuffer, VarName, strlen(VarName)) == 0) {
		strtrim(LineBuffer, VarName, 0);
		VarValue = strtonum(LineBuffer);
	}
	return VarValue;
}
 
public admin_redirect(HLCommand,HLData,HLUser,UserIndex) {
	new Command[MAX_COMMAND_LENGTH];
	new Data[MAX_DATA_LENGTH];
	new User[MAX_NAME_LENGTH];
	new Text[MAX_TEXT_LENGTH];
	new ServerName[MAX_TEXT_LENGTH];
	new Cmd[MAX_DATA_LENGTH];
	new Person[MAX_NAME_LENGTH];
	getstrvar("hostname", ServerName, MAX_TEXT_LENGTH);
 
	convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	convert_string(HLUser,User,MAX_NAME_LENGTH);
 
	if (check_user("Person") == 0) {
		snprintf(Text, MAX_TEXT_LENGTH, "Sorry, %s doesnt exist.", Person);
		selfmessage(Text);
	} else {
		snprintf(Text, MAX_TEXT_LENGTH, "<%s>: You are being redirected to %i, Enjoy!", ServerName, Data);
		messageex(Person, Text, print_console);
		snprintf(Cmd, MAX_DATA_LENGTH, "connect %i", Data);
		execclient(Person, Cmd);
		snprintf(Text, MAX_TEXT_LENGTH, "Manual Player Redirect: %s Redirected <%s> to <%i>^n", User, Person, Data);
		writefile(LogFile, Text);
	}
	return PLUGIN_HANDLED;
}
 
public plugin_connect(HLUserName,HLIP,UserIndex) {
	new Person[MAX_NAME_LENGTH];
	new Text[MAX_TEXT_LENGTH];
	new Cmd[MAX_DATA_LENGTH];
	new ServerName[MAX_TEXT_LENGTH];
	getstrvar("hostname", ServerName, MAX_TEXT_LENGTH);
 
	convert_string(HLUserName, Person, MAX_NAME_LENGTH);
 
	if (getvar("public_slots_free 0") == 1) {
		snprintf(Text, MAX_TEXT_LENGTH, "Sorry, %s but <%s> is full...^n^nYou are being ReDirected to %i.", Person, ServerName, ReDirectAdress);
		consgreet(Text);
		snprintf(Cmd, MAX_DATA_LENGTH, "connect %i", ReDirectAdress);
		execclient(Person, Cmd);
		snprintf(Text, MAX_TEXT_LENGTH, "Auto Player Redirect: Redirected <%s> to <%i>^n", Person, ReDirectAdress);
		writefile(LogFile, Text);
	} else {
		snprintf(Text, MAX_TEXT_LENGTH, "Server is not full, finishing connection... ^n^nWelcome to <%s> and enjoy your stay!", ServerName);
		consgreet(Text);
	}
	return PLUGIN_CONTINUE;
}
 
	public plugin_init() {
	plugin_registerinfo("Sharp's ReDirect Script","If the server is full it will send them to another server.",STRING_VERSION);
	plugin_registercmd("admin_redirect","admin_redirect",ACCESS_RCON,"admin_redirect <IP Adress> : Sends a player to another server");
 
	return PLUGIN_CONTINUE;
}