UNO plugin: give cards to correct player at endgame
[rbot] / 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://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
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     if m.target
86       msg = host2output(m.target.host, m.target.nick)
87     else
88       msg = "no such user on "+@bot.server.hostname.split(".")[-2]
89     end
90     @stack[nick].each do |source|
91       @bot.say source, msg
92     end
93
94     @stack.clear(nick)
95   end
96
97   def geoip(m, params)
98     if params.empty?
99       m.reply host2output(m.source.host, m.source.nick)
100     else
101       if m.replyto.class == Channel
102
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] }
105
106         if user
107           m.reply host2output(user.host, user.nick)
108           return
109         end
110       end
111
112       # input is a host name or an IP
113       if GeoIP::valid_host?(params[:input])
114          m.reply host2output(params[:input])
115
116       # assume input is a nick
117       elsif params[:input] !~ /\./
118         nick = params[:input].downcase
119
120         @stack[nick] << m.replyto
121         @bot.whois(nick)
122       else
123         m.reply "invalid input"
124       end
125     end
126   end
127
128   def host2output(host, nick=nil)
129     return "127.0.0.1 could not be res.. wait, what?" if host == "127.0.0.1"
130
131     begin
132       geo = GeoIP::resolve(host)
133
134       raise if geo[:country].empty?
135     rescue GeoIP::InvalidHostError, RuntimeError
136       return _("#{nick ? "#{nick}'s location" : host} could not be resolved")
137     end
138
139     res = _("%{thing} is #{nick ? "from" : "located in"}") % {
140       :thing   => (nick ? nick : Resolv::getaddress(host)),
141       :country => geo[:country]
142     }
143
144     res << " %{city}," % {
145       :city => geo[:city]
146     } unless geo[:city].to_s.empty?
147
148     res << " %{country}" % {
149       :country => geo[:country]
150     }
151
152     res << " (%{region})" % {
153       :region  => geo[:region]
154     } unless geo[:region].to_s.empty? || geo[:region] == geo[:city]
155
156     return res
157   end
158 end
159
160 plugin = GeoIpPlugin.new
161 plugin.map "geoip [:input]", :action => 'geoip', :thread => true