Class: Irc::Bot::RemoteDispatcher

Inherits:
MessageMapper show all
Defined in:
lib/rbot/core/remote.rb

Overview

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

Instance Attribute Summary

Attributes inherited from MessageMapper

#fallback

Instance Method Summary collapse

Methods inherited from MessageMapper

#each, #last

Constructor Details

#initialize(bot) ⇒ RemoteDispatcher

It is initialized by passing it the bot instance



119
120
121
# File 'lib/rbot/core/remote.rb', line 119

def initialize(bot)
  super
end

Instance Method Details

#handle(m) ⇒ Object

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’ …



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# 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

#map(botmodule, *args) ⇒ Object

The map method for the RemoteDispatcher returns the index of the inserted template



126
127
128
129
# File 'lib/rbot/core/remote.rb', line 126

def map(botmodule, *args)
  super
  return @templates.length - 1
end

#unmap(botmodule, handle) ⇒ Object

The unmap method for the RemoteDispatcher nils the template at the given index, therefore effectively removing the mapping



134
135
136
137
138
139
140
# 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