-- This is a proprietary plugin, property of Andreas Bielawski, (c) 2015 <andi (dot) b (at) outlook (dot) de>
-- DO NOT USE WITHOUT PERMISSION

do

local BASE_URL = 'http://ipinfo.io'

function get_ip_data (ip)
  local url = BASE_URL..'/'..ip..'/json'
  local res,code  = http.request(url)
  if code == 404 then return "Diese IP gibt es nicht!" end
  if code ~= 200 then return "HTTP-FEHLER: "..code end
  local data = json:decode(res)

  if data.hostname == "No Hostname" then
	hostname = ""
  else
    hostname = ' ('..data.hostname..')'
  end
  
  if data.org then
    org = data.org
  else
    org = 'Unbekannt'
  end
  
  
  if data.city == "" or data.city == nil then
	city = "Unbekannt"
  else
    city = data.city
  end
  
 local country = data.country
 
  if data.region == "" or data.region == nil then
	region = ""
  else
    region = ', '..data.region
  end
  
  if data.postal then
    postal = ' (PLZ: '..data.postal..')'
  else
    postal = ''
  end
    local text = 'Der Server von '..ip..' gehört zu '..org..hostname..' und steht in '..country..', genauer in '..city..region..postal
  return text
end

function run(msg, matches)
  local ip = matches[1]
  return get_ip_data(ip)
end

return {
  description = "Sendet IP-Info", 
  usage = "#ip [IP]: Sendet Server-Infos",
  patterns = {"^#ip (.*)$"},
  run = run 
}

end