4 # :title: Geo IP Plugin
6 # Author:: Raine Virta <rane@kapsi.fi>
7 # Copyright:: (C) 2008 Raine Virta
10 # Resolves the geographic locations of users and IP addresses
13 GEO_IP = "http://www.geoiptool.com/en/?IP="
15 def self.resolve(hostname)
16 raw = Irc::Utils.bot.httputil.get(GEO_IP+hostname, :cache => true)
19 :country => raw.scan(%r{Country:.*?<a href=".*?" target="_blank"> (.*?)</a>}m).to_s,
20 :region => raw.scan(%r{Region:.*?<a href=".*?" target="_blank">(.*?)</a>}m).to_s,
21 :city => raw.scan(%r{City:.*?<td align="left" class="arial_bold">(.*?)</td>}m).to_s
26 class GeoIpPlugin < Plugin
27 def help(plugin, topic="")
28 "geoip [<user|hostname|ip>] => returns the geographic location of whichever has been given"
33 m.reply host2output(m.source.host, m.source.nick)
35 if m.replyto.class == Channel
36 # check if there is an user with nick same as input given
37 user = m.replyto.users.find { |user| user.nick == params[:input] }
40 m.reply host2output(user.host, user.nick)
45 if params[:input] =~ /[a-z0-9\-]+(?:\.[a-z0-9\-]+)*\.[a-z]{2,3}/i ||
46 params[:input] =~ Resolv::IPv4::Regex
47 m.reply host2output(params[:input])
49 m.reply "invalid input"
54 def host2output(host, nick=nil)
55 geo = GeoIP::resolve(host)
57 if geo[:country].empty?
58 return _("#{host} could not be resolved")
61 res = _("%{thing} is #{nick ? "from" : "located in"}") % {
62 :thing => (nick ? nick : Resolv::getaddress(host)),
63 :country => geo[:country]
66 res << " %{city}," % {
68 } unless geo[:city].empty?
70 res << " %{country}" % {
71 :country => geo[:country]
74 res << " (%{region})" % {
75 :region => geo[:region]
76 } unless geo[:region].empty? || geo[:region] == geo[:city]
82 plugin = GeoIpPlugin.new
83 plugin.map "geoip [:input]", :action => 'geoip'