]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/keywords.rb
plugin(keywords): remove export to file, see #42
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / keywords.rb
index c2054e3bd54ce00489cfaec0607f205689a74adb..ecb6cca85cecfd6bde062c1e525a1bd1c705db03 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(" | ")
@@ -88,6 +101,9 @@ class Keywords < Plugin
   Config.register Config::IntegerValue.new('keyword.search_results',
     :default => 3,
     :desc => "How many search results to display at a time")
+  Config.register Config::ArrayValue.new('keyword.ignore_words',
+    :default => ["how", "that", "these", "they", "this", "what", "when", "where", "who", "why", "you"],
+    :desc => "A list of words that the bot should passively ignore.")
 
   # create a new KeywordPlugin instance, associated to bot +bot+
   def initialize
@@ -100,9 +116,10 @@ class Keywords < Plugin
     scan
 
     # import old format keywords into DBHash
-    if(File.exist?("#{@bot.botclass}/keywords.rbot"))
+    olds = @bot.path 'keywords.rbot'
+    if File.exist? olds
       log "auto importing old keywords.rbot"
-      IO.foreach("#{@bot.botclass}/keywords.rbot") do |line|
+      IO.foreach(olds) do |line|
         if(line =~ /^(.*?)\s*<=(is|are)?=?>\s*(.*)$/)
           lhs = $1
           mhs = $2
@@ -113,7 +130,7 @@ class Keywords < Plugin
           @keywords[lhs] = Keyword.new(mhs, values).dump
         end
       end
-      File.rename("#{@bot.botclass}/keywords.rbot", "#{@bot.botclass}/keywords.rbot.old")
+      File.rename(olds, olds + ".old")
     end
   end
 
@@ -121,15 +138,12 @@ class Keywords < Plugin
   # have been added
   def scan
     # first scan for old DBHash files, and convert them
-    Dir["#{@bot.botclass}/keywords/*"].each {|f|
+    Dir[datafile('*')].each {|f|
       next unless f =~ /\.db$/
       log "upgrading keyword db #{f} (rbot 0.9.5 or prior) database format"
       newname = f.gsub(/\.db$/, ".kdb")
-      old = BDB::Hash.open f, nil,
-                           "r+", 0600
-      new = BDB::CIBtree.open(newname, nil,
-                              BDB::CREATE | BDB::EXCL,
-                              0600)
+      old = BDB::Hash.open f, nil, "r+", 0600
+      new = BDB::CIBtree.open(newname, nil, BDB::CREATE | BDB::EXCL, 0600)
       old.each {|k,v|
         new[k] = v
       }
@@ -139,7 +153,7 @@ class Keywords < Plugin
     }
 
     # then scan for current DBTree files, and load them
-    Dir["#{@bot.botclass}/keywords/*"].each {|f|
+    Dir[@bot.path('keywords', '*')].each {|f|
       next unless f =~ /\.kdb$/
       hsh = DBTree.new @bot, f, true
       key = File.basename(f).gsub(/\.kdb$/, "")
@@ -148,7 +162,7 @@ class Keywords < Plugin
     }
 
     # then scan for non DB files, and convert/import them and delete
-    Dir["#{@bot.botclass}/keywords/*"].each {|f|
+    Dir[@bot.path('keywords', '*')].each {|f|
       next if f =~ /\.kdb$/
       next if f =~ /CVS$/
       log "auto converting keywords from #{f}"
@@ -176,28 +190,28 @@ class Keywords < Plugin
 
   # upgrade data files found in old rbot formats to current
   def upgrade_data
-    if File.exist?("#{@bot.botclass}/keywords.db")
+    olds = @bot.path 'keywords.db'
+    if File.exist? olds
       log "upgrading old keywords (rbot 0.9.5 or prior) database format"
-      old = BDB::Hash.open "#{@bot.botclass}/keywords.db", nil,
-                           "r+", 0600
+      old = BDB::Hash.open olds, nil, "r+", 0600
       old.each {|k,v|
         @keywords[k] = v
       }
       old.close
       @keywords.flush
-      File.rename("#{@bot.botclass}/keywords.db", "#{@bot.botclass}/keywords.db.old")
+      File.rename(olds, olds + ".old")
     end
 
-    if File.exist?("#{@bot.botclass}/keyword.db")
+    olds.replace(@bot.path('keyword.db'))
+    if File.exist? olds
       log "upgrading old keywords (rbot 0.9.9 or prior) database format"
-      old = BDB::CIBtree.open "#{@bot.botclass}/keyword.db", nil,
-                           "r+", 0600
+      old = BDB::CIBtree.open olds, nil, "r+", 0600
       old.each {|k,v|
         @keywords[k] = v
       }
       old.close
       @keywords.flush
-      File.rename("#{@bot.botclass}/keyword.db", "#{@bot.botclass}/keyword.db.old")
+      File.rename(olds, olds + ".old")
     end
   end
 
@@ -207,7 +221,7 @@ class Keywords < Plugin
   end
 
   def oldsave
-    File.open("#{@bot.botclass}/keywords.rbot", "w") do |file|
+    File.open(@bot.path("keywords.rbot"), "w") do |file|
       @keywords.each do |key, value|
         file.puts "#{key}<=#{value.type}=>#{value.dump}"
       end
@@ -245,6 +259,11 @@ class Keywords < Plugin
     return false
   end
 
+  # is +word+ a passively ignored keyword?
+  def ignored_word?(word)
+    @bot.config["keyword.ignore_words"].include?(word)
+  end
+
   # m::     PrivMessage containing message info
   # key::   key being queried
   # quiet:: optional, if false, complain if +key+ is not found
@@ -306,6 +325,10 @@ class Keywords < Plugin
     case plugin
     when /keyword/
       case topic
+      when 'stats'
+        'keyword stats => show statistics about static facts'
+      when 'wipe'
+        'keyword wipe <keyword> => forgets everything about a keyword'
       when 'lookup'
         'keyword [lookup] <keyword> => look up the definition for a keyword; writing "lookup" is optional'
       when 'set'
@@ -329,7 +352,7 @@ class Keywords < Plugin
       when '<topic>'
         '<topic> => respond by setting the topic to the rest of the definition'
       else
-        'keyword module (fact learning and regurgitation) topics: lookup, set, forget, tell, search, listen, address, <reply>, <action>, <who>, <topic>'
+        'keyword module (fact learning and regurgitation) topics: lookup, set, forget, tell, search, listen, address, stats, export, wipe, <reply>, <action>, <who>, <topic>'
       end
     when "forget"
       'forget <keyword> => forget a keyword'
@@ -430,12 +453,25 @@ 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
+
   # privmsg handler
   def privmsg(m)
     case m.plugin
@@ -445,6 +481,8 @@ class Keywords < Plugin
         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*$/
@@ -486,17 +524,17 @@ class Keywords < Plugin
     # TODO option to do if(m.message =~ /^(.*)$/, ie try any line as a
     # keyword lookup.
     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)
+      keyword_lookup m, $1, true if !ignored_word?($1) && @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
-      keyword_command m, $1, $2, $3, true if @bot.auth.allow?("keycmd", m.source)
+      keyword_command m, $1, $2, $3, true if !ignored_word?($1) && @bot.auth.allow?("keycmd", m.source)
     end
   end
 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