]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/keywords.rb
lart plugin: replace "me" with sourcenick
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / keywords.rb
index 7aa5b9743fb1387c69d7d276d444e5fd5986feb8..5ed5256544a7e4e32a30e4901fccdb4b04d0c66b 100644 (file)
@@ -28,6 +28,19 @@ class Keyword
     end
   end
 
+  # return an array of all the possible values
+  def to_factoids(key)
+    ar = Array.new
+    @values.each { |val|
+      debug "key #{key}, value #{val}"
+      vals = val.split(" or ")
+      vals.each { |v|
+        ar << "%s %s %s" % [key, @type, v]
+      }
+    }
+    return ar
+  end
+
   # describe the keyword (show all values without interpolation)
   def desc
     @values.join(" | ")
@@ -79,13 +92,13 @@ end
 # handle it, checks for a keyword command or lookup, otherwise the message
 # is delegated to plugins
 class Keywords < Plugin
-  BotConfig.register BotConfigBooleanValue.new('keyword.listen',
+  Config.register Config::BooleanValue.new('keyword.listen',
     :default => false,
     :desc => "Should the bot listen to all chat and attempt to automatically detect keywords? (e.g. by spotting someone say 'foo is bar')")
-  BotConfig.register BotConfigBooleanValue.new('keyword.address',
+  Config.register Config::BooleanValue.new('keyword.address',
     :default => true,
     :desc => "Should the bot require that keyword lookups are addressed to it? If not, the bot will attempt to lookup foo if someone says 'foo?' in channel")
-  BotConfig.register BotConfigIntegerValue.new('keyword.search_results',
+  Config.register Config::IntegerValue.new('keyword.search_results',
     :default => 3,
     :desc => "How many search results to display at a time")
 
@@ -117,13 +130,6 @@ class Keywords < Plugin
     end
   end
 
-  # drop static keywords and reload them from files, picking up any new
-  # keyword files that have been added
-  def rescan
-    @statickeywords = Hash.new
-    scan
-  end
-
   # load static keywords from files, picking up any new keyword files that
   # have been added
   def scan
@@ -285,20 +291,27 @@ class Keywords < Plugin
     debug "got keyword command #{lhs}, #{mhs}, #{rhs}"
     return if lhs.strip.empty?
 
+    overwrite = false
+    overwrite = true if(lhs.gsub!(/^no,\s*/, ""))
+    also = false
     also = true if(rhs.gsub!(/^also\s+/, ""))
 
     values = rhs.split(/\s+\|\s+/)
     lhs = Keyword.unescape lhs
 
-    if(also && has_key?(lhs))
+    if(overwrite || also || !has_key?(lhs))
+      if(also && has_key?(lhs))
+        kw = self[lhs]
+        kw << values
+        @keywords[lhs] = kw.dump
+      else
+        @keywords[lhs] = Keyword.new(mhs, values).dump
+      end
+      m.okay if !quiet
+    elsif(has_key?(lhs))
       kw = self[lhs]
-      kw << values
-      @keywords[lhs] = kw.dump
-    else
-      @keywords[lhs] = Keyword.new(mhs, values).dump
+      m.reply "but #{lhs} #{kw.type} #{kw.desc}" if kw && !quiet
     end
-
-    @bot.okay m.target if !quiet
   end
 
   # return help string for Keywords with option topic +topic+
@@ -335,6 +348,8 @@ class Keywords < Plugin
       'forget <keyword> => forget a keyword'
     when "tell"
       'tell <nick> about <keyword> => tell somebody about a keyword'
+    when "learn"
+      'learn that <keyword> is/are <definition> => define a keyword, definition can contain "|" to separate multiple randomly chosen replies'
     else
       'keyword module (fact learning and regurgitation) topics: lookup, set, forget, tell, search, listen, address, <reply>, <action>, <who>, <topic>'
     end
@@ -428,9 +443,45 @@ class Keywords < Plugin
 
   # forget one of the dynamic keywords
   def keyword_forget(m, key)
-    if(@keywords.has_key?(key))
-      @keywords.delete(key)
-      @bot.okay m.replyto
+    if @keywords.delete(key)
+      m.okay
+    else
+      m.reply _("couldn't find keyword %{key}" % { :key => key })
+    end
+  end
+
+  # low-level keyword wipe command for when forget doesn't work
+  def keyword_wipe(m, key)
+    reg = @keywords.registry
+    reg.env.begin(reg) { |t, b|
+      b.delete_if { |k, v|
+        (k == key) && (m.reply "wiping keyword #{key} with stored value #{Marshal.restore(v)}")
+      }
+      t.commit
+    }
+    m.reply "done"
+  end
+
+  # export keywords to factoids file
+  def keyword_factoids_export
+    ar = Array.new
+
+    debug @keywords.keys
+
+    @keywords.each { |k, val|
+      next unless val
+      kw = Keyword.restore(val)
+      ar |= kw.to_factoids(k)
+    }
+
+    # TODO check factoids config
+    # also TODO: runtime export
+    dir = File.join(@bot.botclass,"factoids")
+    fname = File.join(dir,"keyword_factoids.rbot")
+
+    Dir.mkdir(dir) unless FileTest.directory?(dir)
+    Utils.safe_save(fname) do |file|
+      file.puts ar
     end
   end
 
@@ -439,10 +490,19 @@ class Keywords < Plugin
     case m.plugin
     when "keyword"
       case m.params
+      when /^export$/
+        begin
+          keyword_factoids_export
+          m.okay
+        rescue
+          m.reply _("failed to export keywords as factoids (%{err})" % {:err => $!})
+        end
       when /^set\s+(.+?)\s+(is|are)\s+(.+)$/
         keyword_command(m, $1, $2, $3) if @bot.auth.allow?('keycmd', m.source, m.replyto)
       when /^forget\s+(.+)$/
         keyword_forget(m, $1) if @bot.auth.allow?('keycmd', m.source, m.replyto)
+      when /^wipe\s(.+)$/ # note that only one space is stripped, allowing removal of space-prefixed keywords
+        keyword_wipe(m, $1) if @bot.auth.allow?('keycmd', m.source, m.replyto)
       when /^lookup\s+(.+)$/
         keyword_lookup(m, $1) if @bot.auth.allow?('keyword', m.source, m.replyto)
       when /^stats\s*$/
@@ -471,15 +531,19 @@ class Keywords < Plugin
       else
         m.reply "wrong 'tell' syntax"
       end
+    when "learn"
+      if m.params =~ /^that\s+(.+?)\s+(is|are)\s+(.+)$/
+        keyword_command(m, $1, $2, $3) if @bot.auth.allow?('keycmd', m.source, m.replyto)
+      else
+        m.reply "wrong 'learn' syntax"
+      end
     end
   end
 
-  def listen(m)
-    return if m.address?
-    # in channel message, not to me
+  def unreplied(m)
     # TODO option to do if(m.message =~ /^(.*)$/, ie try any line as a
     # keyword lookup.
-    if !@bot.config["keyword.address"] && m.message =~ /^(.*\S)\s*\?\s*$/
+    if m.message =~ /^(.*\S)\s*\?\s*$/ and (m.address? or not @bot.config["keyword.address"])
       keyword_lookup m, $1, true if @bot.auth.allow?("keyword", m.source)
     elsif @bot.config["keyword.listen"] && (m.message =~ /^(.*?)\s+(is|are)\s+(.*)$/)
       # TODO MUCH more selective on what's allowed here
@@ -490,6 +554,7 @@ end
 
 plugin = Keywords.new
 plugin.register 'keyword'
-plugin.register 'forget'
-plugin.register 'tell'
+plugin.register 'forget' rescue nil
+plugin.register 'tell' rescue nil
+plugin.register 'learn' rescue nil