Class ::Bot::RemoteDispatcher
In: lib/rbot/core/remote.rb
Parent: MessageMapper
User HTTPResponse BasicUserMessage Bot\n[lib/rbot/core/remote.rb\nlib/rbot/core/utils/extends.rb\nlib/rbot/core/utils/filters.rb\nlib/rbot/core/utils/wordlist.rb] HttpUtil lib/rbot/core/userdata.rb lib/rbot/core/utils/httputil.rb lib/rbot/core/utils/extends.rb lib/rbot/core/remote.rb lib/rbot/core/utils/httputil.rb ParseTime Utils (null) dot/m_15_0.png

The RemoteDispatcher is a kind of MessageMapper, tuned to handle RemoteMessages

Methods

handle   map   new   unmap  

Public Class methods

It is initialized by passing it the bot instance

[Source]

     # File lib/rbot/core/remote.rb, line 119
119:     def initialize(bot)
120:       super
121:     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’ …

[Source]

     # 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

[Source]

     # 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

[Source]

     # 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

[Validate]