summaryrefslogtreecommitdiff
path: root/data
diff options
context:
space:
mode:
authorRaine Virta <rane@kapsi.fi>2008-08-11 04:08:49 +0300
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2008-08-11 08:36:55 +0200
commit0a900b044b1920942ef99be800d7cd9bd8b60f5c (patch)
tree4368d34f847dc4e34bffe756c3c886ccbe366446 /data
parent9fcb32ca8058384a4bb91d748fcbd1f81378734d (diff)
geoip plugin
Diffstat (limited to 'data')
-rwxr-xr-xdata/rbot/plugins/geoip.rb83
1 files changed, 83 insertions, 0 deletions
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 <rane@kapsi.fi>
+# 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:.*?<a href=".*?" target="_blank"> (.*?)</a>}m).to_s,
+ :region => raw.scan(%r{Region:.*?<a href=".*?" target="_blank">(.*?)</a>}m).to_s,
+ :city => raw.scan(%r{City:.*?<td align="left" class="arial_bold">(.*?)</td>}m).to_s
+ }
+ end
+end
+
+class GeoIpPlugin < Plugin
+ def help(plugin, topic="")
+ "geoip [<user|hostname|ip>] => 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'