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 (network-wide) and IP addresses
13 class InvalidHostError < RuntimeError; end
15 GEO_IP_PRIMARY = "http://lakka.kapsi.fi:40086/lookup.yaml?host="
16 GEO_IP_SECONDARY = "http://www.geoiptool.com/en/?IP="
17 HOST_NAME_REGEX = /^[a-z0-9\-]+(?:\.[a-z0-9\-]+)*\.[a-z]{2,4}/i
20 :country => %r{Country:.*?<a href=".*?" target="_blank"> (.*?)</a>}m,
21 :region => %r{Region:.*?<a href=".*?" target="_blank">(.*?)</a>}m,
22 :city => %r{City:.*?<td align="left" class="arial_bold">(.*?)</td>}m
25 def self.valid_host?(hostname)
26 hostname =~ HOST_NAME_REGEX ||
27 hostname =~ Resolv::IPv4::Regex && (hostname.split(".").map { |e| e.to_i }.max <= 255)
30 def self.resolve(hostname)
31 raise InvalidHostError unless valid_host?(hostname)
33 yaml = Irc::Utils.bot.httputil.get(GEO_IP_PRIMARY+hostname)
36 return YAML::load(yaml)
39 raw = Irc::Utils.bot.httputil.get_response(GEO_IP_SECONDARY+hostname)
40 raw = raw.decompress_body(raw.raw_body)
42 REGEX.each { |key, regex| res[key] = Iconv.conv('utf-8', 'ISO-8859-1', raw.scan(regex).to_s) }
55 @hash[nick] = [] unless @hash[nick]
68 class GeoIpPlugin < Plugin
69 def help(plugin, topic="")
70 "geoip [<user|hostname|ip>] => returns the geographic location of whichever has been given -- note: user can be anyone on the network"
80 nick = m.whois[:nick].downcase
82 # need to see if the whois reply was invoked by this plugin
83 return unless @stack.has_nick?(nick)
86 msg = host2output(m.target.host, m.target.nick)
88 msg = "no such user on "+@bot.server.hostname.split(".")[-2]
90 @stack[nick].each do |source|
99 m.reply host2output(m.source.host, m.source.nick)
101 if m.replyto.class == Channel
103 # check if there is an user on the channel with nick same as input given
104 user = m.replyto.users.find { |user| user.nick == params[:input] }
107 m.reply host2output(user.host, user.nick)
112 # input is a host name or an IP
113 if GeoIP::valid_host?(params[:input])
114 m.reply host2output(params[:input])
116 # assume input is a nick
117 elsif params[:input] !~ /\./
118 nick = params[:input].downcase
120 @stack[nick] << m.replyto
123 m.reply "invalid input"
128 def host2output(host, nick=nil)
129 return "127.0.0.1 could not be res.. wait, what?" if host == "127.0.0.1"
132 geo = GeoIP::resolve(host)
134 raise if geo[:country].empty?
135 rescue GeoIP::InvalidHostError, RuntimeError
136 return _("#{nick ? "#{nick}'s location" : host} could not be resolved")
139 res = _("%{thing} is #{nick ? "from" : "located in"}") % {
140 :thing => (nick ? nick : Resolv::getaddress(host)),
141 :country => geo[:country]
144 res << " %{city}," % {
146 } unless geo[:city].to_s.empty?
148 res << " %{country}" % {
149 :country => geo[:country]
152 res << " (%{region})" % {
153 :region => geo[:region]
154 } unless geo[:region].to_s.empty? || geo[:region] == geo[:city]
160 plugin = GeoIpPlugin.new
161 plugin.map "geoip [:input]", :action => 'geoip', :thread => true