forked from sopel-irc/sopel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ip] use pygeoip and socket instead of rscript
This is a work in progress. Fix issue sopel-irc#265 Convert to 4.0 standard as per issue sopel-irc#276
- Loading branch information
Elad Alfassa
committed
Jun 2, 2013
1 parent
e146063
commit 6b6ed4c
Showing
1 changed file
with
20 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <[email protected]> | ||
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() |