def handle(m)
return false if @templates.empty?
failures = []
@templates.each do |tmpl|
options, failure = tmpl.recognize(m)
if options.nil?
failures << [tmpl, failure]
else
action = tmpl.options[:action]
unless @parent.respond_to?(action)
failures << [tmpl, "class does not respond to action #{action}"]
next
end
auth = tmpl.options[:full_auth_path]
debug "checking auth for #{auth}"
if m.bot.auth.allow?(auth, m.source, m.replyto)
debug "template match found and auth'd: #{action.inspect} #{options.inspect}"
if !m.in_thread && (tmpl.options[:thread] || tmpl.options[:threaded])
Thread.new do
begin
@parent.send(action, m, options)
rescue Exception => e
error "In threaded action: #{e.message}"
debug e.backtrace.join("\n")
end
end
else
@parent.send(action, m, options)
end
return true
end
debug "auth failed for #{auth}"
return false
end
end
failures.each {|f, r|
debug "#{f.inspect} => #{r}"
}
debug "no handler found, trying fallback"
if @fallback && @parent.respond_to?(@fallback)
if m.bot.auth.allow?(@fallback, m.source, m.replyto)
@parent.send(@fallback, m, {})
return true
end
end
return false
end