]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/keywords.rb
keywords plugin: when exporting to factoids, split at ' or '
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / keywords.rb
index 255d7fe6886a2c56fcc6bd90f021a65cf23cc181..38a3ff43667b4741808f53c128626e5697f3146e 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
@@ -443,11 +449,41 @@ class Keywords < Plugin
     end
   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
+
   # privmsg handler
   def privmsg(m)
     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+(.+)$/
@@ -503,7 +539,7 @@ end
 
 plugin = Keywords.new
 plugin.register 'keyword'
-plugin.register 'forget'
-plugin.register 'tell'
-plugin.register 'learn'
+plugin.register 'forget' rescue nil
+plugin.register 'tell' rescue nil
+plugin.register 'learn' rescue nil