mckaygerhard d731f8dbd9 working geoip now, tune up sintax, organize code
* privilegie registration and callback on join player
  detection of geoip mod in same conditional
* check nil player, vali player and engine feature
  at the joining process of the player
2023-06-21 16:41:24 -04:00

54 lines
1.7 KiB
Lua

function format_result(result)
if result and result.status == "success" and result.data and result.data.geo then
local txt = "Geoip result: "
if result.data.geo.country_name then
txt = txt .. " Country: " .. result.data.geo.country_name
end
if result.data.geo.city then
txt = txt .. " City: " .. result.data.geo.city
end
if result.data.geo.timezone then
txt = txt .. " Timezone: " .. result.data.geo.timezone
end
if result.data.geo.asn then
txt = txt .. " ASN: " .. result.data.geo.asn
end
if result.data.geo.isp then
txt = txt .. " ISP: " .. result.data.geo.isp
end
if result.data.geo.ip then
txt = txt .. " IP: " .. result.data.geo.ip
end
return txt
else
return false
end
end
if not governing.modgeoip then
minetest.register_privilege("geoip", {
description = "can do geoip lookups on players for governing",
give_to_singleplayer = false
})
governing.joinplayer_callback = function() end -- function(name, result)
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
governing.lookup(ip, function(data) -- log, but TODO: record in storage log file
local txt = format_result(data)
if txt then minetest.log("info", "[goberning/geoip] result for player " .. name .. ": " .. txt)
else minetest.log("error", "[goberning/geoip] result for player " .. name .. ": seems fails for ip "..ip)
end
governing.joinplayer_callback(name, data) -- execute callback
end)
end)
end