]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/messagemapper.rb
* Fixed the new http.proxy* settings, they work!
[user/henk/code/ruby/rbot.git] / lib / rbot / messagemapper.rb
1 module Irc
2   class MessageMapper
3     attr_writer :fallback
4
5     def initialize(parent)
6       @parent = parent
7       @routes = Array.new
8       @fallback = 'usage'
9     end
10     
11     def map(*args)
12       @routes << Template.new(*args)
13     end
14     
15     def each
16       @routes.each {|route| yield route}
17     end
18     def last
19       @routes.last
20     end
21     
22     def handle(m)
23       return false if @routes.empty?
24       failures = []
25       @routes.each do |route|
26         options, failure = route.recognize(m)
27         if options.nil?
28           failures << [route, failure]
29         else
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)
37             return true
38           end
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
42           return false
43         end
44       end
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, {})
50           return true
51         end
52       end
53       return false
54     end
55
56   end
57
58   class Template
59     attr_reader :defaults # The defaults hash
60     attr_reader :options  # The options hash
61     attr_reader :items
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) : {}
66       self.items = template
67       @options = hash
68     end
69     def items=(str)
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 == ""
73       @items = items
74
75       if @items.first.kind_of? Symbol
76         raise ArgumentError, "Illegal template -- first component cannot be dynamic\n   #{str.inspect}"
77       end
78
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
83           seen[item] = true
84         end
85         seen
86       end
87     end
88
89     # Recognize the provided string components, returning a hash of
90     # recognized values, or [nil, reason] if the string isn't recognized.
91     def recognize(m)
92       components = m.message.split(/\s+/)
93       options = {}
94
95       @items.each do |item|
96         if /^\*/ =~ item.to_s
97           if components.empty?
98             value = @defaults.has_key?(item) ? @defaults[item].clone : []
99           else
100             value = components.clone
101           end
102           components = []
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
109           else
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
114             else
115               return nil, requirements_for(item)
116             end
117           end
118         else
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
122         end
123       end
124
125       return nil, "Unused components were left: #{components.join '/'}" unless components.empty?
126
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?
129       
130       options.delete_if {|k, v| v.nil?} # Remove nil values.
131       return options, nil
132     end
133
134     def inspect
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}>"
138     end
139
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
143
144       case @requirements[name]
145         when nil then true
146         when Regexp then
147           value = value.to_s
148           match = @requirements[name].match(value)
149           match && match[0].length == value.length
150         else
151           @requirements[name] == value.to_s
152       end
153     end
154
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]
159         when nil then nil
160         when Regexp then "match #{@requirements[name].inspect}"
161         else "be equal to #{@requirements[name].inspect}"
162       end
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"
166       end
167     end
168   end
169 end