From 0a900b044b1920942ef99be800d7cd9bd8b60f5c Mon Sep 17 00:00:00 2001 From: Raine Virta Date: Mon, 11 Aug 2008 04:08:49 +0300 Subject: geoip plugin --- data/rbot/plugins/geoip.rb | 83 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100755 data/rbot/plugins/geoip.rb (limited to 'data') diff --git a/data/rbot/plugins/geoip.rb b/data/rbot/plugins/geoip.rb new file mode 100755 index 00000000..2fde9594 --- /dev/null +++ b/data/rbot/plugins/geoip.rb @@ -0,0 +1,83 @@ +#-- vim:sw=2:et +#++ +# +# :title: Geo IP Plugin +# +# Author:: Raine Virta +# Copyright:: (C) 2008 Raine Virta +# License:: GPL v2 +# +# Resolves the geographic locations of users and IP addresses + +module GeoIP + GEO_IP = "http://www.geoiptool.com/en/?IP=" + + def self.resolve(hostname) + raw = Irc::Utils.bot.httputil.get(GEO_IP+hostname, :cache => true) + + { + :country => raw.scan(%r{Country:.*? (.*?)}m).to_s, + :region => raw.scan(%r{Region:.*?(.*?)}m).to_s, + :city => raw.scan(%r{City:.*?(.*?)}m).to_s + } + end +end + +class GeoIpPlugin < Plugin + def help(plugin, topic="") + "geoip [] => returns the geographic location of whichever has been given" + end + + def geoip(m, params) + if params.empty? + m.reply host2output(m.source.host, m.source.nick) + else + if m.replyto.class == Channel + # check if there is an user with nick same as input given + user = m.replyto.users.find { |user| user.nick == params[:input] } + + if user + m.reply host2output(user.host, user.nick) + return + end + end + + if params[:input] =~ /[a-z0-9\-]+(?:\.[a-z0-9\-]+)*\.[a-z]{2,3}/i || + params[:input] =~ Resolv::IPv4::Regex + m.reply host2output(params[:input]) + else + m.reply "invalid input" + end + end + end + + def host2output(host, nick=nil) + geo = GeoIP::resolve(host) + + if geo[:country].empty? + return _("#{host} could not be resolved") + end + + res = _("%{thing} is #{nick ? "from" : "located in"}") % { + :thing => (nick ? nick : Resolv::getaddress(host)), + :country => geo[:country] + } + + res << " %{city}," % { + :city => geo[:city] + } unless geo[:city].empty? + + res << " %{country}" % { + :country => geo[:country] + } + + res << " (%{region})" % { + :region => geo[:region] + } unless geo[:region].empty? || geo[:region] == geo[:city] + + return res + end +end + +plugin = GeoIpPlugin.new +plugin.map "geoip [:input]", :action => 'geoip' -- cgit v1.2.3