]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/core/utils/extends.rb
fake_message duplicates m.reply and m.in_thread from the :from message
[user/henk/code/ruby/rbot.git] / lib / rbot / core / utils / extends.rb
index e9581bc0902a466039ce443003a31c0b7199d957..bc0eeb4674b2f12869907e3f7bbb6607a40d24d6 100644 (file)
@@ -4,8 +4,6 @@
 # :title: Standard classes extensions
 #
 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
-# Copyright:: (C) 2006,2007 Giuseppe Bilotta
-# License:: GPL v2
 #
 # This file collects extensions to standard Ruby classes and to some core rbot
 # classes to be used by the various plugins
@@ -94,13 +92,39 @@ class ::Array
     self[rand(self.length)]
   end
 
-  # This method returns a random element from the array, deleting it from the
-  # array itself. The method returns nil if the array is empty
+  # This method returns a given element from the array, deleting it from the
+  # array itself. The method returns nil if the element couldn't be found.
   #
-  def delete_one
+  # If nil is specified, a random element is returned and deleted.
+  #
+  def delete_one(val=nil)
     return nil if self.empty?
-    self.delete_at(rand(self.length))
+    if val.nil?
+      index = rand(self.length)
+    else
+      index = self.index(val)
+      return nil unless index
+    end
+    self.delete_at(index)
+  end
+
+  # shuffle and shuffle! are defined in Ruby >= 1.8.7
+
+  # This method returns a new array with the same items as
+  # the receiver, but shuffled
+  unless method_defined? :shuffle
+    def shuffle
+      sort_by { rand }
+    end
   end
+
+  # This method shuffles the items in the array
+  unless method_defined? :shuffle!
+    def shuffle!
+      replace shuffle
+    end
+  end
+
 end
 
 # Extensions to the Range class
@@ -376,6 +400,10 @@ module ::Irc
         # the :target, the message :class and whether or not to :delegate. To
         # initialize these entries from an existing message, you can use :from
         #
+        # Additionally, if :from is given, the reply method of created message
+        # is overriden to reply to :from instead. The #in_thread attribute
+        # for created mesage is also copied from :from
+        #
         # If you don't specify a :from you should specify a :source.
         #
         def fake_message(string, opts={})
@@ -394,6 +422,17 @@ module ::Irc
           raise RecurseTooDeep if o[:depth] > MAX_RECURSE_DEPTH
           new_m = o[:class].new(o[:bot], o[:server], o[:source], o[:target], string)
           new_m.recurse_depth = o[:depth]
+          if from
+            # the created message will reply to the originating message
+            class << new_m
+              self
+            end.send(:define_method, :reply) do |*args|
+              debug "replying to '#{from.message}' with #{args.first}"
+              from.reply *args
+            end
+            # the created message will follow originating message's in_thread
+            new_m.in_thread = from.in_thread if from.respond_to?(:in_thread)
+          end
           return new_m unless o[:delegate]
           method = o[:class].to_s.gsub(/^Irc::|Message$/,'').downcase
           method = 'privmsg' if method == 'priv'