X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fkeywords.rb;h=ecb6cca85cecfd6bde062c1e525a1bd1c705db03;hb=e2874c9f8636ed2978eb72fc7a29acab909e7d7b;hp=446a1062bd4651f170acc3bcddf8298dc20190a6;hpb=7cac523563de6473d2f93fd2d053446cde701db1;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/keywords.rb b/data/rbot/plugins/keywords.rb index 446a1062..ecb6cca8 100644 --- a/data/rbot/plugins/keywords.rb +++ b/data/rbot/plugins/keywords.rb @@ -116,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 @@ -129,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 @@ -137,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 } @@ -155,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$/, "") @@ -164,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}" @@ -192,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 @@ -223,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 @@ -261,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 @@ -322,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 => forgets everything about a keyword' when 'lookup' 'keyword [lookup] => look up the definition for a keyword; writing "lookup" is optional' when 'set' @@ -345,7 +352,7 @@ class Keywords < Plugin when '' ' => 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, , , , ' + 'keyword module (fact learning and regurgitation) topics: lookup, set, forget, tell, search, listen, address, stats, export, wipe, , , , ' end when "forget" 'forget => forget a keyword' @@ -465,41 +472,11 @@ class Keywords < Plugin 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 - # 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+(.+)$/