reorganize code, minimize callbacks invokations step 1

* all helper/format/transform functions goin to : administration
* procesing functions related to minetst api goin to : process
* move formating function to administration file, expose in the
  variable api "gapi" to get access from processin file
* move checks for player and traking of chaters to common place
* the callbacks moved to processing
This commit is contained in:
mckaygerhard 2023-08-10 13:53:39 -04:00
parent b804fa5c39
commit 3eb36fe2b2
3 changed files with 104 additions and 66 deletions

View File

@ -1,4 +1,4 @@
-- mod governor by mckaygerhard, geoip specific code
-- mod governor by mckaygerhard, administration specific code
-- Copyright 2023
----------------------------------------------------------------------------
@ -11,8 +11,41 @@
----------------------------------------------------------------------------
--[[ init privileges registrations ]]
function format_result_geoip(result)
minetest.register_privilege("governing", {
description = "can do geoip lookups on players for governing",
give_to_singleplayer = false
})
if not governing.modgeoip then
minetest.register_privilege("geoip", {
description = "governing: ip checks + lookups on players",
give_to_singleplayer = false
})
end
--[[ end privileges registrations ]]
function gapi.is_player(player)
return (type(player) == "userdata" or type(player) == "table") -- check player for real, a player is also a datatable
end
function governing.is_creative(name)
if creative then
return creative.is_creative(name)
else
return minetest.check_player_privs(name, {creative = true}) or creative_mode_cache
end
end
function gapi.format_result_geoip(result)
local txt = " Place: "
if result then
if result.status == "success" and result.data and result.data.geo then
@ -52,7 +85,8 @@ function format_result_geoip(result)
end
end
function format_result_checkip(result)
function gapi.format_result_checkip(result)
local txt = " Place: "
local flg = ""
if result then
@ -108,38 +142,3 @@ function format_result_checkip(result)
end
governing.joinplayer_callback = function() end -- function(name, result)
--[[ init of call back on join of a player, ]]
minetest.register_on_joinplayer(function(player) -- query ip on join, record in logs and execute callback
if not player then return end
if not player:is_player() then return end
if not minetest.get_player_ip then return end
local name = player:get_player_name()
local ip = minetest.get_player_ip(name)
if not ip then return end
-- do log ip check: if not apikey then geoip from mod; if not mod then our own geoip
if governing.checkapikey ~= "" then
governing.checkip(ip, function(data) -- log, but TODO: record in storage log file
local txt = format_result_checkip(data)
if txt then minetest.log("warning", "[goberning/geoip] joined player, " .. name .. ":" .. ip .. "," .. txt)
else minetest.log("error", "[goberning/geoip] joined player, " .. name .. ":" .. ip .. ", seems fails")
end
governing.joinplayer_callback(name, data) -- execute callback
end)
else
if not governing.modgeoip then
governing.lookup(ip, function(data) -- log, but TODO: record in storage log file
local txt = format_result_checkip(data)
if txt then minetest.log("warning", "[goberning/geoip] joined player, " .. name .. ":" .. ip .. "," .. txt)
else minetest.log("error", "[goberning/geoip] joined player, " .. name .. ":" .. ip .. ", seems fails")
end
governing.joinplayer_callback(name, data) -- execute callback
end)
end
end
end)
--[[ end of call back on join of a player, ]]

View File

@ -106,33 +106,6 @@ governing.checkapikey = checkapikey -- need for geoip improved ip information, t
--[[ init privileges registrations ]]
function governing.is_creative(name)
if creative then
return creative.is_creative(name)
else
return minetest.check_player_privs(name, {creative = true}) or creative_mode_cache
end
end
minetest.register_privilege("governing", {
description = "can do geoip lookups on players for governing",
give_to_singleplayer = false
})
if not governing.modgeoip then
minetest.register_privilege("geoip", {
description = "governing: ip checks + lookups on players",
give_to_singleplayer = false
})
end
--[[ end privileges registrations ]]
--[[ init http calls, must be only on main ]]
function governing.lookup(ip, callback)
@ -181,7 +154,7 @@ end
--[[ include code files mod features ]]
dofile(governing.modpath.."/geoip.lua") -- format the geoip responses managed by "callback(data)"
dofile(governing.modpath.."/administration.lua") -- more of commonformats
dofile(governing.modpath.."/anticheats.lua") -- load anticheat mod that combined rnd + beowulf + fairplay
@ -189,6 +162,8 @@ dofile(governing.modpath.."/antispawn.lua") -- load antispawn chat features only
dofile(governing.modpath.."/commands.lua") -- must be at end. so relies on others functionalities
dofile(governing.modpath.."/process.lua") -- must be at end. so relies on others functionalities
--[[ end of include code files mod features ]]

64
process.lua Normal file
View File

@ -0,0 +1,64 @@
-- mod governor by mckaygerhard, geoip specific code
-- Copyright 2023
----------------------------------------------------------------------------
-- this program can be used free but cannot be used commertially or
-- modified for, licenced CC-BY-SA-NC 4.0 unless explicit permission
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
----------------------------------------------------------------------------
--[[ init of call back on join of a player, ]]
minetest.register_on_joinplayer(function(player) -- query ip on join, record in logs and execute callback
local msgsys = "cannot get player ip from minetest api.."
if not player then return end
if not gapi.is_player(player) then return end
if not minetest.get_player_ip then minetest.log("error", msgsys) end
local ip
local name
local info
pos = player:getpos()
name = player:get_player_name()
ip = minetest.get_player_ip(name)
if not ip then
if not name then
minetest.log("error", msgsys.." for no name")
else
minetest.log("error", msgsys.." for "..name)
end
end
-- do log ip check: if not apikey then geoip from mod; if not mod then our own geoip
if governing.checkapikey ~= "" then
governing.checkip(ip, function(data) -- log, but TODO: record in storage log file
local txt = gapi.format_result_checkip(data)
if txt then minetest.log("warning", "[goberning/geoip] joined player, " .. name .. ":" .. ip .. "," .. txt)
else minetest.log("error", "[goberning/geoip] joined player, " .. name .. ":" .. ip .. ", seems fails")
end
governing.joinplayer_callback(name, data) -- execute callback
end)
else
if not governing.modgeoip then
governing.lookup(ip, function(data) -- log, but TODO: record in storage log file
local txt = gapi.format_result_checkip(data)
if txt then minetest.log("warning", "[goberning/geoip] joined player, " .. name .. ":" .. ip .. "," .. txt)
else minetest.log("error", "[goberning/geoip] joined player, " .. name .. ":" .. ip .. ", seems fails")
end
governing.joinplayer_callback(name, data) -- execute callback
end)
end
end
end)
--[[ end of call back on join of a player, ]]