# Author and licensing __Author__ = "b-mcg" __Email__ = "bmcg0890@gmail.com" __License__ = """ Copyright (C) 2014 b-mcg This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . """ # Imports try: import weechat except ImportError: import sys print '\nError: script must be run under Weechat.\n' sys.exit(2) try: import requests except ImportError: weechat.prnt('', 'Error: script requires the requests module.') from contextlib import closing import socket weechat.register('iplookup', "b-mcg", '0.1', 'GPLv3', 'GeoIP lookup for city/country/ISP info', '', '') def help(): """ Print help menu to main Weechat buffer. """ weechat.prnt('', '\n --Start Help-- \n') weechat.prnt('', '--Options--') weechat.prnt('', '[ip address] (positional)') weechat.prnt('', 'main [ip address] (displays GeoIP info in the weechat main buffer)') weechat.prnt('', 'help (display help)') weechat.prnt('', 'Example Usage: /iplookup 76.54.22.123 | /iplookup main 76.54.22.123') weechat.prnt('', '\n --End Help-- \n') return '' def grab_geoip_data(ip): """ Request JSON GeoIP data from http://www.telize.com/geoip/ and returns string containing IP, ISP, City(if available), Country, and Region(if available). """ # Issue GET request and close the connection after getting needed information with closing(requests.get('http://www.telize.com/geoip/{0}'.format(ip))) as req: # Create string from the JSON returned from GET request comprised of City(if available), Country, Region(if availabel), ISP, and IP info geoip_data = 'GeoIP info for IP: {0} ({1})'.format(req.json()['ip'], ''.join(['{0}: {1} | '.format(item, val) for item, val in req.json().iteritems() if item == 'country' \ or item == 'city' or item == 'region' or item == 'isp'])[:-3]) return geoip_data def test_ip(ip): """ Uses the socket module to determine if ip is a valid ipv4 or ipv6 address. """ try: socket.inet_pton(socket.AF_INET, ip) return True except socket.error: try: socket.inet_pton(socket.AF_INET6, ip) return True except socket.error: return False def control(data, buffer, args): """ Parse current Weechat buffer that /iplookup command is launched from for commands and print data to main or current buffer. """ try: # Split Weechat args and check for different cases args_list = args.split() if args_list[0].lower() == 'help': weechat.prnt('', '{0}'.format(help())) elif args_list[0].lower() == 'main' and (test_ip(args_list[1])): weechat.prnt('', grab_geoip_data(args_list[1])) elif args_list[0].lower() != 'main' and (test_ip(args_list[0])): weechat.command(buffer, grab_geoip_data(args_list[0])) else: weechat.prnt('', 'Error: Check that valid IP address was given') # Check for any possible exceptions that may occur and print them to main buffer except Exception as err: weechat.prnt('', str(err)) return weechat.WEECHAT_RC_OK return weechat.WEECHAT_RC_OK # Hook the iplookup command weechat.hook_command('iplookup', 'GeoIP lookup', '/iplookup [ip address] | [help]', """ /iplookup [ip address] | [main] | [help] Example: /iplookup 76.54.22.123 | /iplookup main 76.54.22.123 """, '', 'control', '')