]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/irc.rb
Fix casemap/server mismatch problems when moving the bots between servers with differ...
[user/henk/code/ruby/rbot.git] / lib / rbot / irc.rb
index f3964d837695a5a68cdffcecbfc9b787d417710a..b8ca459304d453b6e8e183ed656b684a1e7da882 100644 (file)
@@ -461,6 +461,13 @@ class ArrayOf < Array
     }\r
   end\r
 \r
+  # We introduce the 'downcase' method, which maps downcase() to all the Array\r
+  # elements, properly failing when the elements don't have a downcase method\r
+  #\r
+  def downcase\r
+    self.map { |el| el.downcase }\r
+  end\r
+\r
   # Modifying methods which we don't handle yet are made private\r
   #\r
   private :[]=, :collect!, :map!, :fill, :flatten!\r
@@ -540,7 +547,7 @@ module Irc
       if self.class == Netmask\r
         return self if fits_with_server_and_casemap?(opts)\r
       end\r
-      return self.fullform.to_irc_netmask(server_and_casemap.merge(opts))\r
+      return self.downcase.to_irc_netmask(opts)\r
     end\r
 \r
     # Converts the receiver into a User with the given (optional)\r
@@ -684,7 +691,10 @@ module Irc
     end\r
 \r
     # We enhance the [] method by allowing it to pick an element that matches\r
-    # a given Netmask or String\r
+    # a given Netmask, a String or a Regexp\r
+    # TODO take into consideration the opportunity to use select() instead of\r
+    # find(), and/or a way to let the user choose which one to take (second\r
+    # argument?)\r
     #\r
     def [](*args)\r
       if args.length == 1\r
@@ -697,6 +707,10 @@ module Irc
           self.find { |mask|\r
             mask.matches?(args[0].to_irc_netmask(:casemap => mask.casemap))\r
           }\r
+        when Regexp\r
+          self.find { |mask|\r
+            mask.fullform =~ args[0]\r
+          }\r
         else\r
           super(*args)\r
         end\r
@@ -807,6 +821,21 @@ module Irc
       end\r
     end\r
 \r
+    # Users can be either simply downcased (their nick only)\r
+    # or fully downcased: this will return the fullform downcased\r
+    # according to the given casemap.\r
+    #\r
+    def full_irc_downcase(cmap=casemap)\r
+      self.fullform.irc_downcase(cmap)\r
+    end\r
+\r
+    # full_downcase() will return the fullform downcased according to the\r
+    # User's own casemap\r
+    #\r
+    def full_downcase\r
+      self.full_irc_downcase\r
+    end\r
+\r
     # Since to_irc_user runs the same checks on server and channel as\r
     # to_irc_netmask, we just try that and return self if it works.\r
     #\r
@@ -814,7 +843,7 @@ module Irc
     #\r
     def to_irc_user(opts={})\r
       return self if fits_with_server_and_casemap?(opts)\r
-      return self.fullform.to_irc_user(server_and_casemap(opts))\r
+      return self.full_downcase.to_irc_user(opts)\r
     end\r
 \r
     # We can replace everything at once with data from another User\r
@@ -850,6 +879,13 @@ module Irc
       @element_class = User\r
     end\r
 \r
+    # Convenience method: convert the UserList to a list of nicks. The indices\r
+    # are preserved\r
+    #\r
+    def nicks\r
+      self.map { |user| user.nick }\r
+    end\r
+\r
   end\r
 \r
 end\r
@@ -1206,6 +1242,13 @@ module Irc
       super(Channel, ar)\r
     end\r
 \r
+    # Convenience method: convert the ChannelList to a list of channel names.\r
+    # The indices are preserved\r
+    #\r
+    def names\r
+      self.map { |chan| chan.name }\r
+    end\r
+\r
   end\r
 \r
 end\r