# import old format keywords into DBHash
if(File.exist?("#{@bot.botclass}/keywords.rbot"))
- puts "auto importing old keywords.rbot"
+ log "auto importing old keywords.rbot"
IO.foreach("#{@bot.botclass}/keywords.rbot") do |line|
if(line =~ /^(.*?)\s*<=(is|are)?=?>\s*(.*)$/)
lhs = $1
# first scan for old DBHash files, and convert them
Dir["#{@bot.botclass}/keywords/*"].each {|f|
next unless f =~ /\.db$/
- puts "upgrading keyword db #{f} (rbot 0.9.5 or prior) database format"
+ 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, "set_pagesize" => 1024,
- "set_cachesize" => [0, 32 * 1024, 0]
- new = BDB::CIBtree.open newname, nil,
- BDB::CREATE | BDB::EXCL | BDB::TRUNCATE,
- 0600, "set_pagesize" => 1024,
- "set_cachesize" => [0, 32 * 1024, 0]
+ "r+", 0600
+ new = BDB::CIBtree.open(newname, nil,
+ BDB::CREATE | BDB::EXCL,
+ 0600)
old.each {|k,v|
new[k] = v
}
Dir["#{@bot.botclass}/keywords/*"].each {|f|
next if f =~ /\.kdb$/
next if f =~ /CVS$/
- puts "auto converting keywords from #{f}"
+ log "auto converting keywords from #{f}"
key = File.basename(f)
unless @statickeywords.has_key?(key)
@statickeywords[key] = DBHash.new @bot, "#{f}.db", true
# upgrade data files found in old rbot formats to current
def upgrade_data
if File.exist?("#{@bot.botclass}/keywords.db")
- puts "upgrading old keywords (rbot 0.9.5 or prior) database format"
+ log "upgrading old keywords (rbot 0.9.5 or prior) database format"
old = BDB::Hash.open "#{@bot.botclass}/keywords.db", nil,
- "r+", 0600, "set_pagesize" => 1024,
- "set_cachesize" => [0, 32 * 1024, 0]
+ "r+", 0600
new = BDB::CIBtree.open "#{@bot.botclass}/keyword.db", nil,
- BDB::CREATE | BDB::EXCL | BDB::TRUNCATE,
- 0600, "set_pagesize" => 1024,
- "set_cachesize" => [0, 32 * 1024, 0]
+ BDB::CREATE | BDB::EXCL,
+ 0600
old.each {|k,v|
new[k] = v
}
# lookup keyword +key+, return it or nil
def [](key)
+ return nil if key.nil?
debug "keywords module: looking up key #{key}"
if(@keywords.has_key?(key))
return Keyword.restore(@keywords[key])
#
# handle a message asking about a keyword
def keyword(m, key, dunno=true)
+ return if key.nil?
unless(kw = self[key])
m.reply @bot.lang.get("dunno") if (dunno)
return
full = false
full = true if str.gsub!(/--full\s+/, "")
- re = Regexp.new(str, Regexp::IGNORECASE)
- if(@bot.auth.allow?("keyword", m.source, m.replyto))
- matches = Array.new
- @keywords.each {|k,v|
- kw = Keyword.restore(v)
- if re.match(k) || (full && re.match(kw.desc))
- matches << [k,kw]
+ begin
+ re = Regexp.new(str, Regexp::IGNORECASE)
+ if(@bot.auth.allow?("keyword", m.source, m.replyto))
+ matches = Array.new
+ @keywords.each {|k,v|
+ kw = Keyword.restore(v)
+ if re.match(k) || (full && re.match(kw.desc))
+ matches << [k,kw]
+ end
+ }
+ if all
+ @statickeywords.each {|k,v|
+ v.each {|kk,vv|
+ kw = Keyword.restore(vv)
+ if re.match(kk) || (full && re.match(kw.desc))
+ matches << [kk,kw]
+ end
+ }
+ }
end
- }
- if all
- @statickeywords.each {|k,v|
- v.each {|kk,vv|
- kw = Keyword.restore(vv)
- if re.match(kk) || (full && re.match(kw.desc))
- matches << [kk,kw]
- end
+ if matches.length == 1
+ rkw = matches[0]
+ m.reply "#{rkw[0]} #{rkw[1].type} #{rkw[1].desc}"
+ elsif matches.length > 0
+ i = 0
+ matches.each {|rkw|
+ m.reply "[#{i+1}/#{matches.length}] #{rkw[0]} #{rkw[1].type} #{rkw[1].desc}"
+ i += 1
+ break if i == 3
}
- }
- end
- if matches.length == 1
- rkw = matches[0]
- m.reply "#{rkw[0]} #{rkw[1].type} #{rkw[1].desc}"
- elsif matches.length > 0
- i = 0
- matches.each {|rkw|
- m.reply "[#{i+1}/#{matches.length}] #{rkw[0]} #{rkw[1].type} #{rkw[1].desc}"
- i += 1
- break if i == 3
- }
- else
- m.reply "no keywords match #{str}"
+ else
+ m.reply "no keywords match #{str}"
+ end
end
+ rescue RegexpError => e
+ m.reply "no keywords match #{str}: #{e}"
+ rescue
+ debug e.inspect
+ m.reply "no keywords match #{str}: an error occurred"
end
end
else
# in channel message, not to me
+ # TODO option to do if(m.message =~ /^(.*)$/, ie try any line as a
+ # keyword lookup.
if(m.message =~ /^'(.*)$/ || (!@bot.config["keyword.address"] && m.message =~ /^(.*\S)\s*\?\s*$/))
keyword m, $1, false if(@bot.auth.allow?("keyword", m.source))
elsif(@bot.config["keyword.listen"] == true && (m.message =~ /^(.*?)\s+(is|are)\s+(.*)$/))