Changeset 9125a2e6d1ef8af8d380da3766c704f9028dec69
- Timestamp:
- 08/11/08 20:26:47
(3 months ago)
- Author:
- Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
- git-committer:
- Giuseppe Bilotta <giuseppe.bilotta@gmail.com> 1218472007 +0200
- git-parent:
[be13464e9f3f41936bea6b4cf5ad6acf965a82a6]
- git-author:
- Raine Virta <rane@kapsi.fi> 1218471087 +0300
- Message:
geoip plugin: added nick based network-wide lookup and fixed some charset issues
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r0a900b0 |
r9125a2e |
|
| 8 | 8 | # License:: GPL v2 |
|---|
| 9 | 9 | # |
|---|
| 10 | | # Resolves the geographic locations of users and IP addresses |
|---|
| | 10 | # Resolves the geographic locations of users (network-wide) and IP addresses |
|---|
| 11 | 11 | |
|---|
| 12 | 12 | module GeoIP |
|---|
| 13 | 13 | GEO_IP = "http://www.geoiptool.com/en/?IP=" |
|---|
| | 14 | REGEX = { |
|---|
| | 15 | :country => %r{Country:.*?<a href=".*?" target="_blank"> (.*?)</a>}m, |
|---|
| | 16 | :region => %r{Region:.*?<a href=".*?" target="_blank">(.*?)</a>}m, |
|---|
| | 17 | :city => %r{City:.*?<td align="left" class="arial_bold">(.*?)</td>}m |
|---|
| | 18 | } |
|---|
| | 19 | |
|---|
| 14 | 20 | |
|---|
| 15 | 21 | def self.resolve(hostname) |
|---|
| 16 | | raw = Irc::Utils.bot.httputil.get(GEO_IP+hostname, :cache => true) |
|---|
| | 22 | res = {} |
|---|
| | 23 | raw = Irc::Utils.bot.httputil.get_response(GEO_IP+hostname) |
|---|
| | 24 | raw = raw.decompress_body(raw.raw_body) |
|---|
| 17 | 25 | |
|---|
| 18 | | { |
|---|
| 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 |
|---|
| 22 | | } |
|---|
| | 26 | REGEX.each { |key, regex| res[key] = Iconv.conv('utf-8', 'ISO-8859-1', raw.scan(regex).to_s) } |
|---|
| | 27 | |
|---|
| | 28 | return res |
|---|
| 23 | 29 | end |
|---|
| 24 | 30 | end |
|---|
| … | … | |
| 26 | 32 | class GeoIpPlugin < Plugin |
|---|
| 27 | 33 | def help(plugin, topic="") |
|---|
| 28 | | "geoip [<user|hostname|ip>] => returns the geographic location of whichever has been given" |
|---|
| | 34 | "geoip [<user|hostname|ip>] => returns the geographic location of whichever has been given -- note: user can be anyone on the network" |
|---|
| | 35 | end |
|---|
| | 36 | |
|---|
| | 37 | def whois(m) |
|---|
| | 38 | # need to see if the whois reply was invoked by this plugin |
|---|
| | 39 | return unless m.whois[:nick] == @nick |
|---|
| | 40 | |
|---|
| | 41 | if m.target |
|---|
| | 42 | @bot.say @source, host2output(m.target.host, m.target.nick) |
|---|
| | 43 | else |
|---|
| | 44 | @bot.say @source, "no such user on "+@bot.server.hostname.split(".")[-2] |
|---|
| | 45 | end |
|---|
| | 46 | |
|---|
| | 47 | @nick, @source = nil |
|---|
| 29 | 48 | end |
|---|
| 30 | 49 | |
|---|
| … | … | |
| 34 | 53 | else |
|---|
| 35 | 54 | if m.replyto.class == Channel |
|---|
| 36 | | # check if there is an user with nick same as input given |
|---|
| | 55 | |
|---|
| | 56 | # check if there is an user on the channel with nick same as input given |
|---|
| 37 | 57 | user = m.replyto.users.find { |user| user.nick == params[:input] } |
|---|
| 38 | 58 | |
|---|
| … | … | |
| 43 | 63 | end |
|---|
| 44 | 64 | |
|---|
| | 65 | # input is a host name or an IP |
|---|
| 45 | 66 | if params[:input] =~ /[a-z0-9\-]+(?:\.[a-z0-9\-]+)*\.[a-z]{2,3}/i || |
|---|
| 46 | 67 | params[:input] =~ Resolv::IPv4::Regex |
|---|
| 47 | 68 | m.reply host2output(params[:input]) |
|---|
| | 69 | |
|---|
| | 70 | # assume input is a nick |
|---|
| 48 | 71 | else |
|---|
| 49 | | m.reply "invalid input" |
|---|
| | 72 | @source = m.replyto |
|---|
| | 73 | @nick = params[:input] |
|---|
| | 74 | |
|---|
| | 75 | @bot.whois(@nick) |
|---|
| 50 | 76 | end |
|---|
| 51 | 77 | end |
|---|
| … | … | |
| 56 | 82 | |
|---|
| 57 | 83 | if geo[:country].empty? |
|---|
| 58 | | return _("#{host} could not be resolved") |
|---|
| | 84 | return _("#{nick ? "#{nick}'s location" : host} could not be resolved") |
|---|
| 59 | 85 | end |
|---|
| 60 | 86 | |
|---|