]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/geoip.rb
geoip plugin: new service for geoip-lookup along with some other enhancements
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / geoip.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: Geo IP Plugin
5 #
6 # Author:: Raine Virta <rane@kapsi.fi>
7 # Copyright:: (C) 2008 Raine Virta
8 # License:: GPL v2
9 #
10 # Resolves the geographic locations of users (network-wide) and IP addresses
11
12 module GeoIP
13   class InvalidHostError < RuntimeError; end
14
15   GEO_IP_PRIMARY   = "http://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
18
19   REGEX  = {
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
23   }
24
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)
28   end
29
30   def self.resolve(hostname)
31     raise InvalidHostError unless valid_host?(hostname)
32
33     yaml = Irc::Utils.bot.httputil.get(GEO_IP_PRIMARY+hostname)
34
35     if yaml
36       return YAML::load(yaml)
37     else
38       res = {}
39       raw = Irc::Utils.bot.httputil.get_response(GEO_IP_SECONDARY+hostname)
40       raw = raw.decompress_body(raw.raw_body)
41
42       REGEX.each { |key, regex| res[key] = Iconv.conv('utf-8', 'ISO-8859-1', raw.scan(regex).to_s) }
43
44       return res
45     end
46   end
47 end
48
49 class Stack
50   def initialize
51     @hash = {}
52   end
53
54   def [](nick)
55     @hash[nick] = [] unless @hash[nick]
56     @hash[nick]
57   end
58
59   def has_nick?(nick)
60     @hash.has_key?(nick)
61   end
62
63   def clear(nick)
64     @hash.delete(nick)
65   end
66 end
67
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"
71   end
72
73   def initialize
74     super
75
76     @stack = Stack.new
77   end
78
79   def whois(m)
80     nick = m.whois[:nick].downcase
81
82     # need to see if the whois reply was invoked by this plugin
83     return unless @stack.has_nick?(nick)
84
85     @stack[nick].each do |source|
86       if m.target
87         @bot.say source, host2output(m.target.host, m.target.nick)
88       else
89         @bot.say source, "no such user on "+@bot.server.hostname.split(".")[-2]
90       end
91     end
92
93     @stack.clear(nick)
94   end
95
96   def geoip(m, params)
97     if params.empty?
98       m.reply host2output(m.source.host, m.source.nick)
99     else
100       if m.replyto.class == Channel
101
102         # check if there is an user on the channel with nick same as input given
103         user = m.replyto.users.find { |user| user.nick == params[:input] }
104
105         if user
106           m.reply host2output(user.host, user.nick)
107           return
108         end
109       end
110
111       # input is a host name or an IP
112       if GeoIP::valid_host?(params[:input])
113          m.reply host2output(params[:input])
114
115       # assume input is a nick
116       elsif params[:input] !~ /\./
117         nick = params[:input].downcase
118
119         @stack[nick] << m.replyto
120         @bot.whois(nick)
121       else
122         m.reply "invalid input"
123       end
124     end
125   end
126
127   def host2output(host, nick=nil)
128     return "127.0.0.1 could not be res.. wait, what?" if host == "127.0.0.1"
129
130     begin
131       geo = GeoIP::resolve(host)
132
133       raise if geo[:country].empty?
134     rescue GeoIP::InvalidHostError, RuntimeError
135       return _("#{nick ? "#{nick}'s location" : host} could not be resolved")
136     end
137
138     res = _("%{thing} is #{nick ? "from" : "located in"}") % {
139       :thing   => (nick ? nick : Resolv::getaddress(host)),
140       :country => geo[:country]
141     }
142
143     res << " %{city}," % {
144       :city => geo[:city]
145     } unless geo[:city].to_s.empty?
146
147     res << " %{country}" % {
148       :country => geo[:country]
149     }
150
151     res << " (%{region})" % {
152       :region  => geo[:region]
153     } unless geo[:region].to_s.empty? || geo[:region] == geo[:city]
154
155     return res
156   end
157 end
158
159 plugin = GeoIpPlugin.new
160 plugin.map "geoip [:input]", :action => 'geoip'