Advertisement
Guest User

Geolocation.lua

a guest
Aug 4th, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.12 KB | None | 0 0
  1. -- Geolocation by giraffe --
  2. -- Execute commands when a player joins based on the player's IP location --
  3.  
  4. -- Config --
  5.  
  6. MATCHES = {
  7.  
  8.     -- First variable is either: 'country', 'region' or 'city' --
  9.     -- Second variable is the name of the country, region, or city --
  10.     -- Third variable is commands to execute if player matches location --
  11.     -- $country, $region, and $city can be used as variables in the commands --
  12.  
  13.     { 'country', 'Mexico', 'say * "$name will not share his WORLD FAMOUS taco recipe with you!";' },
  14.     { 'country', 'Libya', 'say * "Ask $name how hot it is in $country.";' },
  15.     { 'country', 'North Korea', 'say * "$name was kicked. Reason: $country.";sv_kick $n;' },
  16.     { 'region', 'Kentucky', 'say * "$name is eating a bucket of $region Fried Chicken with Colonel Sanders.";' },
  17.     { 'city', 'London', 'say * "$name might just be a werewolf from $city...";' },
  18.  
  19. }
  20.  
  21. -- Commands to execute if player does not match any of the specified locations --
  22. NO_MATCH = 'say * "$name has connected from $country.";'
  23.  
  24. -- End of config --
  25.  
  26. api_version = '1.8.0.0'
  27.  
  28. function OnScriptLoad()
  29.     register_callback(cb['EVENT_JOIN'], 'OnPlayerJoin')
  30. end
  31.  
  32. function string:split(sep)
  33.     local sep, fields = sep or ':', {}
  34.     local pattern = string.format('([^%s]+)', sep)
  35.     self:gsub(pattern, function(c) fields[#fields+1] = c end)
  36.     return fields
  37. end
  38.  
  39. function string:startsWith(prefix)
  40.     return self:sub(1, string.len(prefix)) == prefix
  41. end
  42.  
  43. function get_geolocation(IP)
  44.     if(IP:startsWith('127.') or IP:startsWith('192.168.')) then
  45.         IP = ''
  46.     end
  47.     local p = assert(io.popen('wget -qO- http://ip-api.com/line/' .. IP .. '?fields=country,regionName,city'))
  48.     local result = p:read('*all')
  49.     p:close()
  50.     if(result ~= nil) then
  51.         local geodata = result:split('\n')
  52.         if(#geodata < 3) then
  53.             return nil
  54.         end
  55.         return geodata
  56.     end
  57.     return nil
  58. end
  59.  
  60. function OnPlayerJoin(PlayerIndex)
  61.      local location = get_geolocation(get_var(PlayerIndex,'$ip'):split(':')[1])
  62.      if(location ~= nil) then
  63.          for i=1,#MATCHES do
  64.              if( (MATCHES[i][1]:lower() == 'country' and MATCHES[i][2]:lower() == location[1]:lower()) or (MATCHES[i][1]:lower() == 'region' and MATCHES[i][2]:lower() == location[2]:lower()) or (MATCHES[i][1]:lower() == 'city' and MATCHES[i][2]:lower() == location[3]:lower()) ) then
  65.                  local commands = MATCHES[i][3]
  66.                  commands = string.gsub(commands, '$country', location[1])
  67.                  commands = string.gsub(commands, '$region', location[2])
  68.                  commands = string.gsub(commands, '$city', location[3])
  69.                  execute_command_sequence(commands, PlayerIndex)
  70.                  return
  71.              end
  72.          end
  73.          local commands = NO_MATCH
  74.          commands = string.gsub(commands, '$country', location[1])
  75.          commands = string.gsub(commands, '$region', location[2])
  76.          commands = string.gsub(commands, '$city', location[3])
  77.          execute_command_sequence(commands, PlayerIndex)
  78.      end
  79.      return
  80. end
  81.  
  82. function OnScriptUnload() end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement