/*          plugin_cavey_callvote.sma version 0.18, 
     By Caveman (ICQ# 70710878) and SR71Goku (ICQ# 146538715)
                    Please read the readme                          */
 
 
 
#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
 
#define ACCESS_CANCEL ACCESS_KICK
#define ACCESS_CALLVOTE ACCESS_KICK
#define ACCESS_UNRESTRICT ACCESS_RCON
 
new STRING_VERSION[MAX_DATA_LENGTH] = "2.50.11";
new flag;
 
public plugin_init() {
 
	plugin_registerinfo("Cavey's callvote","Allows users to call votes",STRING_VERSION);
 
	plugin_registercmd("callvote","Callvote",ACCESS_CALLVOTE,"callvote <command>: Calls a vote over command and executes it if it wins");
	plugin_registercmd("admin_callvote","Callvote",ACCESS_CALLVOTE,"");		/*No comment to make it "invisible"*/
	plugin_registercmd("admin_cancel_vote","admin_cancel_vote",ACCESS_CANCEL,"admin_cancel_vote: Allows admins to cancel the vote");
 
	return PLUGIN_CONTINUE;
}
 
 
public admin_cancel_vote(HLCommand,HLData,HLUserName,UserIndex) {
	new Text[MAX_TEXT_LENGTH];
 
	flag=1;	
	snprintf(Text, MAX_TEXT_LENGTH, "[VOTE] Admin attempting to cancel vote");
	say(Text);
	return PLUGIN_HANDLED;
}
 
 
public Callvote(HLCommand, HLData, HLUserName, UserIndex){
	new UserName[MAX_NAME_LENGTH];
	new Data[MAX_DATA_LENGTH];
	new Command[MAX_COMMAND_LENGTH];
	new text[MAX_DATA_LENGTH];
 
	convert_string(HLUserName, UserName, MAX_NAME_LENGTH);
	convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
	convert_string(HLData,Data,MAX_DATA_LENGTH);
 
	/*Filter for commands that you should not allow to run, eg anything to do with rcon*/
	if ( (strcasestr(Data, "rcon") != -1) ) {
		snprintf(text, MAX_TEXT_LENGTH, "[VOTE] %s attempted to screw with the server!", UserName);
		say(text);
		snprintf(text, MAX_TEXT_LENGTH, "[VOTE] %s, you may not callvote over ANYTHING involving rcon!", UserName);
		selfmessage(text);
		kick(UserName);
		return PLUGIN_HANDLED;
	}
 
	/*NOONE will ever want to kill the server, if they have the right to do this, they will have RCON*/
	if ( ( (strcasestr(Data, "exit")!= -1) || (strcasestr(Data, "quit")!= -1) || (strcasestr(Data, "killserver")!= -1) ) ) {
		snprintf(text, MAX_TEXT_LENGTH, "[VOTE] %s decided he wanted to quit!", UserName);
		say(text);
		snprintf(text, MAX_TEXT_LENGTH, "[VOTE] %s, you may not callvote over ANYTHING which will kill the server!", UserName);
		selfmessage(text);
		execclient(UserName,"quit");
		return PLUGIN_HANDLED;
	}
 
	/*An admin may conceivably want a vote on whether to password a server*/	
	if ( (strcasestr(Data, "password")!= -1) && !check_auth(ACCESS_UNRESTRICT) ) {
		snprintf(text, MAX_TEXT_LENGTH, "[VOTE] %s, you may not vote over ANYTHING involving password!", UserName);
		selfmessage(text);
		return PLUGIN_HANDLED;
	}
 
	/*An admin may conceivably want a vote on whether to exec a clan specific config or whatever, which is fair enough*/
	if ( (strcasestr(Data, "exec")!= -1)  && !check_auth(ACCESS_UNRESTRICT) ){
		snprintf(text, MAX_TEXT_LENGTH, "[VOTE] %s, you may not vote over ANYTHING involving exec!", UserName);
		selfmessage(text);
		return PLUGIN_HANDLED;
	}
 
	/*Noone should EVER screw with the logs!*/
	if ( (strcasestr(Data, "log")!= -1) ) {
		snprintf(text, MAX_TEXT_LENGTH, "[VOTE] %s, you may not vote over ANYTHING involving the log!", UserName);
		selfmessage(text);
		return PLUGIN_HANDLED;
	}
	/*NO NO NO NO NO NO NO NO NO NO NO NO NO NO BLOODY NO!!!!*/
	if ( (strcasestr(Data, "startmovie")!= -1) ) {
		snprintf(text, MAX_TEXT_LENGTH, "[VOTE] %s, dont screw with the server, take a 60 minute break!", UserName);
		selfmessage(text);
		ban(UserName,60);
		return PLUGIN_HANDLED;
	}
 
	flag = 0;
	if(vote_allowed()!=1) {
		selfmessage("[VOTE] Vote not allowed at this time");
	} else {
		if ( strlen(Data) > 0 ){
			snprintf(text, MAX_TEXT_LENGTH, "[VOTE] %s called a vote", UserName);
			say(text);
			snprintf(text, MAX_TEXT_LENGTH, "Vote on changes:^n%s", Data);
			vote(text,"Accept","Reject","HandleVote",Data);
		} else {
			snprintf(text, MAX_TEXT_LENGTH, "You must choose something to vote over.", Data);
		}
	}
 
 
	return PLUGIN_CONTINUE;
 
}
 
public HandleVote(WinningOption,HLParam,VoteCount,UserCount) {
	new Text[MAX_TEXT_LENGTH];
	new Data[MAX_DATA_LENGTH];
	new Data2[MAX_DATA_LENGTH];
	convert_string(HLParam,Data,MAX_DATA_LENGTH);
 
	if (flag==0){
		if (WinningOption == 1) {
			snprintf(Text, MAX_TEXT_LENGTH, "[VOTE] Vote for %s was passed.", Data);
			say(Text);
			if ( (strcasestr(Data, "admin_") != -1) ) {
				strbreak(Data, Data, Data2, MAX_DATA_LENGTH);
				plugin_exec(Data,Data2);
			} else {
				exec(Data);
			}
		} else {
			snprintf(Text, MAX_TEXT_LENGTH, "[VOTE] Vote for %s was rejected.", Data);
			say(Text);
		}
	} else {
		snprintf(Text, MAX_TEXT_LENGTH, "[VOTE] Vote succesfully canceled by Admin");
  		say(Text);
		flag = 0;
  	}
}