/*
 * $ plugin_cavey_kickvote.sma version 0.92                      $
 
 * $ For any info, go read the readme!                           $
 * $ Readme not included?  http://gosh.ex.ac.uk/~py99jan/        $
 
*/
 
#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
 
/*###############################################################
############################# OPTIONS ############################*/
 
/*If the map vote fails to kick the target, do you want it to kick
  the person who started it?  THe options are.....
  0 = Not at all
  1 = Only if the vote returned a NO (Recommended)
  2 = Yes, if it does not kick the target, kick the other guy     */
#define KICK_FAILURE 0
 
/*What level of admin access do you want to be alerted to the 
  presence of a user that has been kicked?                        */
#define ACCESS_ALERT ACCESS_CHAT
 
/*Set the access level of admins which can cancel a vote          *
 * Default is ACCESS_KICK, as they could kick them anyway......   */
#define ACCESS_CANCEL_VOTE ACCESS_KICK
 
/*########################## END OPTIONS ##########################
################################# ################################*/
 
 
/*                          DONT FIDDLE!                          *
 *             Dont come crying to me if you break it!            */
 
#define MAX_IPADDRESS 32
#define DELAY 30
 
new STRING_VERSION[MAX_DATA_LENGTH] = "2.50.11";
 
new UserIP[MAX_PLAYERS][MAX_IPADDRESS];
new PlayerTimer[MAX_PLAYERS];
 
new GlobalUser[MAX_NAME_LENGTH];
new VoteCanceled;
 
public admin_kick(HLCommand,HLData,HLUserName,UserIndex) {
		/*Input variables*/
	new Command[MAX_COMMAND_LENGTH];
	new Data[MAX_DATA_LENGTH];
	new User[MAX_NAME_LENGTH];
		/*Identify user variables*/
	new KickTarget[MAX_DATA_LENGTH];
	new TargetName[MAX_NAME_LENGTH];
	new TargetIndex;
		/*"Say" variables*/
	new Text[MAX_TEXT_LENGTH];
	new Reason[MAX_TEXT_LENGTH];
	new Identifier[MAX_IPADDRESS];
		/*File reading and writing*/
	new iBanType = bBanByID;
	new Date[MAX_DATA_LENGTH];
 
 	convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	convert_string(HLUserName,User,MAX_NAME_LENGTH);
	strbreak(Data,KickTarget,Reason, MAX_DATA_LENGTH);
 
	servertime(Date, MAX_DATA_LENGTH, "%H:%M on %B %d");
 
	if (getvar("sv_lan") == 1){
		iBanType = bBanByIP;
	}
	if (check_user(KickTarget)==1) {
		get_username(KickTarget,TargetName,MAX_NAME_LENGTH);
		say_command(User,Command,TargetName);
		if ( check_immunity(TargetName) == 1 ) {
			snprintf(Text, MAX_TEXT_LENGTH, "%s, you can't kick %s, you silly bear.", User, TargetName);
			say(Text);
		} else {
			get_userindex(TargetName, TargetIndex);
			if ( iBanType == bBanByIP ){
				strtok(UserIP[TargetIndex], ":", Identifier, MAX_IPADDRESS);
			} else if ( iBanType == bBanByID ){
				get_userAuthID(TargetName, Identifier);
			} 
 
			snprintf(Text, MAX_TEXT_LENGTH, "%s %s", Identifier, Date);
			writefile("kicked.ini",Text,-1);
 
			if (strlen(Reason) != 0) {
				snprintf(Text, MAX_TEXT_LENGTH, "You have been kicked because %s", Reason);
				message(KickTarget, Text);
			}
			kick(KickTarget);
		} 
	}else {
		selfmessage("Unrecognized player: ");
		selfmessage(KickTarget);
	}
	return PLUGIN_HANDLED;
}
 
public admin_cancel_vote(HLCommand,HLData,HLUserName,UserIndex) {
	new Text[MAX_TEXT_LENGTH];
 
	if ( VoteCanceled == 0){
		VoteCanceled=1;	
		snprintf(Text, MAX_TEXT_LENGTH, "Admin attempting to cancel kick vote");
		say(Text);
		return PLUGIN_HANDLED;
	}
	return PLUGIN_CONTINUE;
}
 
public admin_vote_kick(HLCommand,HLData,HLUserName,UserIndex) {
	new Command[MAX_COMMAND_LENGTH];
	new Data[MAX_DATA_LENGTH];
	new Target[MAX_DATA_LENGTH];
	new Text[MAX_TEXT_LENGTH];
	new User[MAX_NAME_LENGTH];
	new strAuthID[MAX_AUTHID_LENGTH];
	new Reason[MAX_TEXT_LENGTH];
 
	if (vote_allowed()!=1) {
		selfmessage( "Vote not allowed at this time.");
		return PLUGIN_HANDLED;
	}
	VoteCanceled = 0;
	convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	convert_string(HLUserName,User,MAX_NAME_LENGTH);
	convert_string(HLUserName,GlobalUser,MAX_NAME_LENGTH);
 
	strinit(Reason);
	if (check_user(Data) != 1){
		say("Reason");
		strbreak(Data,Target,Reason, MAX_DATA_LENGTH);
		strcpy(Data,Target,MAX_NAME_LENGTH);
	} 
	if (check_user(Data) == 1) {
		new real_user[MAX_NAME_LENGTH];
		get_username(Data,real_user,MAX_NAME_LENGTH);
		snprintf(Text, MAX_TEXT_LENGTH, "%s used admin_vote_kick against %s.", User, real_user);
		say(Text);
		if(check_immunity(real_user)!=0) {
			snprintf(Text, MAX_TEXT_LENGTH, "Laf. You can't kick %s, you silly bear.", real_user);
			say(Text);
		} else {
			snprintf(Text, MAX_TEXT_LENGTH, "Kick %s?", real_user);
			if (strlen(Reason) != 0){
				strcat(Text, "^n(For ",  MAX_TEXT_LENGTH);
				strcat(Text, Reason,  MAX_TEXT_LENGTH);
				strcat(Text, " )",  MAX_TEXT_LENGTH);
			}
			if(getvar("sv_lan")==1) {
				vote(Text,"Yes","No","HandleKickVote",real_user);
			} else {
				get_userAuthID(real_user,strAuthID);
				vote(Text,"Yes","No","HandleKickVote",strAuthID);
			}
		}
	} else {
		selfmessage("Unrecognized user name ");
		selfmessage(Data);
	}
	return PLUGIN_HANDLED;
}
 
public HandleKickVote(WinningOption,HLUser,VoteCount,UserCount) {
	new strNumber[MAX_NUMBER_LENGTH];
	new Text[MAX_TEXT_LENGTH];
	new VoteUser[MAX_DATA_LENGTH];
	convert_string(HLUser,VoteUser,MAX_DATA_LENGTH);
	new KickTarget[MAX_NAME_LENGTH];
 
	if ( VoteCanceled == 1){
		snprintf(Text, MAX_TEXT_LENGTH, "Vote to kick %s was cancelled by the admin.", VoteUser);
		say(Text);
		return PLUGIN_HANDLED;
	}
	if (WinningOption == 1) {
		new Ratio = getvar("kick_ratio");
		if (VoteCount >= Ratio*UserCount/100) {
			strcpy(KickTarget,VoteUser,MAX_NAME_LENGTH);
		} else {
			numtostr(Ratio*UserCount/100,strNumber);
			snprintf(Text, MAX_TEXT_LENGTH, "Kick vote succeeded, but not enough votes to kick %s (needed %s)", VoteUser, strNumber);
			say(Text);
			strinit(KickTarget);
#if KICK_FAILURE >= 2
			strcpy(KickTarget,GlobalUser,MAX_NAME_LENGTH);
#endif
		}
	} else {
		say("Kick vote failed.");
#if KICK_FAILURE >= 1
		strcpy(KickTarget,GlobalUser,MAX_NAME_LENGTH);
#endif
	}
 
	new Date[MAX_DATA_LENGTH];
	new iBanType = bBanByID;
	new Identifier[MAX_IPADDRESS];
	new TargetIndex;
 
	servertime(Date, MAX_DATA_LENGTH, "%H:%M on %B %d");
 
	if (getvar("sv_lan") == 1){
		iBanType = bBanByIP;
	}
	new TargetName[MAX_NAME_LENGTH];
 
	if (check_user(KickTarget)==1) {
		get_username(KickTarget,TargetName,MAX_NAME_LENGTH);
		if ( check_immunity(TargetName) == 1 ) {
			snprintf(Text, MAX_TEXT_LENGTH, "%s cannot be kicked by a vote, you silly bears.", KickTarget);
			say(Text);
		} else {
			get_userindex(TargetName, TargetIndex);
			if ( iBanType == bBanByIP ){
				strtok(UserIP[TargetIndex], ":", Identifier, MAX_IPADDRESS);
			} else if ( iBanType == bBanByID ){
				get_userAuthID(TargetName, Identifier);
			} 
 
			snprintf(Text, MAX_TEXT_LENGTH, "%s %s", Identifier, Date);
			writefile("kicked.ini",Text,-1);
 
			snprintf(Text, MAX_TEXT_LENGTH, "You have been kicked from the results of a vote");
			message(KickTarget, Text);
			kick(KickTarget);
		} 
	}
	VoteCanceled = 1;
	return PLUGIN_HANDLED;
}
 
 
 
public plugin_connect(HLUserName, HLIP, UserIndex) {
	if (UserIndex >= 1 && UserIndex <= MAX_PLAYERS) {
		strinit(UserIP[UserIndex]);
		convert_string(HLIP, UserIP[UserIndex], MAX_IPADDRESS);
	}
	if (getvar("sv_lan") == 1){
		new FileSize;
		new Temp[MAX_DATA_LENGTH];
		new Count;
		new IP[MAX_IPADDRESS];
		new Text[MAX_TEXT_LENGTH];
 
		FileSize = filesize("kicked.ini");
		strtok(UserIP[UserIndex], ":", IP, MAX_IPADDRESS);
 
		for(Count=1;Count<=FileSize;Count++) {
			readfile("kicked.ini",Temp,Count,MAX_DATA_LENGTH);
			if ( (strcasestr(Temp,IP) != -1) ) {
				new i;
				new maxplayers = maxplayercount();
				new Name[MAX_NAME_LENGTH];
				new User[MAX_NAME_LENGTH];
				new Date[MAX_DATA_LENGTH];
				convert_string(HLUserName,User,MAX_NAME_LENGTH);
				strbreak(Temp,Name,Date, MAX_DATA_LENGTH);
				snprintf(Text, MAX_TEXT_LENGTH, "%s(%s) was kicked at %s", User,IP,Date);
				for(i=1; i<=maxplayers; i++) {
					strinit(Name);
					if(playerinfo(i,Name,MAX_NAME_LENGTH)==1) {
						if(access(ACCESS_ALERT,Name)!=0) {
							messageex(Name, Text, print_chat);
						}
					}
				}
			}
		}
	} else {
		if ( PlayerTimer[UserIndex]!=0 ){
			kill_timer(PlayerTimer[UserIndex]);
		}
		new Text[MAX_TEXT_LENGTH];
		snprintf(Text, MAX_TEXT_LENGTH, "%d", UserIndex);
		PlayerTimer[UserIndex] = set_timer("cavey_connect", DELAY, 1, Text);
	}
	return PLUGIN_CONTINUE;
}
 
public cavey_connect(Timer,Repeat,HLUser,HLParam){
	new User[MAX_NAME_LENGTH];
	new UserIndex;
	new Data[MAX_DATA_LENGTH];
	new sAuthID[MAX_AUTHID_LENGTH];
 
	convert_string(HLParam,Data,MAX_NAME_LENGTH);
	UserIndex = strtonum(Data);
	playerinfo(UserIndex,User,MAX_NAME_LENGTH);
 
	get_userAuthID(User,sAuthID);
	new FileSize = filesize("kicked.ini");
	for( new Count=1 ; Count<=FileSize ; Count++ ) {
		new Temp[MAX_TEXT_LENGTH];
		new Date[MAX_DATA_LENGTH];
		new FileID[MAX_DATA_LENGTH];
		readfile("kicked.ini",Temp,Count,MAX_NAME_LENGTH);
		strbreak(Temp,FileID,Date, MAX_DATA_LENGTH);
		if ( (strcasestr(FileID,sAuthID) != -1) ) {
			new maxplayers = maxplayercount();
			new j;
			new Text[MAX_TEXT_LENGTH];
			new Name[MAX_NAME_LENGTH];
			snprintf(Text,MAX_TEXT_LENGTH,"%s(%s) has been recently kicked. (%s)",User,sAuthID,Date);
			for (j=1; j<=maxplayers; j++) {
				strinit(Name);
				if (playerinfo(j,Name,MAX_NAME_LENGTH)==1) {
					if (access(ACCESS_ALERT,Name)!=0) {
						messageex(Name, Text, print_chat);
					}
				}
			}
		}
	}	
	return PLUGIN_CONTINUE;
}
 
public plugin_disconnect(HLUserName, UserIndex) {
	if (UserIndex >= 1 && UserIndex <= MAX_PLAYERS) {
		strinit(UserIP[UserIndex]);
	}
	return PLUGIN_CONTINUE;
}
 
 
public plugin_init() {
	plugin_registerinfo("Caveys Admin Warn Plugin","Command for warning TKers sequentially.",STRING_VERSION);
 
	plugin_registercmd("admin_kick","admin_kick",ACCESS_KICK,"admin_kick <target> [reason]: Kicks the <target> with optional parting [reason].");
	plugin_registercmd("admin_vote_kick","admin_vote_kick",ACCESS_VOTE_KICK,"admin_vote_kick <target> [reason]: Starts a vote to kick the <target> with optional [reason].");
	plugin_registercmd("admin_cancel_vote","admin_cancel_vote",ACCESS_CANCEL_VOTE,"admin_cancel_vote : Allows an admin to cancel a vote.");
 
 
	return PLUGIN_CONTINUE;
}