geoip - fix formating msg and fix callback for joining player

This commit is contained in:
mckaygerhard 2023-06-21 18:02:28 -04:00
parent 2100a574e7
commit 755549525b

View File

@ -1,24 +1,33 @@
function format_result(result)
if result and result.status == "success" and result.data and result.data.geo then
local txt = "Geoip result: "
local txt = " Place: "
if result.data.geo.ip then
if result.data.geo.ip == "127.0.0.1" then return txt .. " seems localhost " ..".. <_< ." end
end
if result.data.geo.continent_name then
if result.data.geo.continent_name ~= "" then txt = txt .. result.data.geo.continent_name .."," end
end
if result.data.geo.country_name then
txt = txt .. " Country: " .. result.data.geo.country_name
if result.data.geo.country_name ~= "" then txt = txt .. result.data.geo.country_name .."," end
end
if result.data.geo.region_name then
if result.data.geo.region_name ~= "" then txt = txt .. result.data.geo.region_name .. "," end
end
if result.data.geo.city then
txt = txt .. " City: " .. result.data.geo.city
if result.data.geo.city ~= "" then txt = txt .. result.data.geo.city .."" end
end
if result.data.geo.timezone then
txt = txt .. " Timezone: " .. result.data.geo.timezone
if result.data.geo.timezone ~= "" then txt = txt .. " Timezone: " .. result.data.geo.timezone end
end
if result.data.geo.asn then
txt = txt .. " ASN: " .. result.data.geo.asn
if result.data.geo.asn ~= "" then txt = txt .. " ASN: " .. result.data.geo.asn end
end
if result.data.geo.isp then
txt = txt .. " ISP: " .. result.data.geo.isp
if result.data.geo.isp ~= "" then txt = txt .. " ISP: " .. result.data.geo.isp end
end
if result.data.geo.ip then
txt = txt .. " IP: " .. result.data.geo.ip
if result.data.geo.ip ~= "" then txt = txt .. " IP: " .. result.data.geo.ip end
end
return txt
else
@ -27,15 +36,18 @@ function format_result(result)
end
if not governing.modgeoip then
minetest.register_privilege("geoip", {
description = "can do geoip lookups on players for governing",
give_to_singleplayer = false
})
end
governing.joinplayer_callback = function() end -- function(name, result)
governing.joinplayer_callback = function() end -- function(name, result)
minetest.register_on_joinplayer(function(player) -- query ip on join, record in logs and execute callback
minetest.register_on_joinplayer(function(player) -- query ip on join, record in logs and execute callback
if not governing.modgeoip then
if not player then return end
if not player:is_player() then return end
if not minetest.get_player_ip then return end
@ -44,10 +56,10 @@ if not governing.modgeoip then
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)
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)