12 @routes << Template.new(*args)
16 @routes.each {|route| yield route}
23 return false if @routes.empty?
25 @routes.each do |route|
26 options, failure = route.recognize(m)
28 failures << [route, failure]
30 action = route.options[:action] ? route.options[:action] : route.items[0]
31 next unless @parent.respond_to?(action)
32 auth = route.options[:auth] ? route.options[:auth] : route.items[0]
33 debug "checking auth for #{auth}"
34 if m.bot.auth.allow?(auth, m.source, m.replyto)
35 debug "route found and auth'd: #{action.inspect} #{options.inspect}"
36 @parent.send(action, m, options)
39 debug "auth failed for #{auth}"
40 # if it's just an auth failure but otherwise the match is good,
41 # don't try any more handlers
45 debug failures.inspect
46 debug "no handler found, trying fallback"
47 if @fallback != nil && @parent.respond_to?(@fallback)
48 if m.bot.auth.allow?(@fallback, m.source, m.replyto)
49 @parent.send(@fallback, m, {})
59 attr_reader :defaults # The defaults hash
60 attr_reader :options # The options hash
62 def initialize(template, hash={})
63 raise ArgumentError, "Second argument must be a hash!" unless hash.kind_of?(Hash)
64 @defaults = hash[:defaults].kind_of?(Hash) ? hash.delete(:defaults) : {}
65 @requirements = hash[:requirements].kind_of?(Hash) ? hash.delete(:requirements) : {}
70 items = str.split(/\s+/).collect {|c| (/^(:|\*)(\w+)$/ =~ c) ? (($1 == ':' ) ? $2.intern : "*#{$2}".intern) : c} if str.kind_of?(String) # split and convert ':xyz' to symbols
71 items.shift if items.first == ""
72 items.pop if items.last == ""
75 if @items.first.kind_of? Symbol
76 raise ArgumentError, "Illegal template -- first component cannot be dynamic\n #{str.inspect}"
79 # Verify uniqueness of each component.
80 @items.inject({}) do |seen, item|
81 if item.kind_of? Symbol
82 raise ArgumentError, "Illegal template -- duplicate item #{item}\n #{str.inspect}" if seen.key? item
89 # Recognize the provided string components, returning a hash of
90 # recognized values, or [nil, reason] if the string isn't recognized.
92 components = m.message.split(/\s+/)
98 value = @defaults.has_key?(item) ? @defaults[item].clone : []
100 value = components.clone
103 def value.to_s() self.join(' ') end
104 options[item.to_s.sub(/^\*/,"").intern] = value
105 elsif item.kind_of? Symbol
106 value = components.shift || @defaults[item]
107 if passes_requirements?(item, value)
108 options[item] = value
110 if @defaults.has_key?(item)
111 options[item] = @defaults[item]
112 # push the test-failed component back on the stack
113 components.unshift value
115 return nil, requirements_for(item)
119 return nil, "No value available for component #{item.inspect}" if components.empty?
120 component = components.shift
121 return nil, "Value for component #{item.inspect} doesn't match #{component}" if component != item
125 return nil, "Unused components were left: #{components.join '/'}" unless components.empty?
127 return nil, "route is not configured for private messages" if @options.has_key?(:private) && !@options[:private] && m.private?
128 return nil, "route is not configured for public messages" if @options.has_key?(:public) && !@options[:public] && !m.private?
130 options.delete_if {|k, v| v.nil?} # Remove nil values.
135 when_str = @requirements.empty? ? "" : " when #{@requirements.inspect}"
136 default_str = @defaults.empty? ? "" : " || #{@defaults.inspect}"
137 "<#{self.class.to_s} #{@items.collect{|c| c.kind_of?(String) ? c : c.inspect}.join(' ').inspect}#{default_str}#{when_str}>"
140 # Verify that the given value passes this route's requirements
141 def passes_requirements?(name, value)
142 return @defaults.key?(name) && @defaults[name].nil? if value.nil? # Make sure it's there if it should be
144 case @requirements[name]
148 match = @requirements[name].match(value)
149 match && match[0].length == value.length
151 @requirements[name] == value.to_s
155 def requirements_for(name)
156 name = name.to_s.sub(/^\*/,"").intern if (/^\*/ =~ name.inspect)
157 presence = (@defaults.key?(name) && @defaults[name].nil?)
158 requirement = case @requirements[name]
160 when Regexp then "match #{@requirements[name].inspect}"
161 else "be equal to #{@requirements[name].inspect}"
163 if presence && requirement then "#{name} must be present and #{requirement}"
164 elsif presence || requirement then "#{name} must #{requirement || 'be present'}"
165 else "#{name} has no requirements"