]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/salut.rb
Salutation improvements when the bot isn't address
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / salut.rb
index 4827601948324570af93c22f154523f497444a9e..4582e5549aea4ca936253cc5427b01dc223a1d6a 100644 (file)
@@ -1,5 +1,8 @@
 # vim: set sw=2 et:\r
 # Salutations plugin: respond to salutations\r
+# TODO allow online editing of salutations\r
+# TODO *REMEMBER* to set @changed to true after edit\r
+# TODO or changes won't be saved\r
 \r
 unless Array.respond_to?(:pick_one)\r
   debug "Defining the pick_one method for Array"\r
@@ -18,6 +21,12 @@ class SalutPlugin < Plugin
     :desc => "Check for a salutation in all languages and not just in the one defined by core.language",\r
     :on_change => Proc.new {|bot, v| bot.plugins['salut'].reload}\r
   )\r
+  BotConfig.register BotConfigBooleanValue.new('salut.address_only',\r
+    :default => true, \r
+    :desc => "When set to true, the bot will only reply to salutations directed at him",\r
+    :on_change => Proc.new {|bot, v| bot.plugins['salut'].reload}\r
+  )\r
+\r
 \r
   def initialize\r
     @salutations = Hash.new\r
@@ -25,6 +34,7 @@ class SalutPlugin < Plugin
     @main_lang_str = nil\r
     @main_lang = nil\r
     @all_langs = true\r
+    @changed = false\r
     super\r
     reload\r
   end\r
@@ -35,12 +45,15 @@ class SalutPlugin < Plugin
 \r
   def create_match\r
     @match = Hash.new\r
+    ar_dest = Array.new\r
     ar_in = Array.new\r
     ar_out = Array.new\r
     ar_both = Array.new\r
     @salutations.each { |lang, hash|\r
       hash.each { |situation, array|\r
         case situation.to_s\r
+        when /^generic-dest$/\r
+          ar_dest += array\r
         when /in$/\r
           ar_in += array\r
         when /out$/\r
@@ -60,19 +73,31 @@ class SalutPlugin < Plugin
       Regexp.escape(txt)\r
     }.join('|') + ")\\b", Regexp::IGNORECASE) unless ar_both.empty?\r
     debug "Matches: #{@match.inspect}"\r
+    @match[:dest] = Regexp.new("\\b(?:" + ar_dest.uniq.map { |txt|\r
+      Regexp.escape(txt)\r
+    }.join('|') + ")\\b", Regexp::IGNORECASE) unless ar_dest.empty?\r
+    @punct = /\s*[.,:!;?]?\s*/ # Punctuation\r
   end\r
 \r
   def listen(m)\r
+    return unless m.kind_of?(PrivMessage)\r
+    to_me = m.address? || m.message =~ /#{Regexp.escape(@bot.nick)}/i\r
+    if @bot.config['salut.address_only']\r
+      return unless to_me\r
+    end\r
     salut = nil\r
     [:both, :in, :out].each { |k|\r
       next unless @match[k]\r
       debug "Checking salutations #{k} (#{@match[k].inspect})"\r
-      if m.message =~ /^#{@match[k]}/\r
+      if m.message =~ @match[k]\r
         salut = k\r
         break\r
       end\r
     }\r
     return unless salut\r
+    # If the bot wasn't addressed, we continue only the match was exact\r
+    # (apart from space and punctuation) or if @match[:dest] matches too\r
+    return unless to_me or m.message =~ @match[:dest] or m.message =~ /^#{@punct}#{@match[salut]}#{@punct}$/\r
     h = Time.new.hour\r
     case h\r
     when 4...12\r
@@ -100,7 +125,7 @@ class SalutPlugin < Plugin
     rep_ar += @salutations[@main_lang].fetch("generic".to_sym, []) unless sfx.empty?\r
     debug "Choosing reply in #{rep_ar.inspect} ..."\r
     if rep_ar.empty?\r
-      if m.public? and (m.address? or m =~ /#{Regexp.escape(@bot.nick)}/)\r
+      if m.public? and (m.address? or m =~ /#{Regexp.escape(@bot.nick)}/)\r
         choice = @bot.lang.get("hello_X") % m.sourcenick\r
       else\r
         choice = @bot.lang.get("hello") % m.sourcenick\r
@@ -134,6 +159,7 @@ class SalutPlugin < Plugin
       @salutations[@main_lang] = load_lang(@main_lang_str)\r
     end\r
     create_match\r
+    @changed = false\r
   end\r
 \r
   def load_lang(lang)\r
@@ -159,15 +185,17 @@ class SalutPlugin < Plugin
 \r
   def save\r
     return if @salutations.empty?\r
+    return unless @changed\r
     @salutations.each { |lang, val|\r
       l = lang.to_s\r
       save_lang(lang, val)\r
     }\r
+    @changed = false\r
   end\r
 \r
   def save_lang(lang, val)\r
-    file = "#{@bot.botclass}/salut/salut-#{lang}"\r
-    Utils.safe_save(file) { |file|\r
+    fn = "#{@bot.botclass}/salut/salut-#{lang}"\r
+    Utils.safe_save(fn) { |file|\r
       file.puts val.to_yaml\r
     }\r
   end\r