]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/core/utils/filters.rb
Utils: time parsing routines
[user/henk/code/ruby/rbot.git] / lib / rbot / core / utils / filters.rb
index 2da89d085766d4697cc545afdd29b83692262cfa..463e6f383ab2037382b36298a0197f3a946f7980 100644 (file)
@@ -4,8 +4,6 @@
 # :title: Stream filters
 #
 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
-# Copyright:: (C) 2008 Giuseppe Bilotta
-# License:: GPL v2
 #
 # This file collects methods to handle 'stream filters', a generic mechanism
 # to transform text+attributes into other text+attributes
@@ -96,6 +94,11 @@ module ::Irc
       @filters.key?(global_filter_name(name, group))
     end
 
+    # This method checks if the bot has a filter group named _name_
+    def has_filter_group?(name)
+      @filter_group.key?(name)
+    end
+
     # This method is used to register a new filter
     def register_filter(name, group=nil, &block)
       raise "No block provided" unless block_given?
@@ -105,7 +108,7 @@ module ::Irc
       if has_filter?(tlkey)
         debug "Overwriting filter #{tlkey}"
       end
-      @filters[tlkey] = DataFilter.new &block
+      @filters[tlkey] = DataFilter.new(&block)
       if group
         gkey = group.to_sym
         @filter_group ||= {}
@@ -129,6 +132,12 @@ module ::Irc
       end
     end
 
+    # This method is used to retrieve the filter group names
+    def filter_groups
+      return [] unless defined? @filter_group
+      return @filter_group.keys
+    end
+
     # This method clears the filter list and installs the identity filter
     def clear_filters
       @filters ||= {}
@@ -139,6 +148,53 @@ module ::Irc
 
       register_filter(:identity) { |stream| stream }
     end
+
+    module Plugins
+      class BotModule
+        # read accessor for the default filter group for this BotModule
+        def filter_group
+          @filter_group ||= name
+        end
+
+        # write accessor for the default filter group for this BotModule
+        def filter_group=(name)
+          @filter_group = name
+        end
+
+        # define a filter defaulting to the default filter group
+        # for this BotModule
+        def define_filter(filter, &block)
+          @bot.register_filter(filter, self.filter_group, &block)
+        end
+
+        # load filters associated with the BotModule by looking in
+        # the path(s) specified by the :path option, defaulting to
+        # * Config::datadir/filters/<name>.rb
+        # * botclass/filters/<name>.rb
+        # (note that as <name> we use #dirname() rather than #name(),
+        # since we're looking for datafiles; this is only relevant
+        # for the very few plugins whose dirname differs from name)
+        def load_filters(options={})
+          case options[:path]
+          when nil
+            file = "#{self.dirname}.rb"
+            paths = [
+              File.join(Config::datadir, 'filters', file),
+              @bot.path('filters', file)
+            ]
+          when Array
+            paths = options[:path]
+          else
+            paths = [options[:path]]
+          end
+
+          paths.each do |file|
+            instance_eval(File.read(file), file) if File.exist?(file)
+          end
+        end
+      end
+    end
+
   end
 end