Changeset 9125a2e6d1ef8af8d380da3766c704f9028dec69

Show
Ignore:
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
  • data/rbot/plugins/geoip.rb

    r0a900b0 r9125a2e  
    88# License:: GPL v2 
    99# 
    10 # Resolves the geographic locations of users and IP addresses 
     10# Resolves the geographic locations of users (network-wide) and IP addresses 
    1111 
    1212module GeoIP 
    1313  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 
    1420 
    1521  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) 
    1725 
    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 
    2329  end 
    2430end 
     
    2632class GeoIpPlugin < Plugin 
    2733  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 
    2948  end 
    3049 
     
    3453    else 
    3554      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 
    3757        user = m.replyto.users.find { |user| user.nick == params[:input] } 
    3858 
     
    4363      end 
    4464 
     65      # input is a host name or an IP 
    4566      if params[:input] =~ /[a-z0-9\-]+(?:\.[a-z0-9\-]+)*\.[a-z]{2,3}/i || 
    4667         params[:input] =~ Resolv::IPv4::Regex 
    4768        m.reply host2output(params[:input]) 
     69 
     70      # assume input is a nick 
    4871      else 
    49         m.reply "invalid input" 
     72        @source = m.replyto 
     73        @nick   = params[:input] 
     74 
     75        @bot.whois(@nick) 
    5076      end 
    5177    end 
     
    5682 
    5783    if geo[:country].empty? 
    58       return _("#{host} could not be resolved") 
     84      return _("#{nick ? "#{nick}'s location" : host} could not be resolved") 
    5985    end 
    6086