--[[
NickPrefix 1.0 LUA 5.1x [Strict][API 2]
By Mutor 03/07/08
Set a prefix to your nick in main chat [alpha-mumeric values only]
plop had done this yet I don't think he ever released it. Here's my
take on it. Perhaps you'll find it as uselesly fun as I do
- Automatically moves this script to end of list [to avoid conflicts]
- Provides context menu [right click]
- Saves to file for hub/script restarts
- Option for minimum maximum prefix character limit
]]
--//--
-- Botname pulled from the hub or use "CustomName"
local Bot = SetMan.GetHubBot().sNick
-- Admins nick for status / error messages
local OpNick = "Mutor"
-- Script command, no prefix [Listens for any valid hub command prefix]
local pfxcmd = "prefix"
--Set your profiles / permissions here.
-- [#] = true/flase, (true = Command Enabled / false = Command Disabled)
local Profiles = {
[-1] = false, --Unregistered User
[0] = true, --Master
[1] = true, --Operator
[2] = true, --Vip
[3] = true, --Registered User
}
local File = "prefixes.dat"
-- Nick prefix ["UserName"] = "prefix"
Prefixes = {
["Mutor"] = "Scripter",
}
-- Minimum number of characters required for prefix [1 - 5]
local MinChar = 3
-- Maximum number of characters allowed for prefix [5 - 35]
local MaxChar = 35
--//--
local Scp = "NickPrefix 1.0 L5.1.lua"
OnStartup = function()
if loadfile(File) then
dofile(File)
else
SaveFile(File,Prefixes,"Prefixes")
end
local t = ScriptMan.GetScripts()
if next(t) and t[#t].sName:lower() ~= Scp:lower() then
while ScriptMan.MoveDown(Scp) do ScriptMan.MoveDown(Scp) end
SetMan.Save()
end
MinChar = math.max(math.min(MinChar,5),1)
MaxChar = math.min(math.max(MaxChar,5),35)
end
UserConnected = function(user)
if Profiles[user.iProfile] then
local p,s = Prefixes[user.sNick],""
if p then s = " ("..p..")" end
local Pfx = SetMan.GetString(29):sub(1,1)
local uc = "$UserCommand 1 1 "..SetMan.GetString(0).."\\"..Scp:gsub(" .+","").."\\Set Prefix"..s..
"$<%[mynick]> "..Pfx..pfxcmd.." %[line:Nick Prefix (Leave Blank To Clear Prefix)]||"
Core.SendToUser(user,uc)
end
end
OpConnected, RegConnected = UserConnected,UserConnected
ChatArrival = function(user,data)
if Profiles[user.iProfile] then
local _,_,cmd = data:lower():find("^%b<> ["..SetMan.GetString(29).."](%a+)")
local spc = string.char(160)
if cmd then
if cmd:lower() == pfxcmd then
local _,_,pfx = data:find("^%b<> %p%a+ ([ %w]+)|$")
local _,_,input = data:find("^%b<> %p%a+ ([^|]+)|$")
if pfx then
if pfx:len() > MinChar then
if pfx:len() < MaxChar then
Prefixes[user.sNick] = pfx:gsub(" ",spc)
SaveFile("prefixes.dat",Prefixes,"Prefixes")
Core.SendToUser(user,"<"..Bot.."> Your prefix is "..
"now set to "..Prefixes[user.sNick].."|")
else
Core.SendToUser(user,"<"..Bot.."> Error. "..pfx.." is too long a prefix.|")
end
else
Core.SendToUser(user,"<"..Bot.."> Error. "..pfx.." is too short a prefix.|")
end
else
if input then
return Core.SendToUser(user,"<"..Bot.."> "..input..
" contains invalid characters..|"),true
end
Prefixes[user.sNick] = nil
SaveFile("prefixes.dat",Prefixes,"Prefixes")
Core.SendToUser(user,"<"..Bot.."> Your prefix has been cleared.|")
end
return true
end
else
local p = Prefixes[user.sNick]
if p then
local d,c = data:gsub("^%<","<"..p..spc)
if c then return Core.SendToAll(d),true end
end
end
end
end
OnError = function(msg)
local user = Core.GetUser(OpNick)
if user then
Core.SendToUser(user,"<"..Bot.."> "..msg.."|")
end
end
SaveFile = function(file,table, tablename )
local hFile = io.open (file , "wb")
Serialize(table, tablename, hFile)
hFile:close()
end
Serialize = function(tTable, sTableName, hFile, sTab)
sTab = sTab or "";
hFile:write(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
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
hFile:write( sTab.."}")
end