]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
geoip plugin: now stacking whois requests to prevent overlaps
authorRaine Virta <rane@kapsi.fi>
Mon, 11 Aug 2008 17:19:51 +0000 (20:19 +0300)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Mon, 11 Aug 2008 17:29:18 +0000 (19:29 +0200)
data/rbot/plugins/geoip.rb

index 15372a8ba7f4fa1d75f8f64e6ac83a271922d510..0eb4eed4bd9ca65b1939ca64fa4ebf3ada799912 100755 (executable)
@@ -17,7 +17,6 @@ module GeoIP
     :city    => %r{City:.*?<td align="left" class="arial_bold">(.*?)</td>}m
   }
 
-
   def self.resolve(hostname)
     res = {}
     raw = Irc::Utils.bot.httputil.get_response(GEO_IP+hostname)
@@ -29,22 +28,51 @@ module GeoIP
   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
   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 m.whois[:nick] == @nick
+    return unless @stack.has_nick?(nick)
 
-    if m.target
-      @bot.say @source, host2output(m.target.host, m.target.nick)
-    else
-      @bot.say @source, "no such user on "+@bot.server.hostname.split(".")[-2]
+    @stack[nick].each do |source|
+      if m.target
+        @bot.say source, host2output(m.target.host, m.target.nick)
+      else
+        @bot.say source, "no such user on "+@bot.server.hostname.split(".")[-2]
+      end
     end
 
-    @nick, @source = nil
+    @stack.clear(m.whois[:nick])
   end
 
   def geoip(m, params)
@@ -63,16 +91,16 @@ class GeoIpPlugin < Plugin
       end
 
       # input is a host name or an IP
-      if params[:input] =~ /[a-z0-9\-]+(?:\.[a-z0-9\-]+)*\.[a-z]{2,3}/i ||
+      if params[:input] =~ /[a-z0-9\-]+(?:\.[a-z0-9\-]+)*\.[a-z]{2,4}/i ||
          params[:input] =~ Resolv::IPv4::Regex
         m.reply host2output(params[:input])
 
       # assume input is a nick
       else
-        @source = m.replyto
-        @nick   = params[:input]
+        nick = params[:input].downcase
 
-        @bot.whois(@nick)
+        @stack[nick] << m.replyto
+        @bot.whois(nick)
       end
     end
   end
@@ -106,4 +134,4 @@ class GeoIpPlugin < Plugin
 end
 
 plugin = GeoIpPlugin.new
-plugin.map "geoip [:input]", :action => 'geoip'
+plugin.map "geoip [:input]", :action => 'geoip'
\ No newline at end of file