ExtraLua mod : my first administrative oriented mod experience :)
This is my first mod. So the code is quite ugly, but I would like to share with you what I' ve done so far.
ExtraLua
The aim is to be able to execute unexpected external lua code at runtime..
For instance ...
-> You want to grant or revoke privs to a player without login/logout yourself (admin) nor modifying auth.txt file and minetest server restart.
-> You would like to change server time or broadcast server messages to players without login yourself (admin)
-> inject new logical behavior on the server without deploying and restarting your minetestserver.
-> etc of the same kind :)..
How to do it?
By placing a brand new lua script file somewhere on your server file system and a minetest mod will load and execute the script file as soon as possible at runtime without restarting the server..
more precisely ...
on server there is bash shell script that manage lua job queue
in minetestserver there is a mod that read job item file and execute it when a particular event occurs : I ve chosen player join event.. but you can choose another event of your choice ... until a "System Signal" will be available in the engine (I'm a bad C programmer :( so I can't propose an engine update )
So firstly the mods/ExtraLua/init.lua file
and job quueing command mods/ExtraLua/addjob.sh
addjob example of usage
when any player will join , the jobs will be executed..
so addjob.sh create or append job.lua.queue lua script you want to execute
job.lua.queue is rename job.lua during its execution by the mod
then job.lua is rename to :
job.lua.TimeStamp.done if ok
job.lua.TimeStamp.compile-error if interpretation syntax error
job.lua.TimeStamp.run-error if runtime execution error
Hope that will help anybody.
ExtraLua
The aim is to be able to execute unexpected external lua code at runtime..
For instance ...
-> You want to grant or revoke privs to a player without login/logout yourself (admin) nor modifying auth.txt file and minetest server restart.
-> You would like to change server time or broadcast server messages to players without login yourself (admin)
-> inject new logical behavior on the server without deploying and restarting your minetestserver.
-> etc of the same kind :)..
How to do it?
By placing a brand new lua script file somewhere on your server file system and a minetest mod will load and execute the script file as soon as possible at runtime without restarting the server..
more precisely ...
on server there is bash shell script that manage lua job queue
in minetestserver there is a mod that read job item file and execute it when a particular event occurs : I ve chosen player join event.. but you can choose another event of your choice ... until a "System Signal" will be available in the engine (I'm a bad C programmer :( so I can't propose an engine update )
So firstly the mods/ExtraLua/init.lua file
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
function file_exists(name)
local f=io.open(name,"r")
if f~=nil then io.close(f) return true else return false end
end
-- copy paste and modified from chatcommands.lua
function revoke(revokename, revokeprivstr)
if not revokename or not revokeprivstr then
return
end
local revokeprivs = minetest.string_to_privs(revokeprivstr)
local privs = minetest.get_player_privs(revokename)
if revokeprivstr == "all" then
privs = {}
else
for priv, _ in pairs(revokeprivs) do
privs[priv] = nil
end
end
minetest.set_player_privs(revokename, privs)
end
-- copy paste and modified from chatcommands.lua
function grant(grantname, grantprivstr)
if not grantname or not grantprivstr then
return
end
local grantprivs = minetest.string_to_privs(grantprivstr)
if grantprivstr == "all" then
grantprivs = minetest.registered_privileges
end
local privs = minetest.get_player_privs(grantname)
local privs_known = true
for priv, _ in pairs(grantprivs) do
if not minetest.registered_privileges[priv] then
privs_known = false
end
privs[priv] = true
end
if not privs_known then
return
end
minetest.set_player_privs(grantname, privs)
end
-- job execution temptative each time a player join
minetest.register_on_joinplayer(function(player)
-- minetest.register_on_systemsignal(function(systemsignal)
luafilename = minetest.get_modpath("ExtraLua").."/job.lua"
if file_exists(luafilename..".queue") and not file_exists(luafilename) then
os.rename(luafilename..".queue", luafilename)
end
while file_exists(luafilename) do
strnow = os.date("%Y%m%d-%H%M%S")
f = loadfile(luafilename)
if f ~= nil then
if pcall(f) then
os.rename(luafilename, luafilename.."."..strnow..".done")
else
os.rename(luafilename, luafilename.."."..strnow..".run-error")
end
else
os.rename(luafilename, luafilename.."."..strnow..".compile-error")
end
if file_exists(luafilename..".queue") then
os.rename(luafilename..".queue", luafilename)
end
end
return true
end)
and job quueing command mods/ExtraLua/addjob.sh
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
#!/bin/bash
# TODO : update the following "cd" command with your right ExtraLua mod path
cd /MinetestServerInstallPath/games/mygamedef/mods/ExtraLua
echo -- `date` >> job.lua.queue
echo $1 >> job.lua.queue
if [ ! -f ./job.lua ]
then
mv ./job.lua.queue ./job.lua
fi
addjob example of usage
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
$> ./addjob.sh 'revoke("fooplayer","interact")'
$> ./addjob.sh 'grant("barPlayer","shout")'
when any player will join , the jobs will be executed..
so addjob.sh create or append job.lua.queue lua script you want to execute
job.lua.queue is rename job.lua during its execution by the mod
then job.lua is rename to :
job.lua.TimeStamp.done if ok
job.lua.TimeStamp.compile-error if interpretation syntax error
job.lua.TimeStamp.run-error if runtime execution error
Hope that will help anybody.