PtokaX forum

Archive => Archived 5.0 boards => Help with scripts => Topic started by: jiten on 01 March, 2005, 08:56:22

Title: Chatrooms v3 by tezlo to LUA 5 ?
Post by: jiten on 01 March, 2005, 08:56:22
I've converted almost all of this script to LUA 5, but I'm getting this error whenever I create a chatroom or try to remove it:
Syntax ...ucrai\Desktop\16.04rls\scripts\chatrooms.lua_5.0.lua:129: attempt to call global `write' (a nil value)
Syntax ...ucrai\Desktop\16.04rls\scripts\chatrooms.lua_5.0.lua:129: attempt to call global `write' (a nil value)

-- chatrooms v3
-- tezlo

function Main()
chatrooms:load()
end

function OnExit()
chatrooms:save()
end

function ChatArrival(user, data)
if string.sub(data, 1, 1) == "<" then
local s, e, cmd, args = string.find(data, "^%b<> %!(%a+)%s*(.*)|$")
if cmd == "mkchat" and user.bOperator then
local s, e, name, profiles = string.find(args, "(%S+)%s*(.*)")
if not s then
user:SendData(">> Syntax: !mkchat [groups]")
elseif chatrooms.items[name] then
user:SendData(">> "..name.." is already a Chatroom.")
elseif GetItemByName(name) then
user:SendData(">> There is a user with that name")
else
frmHub:RegBot(name)
local tmp = chatrooms:new(name, user.sName)
string.gsub(profiles, "(%S+)", function(profile)
profile = tonumber(profile) or GetProfileIdx(profile)
if GetProfileName(profile) then tmp.groups[profile] = 1 end
end); tmp:chat("Hello", name)
chatrooms:save()
return 1
end
end
end
end

function ToArrival(user,data)
local s, e, to, str = string.find(data, "^$To: (%S+) From: %S+ $%b<> (.*)|$")
if chatrooms.items[to] then
local tmp = chatrooms.items[to]
if not tmp.members[user.sName] then
if user.bOperator or tmp.groups[user.iProfile] == 1 then
tmp.members[user.sName] = 1
tmp:chat(user.sName.." joined", to)
tmp:chat(str, user.sName)
chatrooms:save()
else
user:SendPM(to, "You're not a member here.")
end
else
local isowner = (tmp.owner == user.sName)
local s, e, cmd, args = string.find(str, "^%!(%a+)%s*(.*)$")
if not s then
tmp.members[user.sName] = 1
tmp:chat(str, user.sName)
elseif cmd == "away" then
tmp:chat(user.sName.." is away.. "..args, to)
tmp.members[user.sName] = 0
elseif cmd == "leave" then
tmp:chat(user.sName.." left. "..args, to)
tmp.members[user.sName] = nil
chatrooms:save()
elseif cmd == "members" then
local n, na, msg = 0, 0
for nick, stat in tmp.members do
if not GetItemByName(nick) then msg = " (offline)"
elseif stat == 0 then msg = " (away)"
else msg, na = "", na+1
end; n = n+1
user:SendPM(to, "\t"..nick..msg)
end; user:SendPM(to, na.."/"..n.." active members.")
elseif cmd == "invite" then
string.gsub(args, "(%S+)", function(nick)
if not tmp.members[nick] then
tmp.members[nick] = 1
tmp:chat(nick.." has been invited to the room. Type !leave to leave, and !members to see who's invited.", to)
end
end); chatrooms:save()
elseif cmd == "remove" and isowner then
string.gsub(args, "(%S+)", function(nick)
if tmp.members[nick] and nick ~= tmp.owner then
tmp:chat(nick.." has been removed from the room", to)
tmp.members[nick] = nil
end
end); chatrooms:save()
elseif cmd == "delchat" and isowner then
tmp:chat("End of session.", to)
chatrooms.items[to] = nil
chatrooms:save()
frmHub:UnregBot(to)
else
tmp:chat(str, user.sName)
end
end; return 1
end
end

botchat = function(self, msg, from)
for nick, id in self.members do
if nick ~= from and id == 1 then
SendToNick(nick, "$To: "..nick.." From: "..self.name.." $<"..from.."> "..msg)
end
end
end

chatrooms = {
new = function(self, name, owner)
local tmp = {
name = name,
owner = owner,
groups = {},
members = { [owner] = 1 },
chat = botchat
}; self.items[name] = tmp
return tmp
end,

load = function(self)
self.items = dofile("logs/chatrooms.dat") or {}
for name, room in self.items do
frmHub:RegBot(name)
room.chat = botchat
room:chat("Hello", name)
end
end,

save = function(self)
local f = io.open("logs/chatrooms.dat", "w+")
assert(f, "logs/chatrooms.dat") write(f, "return {\n")
for name, tmp in self.items do
write(f, string.format("\t[%q] = {\n\t\tname = %q,\n\t\towner = %q,\n\t\tgroups = {\n", name, tmp.name, tmp.owner))
for id, stat in tmp.groups do
write(f, string.format("\t\t\t[%d] = %d,\n", id, stat))
end; write(f, "\t\t},\n\t\tmembers = {\n")
for nick, stat in tmp.members do
write(f, string.format("\t\t\t[%q] = %d,\n", nick, stat))
end; write(f, "\t\t}\n\t},\n")
end; write(f, "}")
closefile(f)
end
}



Thanks in advance...
Title:
Post by: bastya_elvtars on 01 March, 2005, 10:59:46
I am almost done with your script, just dunno how i could create a file that does not exist yet.
Title:
Post by: jiten on 02 March, 2005, 17:28:16
Thanks bastya. Hum, always the file handling ;)
Title:
Post by: jiten on 02 March, 2005, 19:55:21
Well, i guess that I've converted all of the script. I ain't gettings more errors and everything is working well till now. I'm going to post the script in the LUA 5 Finished Scripts section
Title:
Post by: bastya_elvtars on 02 March, 2005, 19:56:59
OK, i have just converted it too. Forgot to create the logs dir or wtf. :D

Anyway, I post it.

-- chatrooms v3
-- tezlo
-- converted to lua5 by bastya_elvtars (the biggest tezlo fan :P)

function Main()
chatrooms:load()
end


function OnExit()
chatrooms:save()
end


function ChatArrival(user, data)
local s, e, cmd, args = string.find(data, "^%b<> %!(%a+)%s*(.*)|$")
if cmd == "mkchat" and user.bOperator then
local s, e, name, profiles = string.find(args, "(%S+)%s*(.*)")
if not s then
user:SendData(">> Syntax: !mkchat [groups]")
elseif chatrooms.items[name] then
user:SendData(">> "..name.." is already a Chatroom.")
elseif GetItemByName(name) then
user:SendData(">> There is a user with that name")
else
frmHub:RegBot(name)
local tmp = chatrooms:new(name, user.sName)
string.gsub(profiles, "(%S+)", function(profile)
profile = tonumber(profile) or GetProfileIdx(profile)
if GetProfileName(profile) then tmp.groups[profile] = 1 end
end); tmp:chat("Hello", name)
chatrooms:save()
return 1
end
end
end


function ToArrival(user,data)
local s, e, to, str = string.find(data, "^$To: (%S+) From: %S+ $%b<> (.*)|$")
if chatrooms.items[to] then
local tmp = chatrooms.items[to]
if not tmp.members[user.sName] then
if user.bOperator or tmp.groups[user.iProfile] == 1 then
tmp.members[user.sName] = 1
tmp:chat(user.sName.." joined", to)
tmp:chat(str, user.sName)
chatrooms:save()
else
user:SendPM(to, "You're not a member here.")
end
else
local isowner = (tmp.owner == user.sName)
local s, e, cmd, args = string.find(str, "^%!(%a+)%s*(.*)$")
if not s then
tmp.members[user.sName] = 1
tmp:chat(str, user.sName)
elseif cmd == "away" then
tmp:chat(user.sName.." is away.. "..args, to)
tmp.members[user.sName] = 0
elseif cmd == "leave" then
tmp:chat(user.sName.." left. "..args, to)
tmp.members[user.sName] = nil
chatrooms:save()
elseif cmd == "members" then
local n, na, msg = 0, 0
for nick, stat in tmp.members do
if not GetItemByName(nick) then msg = " (offline)"
elseif stat == 0 then msg = " (away)"
else msg, na = "", na+1
end; n = n+1
user:SendPM(to, "\t"..nick..msg)
end; user:SendPM(to, na.."/"..n.." active members.")
elseif cmd == "invite" then
string.gsub(args, "(%S+)", function(nick)
if not tmp.members[nick] then
tmp.members[nick] = 1
tmp:chat(nick.." has been invited to the room. Type !leave to leave, and !members to see who's invited.", to)
end
end); chatrooms:save()
elseif cmd == "remove" and isowner then
string.gsub(args, "(%S+)", function(nick)
if tmp.members[nick] and nick ~= tmp.owner then
tmp:chat(nick.." has been removed from the room", to)
tmp.members[nick] = nil
end
end); chatrooms:save()
elseif cmd == "delchat" and isowner then
tmp:chat("End of session.", to)
chatrooms.items[to] = nil
chatrooms:save()
frmHub:UnregBot(to)
else
tmp:chat(str, user.sName)
end
end; return 1
end
end


botchat = function(self, msg, from)
for nick, id in self.members do
if nick ~= from and id == 1 then
SendToNick(nick, "$To: "..nick.." From: "..self.name.." $<"..from.."> "..msg)
end
end
end


chatrooms = {
new = function(self, name, owner)
local tmp = {
name = name,
owner = owner,
groups = {},
members = { [owner] = 1 },
chat = botchat
}; self.items[name] = tmp
return tmp
end,


load = function(self)
self.items = dofile("logs/chatrooms.dat") or {}
for name, room in self.items do
frmHub:RegBot(name)
room.chat = botchat
room:chat("Hello", name)
end
end,


save = function(self)
local f = io.open("logs/chatrooms.dat", "a+")
-- assert(f, "logs/chatrooms.dat")
if f then
local str="return {\n"
for name, tmp in self.items do
str=str..string.format("\t[%q] = {\n\t\tname = %q,\n\t\towner = %q,\n\t\tgroups = {\n", name, tmp.name, tmp.owner)
for id, stat in tmp.groups do
f:write(str..(string.format("\t\t\t[%d] = %d,\n", id, stat)))
end; f:write(("\t\t},\n\t\tmembers = {\n"))
for nick, stat in tmp.members do
f:write((string.format("\t\t\t[%q] = %d,\n", nick, stat)))
end; f:write(("\t\t}\n\t},\n"))
end; f:write(( "}"))
f:close()
end
end
}


Title:
Post by: jiten on 02 March, 2005, 20:10:46
No probs, m8.
Anyway, LUA 5 ain't that hard.  :D
Thanks for the hints...
Best regards,

jiten