/* MODEL_CHECK for HLDM
* Idea by GSMACK.
* (c) Sir Drink a lot.
*
* Searches autmatically for restricted models set in models2.ini with
* modelname:STEAM_ID.
* Setting up models.ini is not needed anymore.
*
*/
 
#include <console>
#include <string>
#include <admin>
#include <adminlib>
 
new STRING_VERSION[MAX_DATA_LENGTH] = "v1";
 
//File, where combination of modelname:STEAM_ID is set
new g_FileName[MAX_TEXT_LENGTH]="addons/adminmod/config/models2.ini";
 
//Models loaded from models2.ini"
#define MAX_MODELS	30
new g_RestrictedModel[MAX_MODELS][MAX_DATA_LENGTH];
new g_loaded_models;
new g_checktime=15; //in seconds
 
 
public plugin_init() {
	plugin_registerinfo("MODEL_CHECK PLUGIN","Enables changing a player's model /checking for restricted models during game play.",STRING_VERSION);
	plugin_registercmd("change_model","ChangeModel",ACCESS_CONFIG,"change_model [TargetName]: changes a users model.");
	plugin_registercmd("specmode","specmode",ACCESS_ALL);
	load_modelini();
	return PLUGIN_CONTINUE;
}
 
//Check Client on entering game
public specmode(HLCommand,HLData,HLUserName,UserIndex) {
	check_models(UserIndex);
	return PLUGIN_CONTINUE;
}
 
load_modelini(){
	new Data[MAX_DATA_LENGTH];
	new i;
	new iFileSize;
	if(fileexists(g_FileName)){
		iFileSize=filesize(g_FileName);
		for(i=1;i<=iFileSize;i++){
			readfile(g_FileName,Data,i,MAX_DATA_LENGTH);
			//line is no comment
			if(Data[0]!='#' && Data[0]!='/' && Data[0]!=';'){
				strcpy(g_RestrictedModel[g_loaded_models],Data,MAX_DATA_LENGTH);
				selfmessage(g_RestrictedModel[g_loaded_models]);
				g_loaded_models++;
				if(g_loaded_models>MAX_MODELS){
					selfmessage("[MODELS_CHECK] Too many entries in models File. Please increase MAX_MODELS in sourcecode!");
					break;
				}
			}
		}
		snprintf(Data,MAX_DATA_LENGTH,"[ADMIN] [MODELS_CHECK] %i restricted models loaded!",g_loaded_models);
		set_timer("CheckUserModels",g_checktime,99999);
		g_loaded_models--;
	}else{
		selfmessage("[MODEL CHECK] No models file found! Please create one addons/adminmod/config");
	}
}
 
 
//Manually check for target
public ChangeModel(HLCommand,HLData,HLUserName,UserIndex) {
	new Data[MAX_DATA_LENGTH];
	new Target[MAX_NAME_LENGTH];
	new iUID;
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	get_username(Data,Target,MAX_NAME_LENGTH);
	get_userindex(Target,iUID);
	check_models(iUID);
	return PLUGIN_HANDLED;
}
 
//automatic timer check
public CheckUserModels(Timer,Repeat,HLName,HLParam) {
	check_models(0);
}
 
check_models(UserIndex){
	new Name[MAX_NAME_LENGTH];
	//if check is invoked by time, every player has to be checked
	if(UserIndex==0){
		new i;
		new c=maxplayercount();
		for (i=1;i<=c;i++) {
			if (playerinfo(i,Name,MAX_NAME_LENGTH)) {
				check_models2(Name);
			}
		}
	//else check only the player given by command "change_model"
	}else{
		if (playerinfo(UserIndex,Name,MAX_NAME_LENGTH)) {
			check_models2(Name);
		}
	}
}
 
check_models2(Name[]){
	new Text[MAX_TEXT_LENGTH];
	new vModel[MAX_NAME_LENGTH];
	new vAuthID[MAX_AUTHID_LENGTH];
	new sModel[MAX_NAME_LENGTH];
	new sAuthID[MAX_AUTHID_LENGTH];
	new i;
	new iallowed=1;
	//return player model
	get_userinfo(Name,"model",vModel,MAX_NAME_LENGTH);
	get_userAuthID(Name,vAuthID,MAX_AUTHID_LENGTH);
	//eval player model
	for(i=0;i<=g_loaded_models;i++){
		strsep(g_RestrictedModel[i],":",sModel,MAX_NAME_LENGTH,sAuthID,MAX_AUTHID_LENGTH);
		if(strcasecmp(vModel,sModel)==0){
			if(strcmp(vAuthID,sAuthID)==0){
				iallowed=1;
				break;
			}else{
				iallowed=0;
			}
		}
	}	
	if(!iallowed){
		//send msg to player
		snprintf(Text,MAX_TEXT_LENGTH,"[MODELS_CHECK] You are using a restricted ^"%s^" Clan Skin/Model",vModel);
		messageex(Name,Text,print_chat);
		//change player model
		snprintf(Text,MAX_TEXT_LENGTH,"model ^"gordon^"");
		execclient(Name,Text);
		snprintf(Text,MAX_TEXT_LENGTH,"setinfo ^"model^" ^"gordon^"");
		execclient(Name,Text);
		//send msg to player
		snprintf(Text,MAX_TEXT_LENGTH,"[MODELS_CHECK] Your skin/model has been changed.");
		messageex(Name,Text,print_chat);
	}
}