diff --git a/geoip.lua b/geoip.lua index c57ec0c..0c272e6 100644 --- a/geoip.lua +++ b/geoip.lua @@ -1,12 +1,5 @@ -if not governing.modgeoip then - minetest.register_privilege("geoip", { - description = "can do geoip lookups on players for governing", - give_to_singleplayer = false - }) -end - -local function format_result(result) +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 @@ -35,32 +28,26 @@ end if not governing.modgeoip then --- function(name, result) -governing.joinplayer_callback = function() end + minetest.register_privilege("geoip", { + description = "can do geoip lookups on players for governing", + give_to_singleplayer = false + }) --- query ip on join, record in logs and execute callback -minetest.register_on_joinplayer(function(player) - if not minetest.get_player_ip then - return - end + governing.joinplayer_callback = function() end -- function(name, result) - 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 to debug.txt - local txt = format_result(data) - if txt then - minetest.log("action", "[goberning/geoip] result for player " .. name .. ": " .. txt) - else - minetest.log("error", "[goberning/geoip] result for player " .. name .. ": seems fails for ip "..ip) - end - - -- execute callback - governing.joinplayer_callback(name, data) + 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) end diff --git a/init.lua b/init.lua index 34222e7..5265cda 100644 --- a/init.lua +++ b/init.lua @@ -72,31 +72,24 @@ function governing.is_creative(name) end function governing.lookup(ip, callback) - if not httpapi then minetest.log("error", "[governing/geoip] mod not in the trusted http mods!") return end - - -- https://forum.minetest.net/viewtopic.php?t=28636 - -- Only works at init time and must be called from the mod's main scope (not from a function). - httpapi.fetch({ - url = "https://tools.keycdn.com/geo.json?host=" .. ip, - extra_headers = { - "User-Agent: keycdn-tools:https://minetest.org" - }, - timeout = 5, - }, function(res) - if res.code == 200 then - local data = minetest.parse_json(res.data) - callback(data) - else - minetest.log("warning", "[governing/geoip] http request returned status: " .. res.code) - end - end) + httpapi.fetch( { + url = "https://tools.keycdn.com/geo.json?host=" .. ip, + extra_headers = { "User-Agent: keycdn-tools:https://minetest.org" }, + timeout = 5, + }, function(res) + if res.code == 200 then + local data = minetest.parse_json(res.data) + callback(data) + else + minetest.log("warning", "[governing/geoip] http request returned status: " .. res.code) + end + end) end - dofile(governing.modpath.."/geoip.lua") dofile(governing.modpath.."/commands.lua")