/* $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 */

// HEAVILY modified by Belzebub !!

#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>

#define ACCESS_EDIT_REDIRECT 65535 /* Access level to edit Re-Direct settings */

new g_plugin;

new STRING_VERSION[MAX_DATA_LENGTH] = "2.50.0";
new LogFile[]="addons/Adminmod/logs/log_redirected.txt";

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 213.203.216.78:28015, Enjoy!", ServerName);
		messageex(Person, Text, print_console);
		snprintf(Cmd, MAX_DATA_LENGTH, "connect 213.203.216.78:28015", Data);
		execclient(Person, Cmd);
		snprintf(Text, MAX_TEXT_LENGTH, "Manual Player Redirect: %s Redirected <%s> to <213.203.216.78:28015>^n", User, Person);
		writefile(LogFile, Text);
	}
	return PLUGIN_HANDLED;
}

public admin_autoredirect(HLCommand,HLData,HLUserName,UserIndex) {
	new Data[MAX_DATA_LENGTH];
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	if(strlen(Data)){
		g_plugin=check_param(Data);
		set_vaultnumdata("Auto_Redirect",g_plugin);
	}	
	snprintf(Data,MAX_DATA_LENGTH,"[Auto-Redirect] Plugin is set to %i",g_plugin);
	selfmessage(Data);
	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);

	get_vaultnumdata("Auto_Redirect",g_plugin);
	if (g_plugin != 0) {
		snprintf(Text, MAX_TEXT_LENGTH, "You will be redirected to our Professional-Server now! ENJOY!");
		consgreet(Text);
		snprintf(Cmd, MAX_DATA_LENGTH, "connect 213.203.216.78:28015");
		execclient(Person, Cmd);
		snprintf(Text, MAX_TEXT_LENGTH, "Auto Redirect > <%s>", Person);
		writefile(LogFile, Text);
	} 
	else if (getvar("public_slots_free 1") == 1) {
		snprintf(Text, MAX_TEXT_LENGTH, "All Players will be redirected to our Prof. Server now! ENJOY !");
		selfmessage(Text);
		snprintf(Cmd, MAX_DATA_LENGTH, "connect 213.203.216.78:28015");
		execclient_all(Cmd);
		snprintf(Text, MAX_TEXT_LENGTH, "Multi Redirect > <%s>", Person);
		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_EDIT_REDIRECT,"admin_redirect <IP Address> : Sends a player to another server");
	plugin_registercmd("admin_autoredirect","admin_autoredirect",ACCESS_EDIT_REDIRECT,"admin_autoredirect <1/0> : Enables/Disables redirecting People at connect");
	if(!get_vaultnumdata("Auto_Redirect",g_plugin)){
		set_vaultnumdata("Auto_Redirect",g_plugin);
		g_plugin=0;
	}
	return PLUGIN_CONTINUE;
}
