]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/geoip.rb
quiz: stop quizzes and timers on cleanup
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / geoip.rb
index 3b7bf75169a4d7e7de883abd5da6bd15a839008f..c76310d5b553fcb417a977b83dcd32e15d50b9cf 100755 (executable)
@@ -44,11 +44,16 @@ module ::GeoIP
     return YAML::load(yaml)
   end
 
-  def self.blogama(ip)
-    url = "http://ipinfodb.com/ip_query.php?ip="
-    debug "Requesting #{url+ip}"
+  IPINFODB_URL = "http://api.ipinfodb.com/v2/ip_query.php?key=%{key}&ip=%{ip}"
 
-    xml = Irc::Utils.bot.httputil.get(url+ip)
+  def self.ipinfodb(ip)
+    url = IPINFODB_URL % {
+      :ip => ip,
+      :key => Irc::Utils.bot.config['geoip.ipinfodb_key']
+    }
+    debug "Requesting #{url}"
+
+    xml = Irc::Utils.bot.httputil.get(url)
 
     if xml
       obj = REXML::Document.new(xml)
@@ -65,6 +70,12 @@ module ::GeoIP
     end
   end
 
+  JUMP_TABLE = {
+    "ipinfodb" => Proc.new { |ip| ipinfodb(ip) },
+    "kapsi" => Proc.new { |ip| kapsi(ip) },
+    "geoiptool" => Proc.new { |ip| geoiptool(ip) },
+  }
+
   def self.resolve(hostname, api)
     raise InvalidHostError unless valid_host?(hostname)
 
@@ -74,15 +85,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(ip)
   end
 end
 
@@ -107,8 +112,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", "kapsi", "geoiptool" ],
+      :desc => "Which API to use for lookups. Supported values: ipinfodb, kapsi, 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 +153,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)
@@ -183,32 +191,30 @@ class GeoIpPlugin < Plugin
       }
     rescue GeoIP::InvalidHostError, RuntimeError
       if nick
-        return _("#{nick}'s location could not be resolved")
+        return _("%{nick}'s location could not be resolved") % { :nick => nick }
       else
-        return _("#{host} could not be resolved")
+        return _("%{host} could not be resolved") % { :host => host }
       end
     rescue GeoIP::BadAPIError
       return _("The owner configured me to use an API that doesn't exist, bug them!")
     end
 
-    res = _("%{thing} is #{nick ? "from" : "located in"}") % {
-      :thing   => (nick ? nick : Resolv::getaddress(host)),
-      :country => geo[:country]
-    }
+    location = []
+    location << geo[:city] unless geo[:city].nil_or_empty?
+    location << geo[:region] unless geo[:region].nil_or_empty? or geo[:region] == geo[:city]
+    location << geo[:country] unless geo[:country].nil_or_empty?
 
-    res << " %{city}" % {
-      :city => geo[:city]
-    } unless geo[:city].to_s.empty?
-
-    res << " %{region}," % {
-      :region  => geo[:region]
-    } unless geo[:region].to_s.empty? || geo[:region] == geo[:city]
+    if nick
+      res = _("%{nick} is from %{location}")
+    else
+      res = _("%{host} is located in %{location}")
+    end
 
-    res << " %{country}" % {
-      :country => geo[:country]
+    return res % {
+      :nick => nick,
+      :host => host,
+      :location => location.join(', ')
     }
-
-    return res
   end
 end