PtokaX settings - get/set px settings. Lua 5.1 - 5.3 / PtokaX 0.4.2.0 and newer
 

PtokaX settings - get/set px settings. Lua 5.1 - 5.3 / PtokaX 0.4.2.0 and newer

Started by PPK, 27 March, 2015, 19:25:40

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

PPK

This is script to show actual PtokaX settings and to change them. Script require PtokaX 0.4.2.0 or higher. Script is compatible with Lua 5.1, 5.2 and 5.3.

Simply download http://www.ptokax.org/Scripts/pxsettings.lua and move it to PtokaX_dir\scripts directory.
On PtokaX 0.5.0.3 with Lua 5.1 (because of bug) and older PtokaX versions is needed compatibility script http://www.ptokax.org/Scripts/idcompat.lua that must be moved to PtokaX_dir\scripts\compat directory.

By default only Master (profile number 0) is allowed to use this script.
This can be changed in this part of script:
Code: Lua
local tAllowedProfiles = {
	-- true when profile is allowed to use this script or nil when not
	-- when profile is not here then it is same as when is here with nil
	[0] = true, -- Master
	[1] = nil -- Operator
}

For example when you want to allow operator (profile number 1) to use this script then change line
Code: Lua
[1] = nil -- Operator
to
Code: Lua
[1] = true -- Operator


Usage of this script is simple.
With command !help you can list all available commands:
QuoteAvailable commands:
   !getbooleans - display list of all actual boolean settings.
   !getbooleanids - display list of available boolean identificators.

   !getnumbers -  display list of all actual number settings.
   !getnumberids - display list of available number identificators.

   !getstrings -  display list of all actual string settings.
   !getstringids - display list of available string identificators.

   !getboolean <boolean_identificator> - return actual state for given boolean identificator.
   !getnumber <number_identificator> - return actual value for given number identificator.
   !getstring <string_identificator> - return actual string for given number identificator.

   !setboolean <boolean_identificator> <enable/disable> - change state for given boolean identificator.
   !setnumber <number_identificator> <number> - change number for given number identificator.
   !setstring <string_identificator> <string> - change string for given string identificator.

In case of requests, bug reports or questions post them below ;)

Complete script:
Code: Lua
--[[
		PtokaX settings script version 0.9. Script to get and set all PtokaX settings.

		Copyright (c) 2015 Petr Kozelka, PPK at PtokaX dot org

		This script is licensed under 
			Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International Public License.
			See http://creativecommons.org/licenses/by-nc-nd/4.0/ for license details.
]]--

-- tables where we store lower case and sorted settings identificators
local tBooleans = { }
local tNumbers = { }
local tStrings = { }

local tSortedBooleans = { }
local tSortedNumbers = { }
local tSortedStrings = { }

local tAllowedProfiles = {
	-- true when profile is allowed to use this script or nil when not
	-- when profile is not here then it is same as when is here with nil
	[0] = true, -- Master
	[1] = nil -- Operator
}

-- compare function to sort settings identificators
function fCompare(t1, t2)
	if t1[2] < t2[2] then
		return true
	elseif t1[2] == t2[2] and t1[1] > t2[1] then
		return true
	end
end

function OnStartup()
	-- test if we running on supported PtokaX version
	if Core.BuildNumber == nil then
		error("This script require PtokaX 0.4.2.0 build 258 or higher!")
	end

	-- when we run on PtokaX before 0.5.0.3 then we need to load settings ids from compatibility script
	if Core.BuildNumber < 482 then
		dofile(Core.GetPtokaXPath().."scripts/compat/idcompat.lua")
	end

	-- bug in 0.5.0.3 where identificator tables missing when Lua 5.1 is used
	if Core.BuildNumber == 482 and _VERSION == "Lua 5.1" then
		dofile(Core.GetPtokaXPath().."scripts/compat/idcompat.lua")
	end

	RegMan = nil
	BanMan = nil
	ProfMan = nil
	TmrMan = nil
	UDPDbg = nil
	ScriptMan = nil
	IP2Country = nil

	collectgarbage()
	
	for k, v in pairs(SetMan.tBooleans) do
		tBooleans[string.lower(k)] = v
		table.insert(tSortedBooleans, { string.lower(k), v })
	end

	table.sort(tSortedBooleans, fCompare)

	for k, v in pairs(SetMan.tNumbers) do
		tNumbers[string.lower(k)] = v
		table.insert(tSortedNumbers, { string.lower(k), v })
	end

	table.sort(tSortedNumbers, fCompare)

	for k, v in pairs(SetMan.tStrings) do
		tStrings[string.lower(k)] = v
		table.insert(tSortedStrings, { string.lower(k), v })
	end

	table.sort(tSortedStrings, fCompare)
end

function ChatArrival(tUser, sData)
	-- check if user is allowed to use this script
	if tAllowedProfiles[tUser.iProfile] == nil then
		return false
	end

	-- check if received command is starting with chat command prefix
   if string.find(SetMan.GetString(SetMan.tStrings.ChatCommandsPrefixes), string.sub(sData, tUser.sNick:len()+4, tUser.sNick:len()+4), 1, true) == nil then
      return false
   end

	-- get command withou '<nick> ' and ending pipe
	local sCmd = string.sub(sData, tUser.sNick:len()+5, -2)
	local sParam = nil

	-- try to find space -> command with param
	local nFound = string.find(sCmd, " ", 1, true)
	if nFound ~= nil then
		sParam = string.sub(sCmd, nFound+1)
		sCmd = string.sub(sCmd, 1, nFound-1):lower()
	else
		sCmd = sCmd:lower()
	end

	-- commands without param
	if sParam == nil then
		if sCmd == "getbooleans" then -- list actual boolean settings
			local sMsg = string.format("<%s> Actual boolean settings:\n", Core.GetHubSecAlias())

			for i, n in ipairs(tSortedBooleans) do
				local bValue = SetMan.GetBool(n[2])
				if bValue == nil then
					sMsg = sMsg..string.format("%s\t= disabled\n", n[1])
				else
					sMsg = sMsg..string.format("%s\t= enabled\n", n[1])
				end
			end

			Core.SendToUser(tUser, sMsg.."|")
			return true
		elseif sCmd == "getbooleanids" then -- list available boolean identificators
			local sMsg = string.format("<%s> Available boolean settings identificators:\n", Core.GetHubSecAlias())

			for i, n in ipairs(tSortedBooleans) do
				sMsg = sMsg..string.format("%s, ", n[1])
			end

			sMsg = string.sub(sMsg, 1, -3)
			Core.SendToUser(tUser, sMsg.."\n\nMore info about identificators is in PtokaX Wiki: http://wiki.ptokax.org/doku.php?id=luaapi:setting_bools_ids|")
			return true
		elseif sCmd == "getnumbers" then -- list actual number settings
			local sMsg = string.format("<%s> Actual number settings:\n", Core.GetHubSecAlias())

			for i, n in ipairs(tSortedNumbers) do
				sMsg = sMsg..string.format("%s\t= %d\n", n[1], SetMan.GetNumber(n[2]))
			end

			Core.SendToUser(tUser, sMsg.."|")
			return true
		elseif sCmd == "getnumberids" then -- list available number identificators
			local sMsg = string.format("<%s> Available number settings identificators:\n", Core.GetHubSecAlias())

			for i, n in ipairs(tSortedNumbers) do
				sMsg = sMsg..string.format("%s, ", n[1])
			end

			sMsg = string.sub(sMsg, 1, -3)
			Core.SendToUser(tUser, sMsg.."\n\nMore info about identificators is in PtokaX Wiki: http://wiki.ptokax.org/doku.php?id=luaapi:setting_numbers_ids|")
			return true
		elseif sCmd == "getstrings" then -- list actual string settings
			local sMsg = string.format("<%s> Actual string settings:\n", Core.GetHubSecAlias())

			for i, n in ipairs(tSortedStrings) do
				local sValue = SetMan.GetString(n[2])
				if sValue == nil then
					sMsg = sMsg..string.format("%s\t= [empty value]\n", n[1])
				else
					sMsg = sMsg..string.format("%s\t= %s\n", n[1], sValue)
				end
			end

			Core.SendToUser(tUser, sMsg.."|")
			return true
		elseif sCmd == "getstringids" then -- list available string identificators
			local sMsg = string.format("<%s> Available string settings identificators:\n", Core.GetHubSecAlias())

			for i, n in ipairs(tSortedStrings) do
				sMsg = sMsg..string.format("%s, ", n[1])
			end

			sMsg = string.sub(sMsg, 1, -3)
			Core.SendToUser(tUser, sMsg.."\n\nMore info about identificators is in PtokaX Wiki: http://wiki.ptokax.org/doku.php?id=luaapi:setting_strings_ids|")
			return true
		elseif sCmd == "help" then -- list available commands for this script
			local sCommandPrefix = string.sub(SetMan.GetString(SetMan.tStrings.ChatCommandsPrefixes), 1, 1)
			Core.SendToUser(tUser, string.format("<%s> Available commands:\n\t%sgetbooleans - display list of all actual boolean settings.\n\t%sgetbooleanids - display list of available boolean identificators.\n\n\t%sgetnumbers -  display list of all actual number settings.\n\t%sgetnumberids - display list of available number identificators.\n\n\t%sgetstrings -  display list of all actual string settings.\n\t%sgetstringids - display list of available string identificators.\n\n\t%sgetboolean <boolean_identificator> - return actual state for given boolean identificator.\n\t%sgetnumber <number_identificator> - return actual value for given number identificator.\n\t%sgetstring <string_identificator> - return actual string for given number identificator.\n\n\t%ssetboolean <boolean_identificator> <enable/disable> - change state for given boolean identificator.\n\t%ssetnumber <number_identificator> <number> - change number for given number identificator.\n\t%ssetstring <string_identificator> <string> - change string for given string identificator.|",
				Core.GetHubSecAlias(), sCommandPrefix, sCommandPrefix, sCommandPrefix, sCommandPrefix, sCommandPrefix, sCommandPrefix, sCommandPrefix, sCommandPrefix, sCommandPrefix, sCommandPrefix, sCommandPrefix, sCommandPrefix))
			return false
		end
	else -- commands with param
		if sCmd == "getboolean" then -- return value for given boolean identificator
			if sParam:len() == 0 then
				local sCommandPrefix = string.sub(SetMan.GetString(SetMan.tStrings.ChatCommandsPrefixes), 1, 1)
				Core.SendToUser(tUser, string.format("<%s> Missing parameter for command: %sgetboolean <boolean_identificator>!|", Core.GetHubSecAlias(), sCommandPrefix))
				return true
			end

			sParam = sParam:lower()

			if tBooleans[sParam] == nil then
				local sCommandPrefix = string.sub(SetMan.GetString(SetMan.tStrings.ChatCommandsPrefixes), 1, 1)
				Core.SendToUser(tUser, string.format("<%s> Parameter '%s' is not valid for command: %sgetboolean <boolean_identificator>!|", Core.GetHubSecAlias(), sParam, sCommandPrefix))
				return true
			end

			local bValue = SetMan.GetBool(tBooleans[sParam])
			if bValue == nil then
				Core.SendToUser(tUser, string.format("<%s> Boolean value of parameter '%s' is: disabled.|", Core.GetHubSecAlias(), sParam))
			else
				Core.SendToUser(tUser, string.format("<%s> Boolean value of parameter '%s' is: enabled.|", Core.GetHubSecAlias(), sParam))
			end

			return true
		elseif sCmd == "getnumber" then -- return value for given number identificator
			if sParam:len() == 0 then
				local sCommandPrefix = string.sub(SetMan.GetString(SetMan.tStrings.ChatCommandsPrefixes), 1, 1)
				Core.SendToUser(tUser, string.format("<%s> Missing parameter for command: %sgetnumber <number_identificator>!|", Core.GetHubSecAlias(), sCommandPrefix))
				return true
			end

			sParam = sParam:lower()

			if tNumbers[sParam] == nil then
				local sCommandPrefix = string.sub(SetMan.GetString(SetMan.tStrings.ChatCommandsPrefixes), 1, 1)
				Core.SendToUser(tUser, string.format("<%s> Parameter '%s' is not valid for command: %sgetnumber <number_identificator>!|", Core.GetHubSecAlias(), sParam, sCommandPrefix))
				return true
			end

			Core.SendToUser(tUser, string.format("<%s> Number value of parameter '%s' is: %d.|", Core.GetHubSecAlias(), sParam, SetMan.GetNumber(tNumbers[sParam])))
			return true
		elseif sCmd == "getstring" then -- return value for given string
			if sParam:len() == 0 then
				local sCommandPrefix = string.sub(SetMan.GetString(SetMan.tStrings.ChatCommandsPrefixes), 1, 1)
				Core.SendToUser(tUser, string.format("<%s> Missing parameter for command: %sgetstring <string_identificator>!|", Core.GetHubSecAlias(), sCommandPrefix))
				return true
			end

			sParam = sParam:lower()

			if tStrings[sParam] == nil then
				local sCommandPrefix = string.sub(SetMan.GetString(SetMan.tStrings.ChatCommandsPrefixes), 1, 1)
				Core.SendToUser(tUser, string.format("<%s> Parameter '%s' is not valid for command: %sgetstring <string_identificator>!|", Core.GetHubSecAlias(), sParam, sCommandPrefix))
				return true
			end

			local sValue = SetMan.GetString(tStrings[sParam])
			if sValue == nil then
				Core.SendToUser(tUser, string.format("<%s> String value of parameter '%s' is: [empty value].|", Core.GetHubSecAlias(), sParam))
			else
				Core.SendToUser(tUser, string.format("<%s> String value of parameter '%s' is: %s.|", Core.GetHubSecAlias(), sParam, sValue))
			end

			return true
		elseif sCmd == "setboolean" then -- set new value for given boolean identificator
			if sParam:len() == 0 then
				local sCommandPrefix = string.sub(SetMan.GetString(SetMan.tStrings.ChatCommandsPrefixes), 1, 1)
				Core.SendToUser(tUser, string.format("<%s> Missing parameter for command: %ssetboolean <boolean_identificator> <enable/disable>!|", Core.GetHubSecAlias(), sCommandPrefix))
				return true
			end

			local bState = nil
			local sState = nil

			-- try to find space -> command with param and value
			local nFound = string.find(sParam, " ", 2, true)
			if nFound ~= nil then
				sState = string.sub(sParam, nFound+1):lower()
				sParam = string.sub(sParam, 1, nFound-1):lower()

				if sState == "enable" then
					bState = true
				elseif sState == "disable" then
					bState = false
				end
			end

			if bState == nil then
				local sCommandPrefix = string.sub(SetMan.GetString(SetMan.tStrings.ChatCommandsPrefixes), 1, 1)
				Core.SendToUser(tUser, string.format("<%s> enable/disable state for param '%s' is not valid for command: %ssetboolean <boolean_identificator> <enable/disable>!|", Core.GetHubSecAlias(), sParam, sCommandPrefix))
				return true
			end

			if tBooleans[sParam] == nil then
				local sCommandPrefix = string.sub(SetMan.GetString(SetMan.tStrings.ChatCommandsPrefixes), 1, 1)
				Core.SendToUser(tUser, string.format("<%s> Parameter '%s' is not valid for command: %ssetboolean <boolean_identificator> <enable/disable>!|", Core.GetHubSecAlias(), sParam, sCommandPrefix))
				return true
			end

			SetMan.SetBool(tBooleans[sParam], bState) 

			local bValue = SetMan.GetBool(tBooleans[sParam])

			if (bValue == true and bState == true) or (bValue == nil and bState == false) then
				Core.SendToUser(tUser, string.format("<%s> Boolean value of parameter '%s' changed to: %s.|", Core.GetHubSecAlias(), sParam, sState))
			else
				Core.SendToUser(tUser, string.format("<%s> Change for boolean value of parameter '%s' failed!|", Core.GetHubSecAlias(), sParam))
			end

			return true
		elseif sCmd == "setnumber" then -- set value for given number identificator
			if sParam:len() == 0 then
				local sCommandPrefix = string.sub(SetMan.GetString(SetMan.tStrings.ChatCommandsPrefixes), 1, 1)
				Core.SendToUser(tUser, string.format("<%s> Missing parameter for command: %ssetnumber <number_identificator> <number>!|", Core.GetHubSecAlias(), sCommandPrefix))
				return true
			end

			local nValue = nil

			-- try to find space -> command with param and value
			local nFound = string.find(sParam, " ", 2, true)
			if nFound ~= nil then
				nValue = tonumber(string.sub(sParam, nFound+1))
				sParam = string.sub(sParam, 1, nFound-1):lower()
			end

			if nValue == nil then
				local sCommandPrefix = string.sub(SetMan.GetString(SetMan.tStrings.ChatCommandsPrefixes), 1, 1)
				Core.SendToUser(tUser, string.format("<%s> New numeric value for param '%s' is not valid for command: %ssetnumber <number_identificator> <number>!|", Core.GetHubSecAlias(), sParam, sCommandPrefix))
				return true
			end

			if tNumbers[sParam] == nil then
				local sCommandPrefix = string.sub(SetMan.GetString(SetMan.tStrings.ChatCommandsPrefixes), 1, 1)
				Core.SendToUser(tUser, string.format("<%s> Parameter '%s' is not valid for command: %ssetnumber <number_identificator> <number>!|", Core.GetHubSecAlias(), sParam, sCommandPrefix))
				return true
			end

			SetMan.SetNumber(tNumbers[sParam], nValue) 

			local nNewValue = SetMan.GetNumber(tNumbers[sParam])

			if nValue == nNewValue then
				Core.SendToUser(tUser, string.format("<%s> Number value of parameter '%s' changed to: %s.|", Core.GetHubSecAlias(), sParam, nValue))
			else
				Core.SendToUser(tUser, string.format("<%s> Change for number value of parameter '%s' failed!|", Core.GetHubSecAlias(), sParam))
			end

			return true
		elseif sCmd == "setstring" then -- set value for given string identificator
			if sParam:len() == 0 then
				local sCommandPrefix = string.sub(SetMan.GetString(SetMan.tStrings.ChatCommandsPrefixes), 1, 1)
				Core.SendToUser(tUser, string.format("<%s> Missing parameter for command: %ssetstring <string_identificator> <string>!|", Core.GetHubSecAlias(), sCommandPrefix))
				return true
			end

			local sString = nil

			-- try to find space -> command with param and value
			local nFound = string.find(sParam, " ", 2, true)
			if nFound ~= nil then
				sString = string.sub(sParam, nFound+1)
				sParam = string.sub(sParam, 1, nFound-1):lower()
			end

			if sString == nil then
				local sCommandPrefix = string.sub(SetMan.GetString(SetMan.tStrings.ChatCommandsPrefixes), 1, 1)
				Core.SendToUser(tUser, string.format("<%s> String for param '%s' is not valid for command: %ssetstring <string_identificator> <string>!|", Core.GetHubSecAlias(), sParam, sCommandPrefix))
				return true
			end

			if tStrings[sParam] == nil then
				local sCommandPrefix = string.sub(SetMan.GetString(SetMan.tStrings.ChatCommandsPrefixes), 1, 1)
				Core.SendToUser(tUser, string.format("<%s> Parameter '%s' is not valid for command: %ssetstring <string_identificator> <string>!|", Core.GetHubSecAlias(), sParam, sCommandPrefix))
				return true
			end

			SetMan.SetString(tStrings[sParam], sString) 

			local sNewString = SetMan.GetString(tStrings[sParam])

			if (sString == sNewString) or (sNewString == nil and sString:len() == 0) then
				Core.SendToUser(tUser, string.format("<%s> String value of parameter '%s' changed to: %s.|", Core.GetHubSecAlias(), sParam, sString))
			else
				Core.SendToUser(tUser, string.format("<%s> Change for string value of parameter '%s' failed!|", Core.GetHubSecAlias(), sParam))
			end

			return true
		end
	end
end
"Most of you are familiar with the virtues of a programmer. There are three, of course: laziness, impatience, and hubris." - Larry Wall

SMF spam blocked by CleanTalk