class Irc::Bot::RemoteDispatcher
The RemoteDispatcher
is a kind of MessageMapper
, tuned to handle RemoteMessages
Public Class Methods
It is initialized by passing it the bot instance
# File lib/rbot/core/remote.rb, line 119 def initialize(bot) super end
Public Instance Methods
We redefine the handle() method from MessageMapper
, taking into account that @parent is a bot, and that we don’t handle fallbacks.
On failure to dispatch anything, the method returns false. If dispatching is successfull, the method returns a Hash.
Presently, the hash returned on success has only one key, :return, whose value is the actual return value of the successfull dispatch.
TODO this same kind of mechanism could actually be used in MessageMapper
itself to be able to handle the case of multiple plugins having the same ‘first word’ …
# File lib/rbot/core/remote.rb, line 156 def handle(m) return false if @templates.empty? failures = [] @templates.each do |tmpl| # Skip this element if it was unmapped next unless tmpl botmodule = @parent.plugins[tmpl.botmodule] options = tmpl.recognize(m) if options.kind_of? Failure failures << options else action = tmpl.options[:action] unless botmodule.respond_to?(action) failures << NoActionFailure.new(tmpl, m) next end auth = tmpl.options[:full_auth_path] debug "checking auth for #{auth}" # We check for private permission if m.bot.auth.permit?(m.source, auth, '?') debug "template match found and auth'd: #{action.inspect} #{options.inspect}" return :return => botmodule.send(action, m, options) end debug "auth failed for #{auth}" # if it's just an auth failure but otherwise the match is good, # don't try any more handlers return false end end failures.each {|r| debug "#{r.template.inspect} => #{r}" } debug "no handler found" return false end
The map method for the RemoteDispatcher
returns the index of the inserted template
# File lib/rbot/core/remote.rb, line 126 def map(botmodule, *args) super return @templates.length - 1 end
The unmap method for the RemoteDispatcher
nils the template at the given index, therefore effectively removing the mapping
# File lib/rbot/core/remote.rb, line 134 def unmap(botmodule, handle) tmpl = @templates[handle] raise "Botmodule #{botmodule.name} tried to unmap #{tmpl.inspect} that was handled by #{tmpl.botmodule}" unless tmpl.botmodule == botmodule.name debug "Unmapping #{tmpl.inspect}" @templates[handle] = nil @templates.clear unless @templates.compact.size > 0 end