X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fgeoip.rb;h=8c5e44a4f87fbaf100a5ac9a2fdb59c46d1c10c3;hb=0ddf77c9d54236bbc75c2b603871fc0c9e072873;hp=c67f1095bf19522fdf9d9f8a1b60463f5068f7a5;hpb=9586b15bfd4f0b616dcc3994262029c9b4f9a6b6;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/geoip.rb b/data/rbot/plugins/geoip.rb index c67f1095..8c5e44a4 100755 --- a/data/rbot/plugins/geoip.rb +++ b/data/rbot/plugins/geoip.rb @@ -33,23 +33,19 @@ module ::GeoIP raw = Irc::Utils.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.ipinfodb(ip) + key = Irc::Utils.bot.config['geoip.ipinfodb_key'] + return if not key or key.empty? url = IPINFODB_URL % { :ip => ip, - :key => Irc::Utils.bot.config['geoip.ipinfodb_key'] + :key => key } debug "Requesting #{url}" @@ -70,6 +66,11 @@ module ::GeoIP end end + JUMP_TABLE = { + "ipinfodb" => Proc.new { |ip| ipinfodb(ip) }, + "geoiptool" => Proc.new { |ip| geoiptool(ip) }, + } + def self.resolve(hostname, api) raise InvalidHostError unless valid_host?(hostname) @@ -79,15 +80,9 @@ module ::GeoIP raise InvalidHostError end - jump_table = { - "ipinfodb" => Proc.new { |ip| ipinfodb(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 @@ -112,8 +107,8 @@ end class GeoIpPlugin < Plugin Config.register Config::ArrayValue.new('geoip.sources', - :default => [ "ipinfodb", "kapsi", "geoiptool" ], - :desc => "Which API to use for lookups. Supported values: ipinfodb, 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") @@ -153,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) @@ -185,7 +180,7 @@ class GeoIpPlugin < Plugin apis = @bot.config['geoip.sources'] apis.compact.each { |api| geo = GeoIP::resolve(host, api) - if geo[:country] != "" + if geo and geo[:country] != "" break end }