Class: Irc::Bot::WebDispatcher

Inherits:
Object
  • Object
show all
Defined in:
/home/apoc/projects/ruby/rbot/lib/rbot/core/webservice.rb

Overview

works similar to a message mapper but for url paths

Defined Under Namespace

Classes: WebTemplate

Instance Method Summary (collapse)

Constructor Details

- (WebDispatcher) initialize(bot)

Returns a new instance of WebDispatcher



202
203
204
205
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/webservice.rb', line 202

def initialize(bot)
  @bot = bot
  @templates = []
end

Instance Method Details

- (Object) handle(m)

Handle a web service request, find matching mapping and dispatch.

In case authentication fails, sends a 401 Not Authorized response.



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/webservice.rb', line 228

def handle(m)
  if @templates.empty?
    m.send_plaintext('no routes!', 404)
    return false if @templates.empty?
  end
  failures = []
  @templates.each do |tmpl|
    # Skip this element if it was unmapped
    next unless tmpl
    botmodule = @bot.plugins[tmpl.botmodule]
    params = tmpl.recognize(m)
    if params
      action = tmpl.options[:action]
      unless botmodule.respond_to?(action)
        failures << NoActionFailure.new(tmpl, m)
        next
      end
      # check http method:
      unless not tmpl.options.has_key? :method or tmpl.options[:method] == m.method
        debug 'request method missmatch'
        next
      end
      auth = tmpl.options[:full_auth_path]
      debug "checking auth for #{auth.inspect}"
      # We check for private permission
      if m.bot.auth.permit?(m.source || Auth::defaultbotuser, auth, '?')
        debug "template match found and auth'd: #{action.inspect} #{params.inspect}"
        response = botmodule.send(action, m, params)
        if m.res.sent_size == 0 and m.res.body.empty?
          m.send_json(response.to_json)
        end
        return true
      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
      m.send_plaintext('Authentication Required!', 401)
      return false
    end
  end
  failures.each {|r|
    debug "#{r.template.inspect} => #{r}"
  }
  debug "no handler found"
  m.send_plaintext('No Handler Found!', 404)
  return false
end

- (Object) map(botmodule, pattern, options = {})



207
208
209
210
211
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/webservice.rb', line 207

def map(botmodule, pattern, options={})
  @templates << WebTemplate.new(botmodule.to_s, pattern, options)
  debug 'template route: ' + @templates[-1].inspect
  return @templates.length - 1
end

- (Object) unmap(botmodule, index)

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



216
217
218
219
220
221
222
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/webservice.rb', line 216

def unmap(botmodule, index)
  tmpl = @templates[index]
  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