# type of keyword (e.g. "is" or "are")
attr_reader :type
-
+
# type:: type of keyword (e.g "is" or "are")
# values:: array of values
- #
+ #
# create a keyword of type +type+ with values +values+
def initialize(type, values)
@type = type.downcase
end
end
- # keywords class.
+ # keywords class.
#
# Handles all that stuff like "bot: foo is bar", "bot: foo?"
#
BotConfig.register BotConfigBooleanValue.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")
-
+
# create a new Keywords instance, associated to bot +bot+
def initialize(bot)
@bot = bot
@keywords = DBTree.new bot, "keyword"
scan
-
+
# 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
}
#
# 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