PtokaX forum

Development Section => HOW-TO's => Topic started by: Herodes on 30 March, 2005, 02:19:25

Title: HOW TO: Parse the PtokaX .xml files in a table ?
Post by: Herodes on 30 March, 2005, 02:19:25
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) ~= " end; f:close(); return m
end
return tFiles[mode][2]( get_Inner(FileToString(tFiles[mode]))), (os.clock()-x)
end
Title:
Post by: Jelf on 30 March, 2005, 09:15:40
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 `{'
Title:
Post by: jiten on 30 March, 2005, 09:36:13
This is what most of us was looking for  :D
Great work.

Cheers
Title:
Post by: Herodes on 30 March, 2005, 16:06:02
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 ;)
Title:
Post by: Jelf on 30 March, 2005, 20:31:18
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 :((
Title:
Post by: Herodes on 30 March, 2005, 21:35:46
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..
Title:
Post by: johny on 31 March, 2005, 15:43:40
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.
Title:
Post by: Herodes on 31 March, 2005, 16:55:09
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) ~= " end; f:close(); return m
end
local function getFin( tbl )
return { ["perm"] = tbl.BanList.PermBans.Ban, ["temp"] = tbl.BanList.TempBans.Ban }
end
return getFin(get_Inner(FileToString("../cfg/BanList.xml")));
end
Title:
Post by: johny on 31 March, 2005, 17:06:28
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
 

 

 
Title:
Post by: Herodes on 31 March, 2005, 17:09:15
Fixed,.. I was just showing an example of how to use it .. the rest of the script is yours...
Title:
Post by: johny on 31 March, 2005, 17:13:51
fixed in the file above i hope, im not that good in scripting sorry sir.

Title:
Post by: Herodes on 31 March, 2005, 17:17:06
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 ;)
Title:
Post by: johny on 31 March, 2005, 17:23:16
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 `=
Title:
Post by: Herodes on 31 March, 2005, 18:23:43
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...
Title:
Post by: jiten on 31 March, 2005, 18:34:53
:D
Title:
Post by: Jelf on 01 April, 2005, 13:01:19
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.xmlattempt 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?
Title:
Post by: jiten on 01 April, 2005, 13:28:01
I get the same error even though the perm/tempbanlist isn't empty.
Title:
Post by: Dessamator on 01 April, 2005, 18:46:27
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
Title:
Post by: NightLitch on 01 April, 2005, 19:44:40
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
Title:
Post by: Herodes on 01 April, 2005, 21:14:53
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.