/********************************************************************************************************
* Created By: Will (aka Footy)                                                                          *
*																																																				*
* Date: 05/02/05                                                                                        *
*																																																				*
*	Commands: admin_reconnect <on/off> turn reconnection script on or off																	*
*																																																				*
* Versions: 1.0.0 - initial release																																			*
*						1.1.0 - changed: the way plugin checks to see if user can connect or not										*
*										removed: mapchange variable, now the plugin just clears g_File											*
*																																																				*
* Help: If you spot any errors, or need any help, or want something added, email me at:									*
*				william@lesberries.co.uk																																				*
*********************************************************************************************************/
 
 
#include <core>
#include <string>
#include <admin>
#include <adminlib>
 
#define MAX_IP_LENGTH 30
#define MAX_TIME_LENGTH MAX_TEXT_LENGTH - MAX_IP_LENGTH
 
// change to 0 to turn reconnection script off
new g_Reconnect = 1;
 
// you can change the path of the file if you like. note, must be relative to your <mod> directory
new g_File[] = "addons/adminmod/config/reconnect.txt";
 
// time in minutes before user can reconnect to server
new reconnection = 5;
 
new VERSION_STRING[] = "1.1.0";
 
new Players[MAX_PLAYERS][MAX_IP_LENGTH];
 
//------------------------------------------------------------------------------------
public admin_reconnect(HLCommand,HLData,HLUserName,UserIndex) {
	new Command[MAX_COMMAND_LENGTH];
	new Data[MAX_DATA_LENGTH];
	new User[MAX_NAME_LENGTH];
	new Text[MAX_TEXT_LENGTH];
	new toggle;
 
	convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	convert_string(HLUserName,User,MAX_NAME_LENGTH);
 
	strstripquotes(Data);
 
	toggle = check_param(Data);
 
	if (toggle == g_Reconnect) {
		snprintf(Text,MAX_TEXT_LENGTH,"Reconnection script is already %d. 1=On, 0=Off",toggle);
		selfmessage(Text);
	} else {
		snprintf(Text,MAX_TEXT_LENGTH,"Reconnection script is now %d. 1=On, 0=Off",toggle);
		g_Reconnect = toggle;
	}
 
	log_command(User,Command,Data);
 
	return PLUGIN_HANDLED;
}
//------------------------------------------------------------------------------------
public plugin_connect(HLUserName,HLIP,UserIndex) {
	new User[MAX_NAME_LENGTH];
	new IP[MAX_IP_LENGTH];
	new String[MAX_TEXT_LENGTH];
	new fileip[MAX_IP_LENGTH];
	new filestamp[MAX_TIME_LENGTH];
	new istamp;
	new timestamp = systemtime();
	new rectime = reconnection * 60;
	new i;
 
	convert_string(HLUserName,User,MAX_NAME_LENGTH);
	convert_string(HLIP,IP,MAX_IP_LENGTH);
 
	strcpy(Players[UserIndex],IP,MAX_IP_LENGTH);
 
	if (g_Reconnect) {
		if (fileexists(g_File)) {
			new num_lines = filesize(g_File);
			for (i=1;i<=num_lines;i++) {
				readfile(g_File,String,i,MAX_TEXT_LENGTH);
				strsplit(String,"-",fileip,MAX_IP_LENGTH,filestamp,MAX_TIME_LENGTH);
				istamp = strtonum(filestamp);
				if (strcmp(fileip,Players[UserIndex])==0) {
					if (timestamp < (istamp + rectime)) {
						kick(User);
					} else {
						writefile(g_File,"",i);
					}
				}
			}
		}
	}
 
	return PLUGIN_CONTINUE;
}
//------------------------------------------------------------------------------------
public plugin_disconnect(HLUserName,UserIndex) {
	new User[MAX_NAME_LENGTH];
	new Text[MAX_TEXT_LENGTH];
	new IP[MAX_IP_LENGTH];
	new timestamp = systemtime();
 
	convert_string(HLUserName,User,MAX_NAME_LENGTH);
	strcpy(IP,Players[UserIndex],MAX_IP_LENGTH);
 
	snprintf(Text,MAX_TEXT_LENGTH,"%s-%d",IP,timestamp);
	writefile(g_File,Text,-1);
 
	return PLUGIN_CONTINUE;
}
//------------------------------------------------------------------------------------
public plugin_init() {
	if(fileexists(g_File)) {
		resetfile(g_File);
	}
	new Text[MAX_TEXT_LENGTH];
 
	snprintf(Text,MAX_TEXT_LENGTH,"Stops users reconnecting to the server for %d minutes",reconnection);
	plugin_registerinfo("Reconnection Stopper",Text,VERSION_STRING);
	plugin_registercmd("admin_reconnect","admin_reconnect",ACCESS_BAN,"admin_reconnect <on/off> turns reconnect stopper on or off");
 
	return PLUGIN_CONTINUE;
}