summaryrefslogtreecommitdiff
path: root/data/rbot/plugins/geoip.rb
blob: c67f1095bf19522fdf9d9f8a1b60463f5068f7a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#-- 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 (network-wide) and IP addresses

module ::GeoIP
  class InvalidHostError < RuntimeError; end
  class BadAPIError < RuntimeError; end

  HOST_NAME_REGEX  = /^[a-z0-9\-]+(?:\.[a-z0-9\-]+)*\.[a-z]{2,4}/i

  def self.valid_host?(hostname)
    hostname =~ HOST_NAME_REGEX ||
    hostname =~ Resolv::IPv4::Regex && (hostname.split(".").map { |e| e.to_i }.max <= 255)
  end

  def self.geoiptool(ip)
    url = "http://www.geoiptool.com/en/?IP="
    regexes  = {
      :country => %r{Country:.*?<a href=".*?" target="_blank"> (.*?)</a>}m,
      :region  => %r{Region:.*?<a href=".*?" target="_blank">(.*?)</a>}m,
      :city    => %r{City:.*?<td align="left" class="arial_bold">(.*?)</td>}m,
      :lat     => %r{Latitude:.*?<td align="left" class="arial_bold">(.*?)</td>}m,
      :lon     => %r{Longitude:.*?<td align="left" class="arial_bold">(.*?)</td>}m
    }
    res = {}
    raw = Irc::Utils.bot.httputil.get_response(url+ip)
    raw = raw.decompress_body(raw.raw_body)

    regexes.each { |key, regex| res[key] = Iconv.conv('utf-8', 'ISO-8859-1', raw.scan(regex).to_s) }

    return res
  end

  def self.kapsi(ip)
    url = "http://lakka.kapsi.fi:40086/lookup.yaml?host="
    yaml = Irc::Utils.bot.httputil.get(url+ip)
    return YAML::load(yaml)
  end

  IPINFODB_URL = "http://api.ipinfodb.com/v2/ip_query.php?key=%{key}&ip=%{ip}"

  def self.ipinfodb(ip)
    url = IPINFODB_URL % {
      :ip => ip,
      :key => Irc::Utils.bot.config['geoip.ipinfodb_key']
    }
    debug "Requesting #{url}"

    xml = Irc::Utils.bot.httputil.get(url)

    if xml
      obj = REXML::Document.new(xml)
      debug "Found #{obj}"
      newobj = {
        :country => obj.elements["Response"].elements["CountryName"].text,
        :city => obj.elements["Response"].elements["City"].text,
        :region => obj.elements["Response"].elements["RegionName"].text,
      }
      debug "Returning #{newobj}"
      return newobj
    else
      raise InvalidHostError
    end
  end

  def self.resolve(hostname, api)
    raise InvalidHostError unless valid_host?(hostname)

    begin
      ip = Resolv.getaddress(hostname)
    rescue Resolv::ResolvError
      raise InvalidHostError
    end

    jump_table = {
        "ipinfodb" => Proc.new { |ip| ipinfodb(ip) },
        "kapsi" => Proc.new { |ip| kapsi(ip) },
        "geoiptool" => Proc.new { |ip| geoiptool(ip) },
    }

    raise BadAPIError unless jump_table.key?(api)

    return jump_table[api].call(ip)
  end
end

class Stack
  def initialize
    @hash = {}
  end

  def [](nick)
    @hash[nick] = [] unless @hash[nick]
    @hash[nick]
  end

  def has_nick?(nick)
    @hash.has_key?(nick)
  end

  def clear(nick)
    @hash.delete(nick)
  end
end

class GeoIpPlugin < Plugin
  Config.register Config::ArrayValue.new('geoip.sources',
      :default => [ "ipinfodb", "kapsi", "geoiptool" ],
      :desc => "Which API to use for lookups. Supported values: ipinfodb, kapsi, geoiptool")
  Config.register Config::StringValue.new('geoip.ipinfodb_key',
      :default => "",
      :desc => "API key for the IPinfoDB geolocation service")

  def help(plugin, topic="")
    "geoip [<user|hostname|ip>] => returns the geographic location of whichever has been given -- note: user can be anyone on the network"
  end

  def initialize
    super

    @stack = Stack.new
  end

  def whois(m)
    nick = m.whois[:nick].downcase

    # need to see if the whois reply was invoked by this plugin
    return unless @stack.has_nick?(nick)

    if m.target
      msg = host2output(m.target.host, m.target.nick)
    else
      msg = "no such user on "+@bot.server.hostname.split(".")[-2]
    end
    @stack[nick].each do |source|
      @bot.say source, msg
    end

    @stack.clear(nick)
  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 on the channel 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

      # input is a host name or an IP
      if GeoIP::valid_host?(params[:input])
         m.reply host2output(params[:input])

      # assume input is a nick
      elsif params[:input] !~ /\./
        nick = params[:input].downcase

        @stack[nick] << m.replyto
        @bot.whois(nick)
      else
        m.reply "invalid input"
      end
    end
  end

  def host2output(host, nick=nil)
    return "127.0.0.1 could not be res.. wait, what?" if host == "127.0.0.1"

    geo = {:country => ""}
    begin
      apis = @bot.config['geoip.sources']
      apis.compact.each { |api|
        geo = GeoIP::resolve(host, api)
        if geo[:country] != ""
          break
        end
      }
    rescue GeoIP::InvalidHostError, RuntimeError
      if nick
        return _("%{nick}'s location could not be resolved") % { :nick => nick }
      else
        return _("%{host} could not be resolved") % { :host => host }
      end
    rescue GeoIP::BadAPIError
      return _("The owner configured me to use an API that doesn't exist, bug them!")
    end

    location = []
    location << geo[:city] unless geo[:city].nil_or_empty?
    location << geo[:region] unless geo[:region].nil_or_empty? or geo[:region] == geo[:city]
    location << geo[:country] unless geo[:country].nil_or_empty?

    if nick
      res = _("%{nick} is from %{location}")
    else
      res = _("%{host} is located in %{location}")
    end

    return res % {
      :nick => nick,
      :host => host,
      :location => location.join(', ')
    }
  end
end

plugin = GeoIpPlugin.new
plugin.map "geoip [:input]", :action => 'geoip', :thread => true