Chatrooms v3 by tezlo to LUA 5 ?
 

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

Chatrooms v3 by tezlo to LUA 5 ?

Started by jiten, 01 March, 2005, 08:56:22

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

jiten

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...

bastya_elvtars

I am almost done with your script, just dunno how i could create a file that does not exist yet.
Everything could have been anything else and it would have just as much meaning.

jiten

Thanks bastya. Hum, always the file handling ;)

jiten

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

bastya_elvtars

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
}
Everything could have been anything else and it would have just as much meaning.

jiten

No probs, m8.
Anyway, LUA 5 ain't that hard.  :D
Thanks for the hints...
Best regards,

jiten

SMF spam blocked by CleanTalk