]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
ircbot: sendmsg filtering
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Wed, 11 Feb 2009 22:34:16 +0000 (23:34 +0100)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Wed, 11 Feb 2009 22:48:44 +0000 (23:48 +0100)
We allow a filter to manipulate the arguments of sendmsg() by running
them through the filters of the :sendmsg group. The DataStream passed to
the filters has four keys:
  :text => the message text
  :type => the message type (typically, PRIVSMG or NOTICE)
  :dest => the destination (typically, a Channel or User)
  :options => options passed to sendmsg, merged with the default ones

lib/rbot/ircbot.rb

index 389494af7911157f6708145a544b2683db6b5d06..f491b212e1d6b8928cc9305cc3eda946ac318284 100644 (file)
@@ -973,8 +973,22 @@ class Bot
   # Type can be PRIVMSG, NOTICE, etc, but those you should really use the
   # relevant say() or notice() methods. This one should be used for IRCd
   # extensions you want to use in modules.
-  def sendmsg(type, where, original_message, options={})
-    opts = @default_send_options.merge(options)
+  def sendmsg(original_type, original_where, original_message, options={})
+
+    # filter message with sendmsg filters
+    ds = DataStream.new original_message.to_s.dup,
+      :type => original_type, :dest => original_where,
+      :options => @default_send_options.merge(options)
+    filters = filter_names(:sendmsg)
+    filters.each do |fname|
+      debug "filtering #{ds[:text]} with sendmsg filter #{fname}"
+      ds.merge! filter(self.global_filter_name(fname, :sendmsg), ds)
+    end
+
+    opts = ds[:options]
+    type = ds[:type]
+    where = ds[:dest]
+    filtered = ds[:text]
 
     # For starters, set up appropriate queue channels and rings
     mchan = opts[:queue_channel]
@@ -995,7 +1009,7 @@ class Bot
       end
     end
 
-    multi_line = original_message.to_s.gsub(/[\r\n]+/, "\n")
+    multi_line = filtered.gsub(/[\r\n]+/, "\n")
 
     # if target is a channel with nocolor modes, strip colours
     if where.kind_of?(Channel) and where.mode.any?(*config['server.nocolor_modes'])