PtokaX forum

Lua 5.3/5.2/5.1 Scripts (for PtokaX 0.4.0.0 and newer) => Conversion Requests => Topic started by: miago on 21 April, 2008, 12:24:34

Title: Help needed to convert UserDB 2.0 LUA 5.0/5.1 by Mutor
Post by: miago on 21 April, 2008, 12:24:34
Just me again.
Searched for this one too, but havent been able to find it, maybe i missed it.
Thx

--[[
UserDB 2.0 LUA 5.0/5.1

by Mutor 02/07/07

A complete rewrite of an older script.
Saves custom user data to file

- User data saved to file for script/hub restarts
- Option to add/query/delete user info
- Query now case insensitive, returns all matches
- Fields may be left blank
- Provides context menus [right click]
- Option to always reply in PM

UserDB 2.0 Commands
???????????????????????????????????
+hlhelp         UserDB 2.0 Commands
+dbwrite        Add/Edit An Entry
+dbdelete       Delete A User
+dbread         Query Database

???????????????????????????????????
]]

DbCfg = {
-- Botname pulled from the hub [frmHub:GetHubBotName()] or use "CustomName"
Bot = frmHub:GetHubBotName(),
-- Bot description
Desc = "UserDB 2.0 ".._VERSION,
-- Bot email address
Mail = "user@domain.com",
-- Admins nick for status / error messages
AdminNick = "Admin",
-- File to save player scores
File = "Userdb2.dat",
-- Always reply in PM" "yes","no"
PmReply = "yes",
-- Context Menu Title
Menu = "- RC -",
--Context Submenu Title
SubMenu = "- Sub -",
--Set your profiles permissions here.
--profile_idx, menus/commands enabled [0=no 1=yes], "Profile Name"
Profiles = {
[-1] = {0,"Unregistered User"},
[0] = {1,"Master"},
[1] = {0,"Operator"},
[2] = {0,"Vip"},
[3] = {0,"Registered User"},
[4] = {0,"Moderator"},
[5] = {1,"NetFounder"},
[6] = {1,"Owner"},
},

-- User 'Nick' will be used as index. ***Please do not add it here!
-- Add and or delete as many fields as you wish.
-- If you change this table, delete 'DbCfg.File' then save/restart script
-- Command(s) will be updated once script is saved/restarted
Columns = {
"Password",
"Regdate",
"Regby",
"Invitedby",
"Email",
"Other",
},
}

Main = function()
table.insert(DbCfg.Columns,1,"nick")
DbCfg.Script = DbCfg.SubMenu.." for PtokaX ".._VERSION.." by Mutor"
if DbCfg.Bot ~= frmHub:GetHubBotName() or frmHub:GetHubBot()== 0 then
frmHub:RegBot(DbCfg.Bot, 1, DbCfg.Desc.." for PtokaX", DbCfg.Mail)
end
local LuaVer = string.sub(_VERSION,1,7)
if LuaVer == "Lua 5.1" then
DbCfg.Gc,DbCfg.MemOp = "collect",collectgarbage("count")
elseif LuaVer == "Lua 5.0" then
DbCfg.Gc,DbCfg.MemOp = nil,gcinfo()
else
OnError("This script is incompatible with ".._VERSION)
end
if loadfile(DbCfg.File) then
dofile(DbCfg.File)
else
DbCfg.Db = {}
SaveToFile(DbCfg.File,DbCfg.Db,"DbCfg.Db")
end
OnError(DbCfg.Script.." has been started. Memory usage: "..CleanMem())
end

OnError = function(msg)
SendToNick(DbCfg.AdminNick,"<"..DbCfg.Bot.."> "..msg)
end

OnExit = function()
OnError(DbCfg.Script.." has been stopped, freeing "..CleanMem().." of memory.")
end

NewUserConnected = function(user)
if SendCmds(user) then
user:SendData(DbCfg.Bot,DbCfg.Profiles[user.iProfile][2].."'s "..DbCfg.SubMenu.." "..
"commands enabled. Right click hub tab for command menu.")
end
end
OpConnected = NewUserConnected

ChatArrival = function(user,data)
if DbCfg.Profiles[user.iProfile] and DbCfg.Profiles[user.iProfile][1] == 1 then
data = string.sub(data,1,-2)
local s,e,cmd = string.find(data,"%b<>%s%p(%w+)")
if cmd then
cmd = string.lower(cmd)
if cmd and Cmds[cmd] then
if DbCfg.PmReply == "yes" then
return user:SendPM(DbCfg.Bot,Cmds[cmd](user, data)),1
else
return user:SendData(DbCfg.Bot,Cmds[cmd](user, data)),1
end
end
end
end
end
ToArrival = ChatArrival

CleanMem = function()
DbCfg.Mem = string.format("%-.2f Kb.",DbCfg.MemOp)
return DbCfg.Mem
end

Serialize = function(tTable, sTableName, sTab)
assert(tTable, "tTable equals nil")
assert(sTableName, "sTableName equals nil")
assert(type(tTable) == "table", "tTable must be a table!")
assert(type(sTableName) == "string", "sTableName must be a string!")
sTab = sTab or ""
sTmp = ""
sTmp = sTmp..sTab..sTableName.." = {\n"
for key, value in pairs(tTable) do
local sKey = (type(key) == "string") and string.format("[%q]",key) or string.format("[%d]",key)
if(type(value) == "table") then
sTmp = sTmp..Serialize(value, sKey, sTab.."\t")
else
local sValue = (type(value) == "string") and string.format("%q",value) or tostring(value)
sTmp = sTmp..sTab.."\t"..sKey.." = "..sValue
end
sTmp = sTmp..",\n"
end
sTmp = sTmp..sTab.."}"
return sTmp
end

SaveToFile = function(File , Table , Tablename)
local handle = io.open(File,"w")
handle:write(Serialize(Table, Tablename))
handle:flush()
handle:close()
CleanMem()
end

Build = function()
local s1,s2 = "",""
for i,v in ipairs(DbCfg.Columns) do
s1 = s1.." <"..v..">"
s2 = s2..", %[line:"..v.."]"
end
return s1,s2
end

SendCmds = function(user)
if DbCfg.Profiles[user.iProfile] and DbCfg.Profiles[user.iProfile][1] == 1 then
for i,v in pairs(Cmds) do
if i ~= "int" then
local m,s,pfx = DbCfg.Menu,DbCfg.SubMenu,frmHub:GetPrefixes()[1]
local c,n,pm,p = "$UserCommand 1","$<%[mynick]>","$$To: "..
DbCfg.Bot.." From: %[mynick] ","&#124;"
local d,a1,a2 = Cmds[i]()
user:SendData(c.." 1 "..m.."\\"..s.."\\"..d..n.." "..pfx..i..a1..p)
user:SendData(c.." 2 "..m.."\\"..s.."\\"..d..pm..n.." "..pfx..i..a2..p)
end
end
CleanMem()
return true
else
return false
end
end

Cmds = {
dbwrite = function(user,data)
if user then
local tab,pat,usr = {},"%b<>%s%p%w+"
local a,b = 0,table.getn(DbCfg.Columns)
for i,v in ipairs(DbCfg.Columns) do
pat = string.gsub(pat,"[%(%)]","").."%,([%w%s%.%_%-%@%[%]%{%}]+)"
local s,e,z = string.find(data,pat)
local e = DbCfg.Columns[i-1]
if z then
z = string.gsub(z,"^%s+","")
a = a + 1
if v == "nick" then
usr = z
else
tab[v] = z
end
else
return "Invalid charachters in field: "..e
end
end
if usr and a == b then
local status,str = "added",""
if DbCfg.Db[usr] then
status = "edited"
end
DbCfg.Db[usr] = tab
SaveToFile(DbCfg.File,DbCfg.Db,"DbCfg.Db")
for i,v in pairs(DbCfg.Db[usr]) do
str = str.."\t[ "..i.." ] = "..v.."\r\n"
end
return "\r\n\r\n\tThe user *"..usr.."* was "..status..".\r\n\r\n"..str
end
else
local s1,s2 = Build()
return "Add/Edit An Entry",s2,s2
end
end,
dbdelete = function(user,data)
if user then
local s,e,usr = string.find(data,"%b<>%s%p%w+%s(%S+)")
local str = ""
if usr then
for x,_ in pairs(DbCfg.Db) do
local z = string.lower(x)
if z == string.lower(usr) then
z = x
DbCfg.Db[x] = nil
SaveToFile(DbCfg.File,DbCfg.Db,"DbCfg.Db")
str =  "The user *"..z.."* has been deleted"
end
end
if str ~= "" then
return str
else
return "Sorry "..user.sName..", "..usr..
" was not found in the database."
end
end
else
return "Delete A User"," %[line:User Name ]"," %[line:User Name]"
end
end,
dbread = function(user,data)
if user then
local s,e,usr = string.find(data,"%b<>%s%p%w+%s(%S+)")
local str,qry = "",""
if usr then
for x,y in pairs(DbCfg.Db) do
local z = string.lower(x)
if string.find(z,string.lower(usr)) then
str = str.."\r\n\r\n\tDetails for user *"..x.."*\r\n\r\n"
for i,v in pairs(y) do
str = str.."\t[ "..i.." ] = "..v.."\r\n"
end
if string.len(usr) ~= string.len(x) then
qry = "\r\n\r\n\tQuery string: "..
string.format("%q",usr).."\r\n\r\n"
end
end
end
if str ~= "" then
return str..qry
else
return "Sorry "..user.sName..", no pattern of "..usr..
" was found in the database."
end
else
for a,b in pairs(DbCfg.Db) do
str = str.."\r\n\r\n\tDetails for user *"..a.."*\r\n\r\n"
for c,d in pairs(b) do
str = str.."\t[ "..c.." ] = "..d.."\r\n"
end
end
return str
end
else
return "Query Database"," %[line:User Name (None for all)]"," %[line:User Name (None for all)]"
end
end,
hlhelp = function(user,data)
if user then
local reply = "\n\n\t\t"..DbCfg.SubMenu.." Commands\n\t"..string.rep("?",35).."\n"
for i,v in pairs(Cmds) do
if i ~= "int" then
local desc,args = Cmds[i]()
reply = reply.."\t+"..string.format("%-15s",i).."\t"..desc.."\n"
end
end
return reply.."\n\t"..string.rep("?",35).."\n\n"
else
return DbCfg.SubMenu.." Commands","",""
end
end,
}
Title: Re: Help needed to convert UserDB 2.0 LUA 5.0/5.1 by Mutor
Post by: miago on 27 June, 2008, 08:24:02
Hi.

Gonna make an own try to convert this, hope it is ok when I get stucked to post the lua here as far as I've been able to convert, and maybe get some help.
If it should be posted somewhere else, please let me know.

cheers
miago

I rly have no hope on getting it to work, but better try than do nothing  ;)
Title: Re: Help needed to convert UserDB 2.0 LUA 5.0/5.1 by Mutor
Post by: miago on 27 June, 2008, 14:41:21
Ok.

Ran the converter and I've almost got it I think.
I can add, delete, edit and query. The db gets done and saved. RC meny appears only for masters. So far so good  :)

But, when adding, editing, deleting or querying, the commandline that is being done after filling in the fields, shows up in MC both for masters and regs. How do I do to get rid of that?  ???

Ex:
Here Ive added and deleted and searched via RC-commands.
[2008-06-27-14:10:22] <-[Admin]> !dbwrite, richard, marmelade, 2008-06-25, Admin, test3,old friend ,
[2008-06-27-14:11:10] <-[Admin]> !dbwrite, 2ndrichard, missfit, 2008-06-25, Admin, Admin, ,
[2008-06-27-14:11:30] <-[Admin]> !dbwrite, max, burger, 2008-06-25, Admin, 2ndrichard,richardsbestfriend ,
[2008-06-27-14:11:39] <-[Admin]> !dbread rich
[2008-06-27-14:11:55] <-[Admin]> !dbread
[2008-06-27-14:12:08] <-[Admin]> !dbdelete max
[2008-06-27-14:12:18] <-[Admin]> !dbread

Here Ive written the command in MC and it shouldnt be shown.
[2008-06-27-14:31:46] <-[Admin]>  +hlhelp
[2008-06-27-14:32:21] <-[Admin]>  !hlhelp

Would be rly thankful for help with this problem.  :-*
Cheers
miago


--[[
UserDB 2.0 LUA 5.0/5.1

by Mutor 02/07/07

A complete rewrite of an older script.
Saves custom user data to file

- User data saved to file for script/hub restarts
- Option to add/query/delete user info
- Query now case insensitive, returns all matches
- Fields may be left blank
- Provides context menus [right click]
- Option to always reply in PM

Conversion to new API  with Ptokax LUA API Converter v0.9 by miago  (1st try)       25/06/08

UserDB 2.0 Commands
???????????????????????????????????
+hlhelp         UserDB 2.0 Commands
+dbwrite        Add/Edit An Entry
+dbdelete       Delete A User
+dbread         Query Database

???????????????????????????????????
]]--

DbCfg = {
-- Botname pulled from the hub [frmHub:GetHubBotName()] or use "CustomName"
Bot = SetMan.GetString(21),
-- Bot description
Desc = "UserDB 2.0 ".._VERSION,
-- Bot email address
Mail = "user@domain.com",
-- Admins nick for status / error messages
AdminNick = "Admin",
-- Command Prefix
Prefix = "!",
-- File to save player scores
File = "Userdb2.dat",
-- Always reply in PM" "yes","no"
PmReply = "yes",
-- Context Menu Title
Menu = "- RC -",
--Context Submenu Title
SubMenu = "- Sub -",
--Set your profiles permissions here.
--profile_idx, menus/commands enabled [0=no 1=yes], "Profile Name"
Profiles = {
[-1] = {0,"Unregistered User"},
[0] = {1,"Master"},
[1] = {0,"Operator"},
[2] = {0,"Vip"},
[3] = {0,"Registered User"},
[4] = {0,"Moderator"},
[5] = {1,"NetFounder"},
[6] = {1,"Owner"},
},

-- User 'Nick' will be used as index. ***Please do not add it here!
-- Add and or delete as many fields as you wish.
-- If you change this table, delete 'DbCfg.File' then save/restart script
-- Command(s) will be updated once script is saved/restarted
Columns = {
"Password",
"Regdate",
"Regby",
"Invitedby",
"Email",
"Other",
},
}

OnStartup = function()
table.insert(DbCfg.Columns,1,"nick")
DbCfg.Script = DbCfg.SubMenu.." for PtokaX ".._VERSION.." by Mutor"
if DbCfg.Bot ~= SetMan.GetString(21) or (SetMan.GetBool(17) and 1 or 0)== 0 then
Core.RegBot(DbCfg.Bot,DbCfg.Desc.." for PtokaX",DbCfg.Mail,true)
end
local LuaVer = string.sub(_VERSION,1,7)
if LuaVer == "Lua 5.1" then
DbCfg.Gc,DbCfg.MemOp = "collect",collectgarbage("count")
elseif LuaVer == "Lua 5.0" then
DbCfg.Gc,DbCfg.MemOp = nil,collectgarbage(count)
else
OnError("This script is incompatible with ".._VERSION)
end
if loadfile(DbCfg.File) then
dofile(DbCfg.File)
else
DbCfg.Db = {}
SaveToFile(DbCfg.File,DbCfg.Db,"DbCfg.Db")
end
OnError(DbCfg.Script.." has been started. Memory usage: "..CleanMem())
end

OnError = function(msg)
Core.SendToNick(DbCfg.AdminNick,"<"..DbCfg.Bot.."> "..msg)
end

OnExit = function()
OnError(DbCfg.Script.." has been stopped, freeing "..CleanMem().." of memory.")
end

UserConnected = function(user)
Core.GetUserAllData(user)
if SendCmds(user) then
Core.SendToNick(user.sNick,"<"..DbCfg.Bot.."> "..DbCfg.Profiles[user.iProfile][2].."'s "..DbCfg.SubMenu.." "..
"commands enabled. Right click hub tab for command menu.")
end
end
OpConnected = UserConnected
RegConnected = UserConnected

ChatArrival = function(user,data)
Core.GetUserAllData(user)
if DbCfg.Profiles[user.iProfile] and DbCfg.Profiles[user.iProfile][1] == 1 then
data = string.sub(data,1,-2)
local s,e,cmd = string.find(data,"%b<>%s%p(%w+)")
if cmd then
cmd = string.lower(cmd)
if cmd and Cmds[cmd] then
if DbCfg.PmReply == "yes" then
return Core.SendPmToNick(user.sNick,DbCfg.Bot,Cmds[cmd](user, data)),1
else
return Core.SendToNick(user.sNick,"<"..DbCfg.Bot.."> "..Cmds[cmd](user, data)),1
end
end
end
end
end
ToArrival = ChatArrival

CleanMem = function()
DbCfg.Mem = string.format("%-.2f Kb.",DbCfg.MemOp)
return DbCfg.Mem
end

Serialize = function(tTable, sTableName, sTab)
assert(tTable, "tTable equals nil")
assert(sTableName, "sTableName equals nil")
assert(type(tTable) == "table", "tTable must be a table!")
assert(type(sTableName) == "string", "sTableName must be a string!")
sTab = sTab or ""
sTmp = ""
sTmp = sTmp..sTab..sTableName.." = {\n"
for key, value in pairs(tTable) do
local sKey = (type(key) == "string") and string.format("[%q]",key) or string.format("[%d]",key)
if(type(value) == "table") then
sTmp = sTmp..Serialize(value, sKey, sTab.."\t")
else
local sValue = (type(value) == "string") and string.format("%q",value) or tostring(value)
sTmp = sTmp..sTab.."\t"..sKey.." = "..sValue
end
sTmp = sTmp..",\n"
end
sTmp = sTmp..sTab.."}"
return sTmp
end

SaveToFile = function(File , Table , Tablename)
local handle = io.open(File,"w")
handle:write(Serialize(Table, Tablename))
handle:flush()
handle:close()
CleanMem()
end

Build = function()
local s1,s2 = "",""
for i,v in ipairs(DbCfg.Columns) do
s1 = s1.." <"..v..">"
s2 = s2..", %[line:"..v.."]"
end
return s1,s2
end

SendCmds = function(user)
if DbCfg.Profiles[user.iProfile] and DbCfg.Profiles[user.iProfile][1] == 1 then
for i,v in pairs(Cmds) do
if i ~= "int" then
--function getcmdprefixarray() local p = {} for i=1,#SetMan.GetString(29) do table.insert(p,SetMan.GetString(29):sub(i,i)) end end
local m,s,pfx = DbCfg.Menu,DbCfg.SubMenu,DbCfg.Prefix
local c,n,pm,p = "$UserCommand 1","$<%[mynick]>","$$To: "..
DbCfg.Bot.." From: %[mynick] ","&#124;"
local d,a1,a2 = Cmds[i]()
Core.SendToNick(user.sNick,c.." 1 "..m.."\\"..s.."\\"..d..n.." "..pfx..i..a1..p)
Core.SendToNick(user.sNick,c.." 2 "..m.."\\"..s.."\\"..d..pm..n.." "..pfx..i..a2..p)
end
end
CleanMem()
return true
else
return false
end
end

Cmds = {
dbwrite = function(user,data)
if user then
local tab,pat,usr = {},"%b<>%s%p%w+"
local a,b = 0,#DbCfg.Columns
for i,v in ipairs(DbCfg.Columns) do
pat = string.gsub(pat,"[%(%)]","").."%,([%w%s%.%_%-%@%[%]%{%}]+)"
local s,e,z = string.find(data,pat)
local e = DbCfg.Columns[i-1]
if z then
z = string.gsub(z,"^%s+","")
a = a + 1
if v == "nick" then
usr = z
else
tab[v] = z
end
else
return "Invalid charachters in field: "..e
end
end
if usr and a == b then
local status,str = "added",""
if DbCfg.Db[usr] then
status = "edited"
end
DbCfg.Db[usr] = tab
SaveToFile(DbCfg.File,DbCfg.Db,"DbCfg.Db")
for i,v in pairs(DbCfg.Db[usr]) do
str = str.."\t[ "..i.." ] = "..v.."\r\n"
end
return "\r\n\r\n\tThe user *"..usr.."* was "..status..".\r\n\r\n"..str
end
else
local s1,s2 = Build()
return "Add/Edit An Entry",s2,s2
end
end,
dbdelete = function(user,data)
if user then
local s,e,usr = string.find(data,"%b<>%s%p%w+%s(%S+)")
local str = ""
if usr then
for x,_ in pairs(DbCfg.Db) do
local z = string.lower(x)
if z == string.lower(usr) then
z = x
DbCfg.Db[x] = nil
SaveToFile(DbCfg.File,DbCfg.Db,"DbCfg.Db")
str =  "The user *"..z.."* has been deleted"
end
end
if str ~= "" then
return str
else
return "Sorry "..user.sNick..", "..usr..
" was not found in the database."
end
end
else
return "Delete A User"," %[line:User Name ]"," %[line:User Name]"
end
end,
dbread = function(user,data)
if user then
local s,e,usr = string.find(data,"%b<>%s%p%w+%s(%S+)")
local str,qry = "",""
if usr then
for x,y in pairs(DbCfg.Db) do
local z = string.lower(x)
if string.find(z,string.lower(usr)) then
str = str.."\r\n\r\n\tDetails for user *"..x.."*\r\n\r\n"
for i,v in pairs(y) do
str = str.."\t[ "..i.." ] = "..v.."\r\n"
end
if string.len(usr) ~= string.len(x) then
qry = "\r\n\r\n\tQuery string: "..
string.format("%q",usr).."\r\n\r\n"
end
end
end
if str ~= "" then
return str..qry
else
return "Sorry "..user.sNick..", no pattern of "..usr..
" was found in the database."
end
else
for a,b in pairs(DbCfg.Db) do
str = str.."\r\n\r\n\tDetails for user *"..a.."*\r\n\r\n"
for c,d in pairs(b) do
str = str.."\t[ "..c.." ] = "..d.."\r\n"
end
end
return str
end
else
return "Query Database"," %[line:User Name (None for all)]"," %[line:User Name (None for all)]"
end
end,
hlhelp = function(user,data)
if user then
local reply = "\n\n\t\t"..DbCfg.SubMenu.." Commands\n\t"..string.rep("?",35).."\n"
for i,v in pairs(Cmds) do
if i ~= "int" then
local desc,args = Cmds[i]()
reply = reply.."\t+"..string.format("%-15s",i).."\t"..desc.."\n"
end
end
return reply.."\n\t"..string.rep("?",35).."\n\n"
else
return DbCfg.SubMenu.." Commands","",""
end
end,
}

Title: Re: Help needed to convert UserDB 2.0 LUA 5.0/5.1 by Mutor
Post by: Yahoo on 09 August, 2008, 16:54:26
Hello Mutor
Is it possible to make some upgradation in this script

1)All Columns must be optional to profile
for eg the password column which must be only seen by masters and not reg users

2)Compulsory/optional Birthdate column
this will eliminate the use of additional birthdate script (but must have same features as birthdayman for eg +Todaybirthdays)

3)When a user types +dbread he must also shown ip and lastseen of that user
for eg
Nick :      PoTtEr
Birthday:    26 October
IP:      10.12.218.67
Lastseen:   Currently Online


Thanks in Advance
Title: Re: Help needed to convert UserDB 2.0 LUA 5.0/5.1 by Mutor
Post by: Yahoo on 11 August, 2008, 19:30:24
Thanks a lot Mutor