# plugins/geoip.py
#
# geoiplookup has te be installed first!! see net/GeoIP

""" lookup location of hostname/ip """

__copyright__ = 'this file is in the public domain'
__thnx__ = 'snufj'

from gozerbot.commands import cmnds
from gozerbot.examples import examples
from gozerbot.generic import gozerpopen

def handle_geoip(bot, ievent):
    """ geoip <domain|ip> .. show geoip data of domain or ip .. GeoIP must
        be installed """
    try:
        geoip = ievent.args[0]
    except IndexError:
        ievent.missing('<domain|ipaddress>')
        return
    argslist = ['geoiplookup', ]
    userlist = [geoip, ]
    proces = gozerpopen(argslist, userlist)
    data = proces.fromchild.readlines()
    result = proces.close()
    if result == 0:
        resultstr = ""
        for i in data:
            i = i.strip()
            if '--' in i:
                continue
            if not i:
                continue
            resultstr += "%s .. " % i
        ievent.reply(resultstr[:-4])
    else:
        ievent.reply('error running geoiplookup')
        return

cmnds.add('geoip', handle_geoip, 'USER')
examples.add('geoip', 'geoip <domain|ip> .. show geoip countryinfo', \
'geoip r8.cg.nu')
