PtokaX forum

Lua 5.3/5.2/5.1 Scripts (for PtokaX 0.4.0.0 and newer) => Finished Scripts => Topic started by: Thor on 07 June, 2008, 15:29:29

Title: RegBot (Verli style) [API2]
Post by: Thor on 07 June, 2008, 15:29:29
Converted, but haven't been tested

--[[
Register bot by Hungarista
If you don't like asking users to give password, use this script, it register the users as Verli does:
Users will be registered, and they have to set up their passwords by a simple command. If they don't write
password, the script generate one, and they can also use this generator. Commands are the same as in Verli:
!regnew (!rn) <nick> <profile>
!regdelete (!rd) <nick>
!reglist [<profile/all>]
!regpasswd <nick> <pass>
!regenable
!regdisable
!regme <pass>
Made for PtokaX 0.3.5.2. (LUA5.11)
Version 1.2
Used codes: Mutor's password-generator, Madman's table-commands
############################################### Changelog ###############################################

@@@ 2006. 12. 15. (v1.0) @@@
Main script is ready

@@@ 2006. 12. 16. (v1.1) @@@
Added: !regpasswd command to change user's password (requested by -SkA-)
Added: !regclass command to change registered users's profile
Changed: Code optimization for lower memory & CPU usage, thanks the advices for NightLitch

@@@ 2006. 12. 17. (v1.2) @@@
Added: !help command to see what commands are avaible for this script
Changed: !reglist command to list all registered users (or just the given profile) (requested by Alexinno)
Changed: Many part cleaned up and became easier than before
Changed: Table-commands return values (help need't to be discard)

@@@ 2oo7. 04. 26. @@@
Added classdiff function by Doni

##########################################################################################################
]]

------------- Settings Start ---------------

-- Change the order if needed. If you have KVip instead of Moderator (that's lower as Operator) then
-- [4] = 3, [1] = 4, [0] = 5, stb...
-- If you have no such profile, comment that line (--)
tClasses = {
[-1] = 0, -- UnRegistered
[3] = 1, -- Registered
[2] = 2, -- VIP
[1] = 3, -- Operator
[0] = 4, -- Master
--[4] = 5, -- Moderator
--[5] = 6, -- NetFounder
--[6] = 7, -- Owner
}
Class_Diff = 1 -- minimum "profile difference" for registration. Highest class won't be checked... (in this case: Master)

------------- Settings End ---------------
function GetPermission(UserProfileID,RegProfileID)
if tClasses[UserProfileID] and tClasses[UserProfileID] == (GetSize(tClasses)-1) then return true end
if RegProfileID and tClasses[RegProfileID] then
if ((tClasses[RegProfileID] + Class_Diff) > tClasses[UserProfileID]) then
return false
else
return true
end
else
return false
end
end

function OnStartup()
sBot = SetMan.GetString(21)
if loadfile("regusers.db") then
dofile("regusers.db")
else
tRegisteredUsers = {}
end
tmr = TmrMan.AddTimer(1000)
bAutoRegme = true
end

function ChatArrival(curUser,data)
data = data:sub(1,-2)
_,_,data = data:find("%b<>%s*(.*)")
local _,_,cmd = data:find("^%p(%S+)")
if cmd then
if Core.GetUserData(curUser,11) then
local tCmds = {
["regnew"] = function(curUser, data)
local _,_,nick,class = data:find("%p%S+%s(%S+)%s*(%S*)")
if nick then
if class == "" then class = "reg" end
if RegMan.GetReg(nick) then
Core.SendPmToNick(curUser.sNick,sBot,nick.." is already a registered user as "..ProfMan.GetProfile(RegMan.GetReg(nick).iProfile).sProfileName..". If you "..
"want to register him/her into a new class, please delete him/her with the !regdelete <nick> command.")
else
if not tRegisteredUsers[nick] then
if ProfMan.GetProfile(class) and ProfMan.GetProfile(class).iProfileNumber ~= -1 then
                                if GetPermission(curUser.iProfile,ProfMan.GetProfile(class).iProfileNumber) then
tRegisteredUsers[nick] = {["nick"] = nick, ["class"] = ProfMan.GetProfile(class).iProfileNumber}
Core.SendPmToNick(curUser.sNick,sBot,nick.." has been successfully registered as "..class..".")
local user = Core.GetUser(nick)
if user then
Core.SendPmToNick(user.sNick,sBot,"You have been registered as "..class..", please set up your password NOW\r\n"..
"Use command +passwd <your_passwd> or just +passwd to enable the script to generate you a random and "..
"safe password. To make a safe password use +genpass <length> command where length is an integer between 4 and 20.")
end
else
Core.SendPmToNick(curUser.sNick,sBot,"You have no rights to use this command!")
end
else
                                local profiles = {}
for k,v in pairs(ProfMan.GetProfiles()) do
table.insert(profiles,v.sProfileName)
end
Core.SendPmToNick(curUser.sNick,sBot,"There is no "..class.." userclass. (Avaible are: "..table.concat(profiles,", ")..")")
end
else
Core.SendPmToNick(curUser.sNick,sBot,nick.." is already registered user.")
end
end
else
Core.SendPmToNick(curUser.sNick,sBot,"Usage: !regnew <nick> <class>")
end
return true
end,
["regdelete"] = function(curUser,data)
local _,_,nick = data:find("%p%S+%s(%S+)")
if nick then
if tRegisteredUsers[nick] then
                        if GetPermission(curUser.iProfile,tRegisteredUsers[nick]["class"]) then
tRegisteredUsers[nick] = nil
Core.SendPmToNick(curUser.sNick,sBot,nick.."'s registration has been deleted.")
else
Core.SendPmToNick(curUser.sNick,sBot,"You have no right to use this command!")
end
elseif RegMan.GetReg(nick) then
if GetPermission(curUser.iProfile,(RegMan.GetReg(nick) and RegMan.GetReg(nick).iProfile)) then
local RegUser = GetItem
RegMan.DelReg(nick)
Core.SendPmToNick(curUser.sNick,sBot,nick.."'s registration has been deleted.")
else
Core.SendPmToNick(curUser.sNick,sBot,"You have no right to use this command!")
end
else
Core.SendPmToNick(curUser.sNick,sBot,nick.." is not in the registered users list.")
end
else
Core.SendPmToNick(curUser.sNick,sBot,"Usage: !regdelete <nick>")
end
return true
end,
["reglist"] = function(curUser,data)
local _,_,profile = string.find(data,"%p%S+%s(%S+)")
if not profile then
if GetSize(tRegisteredUsers) > 0 then
local msg = "Currently registered, but passwordless users are:\r\n"
for k in pairs(tRegisteredUsers) do
msg = msg.."• "..tRegisteredUsers[k]["nick"].." - "..ProfMan.GetProfile(tRegisteredUsers[k]["class"]).sProfileName.."\r\n"
end
Core.SendPmToNick(curUser.sNick,sBot,msg)
else
Core.SendPmToNick(curUser.sNick,sBot,"The list is empty.")
end
elseif ProfMan.GetProfile(profile).iProfileNumber <= #ProfMan.GetProfiles() and ProfMan.GetProfile(profile).iProfileNumber ~= -1 then
local msg = "All users whoes are registered as "..ProfMan.GetProfile(profile).sProfileName..":\r\n"
for i,v in ipairs(RegMan.GetRegs()) do
if GetSize(v) > 0 then
if RegMan.GetReg(v["sNick"]).iProfile == ProfMan.GetProfile(profile) then
msg = msg.." • "..v["sNick"].."\r\n"
end
else
msg = "There is no user registered as "..profile
end
end
Core.SendPmToNick(curUser.sNick,sBot,msg)
elseif profile == "all" then
local tUsers = {}
local msg = "Registered users on the hub:\r\n  Profile\t\tNickname\r\n"
for i,v in ipairs(RegMan.GetRegs()) do
tUsers[v["sNick"]] = ProfMan.GetProfile(v["iProfile"]).sProfileName
end
for i,v in ipairs(ProfMan.GetProfiles()) do
msg = msg.."\r\n"..string.rep("Z",40).."\r\n\t\t"..v.sProfileName.." users\r\n"..string.rep("Z",40).."\r\n"
for k in SortByKeys(tUsers) do
local space = "\t\t"
if string.len(tUsers[k]) > 6 then space = "\t" end
if tUsers[k] == v.sProfileName then
msg = msg.." • "..tUsers[k]..space..k.."\r\n"
end
end
end
Core.SendPmToNick(curUser.sNick,sBot,msg)
else
                    local profiles = {}
                    for k,v in pairs(ProfMan.GetProfiles()) do
table.insert(profiles,v.sProfileName)
end
Core.SendPmToNick(curUser.sNick,sBot,"Usage: !reglist [<profile/all>] Avaible profiles are: "..table.concat(profiles,", "))
end
return true
end,
["regpasswd"] = function(curUser,data)
local _,_,nick,passwd = string.find(data,"%p%S+%s(%S+)%s*(%S*)")
if passwd == "" then passwd = PassCode(8) end
if nick then
if RegMan.GetReg(nick) then
                        if GetPermission(curUser.iProfile,RegMan.GetReg(nick).iProfile) then
RegMan.ChangeReg(nick,passwd,RegMan.GetReg(nick).iProfile)
local user = Core.GetUser(nick,true)
if user then
Core.SendPmToNick(user.sNick,sBot,"Your password has been changed to "..passwd.." Please modify it on the favorite hubs.")
end
Core.SendPmToNick(curUser.sNick,sBot,nick.."'s password changed to: "..passwd)
else
Core.SendPmToNick(curUser.sNick,sBot,"You have no right to use this command!")
end
elseif tRegisteredUsers[nick] then
                        if GetPermission(curUser.iProfile,tRegisteredUsers[nick]["class"]) then
RegMan.ChangeReg(nick,passwd,tRegisteredUsers[nick]["class"])
tRegisteredUsers[nick] = nil
local user = Core.GetUser(nick,true)
if user then
Core.SendPmToNick(user.sNick,sBot,"Your password has been set up: "..passwd)
end
Core.SendPmToNick(curUser.sNick,sBot,nick.."'s password has been set up: "..passwd)
else
Core.SendPmToNick(curUser.sNick,sBot,"You have no right to use this command!")
end
else
Core.SendPmToNick(curUser.sNick,sBot,nick.." isn't registered user, please register first!")
end
else
Core.SendPmToNick(curUser.sNick,sBot,"Usage: !regpasswd <nick> <password>")
end
return true
end,
["regclass"] = function(curUser,data)
local _,_,nick,class = data:find("%p%S+%s(%S+)%s+(%S+)")
if nick then
                    if RegMan.GetReg(nick) then
if GetPermission(curUser.iProfile,RegMan.GetReg(nick).iProfile) then
if RegMan.GetReg(nick).iProfile == ProfMan.GetProfile(class).iProfileNumber then
Core.SendPmToNick(curUser.sNick,sBot,nick.."'s profile is still "..class)
else
if ProfMan.GetProfile(class).iProfileNumber <= #ProfMan.GetProfiles() then
RegMan.ChangeReg(nick,RegMan.GetReg(nick).sPassword,ProfMan.GetProfile(class) and ProfMan.GetProfile(class).iProfileNumber)
Core.SendPmToNick(curUser.sNick,sBot,nick.."'s profile has been changed to "..class)
else
                                    local profiles = {}
for k,v in pairs(ProfMan.GetProfiles()) do
table.insert(profiles,v.sProfileName)
end
Core.SendPmToNick(curUser.sNick,sBot,"There is no "..class.." class. (Avaible are: "..table.concat(profiles,", ")..")")
end
end
else
Core.SendPmToNick(curUser.sNick,sBot,"You have no right to use this command!")
end
elseif tRegisteredUsers[nick] then
if GetPermission(curUser.iProfile,tRegisteredUsers[nick]["class"]) then
if RegMan.GetReg(nick).iProfile == ProfMan.GetProfile(class).iProfileNumber then
Core.SendPmToNick(curUser.sNick,sBot,nick.."'s profile is still "..class)
else
if ProfMan.GetProfile(class).iProfileNumber <= #ProfMan.GetProfiles() then
tRegisteredUsers[nick]["class"] = ProfMan.GetProfile(class).iProfileNumber
Core.SendPmToNick(curUser.sNick,sBot,nick.."'s profile has been changed to "..class)
else
                                    local profiles = {}
for k,v in pairs(ProfMan.GetProfiles()) do
table.insert(profiles,v.sProfileName)
end
Core.SendPmToNick(curUser.sNick,sBot,"There is no "..class.." class. (Avaible are: "..table.concat(profiles,", ")..")")
end
end
else
Core.SendPmToNick(curUser.sNick,sBot,"You have no right to use this command!")
end
else
Core.SendPmToNick(curUser.sNick,sBot,nick.." isn't registered user, please register first!")
end
else
Core.SendPmToNick(curUser.sNick,sBot,"Usage: !regclass <nick> <oszt?ly>")
end
return true
end,
["regenable"] = function(curUser,data)
if bAutoRegme then
Core.SendPmToNick(curUser.sNick,sBot,"Auto-register is already enabled.")
else
bAutoRegme = true
Core.SendPmToNick(curUser.sNick,sBot,"Auto-register has been enabled.")
end
return 1
end,
["regdisable"] = function(curUser,data)
if bAutoRegme then
bAutoRegme = nil
Core.SendPmToNick(curUser.sNick,sBot,"Auto-register has been disabled.")
else
Core.SendPmToNick(curUser.sNick,sBot,"Auto-register is already disabled.")
end
return 1
end,
}
tCmds["rn"] = tCmds["regnew"] tCmds["rd"] = tCmds["regdelete"] tCmds["rl"] = tCmds["reglist"]
tCmds["rp"] = tCmds["regpasswd"] tCmds["rc"] = tCmds["regclass"]
if tCmds[cmd] then
return tCmds[cmd](curUser,data)
end
end
local tCmds = {
["passwd"] = function(curUser,data)
local _,_,passwd = data:find("^%p%S+%s(%S+)")
if tRegisteredUsers[curUser.sNick] then
if not passwd then passwd = PassCode(8) end
RegMan.AddReg(curUser.sNick,passwd,tRegisteredUsers[curUser.sNick]["class"])
Core.SendPmToNick(curUser.sNick,sBot,"Your password has been set up successfully. Your password is: "..passwd)
tRegisteredUsers[curUser.sName] = nil
-- Use inbuilt !passwd instead
--[[elseif curUser.bRegistered then
if not passwd then passwd = PassCode(8) end
AddRegUser(curUser.sName,passwd,curUser.iProfile)
curUser:SendPM(sBot,"Your password has been refreshed successfully. Your password is: "..passwd)]]
end
return true
end,
["genpass"] = function(curUser,data)
local _,_,length = data:find("^%p%S+%s(%d+)")
if length then
if tonumber(length) <= 20 then
if tonumber(length) >= 4 then
Core.SendPmToNick(curUser.sNick,sBot,"Generated password: "..PassCode(tonumber(length)))
else
Core.SendPmToNick(curUser.sNick,sBot,"The minimal password length is 4 character.")
end
else
Core.SendPmToNick(curUser.sNick,sBot,"The maximal password length is 20 character.")
end
else
Core.SendPmToNick(curUser.sNick,sBot,"Generated password: "..PassCode(8))
end
return true
end,
["regme"] = function(curUser,data)
local _,_,passwd = data:find("^%p%S+%s(%S+)")
if RegMan.GetReg(curUser.sNick) then
Core.SendPmToNick(curUser.sNick,sBot,"You are already registered as "..ProfMan.GetProfile(curUser.iProfile).sProfileName..".")
else
if tRegisteredUsers[curUser.sNick] then
Core.SendPmToNick(curUser.sNick,sBot,"You are already registered, please set up your password with command "..
"+passwd <your_passwd> or just +passwd to enable the script to generate you a random and safe "..
"password. To make a safe password use +genpass <length> command where length is an integer between 4 and 20.")
else
if bAutoRegme then
if passwd then
RegMan.AddReg(curUser.sNick,passwd,ProfMan.GetProfile("Reg") and ProfMan.GetProfile("Reg").iProfileNumber)
Core.SendPmToNick(curUser.sNick,sBot,"Sikeresen regisztr?ltad magad Reg oszt?lyba a k?vetkező jelsz?val: "..passwd)
else
tRegisteredUsers[curUser.sName] = {
["nick"] = curUser.sNick,
["class"] = ProfMan.GetProfile("Reg") and ProfMan.GetProfile("Reg").iProfileNumber,
}
Core.SendPmToNick(curUser.sNick,sBot,"Sikeresen regisztr?ltad magad Reg oszt?lyba, ?ll?tsd be a jelszavad a +passwd <jelsz?> paranccsal. "..
"Ha nem adsz meg jelsz?t, automatikusan gener?l?dik egy biztons?gos jelsz?. Jelsz? k?sz?t?shez haszn?ld a +genpass <hossz> parancsot, ahol a hossz egy 4 ?s 20 k?z?tti eg?sz sz?m.")
end
else
Core.SendPmToNick(curUser.sNick,sBot,"Your request has been sent.")
Core.SendToOpChat(curUser.sNick.." want to be registered with password: "..passwd or ("<not specified>"))
end
end
end
return true
end,
["help"] = function(curUser,data)
if Core.GetUserData(curUser,11) then
bHelp = curUser.sNick
end
return false
end,
}
if tCmds[cmd] then
return tCmds[cmd](curUser,data)
end
end
end

function ToArrival(curUser,data)
local _,_, nickto,data = data:find("^%$To:%s(%S+)%sFrom:%s%S+%s%$(.*)")
if nickto == sBot then
ChatArrival(curUser,data)
end
end

function UserConnected(curUser)
if tRegisteredUsers[curUser.sNick] then
tRegisteredUsers[curUser.sNick]["bTimer"] = true
curUser:SendPM(sBot,"You have been registered as "..ProfMan.GetProfile(curUser.iProfile).sProfileName..", "..
"please set up your password NOW\r\nUse command +passwd <your_passwd> or just +passwd to enable the script to "..
"generate you a random and safe password. To make a safe password use +genpass <length> command where length is "..
"an integer between 4 and 20. You have 5 minutes to do it!")
end
local msg = "Your info:\r\n"..
"Nick: "..curUser.sNick.."\r\n"..
"Class: "..((ProfMan.GetProfile(curUser.iProfile) and ProfMan.GetProfile(curUser.iProfile).sProfileName) or "Unregistered").." ("..curUser.iProfile..")".."\r\n"..
"IP: "..curUser.sIP
if RegMan.GetReg(curUser.sNick) or tRegisteredUsers[curUser.sNick] then
msg = msg.."\r\nPwd set?:"..GetPasswd(curUser.sNick).."\r\n"
end
Core.SendToNick(curUser.sNick,msg)
end
OpConnected = UserConnected

function OnTimer(tmr)
for k in pairs(tRegisteredUsers) do
local user = Core.GetUser(k)
if user then
if tRegisteredUsers[user.sNick]["bTimer"] then
if os.difftime(os.time(),Core.GetUserData(user,25)) > 300 then
Core.SendPmToNick(user.sNick,sBot,"Password setting timeout! You are being disconnected.")
Core.Disconnect(user)
tRegisteredUsers[user.sNick]["bTimer"] = nil
end
end
end
end
if bHelp then
local sHelp = "\r\n"..
"Script-side help: (register script)\r\n"..
"\t!regnew <nick> [<profile>] - Use this to register a new user. If no profile given, it will be Reg. Short command: !rn\r\n"..
"\t!regdelete <nick> - Use this to delete a registration of a user. You can delete registered and pre-registered users too. Short command: !rd\r\n"..
"\t!reglist [<profile/all>] - Without parameter it will list those users, whoes have been pre-registered, but didn't set up their passwords. "..
"If profile specified, just those users will be shown. Else parameter is all, then all registered users will be shown per profile. Short command: !rl\r\n"..
"\t!regpasswd <nick> [<password>] - Set up or modify the user's password. If no password given, the script will generate one. Short command: !rp\r\n"..
"\t!regclass <nick> <class> - Use this to change a registered or pre-registered user's profile. Short command: !rc\r\n"..
"\t!passwd [<password>] - With this command can you set up your password, if no password given, script will generate a safety one.\r\n"..
"\t!genpass [<length>] - Use this command to generate a safety password. If no length given, it will be 8 character long. (min length: 4, max length: 20)\r\n"..
"\t!regme [<password>] - Enable the user to register himself/herself on the hub. If no password given, he/she will be added to the pre-registered users, and can set up his/her password later.\r\n"..
"\t!regenable - To enable the regme function (basely enabled)\r\n"..
"\t!regdisable - To disable the regme function."
Core.SendPmToNick(bHelp,sBot,sHelp)
bHelp = nil
end
end

function OnExit()
local file = io.open("regusers.db","w+")
Serialize(tRegisteredUsers,"tRegisteredUsers",file)
file:close()
end

function GetPasswd(nick)
if RegMan.GetReg(nick) then
return "yes"
else
return "no"
end
end

function GetSize(tTable)
local iSize = 0
for k in pairs(tTable) do
iSize = iSize + 1
end
return iSize
end

function Serialize(tTable,sTableName,hFile,sTab)
sTab = sTab or "";
hFile:write(sTab..sTableName.." = {\n");
for key,value in pairs(tTable) do
if (type(value) ~= "function") then
local sKey = (type(key) == "string") and string.format("[%q]",key) or string.format("[%d]",key);
if(type(value) == "table") then
Serialize(value,sKey,hFile,sTab.."\t");
else
local sValue = (type(value) == "string") and string.format("%q",value) or tostring(value);
hFile:write(sTab.."\t"..sKey.." = "..sValue);
end
hFile:write(",\n");
end
end
hFile:write(sTab.."}");
end

function SortByKeys(t, f)
local a = {}
for n in pairs(t) do table.insert(a, n) end
table.sort(a, f)
local i = 0
local iter = function ()
i = i + 1
if a[i] == nil then return nil
else return a[i], t[a[i]]
end
end
return iter
end

PassCode = function(int)--By Mutor
local t = {{48,57},{65,90},{97,122}} -- ASCII range(s)
local msg = ""
for i=1, int do
local r = math.random(1,#t)
msg = msg..string.char(math.random(t[r][1],t[r][2]))
end
return msg
end

Bug reports always welcomed
Title: help with RegBot (Verli style) [API2]
Post by: atonal on 08 June, 2008, 03:08:40
Hi
the scripts is working good never see any bogs
only i nead good help for profiles
because this profile is not in order for my i use this

-1 unreg
0 owner
1 master
2 SuperUser
3 Operator
4 KVIP
5 Vip
6 Reg

-------------

now the order for yuo escript are
[-1] = 0,   -- UnRegistered
[3] = 1,   -- Registered
[2] = 2,   -- VIP
[1] = 3,   -- Operator
Title: Re: RegBot (Verli style) [API2]
Post by: Thor on 08 June, 2008, 12:30:38
I think this will be good for you:
[-1] = 0, -- unreg
[6] = 1, -- Reg
[5] = 2, -- Vip
[4] = 3, -- KVIP
[3] = 4, -- Operator
[2] = 5, -- SuperUser
[1] = 6, -- Master
Title: Re: RegBot (Verli style) [API2]
Post by: atonal on 08 June, 2008, 15:02:34
ok i set in order all profiles and is the same  only workin   owner, master and Operator

i nead set all profiles is no way to do ?

only i see 

Your info:
Nick: ?Owner?Atonal25?
Class: Owner (0)
IP: 212.21.233
Pwd set?:yes

Your info:
Nick: ?Owner?Atonal25?
Class: Owner (0)
IP: 212.21.233
Pwd set?:yes

Your info:
Nick: ?Owner?Atonal25?
Class: Owner (0)
IP: 212.21.233
Pwd set?:yes


see and i cant see for
SuperUser
KVIP
Unreg
Reg
vip

is posible to do ?  olso i habe all profiles in order

thanks agaen and sorry for tapy mani times  habe  good weekend



Title: Re: RegBot (Verli style) [API2]
Post by: -B-G- on 09 June, 2008, 12:35:19
Hy all,i've this problem "Ptokax 0.4.1\scripts\register.lua:162: attempt to index a nil value",do you know why?
The only change I made was to eliminate a profile because I use 5 profiles and not six,can be this the problem.

[-1] = 0,   -- UnRegistered
[5] = 1,   -- Registered
[4] = 2,   -- VIP
[3] = 3,   -- Super Vip
[2] = 4,   -- Op
--[1] = 5,   -- Master
--[0] = 6,   -- Owner
Title: Re: RegBot (Verli style) [API2]
Post by: XTC on 01 August, 2008, 23:08:38
I get this when I try to load the script : scripts\RegBot.lua:356: ')' expected near 'jelsz'

What do I need to do ??