| Class | ::Bot::Plugins::BotModule |
| In: |
lib/rbot/core/utils/filters.rb
lib/rbot/core/utils/extends.rb |
| Parent: | Object |
Sometimes plugins need to create a new fake message based on an existing message: for example, this is done by alias, linkbot, reaction and remotectl.
This method simplifies the message creation, including a recursion depth check.
In the options you can specify the :bot, the :server, the :source, 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.
# File lib/rbot/core/utils/extends.rb, line 485
485: def fake_message(string, opts={})
486: if from = opts[:from]
487: o = {
488: :bot => from.bot, :server => from.server, :source => from.source,
489: :target => from.target, :class => from.class, :delegate => true,
490: :depth => from.recurse_depth + 1
491: }.merge(opts)
492: else
493: o = {
494: :bot => @bot, :server => @bot.server, :target => @bot.myself,
495: :class => PrivMessage, :delegate => true, :depth => 1
496: }.merge(opts)
497: end
498: raise RecurseTooDeep if o[:depth] > MAX_RECURSE_DEPTH
499: new_m = o[:class].new(o[:bot], o[:server], o[:source], o[:target], string)
500: new_m.recurse_depth = o[:depth]
501: if from
502: # the created message will reply to the originating message
503: class << new_m
504: self
505: end.send(:define_method, :reply) do |*args|
506: debug "replying to '#{from.message}' with #{args.first}"
507: from.reply(*args)
508: end
509: # the created message will follow originating message's in_thread
510: new_m.in_thread = from.in_thread if from.respond_to?(:in_thread)
511: end
512: return new_m unless o[:delegate]
513: method = o[:class].to_s.gsub(/^Irc::|Message$/,'').downcase
514: method = 'privmsg' if method == 'priv'
515: o[:bot].plugins.irc_delegate(method, new_m)
516: end
load filters associated with the BotModule by looking in the path(s) specified by the :path option, defaulting to
(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)
# File lib/rbot/core/utils/filters.rb, line 177
177: def load_filters(options={})
178: case options[:path]
179: when nil
180: file = "#{self.dirname}.rb"
181: paths = [
182: File.join(Config::datadir, 'filters', file),
183: @bot.path('filters', file)
184: ]
185: when Array
186: paths = options[:path]
187: else
188: paths = [options[:path]]
189: end
190:
191: paths.each do |file|
192: instance_eval(File.read(file), file) if File.exist?(file)
193: end
194: end