Class | ::Bot::RemoteDispatcher |
In: |
lib/rbot/core/remote.rb
|
Parent: | MessageMapper |
The RemoteDispatcher is a kind of MessageMapper, tuned to handle RemoteMessages
It is initialized by passing it the bot instance
# File lib/rbot/core/remote.rb, line 119 119: def initialize(bot) 120: super 121: end
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 156: def handle(m) 157: return false if @templates.empty? 158: failures = [] 159: @templates.each do |tmpl| 160: # Skip this element if it was unmapped 161: next unless tmpl 162: botmodule = @parent.plugins[tmpl.botmodule] 163: options = tmpl.recognize(m) 164: if options.kind_of? Failure 165: failures << options 166: else 167: action = tmpl.options[:action] 168: unless botmodule.respond_to?(action) 169: failures << NoActionFailure.new(tmpl, m) 170: next 171: end 172: auth = tmpl.options[:full_auth_path] 173: debug "checking auth for #{auth}" 174: # We check for private permission 175: if m.bot.auth.permit?(m.source, auth, '?') 176: debug "template match found and auth'd: #{action.inspect} #{options.inspect}" 177: return :return => botmodule.send(action, m, options) 178: end 179: debug "auth failed for #{auth}" 180: # if it's just an auth failure but otherwise the match is good, 181: # don't try any more handlers 182: return false 183: end 184: end 185: failures.each {|r| 186: debug "#{r.template.inspect} => #{r}" 187: } 188: debug "no handler found" 189: return false 190: end
The map method for the RemoteDispatcher returns the index of the inserted template
# File lib/rbot/core/remote.rb, line 126 126: def map(botmodule, *args) 127: super 128: return @templates.length - 1 129: 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 134: def unmap(botmodule, handle) 135: tmpl = @templates[handle] 136: raise "Botmodule #{botmodule.name} tried to unmap #{tmpl.inspect} that was handled by #{tmpl.botmodule}" unless tmpl.botmodule == botmodule.name 137: debug "Unmapping #{tmpl.inspect}" 138: @templates[handle] = nil 139: @templates.clear unless @templates.compact.size > 0 140: end