diff --git a/ip.py b/ip.py index 209896b06c..ec7ec2cb9a 100644 --- a/ip.py +++ b/ip.py @@ -1,54 +1,38 @@ # coding=utf-8 """ ip.py - Willie IP Lookup Module -Copyright 2011, Dimitri Molenaars, TyRope.nl +Copyright 2011, Dimitri Molenaars, TyRope.nl, +Copyright © 2013, Elad Alfassa Licensed under the Eiffel Forum License 2. http://willie.dftba.net """ import re -import willie.web as web - +import pygeoip +import socket +from willie.module import commands, example +@commands('iplookup', 'ip') +@example('.ip 8.8.8.8') def ip(willie, trigger): """IP Lookup tool""" if not trigger.group(2): return willie.reply("No search term.") query = trigger.group(2) - uri = 'http://www.rscript.org/lookup.php?type=ipdns&ip=' - answer = web.get(uri + web.quote(query.replace('+', '%2B'))) - if answer: - invalid = re.search("(?:INVALID: )([\S ]*)", answer) - if invalid: - response = "[IP/Host Lookup] " + invalid.group(1) - else: - #parse stuffs. - host = re.search("(?:Hostname:[ ]?)([\S ]*)", answer) - isp = re.search("(?:ISP:[ ]?)([\S ]*)", answer) - org = re.search("(?:Organization:[ ]?)([\S ]*)(?:Services:)", answer) - typ = re.search("(?:Type:[ ]?)([\S ]*)", answer) - assign = re.search("(?:Assignment:[ ]?)([\S ]*)", answer) - city = re.search("(?:City:[ ]?)([\S ]*)", answer) - state = re.search("(?:State/Region:[ ]?)([\S ]*)", answer) - country = re.search("(?:Country:[ ]?)([\S ]*)(?: )", answer) - - if not host or not isp or not org or not typ or not assign or not city or not state or not country: - response = "[IP/Host Lookup] Something went wrong, please try again." - else: - response = "[IP/Host Lookup] Hostname: " + host.group(1) - response += " | ISP: " + isp.group(1) - response += " | Organization: " + org.group(1) - response += " | Type: " + typ.group(1) - response += " | Assignment: " + assign.group(1) - response += " | Location: " + city.group(1) - response += ", " + state.group(1) - response += ", " + country.group(1) + "." - willie.say(response) - else: - willie.reply('Sorry, no result.') -ip.commands = ['iplookup', 'ip'] -ip.example = '.iplookup 8.8.8.8' + # FIXME: This shouldn't be hardcoded + gi_city = pygeoip.GeoIP('/usr/share/GeoIP/GeoLiteCity.dat') + gi_org = pygeoip.GeoIP('/usr/share/GeoIP/GeoIPASNum.dat') + host = socket.getfqdn(query) + response = "[IP/Host Lookup] Hostname: " + host + response += " | Location: " + gi_city.country_name_by_name(query) + region = gi_city.region_by_name(query)['region_name'] + if region is not '': + response += " | Region: %s" % region + isp = gi_org.org_by_name(query) + isp = re.sub('^AS\d+ ', '', isp) + response += " | ISP: %s" % isp + willie.say(response) if __name__ == '__main__': print __doc__.strip()