/*
 * Brazil's Access plugin, derived and heavily modified from the 
 * original 
   * Bud-froggy Productions -- April 29th, 2001
   * Access levelĀ®
   * Change people's access level through console
 *
 * Email: brazilnathan@hotmail.com
 * Server: Fields of Blood 12.34.220.4:27015 (6PM-6AM EST)
 *
 * New Admin commands and features that let you manipulate the 
 * users.ini file from the console.  I use this to give my clan
 * leaders the ability to add clan members for reserved slots
 * And it saves me from manually editing the users.ini file and
 * then running HL and admin_reload (or resetting the server)
 *
 * admin_user_access <name in users.ini> <new access level>
 * admin_user_add <name in users.ini> <password>  - Defaults to 32769 access level
 * admin_user_del <name in users.ini>
 */
 
/*
 *
 * Updated by LA - 07/05/01
 * 
 * Temporary file name changed from "users.new" to "users.tmp".
 *
 * Uses boolean "Found" rather than linenumber stuff. (Personal preference)
 * 
 * Makes use of "users_file" cvar, rather than blindly looking for users.ini in mod directory.
 *
 * Deletes temporary file "users.tmp" at end of functions.
 *
 * Changed "#define DEFAULT_ACCESS" to a text string, did not work properly on Windows system.
 *
 * No longer reload()'s, or copyfile()'s when there is no need to do so.
 * Also blank arguments can cause problem, should be handled now.
 * 
 * Changed some messages to be more informative, fixed punctuation. (Pet pieve)
 *
 * Now uses ':' to seperate name from access or password, instead of whitespace.
 * E.g. "admin_user_add Multi word name:qwerty" will successfuly add a new user
 * with the name "Multi word name" and password "qwerty" without using quotes.
 * -- NOTE: HLData seemed to have the quotes stripped beforehand, which was causing
 * problems on my server. (win2000)
 * 
 * -LA 07/05/01
 */
 
#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
 
#define ACCESS_ACCESS  65536
#define ACCESS_ADDUSER 4096
#define ACCESS_DELUSER 65536
#define STEAM 1
 
 
new DEFAULT_ACCESS[MAX_TEXT_LENGTH] = "32769";
new STRING_VERSION[MAX_DATA_LENGTH] = "2.50.2";
 
 
copyfile(sFile[],dFile[])
{
	new LineData[100];
	new iLine = 1;
	new i;
 
	resetfile(dFile);
	i = readfile(sFile,LineData,iLine,99);
	while (i > 0)
	{
		writefile(dFile,LineData,-1);
		iLine++;
		i = readfile(sFile,LineData,iLine,99);
	}
}
 
 
public admin_user_access(HLCommand,HLData,HLUser,UserIndex)
{
	new Data[MAX_DATA_LENGTH];
	new Command[MAX_COMMAND_LENGTH];
	new User[MAX_NAME_LENGTH];
	new TargetName[MAX_NAME_LENGTH];
	new Target[MAX_DATA_LENGTH];
	new Message[MAX_DATA_LENGTH];
	new sNewAccess[MAX_DATA_LENGTH];
	new sPassword[MAX_DATA_LENGTH];
	new sAccess[MAX_DATA_LENGTH];
	new sName[MAX_NAME_LENGTH] = "";
	new LineData[MAX_DATA_LENGTH];
	new fName[MAX_TEXT_LENGTH]; // I keep mine in admin/config/users.ini
	new iLine = 1;
	new Found = FALSE;
	new ret;
 
	convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	convert_string(HLUser,User,MAX_NAME_LENGTH);
 
	// Use ':' to seperate username from new access, no quotes
	// required for multi-word names.
	// -LA 07/05/01
	if (strlen(Data) < 1)
	{
		selfmessage("Usage: admin_user_access <name>:<newaccess>");
		return PLUGIN_HANDLED;
	}
	strsplit(Data, ":", TargetName, MAX_NAME_LENGTH, sNewAccess, MAX_DATA_LENGTH);
 
	// Trim leading and trailing whitespace.
	// -LA 07/05/01
	strtrim(TargetName, " ",2);
	strtrim(sNewAccess, " ", 2);
 
	// Get users file from the users_file cvar.
	// -LA 07/05/01
	getstrvar("users_file",fName,MAX_TEXT_LENGTH);
 
	get_username(TargetName,Target,MAX_NAME_LENGTH);
 
	if(strcmp(Target,User)==0)
	{
		selfmessage("You can't change your own access level.");
		return PLUGIN_HANDLED;
	}
 
	/* Reset the new file copy */
	resetfile("users.tmp");
 
	/* 
	   Copy data from users.ini to users.tmp and change the
           user that you requested 
	*/
 
	ret = readfile(fName,LineData,iLine,99);
	while(ret > 0)
	{
		if (strcount(LineData,':')==2)
		{	
			strsplit(LineData,":",sName,MAX_NAME_LENGTH,sPassword,MAX_DATA_LENGTH,sAccess,MAX_DATA_LENGTH);
			if(strcmp(sName,TargetName)==0)
			{
				snprintf(LineData,MAX_DATA_LENGTH,"%s:%s:%s", sName, sPassword,sNewAccess);
				Found = TRUE;
			} // if(strcmp())
		} // if(strcount())
		writefile("users.tmp",LineData,-1);
		iLine++;
		ret = readfile(fName,LineData,iLine,99);
	} // while()
 
	/* If name is not found, bitch */
	if (Found == FALSE)
	{
		snprintf(Message, MAX_NAME_LENGTH, "Unable to find player: %s", TargetName);
		selfmessage(Message);
	}
	else
	{
		copyfile("users.tmp", fName);
		snprintf(Message, MAX_DATA_LENGTH, "User: %s now has access level %s", TargetName, sNewAccess);
		selfmessage(Message);
		log_command(User,Command,Data);
		reload();
 
	}
	deletefile("users.tmp");
	return PLUGIN_HANDLED;
}
 
 
/*
 * admin_user_add <name> <password>
 * Adds a NEW user to the users.ini file and gives them
 * a default access level of reserved slot (32769)
 * If the user exists, the access level is reset to 32769
 */
public admin_user_add(HLCommand,HLData,HLUser,UserIndex)
{
	new Data[MAX_DATA_LENGTH];
	new Command[MAX_COMMAND_LENGTH];
	new User[MAX_NAME_LENGTH];
	new TargetName[MAX_NAME_LENGTH];
	new Message[MAX_DATA_LENGTH];
	new sNewPassword[MAX_DATA_LENGTH];
	new sPassword[MAX_DATA_LENGTH];
	new sAccess[MAX_DATA_LENGTH];
	new sName[MAX_NAME_LENGTH] = "";
	new LineData[MAX_DATA_LENGTH];
	new fName[MAX_TEXT_LENGTH]; 
	new iLine = 1;
	new Found = FALSE;
	new ret;
 
	convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	convert_string(HLUser,User,MAX_NAME_LENGTH);
 
	if (strlen(Data) < 1)
	{
		selfmessage("Usage: admin_user_add <name>:<password>");
		return PLUGIN_HANDLED;
	}
 
	// Use ':' to seperate username from new access, no quotes
	// required for multi-word names.
	// -LA 07/05/01
	strsplit(Data, ":", TargetName, MAX_NAME_LENGTH, sNewPassword, MAX_DATA_LENGTH);
 
	// Trim leading and trailing whitespace
	// -LA 07/05/01
	strtrim(TargetName, " ",2);
	strtrim(sNewPassword, " ", 2);
 
	/* Reset the new file copy */
	resetfile("users.tmp");
 
	// Get users file from the users_file cvar.
	// -LA 07/05/01
	getstrvar("users_file",fName,MAX_TEXT_LENGTH);
 
	/* 
	   Copy data from users.ini to users.tmp and find the
           new user name in the file
	*/
 
	ret = readfile(fName,LineData,iLine,99);
	while(ret > 0)
	{
		if (strcount(LineData,':')==2)
		{	
			strsplit(LineData,":",sName,MAX_NAME_LENGTH,sPassword,MAX_DATA_LENGTH,sAccess,MAX_DATA_LENGTH);
			if(strcmp(sName,TargetName)==0)
			{
				snprintf(Message,MAX_DATA_LENGTH,"Sorry, player '%s' is already present",sName);
				selfmessage(Message);
				Found = TRUE;
				break; // No use going through the rest of the file
			}
		}
		writefile("users.tmp",LineData,-1);
		iLine++;
		ret = readfile(fName,LineData,iLine,99);
	}
	/* If name is not found, ADD */
	if (Found == FALSE) // No use logging, reloading, and copying if we didn't do anything.
	{
		snprintf(LineData,MAX_DATA_LENGTH,"%s:%s:%s",TargetName,sNewPassword,DEFAULT_ACCESS);
		writefile("users.tmp",LineData,-1);
		snprintf(Message,MAX_DATA_LENGTH,"[ADMIN] Added user '%s' with password '%s'.",TargetName, sNewPassword);
		selfmessage(Message);
		copyfile("users.tmp", fName);
		log_command(User,Command,Data);
		reload();
	}
	deletefile("users.tmp");
	return PLUGIN_HANDLED;
}
 
 
/*
 * admin_user_del <name>
 * Searches users.ini and deletes <name>
 */
public admin_user_del(HLCommand,HLData,HLUser,UserIndex)
{
	new Data[MAX_DATA_LENGTH];
	new Command[MAX_COMMAND_LENGTH];
	new User[MAX_NAME_LENGTH];
	new Message[MAX_DATA_LENGTH];
	new sPassword[MAX_DATA_LENGTH];
	new sAccess[MAX_DATA_LENGTH];
	new sName[MAX_NAME_LENGTH] = "";
	new LineData[MAX_DATA_LENGTH];
	new fName[MAX_TEXT_LENGTH];
	new iLine = 1;
	new ret;
	new Found = FALSE;
 
	convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	convert_string(HLUser,User,MAX_NAME_LENGTH);
 
	if (strlen(Data) < 1)
	{
		selfmessage("Usage: admin_user_del <name>");
		return PLUGIN_HANDLED;
	}
 
	/* Reset the new file copy */
	resetfile("users.tmp");
 
	// Get users file from the users_file cvar.
	// -LA 07/05/01
	getstrvar("users_file",fName,MAX_TEXT_LENGTH);
 
	ret = readfile(fName,LineData,iLine,99);
	while(ret > 0)
	{
		if (strcount(LineData,':')==2)
		{	
			strsplit(LineData,":",sName,MAX_NAME_LENGTH,sPassword,MAX_DATA_LENGTH,sAccess,MAX_DATA_LENGTH);
			if(strcmp(sName,Data)==0)
			{
				snprintf(Message,MAX_DATA_LENGTH,"User '%s' deleted",sName);
				selfmessage(Message);
				Found = TRUE;
			}
			else
			{
				writefile("users.tmp",LineData,-1);
			}
		}
		else
		{
			writefile("users.tmp",LineData,-1);
		}
		iLine++;
		ret = readfile(fName,LineData,iLine,99);
	}
	if (Found) // Let's only reload and such if we did something
	{
		copyfile("users.tmp",fName); 
		log_command(User,Command,Data);
		reload();
	}
	else
	{
		snprintf(Message, MAX_DATA_LENGTH, "User '%s' not found.", Data);
		selfmessage(Message);
	}
	deletefile("users.tmp");
 
	return PLUGIN_HANDLED;
}
 
 
public plugin_init()
{
	plugin_registerinfo("Access level changer","Change people's access level through console.",STRING_VERSION);
  	plugin_registercmd("admin_user_access", "admin_user_access", ACCESS_ACCESS, "admin_user_access  Change people's access level through console.");
  	plugin_registercmd("admin_user_add", "admin_user_add", ACCESS_ADDUSER, "admin_user_add <name> <password> Adds a reserved slot");
  	plugin_registercmd("admin_user_del", "admin_user_del", ACCESS_DELUSER, "admin_user_del <name> Deletes a user from users.ini");
	return PLUGIN_CONTINUE;
}