Changeset 940264d1718b5a32d34ac82276a29f0cc1a51941

Show
Ignore:
Timestamp:
08/13/08 07:32:59 (3 months ago)
Author:
Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
git-committer:
Giuseppe Bilotta <giuseppe.bilotta@gmail.com> 1218598379 +0200
git-parent:

[5604d4bfb78ff357997bcce0ee3fd010cf7d9c8d]

git-author:
Raine Virta <rane@kapsi.fi> 1218591091 +0300
Message:

geoip plugin: new service for geoip-lookup along with some other enhancements

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • data/rbot/plugins/geoip.rb

    r5604d4b r940264d  
    1111 
    1212module GeoIP 
    13   GEO_IP = "http://www.geoiptool.com/en/?IP=" 
     13  class InvalidHostError < RuntimeError; end 
     14 
     15  GEO_IP_PRIMARY   = "http://kapsi.fi:40086/lookup.yaml?host=" 
     16  GEO_IP_SECONDARY = "http://www.geoiptool.com/en/?IP=" 
     17  HOST_NAME_REGEX  = /[a-z0-9\-]+(?:\.[a-z0-9\-]+)*\.[a-z]{2,4}/i 
     18 
    1419  REGEX  = { 
    1520    :country => %r{Country:.*?<a href=".*?" target="_blank"> (.*?)</a>}m, 
     
    1823  } 
    1924 
     25  def self.valid_host?(hostname) 
     26    hostname =~ HOST_NAME_REGEX || 
     27    hostname =~ Resolv::IPv4::Regex && (hostname.split(".").map { |e| e.to_i }.max <= 255) 
     28  end 
     29 
    2030  def self.resolve(hostname) 
    21     res = {} 
    22     raw = Irc::Utils.bot.httputil.get_response(GEO_IP+hostname) 
    23     raw = raw.decompress_body(raw.raw_body) 
     31    raise InvalidHostError unless valid_host?(hostname) 
    2432 
    25     REGEX.each { |key, regex| res[key] = Iconv.conv('utf-8', 'ISO-8859-1', raw.scan(regex).to_s) } 
     33    yaml = Irc::Utils.bot.httputil.get(GEO_IP_PRIMARY+hostname) 
    2634 
    27     return res 
     35    if yaml 
     36      return YAML::load(yaml) 
     37    else 
     38      res = {} 
     39      raw = Irc::Utils.bot.httputil.get_response(GEO_IP_SECONDARY+hostname) 
     40      raw = raw.decompress_body(raw.raw_body) 
     41 
     42      REGEX.each { |key, regex| res[key] = Iconv.conv('utf-8', 'ISO-8859-1', raw.scan(regex).to_s) } 
     43 
     44      return res 
     45    end 
    2846  end 
    2947end 
     
    7391    end 
    7492 
    75     @stack.clear(m.whois[:nick]
     93    @stack.clear(nick
    7694  end 
    7795 
     
    92110 
    93111      # input is a host name or an IP 
    94       if params[:input] =~ /[a-z0-9\-]+(?:\.[a-z0-9\-]+)*\.[a-z]{2,4}/i || 
    95          params[:input] =~ Resolv::IPv4::Regex 
    96         m.reply host2output(params[:input]) 
     112      if GeoIP::valid_host?(params[:input]) 
     113         m.reply host2output(params[:input]) 
    97114 
    98115      # assume input is a nick 
    99       else 
     116      elsif params[:input] !~ /\./ 
    100117        nick = params[:input].downcase 
    101118 
    102119        @stack[nick] << m.replyto 
    103120        @bot.whois(nick) 
     121      else 
     122        m.reply "invalid input" 
    104123      end 
    105124    end 
     
    107126 
    108127  def host2output(host, nick=nil) 
    109     geo = GeoIP::resolve(host) 
     128    return "127.0.0.1 could not be res.. wait, what?" if host == "127.0.0.1" 
    110129 
    111     if geo[:country].empty? 
     130    begin 
     131      geo = GeoIP::resolve(host) 
     132 
     133      raise if geo[:country].empty? 
     134    rescue GeoIP::InvalidHostError, RuntimeError 
    112135      return _("#{nick ? "#{nick}'s location" : host} could not be resolved") 
    113136    end 
     
    120143    res << " %{city}," % { 
    121144      :city => geo[:city] 
    122     } unless geo[:city].empty? 
     145    } unless geo[:city].to_s.empty? 
    123146 
    124147    res << " %{country}" % { 
     
    128151    res << " (%{region})" % { 
    129152      :region  => geo[:region] 
    130     } unless geo[:region].empty? || geo[:region] == geo[:city] 
     153    } unless geo[:region].to_s.empty? || geo[:region] == geo[:city] 
    131154 
    132155    return res