X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=lib%2Frbot%2Fcore%2Futils%2Ffilters.rb;h=d188aa3f881e3e9c37a4f029624a4b06dcf1d1ce;hb=56e4713c5c0498838ed77a409e44fbc3251acde2;hp=6d560c9150df8d23cd8d4b7f8d068e825cd77e07;hpb=6e62535cd38c5a1f00e76a6984df671d76e6c70a;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/lib/rbot/core/utils/filters.rb b/lib/rbot/core/utils/filters.rb index 6d560c91..d188aa3f 100644 --- a/lib/rbot/core/utils/filters.rb +++ b/lib/rbot/core/utils/filters.rb @@ -4,8 +4,6 @@ # :title: Stream filters # # Author:: Giuseppe "Oblomov" Bilotta -# 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 @@ -110,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 ||= {} @@ -150,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/.rb + # * botclass/filters/.rb + # (note that as 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 + us = "#{self.dirname}.rb" + paths = [ + File.join(Config::datadir, 'filters', us), + @bot.path('filters', us) + ] + 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