From 2d7853ce6e3683c8eb5e858ba7c5eb2dcaeba5eb Mon Sep 17 00:00:00 2001 From: Giuseppe Bilotta Date: Tue, 30 Jan 2007 10:40:31 +0000 Subject: [PATCH] IRC Framework: accept nil or empty nicks and channel names when looking for a user or server on a channel --- ChangeLog | 6 ++++++ lib/rbot/irc.rb | 29 +++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index ad0315d0..0a6b03b3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2007-01-30 Giuseppe Bilotta + + * 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 * Timer rescheduling: it is now possible to reschedule the period diff --git a/lib/rbot/irc.rb b/lib/rbot/irc.rb index 31c14802..0cf70d5a 100644 --- a/lib/rbot/irc.rb +++ b/lib/rbot/irc.rb @@ -19,6 +19,16 @@ require 'singleton' +class Object + + # We extend the Object class with a method that + # checks if the receiver is nil or empty + def nil_or_empty? + return true unless self + return true if self.respond_to? :empty and self.empty? + return false + end +end # The Irc module is used to keep all IRC-related classes # in the same namespace @@ -1437,6 +1447,7 @@ module Irc # Checks if the receiver already has a channel with the given _name_ # def has_channel?(name) + return false if name.nil_or_empty? channel_names.index(name.irc_downcase(casemap)) end alias :has_chan? :has_channel? @@ -1444,6 +1455,7 @@ module Irc # Returns the channel with name _name_, if available # def get_channel(name) + return nil if name.nil_or_empty? idx = has_channel?(name) channels[idx] if idx end @@ -1452,9 +1464,15 @@ module Irc # Create a new Channel object bound to the receiver and add it to the # list of Channels on the receiver, unless the channel was # present already. In this case, the default action is to raise an - # exception, unless _fails_ is set to false + # exception, unless _fails_ is set to false. An exception can also be + # raised if _str_ is nil or empty, again only if _fails_ is set to true; + # otherwise, the method just returns nil # def new_channel(name, topic=nil, users=[], fails=true) + if name.nil_or_empty? + raise "Tried to look for empty or nil channel name #{name.inspect}" if fails + return nil + end ex = get_chan(name) if ex raise "Channel #{name} already exists on server #{self}" if fails @@ -1541,6 +1559,7 @@ module Irc # Checks if the receiver already has a user with the given _nick_ # def has_user?(nick) + return false if nick.nil_or_empty? user_nicks.index(nick.irc_downcase(casemap)) end @@ -1554,9 +1573,15 @@ module Irc # Create a new User object bound to the receiver and add it to the list # of Users on the receiver, unless the User was present # already. In this case, the default action is to raise an exception, - # unless _fails_ is set to false + # unless _fails_ is set to false. An exception can also be raised + # if _str_ is nil or empty, again only if _fails_ is set to true; + # otherwise, the method just returns nil # def new_user(str, fails=true) + if str.nil_or_empty? + raise "Tried to look for empty or nil user name #{str.inspect}" if fails + return nil + end tmp = str.to_irc_user(:server => self) old = get_user(tmp.nick) # debug "Tmp: #{tmp.inspect}" -- 2.39.2