yl_speak_up/privs.lua

99 lines
3.0 KiB
Lua

-- the privs players need some privs
-- npc_talk_owner is required to set up an NPC;
-- npc_talk_master allows to edit NPC of other players;
-- npc_master allows to set dangerous commands like execute lua code
local npc_master_priv_definition = {
description="Can use the staffs to command NPCs",
give_to_singleplayer = false,
give_to_admin = true,
}
minetest.register_privilege("npc_master", npc_master_priv_definition)
local npc_talk_owner_priv_definition = {
description="Can edit the dialogs of his/her own NPCs",
give_to_singleplayer = false,
give_to_admin = true,
}
minetest.register_privilege("npc_talk_owner", npc_talk_owner_priv_definition)
local npc_talk_master_priv_definition = {
description="Can edit the dialogs of NPCs independent of owner",
give_to_singleplayer = false,
give_to_admin = true,
}
minetest.register_privilege("npc_talk_master", npc_talk_master_priv_definition)
-- NPC also need privs to execute more dangerous commands
-- this table will hold the actual privs in the form of
-- indices of the form t[<npc_name>][<priv_name>] = True
yl_speak_up.npc_priv_table = {}
-- where shall the privs be stored so that they will be available after server restart?
yl_speak_up.npc_priv_path = minetest.get_worldpath().."/yl_speak_up_npc_privs.data"
-- these are deemed dangerous and checked
yl_speak_up.npc_priv_names = {
"precon_exec_lua",
"effect_exec_lua", "effect_give_item", "effect_take_item", "effect_move_player",
}
-- either the npc with n_id *or* if generic_npc_id is set the generic npc with the
-- id generic_npc_id needs to have been granted priv_name
yl_speak_up.npc_has_priv = function(n_id, priv_name, generic_npc_id)
-- fallback: disallow
if(not(n_id) or not(priv_name)) then
return false
end
-- remove the leading "_" from the n_id:
if(generic_npc_id) then
generic_npc_id = string.sub(generic_npc_id, 2)
end
-- if the precondition or effect come from a generic_npc and that
-- generic npc has the desired priv, then the priv has been granted
if(generic_npc_id
and yl_speak_up.npc_priv_table[generic_npc_id]
and yl_speak_up.npc_priv_table[generic_npc_id][priv_name]) then
return true
end
if(not(yl_speak_up.npc_priv_table[n_id])
or not(yl_speak_up.npc_priv_table[n_id][priv_name])) then
yl_speak_up.log_change("-", n_id,
"error: NPC was denied priv priv "..tostring(priv_name)..".")
return false
end
return true
end
yl_speak_up.npc_privs_load = function()
local file,err = io.open( yl_speak_up.npc_priv_path, "rb")
if (file == nil) then
yl_speak_up.npc_priv_table = {}
return
end
local data = file:read("*all")
file:close()
yl_speak_up.npc_priv_table = minetest.deserialize(data)
end
yl_speak_up.npc_privs_store = function()
local file,err = io.open( yl_speak_up.npc_priv_path, "wb")
if (file == nil) then
return
end
file:write(minetest.serialize(yl_speak_up.npc_priv_table))
file:close()
end
-- when the game is started: load the npc privs
yl_speak_up.npc_privs_load()
-- the privs for NPC can be set via the chat command in command_npc_talk_priv.lua