X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=lib%2Frbot%2Fmessagemapper.rb;h=3966bc17f7360ec3853cda6995c479d3ab9e7590;hb=f287bf1e73829434d92b46c333c3185373198518;hp=8b52baa4a04392ee4c5d8ab78070b644e46ee39f;hpb=6f5528a63b44e610a3d25d7fe583399163d7d2da;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/lib/rbot/messagemapper.rb b/lib/rbot/messagemapper.rb index 8b52baa4..3966bc17 100644 --- a/lib/rbot/messagemapper.rb +++ b/lib/rbot/messagemapper.rb @@ -13,7 +13,22 @@ class Regexp new = self.source.gsub(/(^|[^\\])((?:\\\\)*)\(([^?])/) { "%s%s(?:%s" % [$1, $2, $3] } - Regexp.new(new) + Regexp.new(new, self.options) + end + + # We may want to remove head and tail anchors + def remove_head_tail + new = self.source.sub(/^\^/,'').sub(/\$$/,'') + Regexp.new(new, self.options) + end + + # The MessageMapper cleanup method: does both remove_capture + # and remove_head_tail + def mm_cleanup + new = self.source.gsub(/(^|[^\\])((?:\\\\)*)\(([^?])/) { + "%s%s(?:%s" % [$1, $2, $3] + }.sub(/^\^/,'').sub(/\$$/,'') + Regexp.new(new, self.options) end end @@ -37,6 +52,57 @@ class Bot # {:option => "bar", :otheroption => "baz"} # See the #map method for more details. class MessageMapper + + class Failure + STRING = "template %{template} failed to recognize message %{message}" + FRIENDLY = "I failed to understand the command" + attr_reader :template + attr_reader :message + def initialize(tmpl, msg) + @template = tmpl + @message = msg + end + + def to_s + STRING % { + :template => template.template, + :regexp => template.regexp, + :message => message.message, + :action => template.options[:action] + } + end + end + + # failures with a friendly message + class FriendlyFailure < Failure + def friendly + self.class::FRIENDLY rescue FRIENDLY + end + end + + class NotPrivateFailure < FriendlyFailure + STRING = "template %{template} is not configured for private messages" + FRIENDLY = "the command must not be given in private" + end + + class NotPublicFailure < FriendlyFailure + STRING = "template %{template} is not configured for public messages" + FRIENDLY = "the command must not be given in public" + end + + class NoMatchFailure < Failure + STRING = "%{message} does not match %{template} (%{regex})" + end + + class PartialMatchFailure < Failure + STRING = "%{message} only matches %{template} (%{regex}) partially" + end + + class NoActionFailure < FriendlyFailure + STRING = "%{template} calls undefined action %{action}" + FRIENDLY = "uh-ho, somebody forgot to tell me how to do that ..." + end + # used to set the method name used as a fallback for unmatched messages. # The default fallback is a method called "usage". attr_writer :fallback @@ -129,16 +195,16 @@ class Bot # threaded:: # a boolean (defaults to false) that determines whether the action should be # called in a separate thread. - # + # # # Further examples: # - # # match 'karmastats' and call my stats() method - # plugin.map 'karmastats', :action => 'stats' - # # match 'karma' with an optional 'key' and call my karma() method - # plugin.map 'karma :key', :defaults => {:key => false} - # # match 'karma for something' and call my karma() method - # plugin.map 'karma for :key' + # # match 'pointstats' and call my stats() method + # plugin.map 'pointstats', :action => 'stats' + # # match 'points' with an optional 'key' and call my points() method + # plugin.map 'points :key', :defaults => {:key => false} + # # match 'points for something' and call my points() method + # plugin.map 'points for :key' # # # two matches, one for public messages in a channel, one for # # private messages which therefore require a channel argument @@ -179,23 +245,40 @@ class Bot return false if @templates.empty? failures = [] @templates.each do |tmpl| - options, failure = tmpl.recognize(m) - if options.nil? - failures << [tmpl, failure] + params = tmpl.recognize(m) + if params.kind_of? Failure + failures << params else action = tmpl.options[:action] unless @parent.respond_to?(action) - failures << [tmpl, "class does not respond to action #{action}"] + failures << NoActionFailure.new(tmpl, m) 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 tmpl.options[:thread] || tmpl.options[:threaded] - Thread.new { @parent.send(action, m, options) } + debug "template match found and auth'd: #{action.inspect} #{params.inspect}" + if !m.in_thread and (tmpl.options[:thread] or tmpl.options[:threaded]) and + (defined? WebServiceUser and not m.source.instance_of? WebServiceUser) + # Web service: requests are handled threaded anyway and we want to + # wait for the responses. + + # since the message action is in a separate thread, the message may be + # delegated to unreplied() before the thread has a chance to actually + # mark it as replied. since threading is used mostly for commands that + # are known to take some processing time (e.g. download a web page) before + # giving an output, we just mark these as 'replied' here + m.replied = true + Thread.new do + begin + @parent.send(action, m, params) + rescue Exception => e + error "In threaded action: #{e.message}" + debug e.backtrace.join("\n") + end + end else - @parent.send(action, m, options) + @parent.send(action, m, params) end return true @@ -206,13 +289,13 @@ class Bot return false end end - failures.each {|f, r| - debug "#{f.inspect} => #{r}" + failures.each {|r| + debug "#{r.template.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, {}) + @parent.send(@fallback, m, {:failures => failures}) return true end end @@ -311,7 +394,7 @@ class Bot mul = multi? ? " multi" : " single" opt = optional? ? " optional" : " needed" if @regexp - reg = " regexp=%s index=%d" % [@regexp, @index] + reg = " regexp=%s index=%s" % [@regexp, @index] else reg = nil end @@ -413,6 +496,7 @@ class Bot end pre = nil if extra.sub!(/^!/, "") post = nil if extra.sub!(/!$/, "") + extra = nil if extra.empty? else extra = nil end @@ -476,13 +560,13 @@ class Bot sub = is_single ? "\\S+" : ".*?" when Regexp # Remove captures and the ^ and $ that are sometimes placed in requirement regexps - sub = has_req.remove_captures.source.sub(/^\^/,'').sub(/\$$/,'') + sub = has_req.mm_cleanup when String sub = Regexp.escape(has_req) when Array - sub = has_req[0].remove_captures.source.sub(/^\^/,'').sub(/\$$/,'') + sub = has_req[0].mm_cleanup when Hash - sub = has_req[:regexp].remove_captures.source.sub(/^\^/,'').sub(/\$$/,'') + sub = has_req[:regexp].mm_cleanup else warning "Odd requirement #{has_req.inspect} of class #{has_req.class} for parameter '#{name}'" sub = Regexp.escape(has_req.to_s) rescue "\\S+" @@ -491,12 +575,15 @@ class Bot s = "#{not_needed ? "(?:" : ""}#{whites}(#{sub})#{ not_needed ? ")?" : ""}" } # debug "Replaced dyns: #{rx.inspect}" - rx.gsub!(/((?:\\ )*)\\\[/, "(?:\\1") + rx.gsub!(/((?:\\ )*)((?:\\\[)+)/, '\2\1') + # debug "Corrected optionals spacing: #{rx.inspect}" + rx.gsub!(/\\\[/, "(?:") rx.gsub!(/\\\]/, ")?") # debug "Delimited optionals: #{rx.inspect}" rx.gsub!(/(?:\\ )+/, "\\s+") # debug "Corrected spaces: #{rx.inspect}" - @regexp = Regexp.new("^#{rx}$") + # Created message (such as by fake_message) can contain multiple lines + @regexp = /\A#{rx}\z/m end # Recognize the provided string components, returning a hash of @@ -505,20 +592,19 @@ class Bot debug "Testing #{m.message.inspect} against #{self.inspect}" - # Early out - return nil, "template #{@template} is not configured for private messages" if @options.has_key?(:private) && !@options[:private] && m.private? - return nil, "template #{@template} is not configured for public messages" if @options.has_key?(:public) && !@options[:public] && !m.private? - - options = {} - matching = @regexp.match(m.message) - return nil, "#{m.message.inspect} doesn't match #{@template} (#{@regexp})" unless matching - return nil, "#{m.message.inspect} only matches #{@template} (#{@regexp}) partially: #{matching[0].inspect}" unless matching[0] == m.message + return MessageMapper::NoMatchFailure.new(self, m) unless matching + return MessageMapper::PartialMatchFailure.new(self, m) unless matching[0] == m.message + + return MessageMapper::NotPrivateFailure.new(self, m) if @options.has_key?(:private) && !@options[:private] && m.private? + return MessageMapper::NotPublicFailure.new(self, m) if @options.has_key?(:public) && !@options[:public] && !m.private? debug_match = matching[1..-1].collect{ |d| d.inspect}.join(', ') debug "#{m.message.inspect} matched #{@regexp} with #{debug_match}" debug "Associating #{debug_match} with dyn items #{@dyn_items.join(', ')}" + options = @defaults.dup + @dyn_items.each_with_index { |it, i| next if i == 0 item = it.name @@ -563,7 +649,7 @@ class Bot } options.delete_if {|k, v| v.nil?} # Remove nil values. - return options, nil + return options end def inspect