]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
IRC Framework: accept nil or empty nicks and channel names when looking for a user...
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Tue, 30 Jan 2007 10:40:31 +0000 (10:40 +0000)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Tue, 30 Jan 2007 10:40:31 +0000 (10:40 +0000)
ChangeLog
lib/rbot/irc.rb

index ad0315d048ccb56a01f4bc287a5b5dd6acce90ab..0a6b03b3c3b8f579177a9a8afdda1c357eb617ca 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2007-01-30  Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
+
+       * New IRC Framework: Server methods to retrieve a Channel or User are
+       now more robust to empty or nil nicks and channel names passed as
+       parameters.
+
 2007-01-29  Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
 
        * Timer rescheduling: it is now possible to reschedule the period
index 31c14802d4aa7c2f83d10cc38ec193b9bf6cb390..0cf70d5a6253e7a086b2f8ad0ac868b7d3ac7cf2 100644 (file)
 \r
 require 'singleton'\r
 \r
+class Object\r
+\r
+  # We extend the Object class with a method that\r
+  # checks if the receiver is nil or empty\r
+  def nil_or_empty?\r
+    return true unless self\r
+    return true if self.respond_to? :empty and self.empty?\r
+    return false\r
+  end\r
+end\r
 \r
 # The Irc module is used to keep all IRC-related classes\r
 # in the same namespace\r
@@ -1437,6 +1447,7 @@ module Irc
     # Checks if the receiver already has a channel with the given _name_\r
     #\r
     def has_channel?(name)\r
+      return false if name.nil_or_empty?\r
       channel_names.index(name.irc_downcase(casemap))\r
     end\r
     alias :has_chan? :has_channel?\r
@@ -1444,6 +1455,7 @@ module Irc
     # Returns the channel with name _name_, if available\r
     #\r
     def get_channel(name)\r
+      return nil if name.nil_or_empty?\r
       idx = has_channel?(name)\r
       channels[idx] if idx\r
     end\r
@@ -1452,9 +1464,15 @@ module Irc
     # Create a new Channel object bound to the receiver and add it to the\r
     # list of <code>Channel</code>s on the receiver, unless the channel was\r
     # present already. In this case, the default action is to raise an\r
-    # exception, unless _fails_ is set to false\r
+    # exception, unless _fails_ is set to false.  An exception can also be\r
+    # raised if _str_ is nil or empty, again only if _fails_ is set to true;\r
+    # otherwise, the method just returns nil\r
     #\r
     def new_channel(name, topic=nil, users=[], fails=true)\r
+      if name.nil_or_empty?\r
+        raise "Tried to look for empty or nil channel name #{name.inspect}" if fails\r
+        return nil\r
+      end\r
       ex = get_chan(name)\r
       if ex\r
         raise "Channel #{name} already exists on server #{self}" if fails\r
@@ -1541,6 +1559,7 @@ module Irc
     # Checks if the receiver already has a user with the given _nick_\r
     #\r
     def has_user?(nick)\r
+      return false if nick.nil_or_empty?\r
       user_nicks.index(nick.irc_downcase(casemap))\r
     end\r
 \r
@@ -1554,9 +1573,15 @@ module Irc
     # Create a new User object bound to the receiver and add it to the list\r
     # of <code>User</code>s on the receiver, unless the User was present\r
     # already. In this case, the default action is to raise an exception,\r
-    # unless _fails_ is set to false\r
+    # unless _fails_ is set to false. An exception can also be raised\r
+    # if _str_ is nil or empty, again only if _fails_ is set to true;\r
+    # otherwise, the method just returns nil\r
     #\r
     def new_user(str, fails=true)\r
+      if str.nil_or_empty?\r
+        raise "Tried to look for empty or nil user name #{str.inspect}" if fails\r
+        return nil\r
+      end\r
       tmp = str.to_irc_user(:server => self)\r
       old = get_user(tmp.nick)\r
       # debug "Tmp: #{tmp.inspect}"\r