Skip to content

Commit

Permalink
Maybe net messages?
Browse files Browse the repository at this point in the history
  • Loading branch information
BytewaveMLP committed Nov 8, 2014
1 parent c6370d7 commit 89c418b
Showing 1 changed file with 49 additions and 31 deletions.
80 changes: 49 additions & 31 deletions lua/ulx/modules/sh/geoip.lua
Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@
local CATEGORY_NAME = "Utility"

if SERVER then
util.AddNetworkString( "ulx_geoip_msg" )
util.AddNetworkString( "ulx_geoip_data" )
end

if CLIENT then
net.Receive( "ulx_geoip_msg",
function( len )
MsgN( "Received net message ulx_geoip_msg with length " .. len )

MsgN( net.ReadString() )
end )
net.Receive( "ulx_geoip_data",
function( len )
MsgN( "Received net message ulx_geoip_data with length" .. len )

for k, v in pairs( data ) do
MsgN( string.format( "%s: %s", k, v ) )
end
end )
end

function ulx.geoip( calling_ply, target_ply )
if target_ply:IsValid() then
user_name = target_ply:Nick()

-- Thanks to Garry, LocalPlayer():IPAddress() returns <IP>:<PORT>. All we want is <IP>.
-- Code below courtesy of Cobalt.
user_ip = tostring(
string.sub(
tostring(
target_ply:IPAddress()
),
1,
string.len(
tostring(
target_ply:IPAddress()
)
) - 6
) -- /sub
) -- /tostring

-- Thanks, freegeoip.net for the wonderful REST API!
query_string = "http://freegeoip.net/json/" .. user_ip
user_ip = target_ply:IPAddress():match("%d+%.%d+%.%d+%.%d+") or ""

-- Thanks, http://www.telize.com/ for the wonderful REST API!
-- (Switched from FreeGeoIP.net because of better information, considering switching back)
query_string = "http://www.telize.com/geoip/" .. user_ip

-- Get the JSON table containing the users' location, etc
http.Fetch( query_string, function ( json, number, headers, status )
http.Fetch( query_string,
function ( json, number, headers, status )
local data = util.JSONToTable( json )

-- Type consistency checks
Expand All @@ -39,10 +50,15 @@ function ulx.geoip( calling_ply, target_ply )

-- Notifications
if calling_ply:IsValid() then
-- Tell calling_ply where the information is
calling_ply:PrintMessage( HUD_PRINTTALK, string.format( "Location data for %s (%s) printed to console.", user_name, user_ip ) )
-- Note that the information displayed is what they were looking for
calling_ply:SendLua( [[MsgN( string.format( "Location data for %s (%s):", "]] .. user_name .. [[", "]] .. user_ip .. [[" ) )]])
if SERVER then
net.Start( "ulx_geoip_msg" )
net.WriteString( string.format( "Location data for %s (%s):", user_name, user_ip ) )
net.Send( calling_ply )

ServerLog( "Sent message ulx_geoip_msg to " .. calling_ply:Nick() )
end
else
-- Tell console that this is in fact the information it's looking for
ServerLog( string.format( "Location data for %s (%s):", user_name, user_ip ) )
Expand All @@ -51,18 +67,19 @@ function ulx.geoip( calling_ply, target_ply )
-- Tell target_ply they've been tracked
target_ply:PrintMessage( HUD_PRINTTALK, "An admin captured your GeoIP data." )

for k, v in pairs( data ) do
-- Print the information
if calling_ply:IsValid() then
-- Client call
calling_ply:SendLua( [[MsgN( string.format( "%s: %s", "]] .. k .. [[", "]] .. v .. [[" ) )]])
else
-- Server call
if calling_ply:IsValid() then
if SERVER then
net.Start( "ulx_geoip_data" )
net.WriteTable( data )
net.Send( calling_ply )

ServerLog( "Sent message ulx_geoip_data to " .. calling_ply:Nick() )
end
else
for k, v in pairs( data ) do
ServerLog( string.format( "%s: %s", k, v ) )
end
end


end,
function ( status, user_name )
-- Display HTTP status code to calling_ply
Expand All @@ -82,9 +99,10 @@ function ulx.geoip( calling_ply, target_ply )
end
end

ulx.fancyLogAdmin( calling_ply, "#A got the GeoIP location of #T", target_ply )
ulx.fancyLogAdmin( calling_ply, "#A captured the GeoIP location data for #T", target_ply )
end

local geoip = ulx.command( CATEGORY_NAME, "ulx geoip", ulx.geoip, "!geoip" )
geoip:addParam{ type=ULib.cmds.PlayerArg }
geoip:defaultAccess( ULib.ACCESS_SUPERADMIN )
geoip:help( "Prints geographical information about a user from their IP to chat." )
geoip:help( "Prints geographical information about a user from their IP to console." )

0 comments on commit 89c418b

Please sign in to comment.