HOW TO: Parse the PtokaX .xml files in a table ?
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

HOW TO: Parse the PtokaX .xml files in a table ?

Started by Herodes, 30 March, 2005, 02:19:25

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Herodes

Okie .. sry for that little bug.. I hope its fixed.
Also this one is a bit optimised ..
--[[ -------------------------------------------------------------------------------
Author: Herodes
Thanks : RambitWombat for the Lua 4 XMLtoTable func

Usage : 
	table, time = XMLFiletoTable ( f ) 
		'table' is the file signified by 'f' as a table, 
		'time' the time took for parsing it (usually 0.00xxx s)
		'f' can be 1 =BanList.xml / 2 =Profiles.xml / 3 =RegisteredUsers.xml / 4 =ReservedNicks.xml / 5 =Scripts.xml
--]] -------------------------------------------------------------------------------
function XMLFiletoTable( mode )
	local tFiles, x = {
		{ "../cfg/BanList.xml", function( tbl )
					return { ["perm"] = tbl.BanList.PermBans.Ban, ["temp"] = tbl.BanList.TempBans.Ban } 
					end },
		--  "../cfg/ClientTags.xml", -- disabled because of loop problem in the parser..
		-- The XML has a Child Node identical to Parent Node situation in the ClientTags
		{ "../cfg/Profiles.xml", function( tbl )
					local res, t = {}, tbl.Profiles.Profile
					for i,v in t do res[v.Name] = v.Permissions end
					return res
				end, },
		{ "../cfg/RegisteredUsers.xml", function( tbl )
					local res, t = {}, tbl.RegisteredUsers.RegisteredUser
					if table.getn( t ) == 0 then
						res[t.Nick] = { ["pass"] = t.Password, ["profile"] = t.Profile }
					else
						for i,v in t do res[v.Nick] = { ["pass"] = v.Password, ["profile"] = v.Profile } end
					end
					return res
				end, },
		{ "../cfg/ReservedNicks.xml", function( tbl ) return tbl.ReservedNicks.ReservedNick end, },
		{ "../cfg/Scripts.xml", function( tbl )
					local res = {}
					if table.getn( tbl.Scripts.Script ) == 0 then
						res[tbl.Scripts.Script.Name] = tbl.Scripts.Script.Enabled
					else
						for i,v in tbl.Scripts.Scripts do res[v.Name] = v.Enabled end
					end
					return res
				end, },
		}, os.clock()

	local function get_Inner( sString, b ) --- based on the XMLtoTable provided by RambitWombat in the Lua Board
					if( not sString ) then return ""; end
					local s,e, sTag, sTemp = string.find(sString, "^%s*<([^>]+)>(.*)$");
					local sInner;
					-- we have a tag
					if (sTag) then
						sString = sTemp;
						local tTable = {};
						while sTag do
							s,e, sTag = string.find(sTag, "^(%S+)");
							s,e, sInner, sTemp = string.find(sString, "^(.-)(.*)$");
							if( not tTable[sTag] ) then tTable[sTag] = {}; end
							table.insert( tTable[sTag], get_Inner( sInner ) );
							if sTemp then
								s, e, sTag, sString = string.find( sTemp, "^%s*<([^>]-)>(.*)$");
							end
						end
						if table.getn(tTable) == 1 then return tTable[1]; end
						return tTable;
					else -- we have data
						return sString;
					end
				end

	local function FileToString( file )
			local m = ""; local f = io.open( file, "r");
			for line in f:lines() do
				if string.sub( line, 1, 5) ~= "

Jelf

#1
Exellent job Herodes and RambitWombat, ty

*Edit
Just need to work out how to use it, lmao
As if I use complete script it gives an error `end' expected (to close `function' at line 3) near `{'

jiten

This is what most of us was looking for  :D
Great work.

Cheers

Herodes

QuoteOriginally posted by Jelf
Exellent job Herodes and RambitWombat, ty

*Edit
Just need to work out how to use it, lmao
As if I use complete script it gives an error `end' expected (to close `function' at line 3) near `{'
Thx for the report .. that syntax bug is now fixed...
my first post has changed ;)

Jelf

Still got a problem, firstly a very minor one, I left the usage in and had to change -- [[ to --[[ on line 1 and -- ]] to ]]-- on line 12.
But now I get an error... unexpected symbol near `}' on line 44 :((

Herodes

QuoteOriginally posted by Jelf
Still got a problem, firstly a very minor one, I left the usage in and had to change -- [[ to --[[ on line 1 and -- ]] to ]]-- on line 12.
But now I get an error... unexpected symbol near `}' on line 44 :((
Again sry about that .. I have edited the post..

johny

ok great work guy's
but how do i implement this script into this all????

sBot = "Bad Password Cleaner"

fPermBan = "../cfg/BanList.xml"
arrAux = {}
sec = 1000
min = 60*sec
hour = 60*min
iTime = 5*min-- Checking Time

function Main()
--	frmHub:RegBot(sBot)
	SetTimer(iTime)
	StartTimer()
end

function OnTimer()
	CheckForBan()
	CleanBan()
end

function CheckForBan()
	local file = io.input(fPermBan); 
	while 1 do 
		local sLine = io.read() 
		if (sLine == nil) then 
			break
		else
			local s,e,ip,reason = string.find(sLine, "(%S+)|(%S+%s+%S+%s+%S+)")
			if (ip ~= nil and reason ~= nil and reason == "3x bad password") then
				arrAux[ip] = 1
			end
		end
	end
	file:read()
end

function CleanBan()
	local ip,aux
	for ip, aux in arrAux do
		Unban(ip)
	end
end

soryy but i need a lil bit help in this.

Herodes

#7
Try smth like this .. I have cut down the function to suit the specific purpose...
!Note : This current version of the XMLFiletoTable isn't working on empty xmls...
if you want to see how an empty xml is then look at BanList.xml when the BanList in PtokaX is empty...
sBot = "Bad Password Cleaner"

sec = 1000
min = 60*sec
hour = 60*min
iTime = 5*min-- Checking Time

function Main()
	SetTimer(iTime)
	StartTimer()
end

function OnTimer()
	local t = XMLFiletoTable();
	for i,v in t.perm do
		if (v.ip and v.nick) then
			if (v.nick == "3x bad password") then
				UnBan(v.ip)
			end
		end
	end
end

--[[ -------------------------------------------------------------------------------
Author: Herodes
Thanks : RambitWombat for the Lua 4 XMLtoTable func

Usage : 
	table, time = XMLFiletoTable ( f ) 
		'table' is the file signified by 'f' as a table, 
		'time' the time took for parsing it (usually 0.00xxx s)
		'f' can be 1 =BanList.xml / 2 =Profiles.xml / 3 =RegisteredUsers.xml / 4 =ReservedNicks.xml / 5 =Scripts.xml
--]] -------------------------------------------------------------------------------
function XMLFiletoTable()
	local function get_Inner( sString, b ) --- based on the XMLtoTable provided by RambitWombat in the Lua Board
					if( not sString ) then return ""; end
					local s,e, sTag, sTemp = string.find(sString, "^%s*<([^>]+)>(.*)$");
					local sInner;
					-- we have a tag
					if (sTag) then
						sString = sTemp;
						local tTable = {};
						while sTag do
							s,e, sTag = string.find(sTag, "^(%S+)");
							s,e, sInner, sTemp = string.find(sString, "^(.-)(.*)$");
							if( not tTable[sTag] ) then tTable[sTag] = {}; end
							table.insert( tTable[sTag], get_Inner( sInner ) );
							if sTemp then
								s, e, sTag, sString = string.find( sTemp, "^%s*<([^>]-)>(.*)$");
							end
						end
						if table.getn(tTable) == 1 then return tTable[1]; end
						return tTable;
					else -- we have data
						return sString;
					end
				end

	local function FileToString( file )
			local m = ""; local f = io.open( file, "r");
			for line in f:lines() do
				if string.sub( line, 1, 5) ~= "

johny

hmm im getting this error now
Syntax D:\Robocop 10.01c\scripts\3XBadPassWord.xml.lua5.lua:17: `then' expected near `if'
 
- 
- 
- 
  192.168.1.25 
  3x bad password 
  
  
  

Herodes

Fixed,.. I was just showing an example of how to use it .. the rest of the script is yours...

johny

#10
fixed in the file above i hope, im not that good in scripting sorry sir.


Herodes

QuoteOriginally posted by johny
fixed in the file above i hope, im not that good in scripting sorry sir.
Yep fixed in the second script that I have posted on this thread that is a modification of the one you posted ;)

johny

damned man i think im realy stupid .

got this error now
Syntax D:\Robocop 10.01c\scripts\3XBadPassWord.xml.lua5.lua:18: unexpected symbol near `=

Herodes

QuoteOriginally posted by johny
damned man i think im realy stupid .

got this error now
Syntax D:\Robocop 10.01c\scripts\3XBadPassWord.xml.lua5.lua:18: unexpected symbol near `=
edited again the last posted code by me.. Btw this is a how-to, not an others-do thread...

jiten


Jelf

Hi I still have a problem using this, it seems to be todo with the directorys in the xml's, I get this error if I try to read the Banlist.xml
attempt to index field `permban' (a nil value)
even though the xml is correctly formatted and contains banned Nicks and IPs.

So, just to see what was happening I made the xml with only the Banlist directory which contained only IP and Nick and changed
return { ["perm"] = tbl.BanList.PermBans.Ban }
to
return { ["perm"] = tbl.BanList }
and the error goes, is there a problem reading the xml's?

jiten

I get the same error even though the perm/tempbanlist isn't empty.

Dessamator

QuoteOriginally posted by Herodes
QuoteOriginally posted by johny
damned man i think im realy stupid .

got this error now
Syntax D:\Robocop 10.01c\scripts\3XBadPassWord.xml.lua5.lua:18: unexpected symbol near `=
edited again the last posted code by me.. Btw this is a how-to, not an others-do thread...

well, it seems that it is a how-to, "how-to" get other ppl to do ur dirty work for u,  :D
Ignorance is Bliss.

NightLitch

well nice to see such good programming, but why don't you request simple PtokaX commands instead.

I have requested:


GetTempBanList() -- return table

GetBanList() -- return table

then with those a simple loop with:

GetBannedItemIp(Nick)
GetBannedItemName(IP)

GetTempBannedItemIp(Nick)
GetTempBannedItemName(IP)

can be done, it is a waste of rescources to parse an XML.

but good coding Herodes and Wombat.

not that it is wrong, but why not request 2 simple things...

well no more critic now... ;-)  Just wanted to say my view.

Cheers // NightLitch
//NL

Herodes

Yep we are missing those functions from the API.. I agree with NightLitch, I second his request.
.. it just felt nice to have a func doing it while we can't have those.

SMF spam blocked by CleanTalk