X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;ds=sidebyside;f=lib%2Frbot%2Fmessagemapper.rb;h=9501ac592e6265d8f8f5a08ed530f2bcc0cbffa7;hb=783ffa4235330029d661752b1023db635b26f2b3;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..9501ac59 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 @@ -129,7 +144,7 @@ class Bot # threaded:: # a boolean (defaults to false) that determines whether the action should be # called in a separate thread. - # + # # # Further examples: # @@ -192,8 +207,15 @@ class Bot 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) } + 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 @@ -413,6 +435,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 +499,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+" @@ -496,7 +519,8 @@ class Bot # 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