MainStop v 1 (lua5)
 

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

MainStop v 1 (lua5)

Started by Dessamator, 20 April, 2005, 16:04:13

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Dessamator

--- lua5 version (requested by madman), conversion by Dessamator
---changed the tprofiles table a bit, nothing much , :D
--- --- --- --- --- --- --- --- --- --- ---
--- MainStop v 1 
--- by Herodes
--- --- --- --- --- --- --- --- --- --- ---
---- Look at the NOTES of this script
--- stops mainchat for selected users or whole profiles
--- remembers state after hub restarts..
--- protects profiles ( set the priorities correctly look at the notes below .. )
--- --- --- --- --- --- --- --- --- --- ---
	--- NOTES: 
--- Put this file in your scripts folder of Ptokax and then Start and Stop the Hub ...
--- Then go and open the File MainStop-Profiles.dat with Notepad.exe
--- explanation of contents follows ...
--
--	tProfiles = {
--		["1"] = { --- this is the profile number for which the following settings apply ...
--			[1] = 1, ---  [1] = 0 means no chat, no scripts commands, 1 chat, no script commands, 2 both chat and command use allowed
--			[2] = 2, --- [2] = is the priority number .. the lower the better .. ( no negative values pls ... )
--			[3] = "Operator", 
--		},	--- end for the settings of profile number : "1"
--		["0"] = {
--			[1] = 2,
--			[2] = 1,
--			[3] = "Master",
--		},
--	}
--
--- --- --- --- --- --- --- --- --- --- ---
---- !!!!! Look at the NOTES of this script !!!!!!
cmdclist = "!lschat"
cmdcstop = "!chat"
cmdchelp = "!chelp"
cmdcprof = "!cprofile"
FiletoSaveNoChatNicks = "MainStop-Nicks.dat"
FiletoSaveNoChatProfiles = "MainStop-Profiles.dat"
tUsrs = {}

function Main()
	local f = io.open(FiletoSaveNoChatProfiles, "r")
	if f then
		f:close()
		LoadFromFile(FiletoSaveNoChatProfiles)
	else
		tProfiles = {}
		local nu = 0
		for i, v in GetProfiles() do
			nu = nu + 1
		if v=="Master" then 
			tProfiles[(0)] = {2,nu,v}
		else
			tProfiles[GetProfileIdx(v)] = { 1, nu, v }
		end			
		end
		tProfiles[(-1)] = { 1, nu + 1, "Unregs" }
		SaveToFile(FiletoSaveNoChatProfiles , tProfiles , "tProfiles")
		LoadFromFile(FiletoSaveNoChatProfiles)
	end
end

function NewUserConnected(user)
	if tUsrs[user.sName] == 1 or tProfiles[(user.iProfile)][1] == 0 then
		user:SendData("MainChatBlocker", "You cant use the mainchat for now. ...")
	end
end

function ChatArrival(user, data)
	if tUsrs[user.sName] == 1 or tProfiles[(user.iProfile)][1] == 0   then
		user:SendData("MainChatBlocker", "You cant chat in main...")
		return 1 
	else
		if tProfiles[user.iProfile][1] == 2 then
			data = string.sub(data, 1, -2)
			s,e,cmd,args = string.find(data, "%b<>%s+(%S+)%s*(%S*)")
			if cmd == cmdclist then
				
				local msg = "\r\n - The following users are gagged in mainchat\r\n"
				for nick, v in tUsrs do
					msg = msg.."\t -- "..nick.."\r\n"
				end
				msg = msg.." - The profile permissions are as follows\r\n"
				msg = msg.." - \tProfile Name\tCmds/MainChat\r\n"
				local cmds, chat = "yes", "yes"
				for i,v in tProfiles do
					if v[1] == 0 then
						cmds, chat = "no", "no"
					elseif v[1] == 1 then
						cmds , chat = "no", "yes"
					elseif v[1] == 2 then
						cmds , chat = "yes", "yes"
					end
					local prstr = v[3].." ("..i..")"
					if string.len(prstr) < 8 then 
						prstr = prstr.."\t"
					end
					msg = msg.."\t+ "..prstr.."\t   "..cmds.."/"..chat.."\r\n"
				end
				user:SendData("MainChatBlocker", msg)
				return 1
			elseif cmd == cmdcstop then
				if not(args) or (string.len(args) == 0) then
					user:SendData("MainChatBlocker", "Syntax Error! Please Use like : "..cmdcstop.." ")
				else
					local usr = GetItemByName(args)
					if usr then
						if tUsrs[(usr.sName)] then
							tUsrs[(usr.sName)] = nil
							usr:SendData("MainChatBlocker",usr.sName..", you are now able to chat in mainchat.")
							user:SendData("MainChatBlocker", usr.sName.." now can chat in main")
					
						else 
						--if usr then
							tUsrs[usr.sName] = 1
							usr:SendData("MainChatBlocker", GetProfileName(user.iProfile).." "..user.sName.." has blocked your ability to chat in mainchat.")
							user:SendData("MainChatBlocker", usr.sName.." now can't chat in main")
						end
					else 
						user:SendData("MainChatBlocker", "Error : "..args.." is not in the hub.")
						--end
					end
				end
				SaveToFile(FiletoSaveNoChatNicks , tUsrs , "tUsrs")
				return 1
			elseif cmd == cmdchelp then
				local msg = "\t\t--- MainChatBlocker Commands --- \r\n"
				msg = msg.."][\t "..cmdclist.."\t - lists the users that are blocked\r\n"
				msg = msg.."][\t "..cmdcstop.."  \t - gags the mainchat of nick\r\n"
				msg = msg.."][\t "..cmdcprof.." \t - It toggles the permission to chat in main for the specified profile number\r\n"
				msg = msg.."][\t "..cmdchelp.."\t - shows this helpful text :P\r\n"
				user:SendData(msg)
				return 1
			elseif cmd == cmdcprof then
				if (not args) or (string.len(args) == 0) then
					user:SendData("MainChatBlocker", "Syntax Error! Please Use like : "..cmdcprof.." ")
				else
					if args == "-1" then
						if tProfiles[(-1)][1] == 1 then
							tProfiles[(-1)][1] = 0
							user:SendData("MainChatBlocker", "Normal Users now are unable to chat in mainchat")
							SendToAll("MainChatBlocker", "Notice! : Normal Users now are unable to chat in mainchat")
						elseif tProfiles[(-1)][1] == 0 then
							tProfiles[(-1)][1] = 1
							user:SendData("MainChatBlocker", "Normal Users now are able to chat in mainchat")
							SendToAll("MainChatBlocker", "Notice: Normal Users now are able to chat in mainchat")
						end
					else 
						s,e,pr = string.find(args, "(%d+)")
						pr = tonumber(pr)
						if GetProfiles()[pr] then
							if tProfiles[user.iProfile][2] < tProfiles[pr][2] then --- priority checking ..
								if tProfiles[pr][1] == 0 then 
									tProfiles[pr][1] = 1 
									user:SendData("MainChatBlocker", GetProfileName(pr).."s now are unable to chat in mainchat")
									SendToAll("MainChatBlocker", "Notice: "..GetProfileName(pr).."s now are unable to chat in mainchat")
								elseif  tProfiles[pr][1] == 1 then
									tProfiles[pr][1] = 0
									user:SendData("MainChatBlocker", GetProfileName(pr).."s now are able to chat in mainchat")
									SendToAll("MainChatBlocker", "Notice: "..GetProfileName(pr).."s now are able to chat in mainchat")
								end
							else
								user:SendData("MainChatBlocker", "Error! : You are not allowed to block the mainchat of "..GetProfileName(pr).."s")
								SendToOps("MainChatBlocker", "Mr."..user.sName.." behaves stupidly ...")
							end
						else 
							user:SendData("MainChatBlocker", "There is no such profile number")
						end
					end
					SaveToFile(FiletoSaveNoChatProfiles , tProfiles , "tProfiles")
				end
				return 1
			end
		end
	end
end

function Serialize(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 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

function SaveToFile(file , table , tablename)
	local handle = io.open(file,"w+")
        handle:write(Serialize(table, tablename))
	handle:flush()
        handle:close()
end

function LoadFromFile(file)
local handle = io.open(file,"r")
        if (handle ~= nil) then
                dofile(file)
handle:flush()
handle:close()
        end

end
---------------------------------------------------------------------------------------------------


Done !!!

*post edited* added herodes "performance tip"
*post edited* added hawk's "tip"
Ignorance is Bliss.

Herodes

#1
a small mem/perf tip:
if you are using a function only once then it is better to include it inside the block that it is beeing used as a local.
Example:
function doUnNeeded()
	return "ok now"
end

function TheProperMainFunction()
	send( doUnNeeded() )
end
this could become
function TheProperMainFunction()

	local function doUnNeeded()
		return "ok now"
	end

	send( doUnNeeded() )

end
I just saw that 'verify' function of yours and it triggered me ;)

Dessamator

QuoteOriginally posted by Herodes
a small mem/perf tip:
if you are using a function only once then it is better to include it inside the block that it is beeing used as a local.
Example:
function doUnNeeded()
	return "ok now"
end

function TheProperMainFunction()
	send( doUnNeeded() )
end
this could become
function TheProperMainFunction()

	local function doUnNeeded()
		return "ok now"
	end

	send( doUnNeeded() )

end
I just saw that 'verify' function of yours and it triggered me ;)

thnx for the hint ,but are u referring to this script, or to scripts in general, and if ur referring to this script, what function???
Ignorance is Bliss.

Herodes

QuoteOriginally posted by Dessamator
thnx for the hint ,but are u referring to this script, or to scripts in general, and if ur referring to this script, what function???
refering to this function inside this script
function verify(filename)
	local f = io.open(filename, "r")
	if f then
		f:close()
		return true
	end
end
and also mentioning it as a good general practice in any lua script ;)

Dessamator

hmm, ok ill change the script asap, unless ud like to do it?, its UR script,  :D
Ignorance is Bliss.

Dessamator

QuoteOriginally posted by Herodes
QuoteOriginally posted by Dessamator
thnx for the hint ,but are u referring to this script, or to scripts in general, and if ur referring to this script, what function???
refering to this function inside this script
function verify(filename)
	local f = io.open(filename, "r")
	if f then
		f:close()
		return true
	end
end
and also mentioning it as a good general practice in any lua script ;)

done!!
Ignorance is Bliss.

??????Hawk??????

#6
you could try it like this  and eliminate the function...


function Main()
	local f = io.open(filename, "r")
		if f then
			f:close()
			LoadFromFile(FiletoSaveNoChatProfiles)
		else
			f:close()
			tProfiles = {}
			local nu = 0
			for i, v in GetProfiles() do
				nu = nu + 1
				tProfiles[GetProfileIdx(v)] = { 1, nu, v }			
			end
			tProfiles[(-1)] = { 1, nu + 1, "Unregs" }
			SaveToFile(FiletoSaveNoChatProfiles , tProfiles , "tProfiles")
			LoadFromFile(FiletoSaveNoChatProfiles)
		end
end

Dessamator

QuoteOriginally posted by ?˜”??•Hawk•??”˜?
you could try it like this  and eliminate the function...


function Main()
	local f = io.open(filename, "r")
		if f then
			f:close()
			LoadFromFile(FiletoSaveNoChatProfiles)
		else
			f:close()
			tProfiles = {}
			local nu = 0
			for i, v in GetProfiles() do
				nu = nu + 1
				tProfiles[GetProfileIdx(v)] = { 1, nu, v }			
			end
			tProfiles[(-1)] = { 1, nu + 1, "Unregs" }
			SaveToFile(FiletoSaveNoChatProfiles , tProfiles , "tProfiles")
			LoadFromFile(FiletoSaveNoChatProfiles)
		end
end
done !!, (post edited)
Ignorance is Bliss.

Anna

nice script, or rather would be if it worked for me :) I have a new ptokax with xml files for profiles and such, could that be why it dont work for me?

I get no errors from it in prokax scripteditor, it just refuses to read my commands as commands, looks just as if it was plain text i typed in main...

Madman

hmm.. try change the  MainStop-Profiles.dat

The profilenumbers are wrong.. noticed before.. just forgot to tell... ;p  for example the MasterProfile should have  0 and not 1

Dont know if it still does that with the new version... i was using the first version.. before the tips from herodes and hawk... =)
We suffer in silence, we lurk in the shadows, we kill in the night
Site currently down, ETA of returning online is 2099 ;p

Dessamator

#10
QuoteOriginally posted by madman
hmm.. try change the  MainStop-Profiles.dat

The profilenumbers are wrong.. noticed before.. just forgot to tell... ;p  for example the MasterProfile should have  0 and not 1

Dont know if it still does that with the new version... i was using the first version.. before the tips from herodes and hawk... =)
hmm, i had fixed it the profiles are correct, and i added a small feature, now masters have access to the script commands by default !, just copy the script again ana, dont forget to stop the other script and delete the "MainStop-Profiles.dat" .

Good luck
Ignorance is Bliss.

SMF spam blocked by CleanTalk