]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/reaction.rb
reaction plugin: no more :stuff, but :before and :after for the pre and postmatch...
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / reaction.rb
index 2887d574b3861eee25ca7fdf79edb8fe32f4935e..4f757ddd5adf0be122a90c6385d1a66b74e85250 100644 (file)
@@ -16,7 +16,8 @@ class ::Reaction
   attr_reader :raw_trigger, :raw_replies
 
   class ::Reply
-    attr_reader :act, :reply, :pct, :range
+    attr_reader :act, :reply, :pct
+    attr_accessor :range
     attr_reader :author, :date, :channel
     attr_writer :date
 
@@ -65,9 +66,10 @@ class ::Reaction
     end
     @trigger = [act]
     if rex.sub!(%r@^([/!])(.*)\1$@, '\2')
-      @trigger << Regexp.new(rex)
+      @trigger << Regexp.new(rex, true)
     else
-      @trigger << Regexp.new(/\b#{Regexp.escape(rex)}\b/u)
+      rex.sub!(/^(["'])(.*)\1$/, '\2')
+      @trigger << Regexp.new(/\b#{Regexp.escape(rex)}\b/ui)
     end
   end
 
@@ -194,7 +196,7 @@ class ReactionPlugin < Plugin
     when :reply, :replies
       "reaction replies are simply messages that the bot will reply when a trigger is matched. " +
       "Replies can be prefixed by 'act:' (e.g. act:goes shopping) to signify that the bot should act instead of saying the message. " +
-      "Replies can use the %%{key} syntax to access one of the following keys: " +
+      "Replies can use the %{key} syntax to access one of the following keys: " +
       "who (the user that said the trigger), bot (the bot's own nick), " +
       "target (the first word following the trigger), what (whatever follows target), " +
       "stuff (everything that follows the trigger), match (the actual matched text), " +
@@ -212,20 +214,25 @@ class ReactionPlugin < Plugin
     return unless PrivMessage === m
     debug "testing #{m} for reactions"
     return if @reactions.empty?
-    wanted = @reactions.find { |react|
-      react === m
-    }
-    return unless wanted
-    match = wanted === m
+    candidates = @reactions.map { |react|
+      blob = react === m
+      blob ? [blob, react] : nil
+    }.compact
+    return if candidates.empty?
+    match, wanted = candidates.sort { |m1, m2|
+      m1.first[0].length <=> m2.first[0].length
+    }.last
     matched = match[0]
-    stuff = match.post_match.strip
-    target, what = stuff.split(/\s+/, 2)
+    before = match.pre_match.strip
+    after = match.post_match.strip
+    target, what = after.split(/\s+/, 2)
     extra = {
       :who => m.sourcenick,
       :match => matched,
       :target => target,
       :what => what,
-      :stuff => stuff
+      :before => before,
+      :after => after
     }
     match.to_a.each_with_index { |d, i|
       extra[:"match#{i}"] = d
@@ -240,7 +247,7 @@ class ReactionPlugin < Plugin
 
   def find_reaction(trigger)
     @reactions.find { |react|
-      react.raw_trigger == trigger
+      react.raw_trigger.downcase == trigger.downcase
     }
   end