/* antiname_flood*/
#include <string>
#include <admin>
#include <adminlib>
 
#define MAX_SECS 2
#define MAX_WARNING 5
 
new STRING_VERSION[MAX_TEXT_LENGTH]="0.8";
 
new g_LastTime[MAX_PLAYERS];
new g_Count[MAX_PLAYERS];
 
public plugin_init(){
	plugin_registerinfo("Anti-Name-Flood Plugin", "Autokicks flooders who change nicknames too often", STRING_VERSION);
	return PLUGIN_CONTINUE;
}
 
public plugin_connect(HLUserName, HLIP, UserIndex){
	g_LastTime[UserIndex]=0;
	g_Count[UserIndex]=0;
	return PLUGIN_CONTINUE;
}
 
public plugin_disconnect(HLUserName,UserIndex) {
	g_LastTime[UserIndex]=0;
	g_Count[UserIndex]=0;
	return PLUGIN_CONTINUE;
}
 
public plugin_info(HLOldName, HLNewName, UserIndex){
	new Text[MAX_TEXT_LENGTH];
	new OldName[MAX_NAME_LENGTH];
	new NewName[MAX_NAME_LENGTH];
	new AuthID[MAX_AUTHID_LENGTH];
	new Date[MAX_NUMBER_LENGTH];
	new IP[MAX_NUMBER_LENGTH];
	new curtime;
 
	convert_string(HLOldName,OldName,MAX_NAME_LENGTH);
	convert_string(HLNewName,NewName,MAX_NAME_LENGTH);
 
	//if user has no immunity, start checking
	if(!check_immunity(OldName)){
		if (strcmp(OldName,NewName)!=0){
			if(!g_LastTime[UserIndex]){
				g_LastTime[UserIndex]=systemtime();
				return PLUGIN_CONTINUE;
			}
 
			curtime = systemtime() - g_LastTime[UserIndex];
 
			if(curtime<MAX_SECS){
				g_Count[UserIndex]+=1;
				if(g_Count[UserIndex]<=MAX_WARNING){
					snprintf(Text,MAX_TEXT_LENGTH,"[ANTI-NAME-FLOOD] Stop flooding the server! Warning: %i of %i",g_Count[UserIndex],MAX_WARNING);
					messageex(OldName,Text,print_chat);
					messageex(OldName,Text,print_console);
				}else{
					get_userAuthID(OldName,AuthID,MAX_AUTHID_LENGTH);
					get_userIP(OldName,IP,MAX_NUMBER_LENGTH);
					servertime(Date, MAX_NUMBER_LENGTH, "%m/%d/%y %H:%M:%S");
					messageex(OldName, "[ANTI-NAME-FLOOD] You were warned, but you did not stop flooding the server.",print_console);
					snprintf(Text, MAX_TEXT_LENGTH, "%s - %s<%s><%s> was kicked using command ^"name^" too often", Date, OldName, IP, AuthID);
					writefile("FloodLog.txt", Text);
					log(Text);
					kick(OldName);
					return PLUGIN_HANDLED;
				}
 
			}
			g_LastTime[UserIndex]=systemtime();
		}
	}
	return PLUGIN_CONTINUE;
}