]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/geoip.rb
refactor: httputil no longer core module see #38
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / geoip.rb
old mode 100755 (executable)
new mode 100644 (file)
index 99ed71e..48391a1
@@ -20,7 +20,7 @@ module ::GeoIP
     hostname =~ Resolv::IPv4::Regex && (hostname.split(".").map { |e| e.to_i }.max <= 255)
   end
 
-  def self.geoiptool(ip)
+  def self.geoiptool(bot, ip)
     url = "http://www.geoiptool.com/en/?IP="
     regexes  = {
       :country => %r{Country:.*?<a href=".*?" target="_blank"> (.*?)</a>}m,
@@ -30,25 +30,26 @@ module ::GeoIP
       :lon     => %r{Longitude:.*?<td align="left" class="arial_bold">(.*?)</td>}m
     }
     res = {}
-    raw = Irc::Utils.bot.httputil.get_response(url+ip)
+    raw = bot.httputil.get_response(url+ip)
     raw = raw.decompress_body(raw.raw_body)
 
-    regexes.each { |key, regex| res[key] = Iconv.conv('utf-8', 'ISO-8859-1', raw.scan(regex).to_s) }
+    regexes.each { |key, regex| res[key] = raw.scan(regex).join('') }
 
     return res
   end
 
-  def self.kapsi(ip)
-    url = "http://lakka.kapsi.fi:40086/lookup.yaml?host="
-    yaml = Irc::Utils.bot.httputil.get(url+ip)
-    return YAML::load(yaml)
-  end
+  IPINFODB_URL = "http://api.ipinfodb.com/v2/ip_query.php?key=%{key}&ip=%{ip}"
 
-  def self.blogama(ip)
-    url = "http://ipinfodb.com/ip_query.php?ip="
-    debug "Requesting #{url+ip}"
+  def self.ipinfodb(bot, ip)
+    key = bot.config['geoip.ipinfodb_key']
+    return if not key or key.empty?
+    url = IPINFODB_URL % {
+      :ip => ip,
+      :key => key
+    }
+    debug "Requesting #{url}"
 
-    xml = Irc::Utils.bot.httputil.get(url+ip)
+    xml = bot.httputil.get(url)
 
     if xml
       obj = REXML::Document.new(xml)
@@ -65,7 +66,12 @@ module ::GeoIP
     end
   end
 
-  def self.resolve(hostname, api)
+  JUMP_TABLE = {
+    "ipinfodb" => Proc.new { |bot, ip| ipinfodb(bot, ip) },
+    "geoiptool" => Proc.new { |bot, ip| geoiptool(bot, ip) },
+  }
+
+  def self.resolve(bot, hostname, api)
     raise InvalidHostError unless valid_host?(hostname)
 
     begin
@@ -74,15 +80,9 @@ module ::GeoIP
       raise InvalidHostError
     end
 
-    jump_table = {
-        "blogama" => Proc.new { |ip| blogama(ip) },
-        "kapsi" => Proc.new { |ip| kapsi(ip) },
-        "geoiptool" => Proc.new { |ip| geoiptool(ip) },
-    }
-
-    raise BadAPIError unless jump_table.key?(api)
+    raise BadAPIError unless JUMP_TABLE.key?(api)
 
-    return jump_table[api].call(ip)
+    return JUMP_TABLE[api].call(bot, ip)
   end
 end
 
@@ -107,8 +107,11 @@ end
 
 class GeoIpPlugin < Plugin
   Config.register Config::ArrayValue.new('geoip.sources',
-      :default => [ "blogama", "kapsi", "geoiptool" ],
-      :desc => "Which API to use for lookups. Supported values: blogama, kapsi, geoiptool")
+      :default => [ "ipinfodb", "geoiptool" ],
+      :desc => "Which API to use for lookups. Supported values: ipinfodb, geoiptool")
+  Config.register Config::StringValue.new('geoip.ipinfodb_key',
+      :default => "",
+      :desc => "API key for the IPinfoDB geolocation service")
 
   def help(plugin, topic="")
     "geoip [<user|hostname|ip>] => returns the geographic location of whichever has been given -- note: user can be anyone on the network"
@@ -145,7 +148,7 @@ class GeoIpPlugin < Plugin
       if m.replyto.class == Channel
 
         # check if there is an user on the channel with nick same as input given
-        user = m.replyto.users.find { |user| user.nick == params[:input] }
+        user = m.replyto.users.find { |usr| usr.nick == params[:input] }
 
         if user
           m.reply host2output(user.host, user.nick)
@@ -176,8 +179,8 @@ class GeoIpPlugin < Plugin
     begin
       apis = @bot.config['geoip.sources']
       apis.compact.each { |api|
-        geo = GeoIP::resolve(host, api)
-        if geo[:country] != ""
+        geo = GeoIP::resolve(@bot, host, api)
+        if geo and geo[:country] != ""
           break
         end
       }