5 # Encapsulates a keyword ("foo is bar" is a keyword called foo, with type
6 # is, and has a single value of bar).
7 # Keywords can have multiple values, to_s() will choose one at random
10 # type of keyword (e.g. "is" or "are")
13 # type:: type of keyword (e.g "is" or "are")
14 # values:: array of values
16 # create a keyword of type +type+ with values +values+
17 def initialize(type, values)
22 # pick a random value for this keyword and return it
24 if(@values.length > 1)
25 Keyword.unescape(@values[rand(@values.length)])
27 Keyword.unescape(@values[0])
31 # describe the keyword (show all values without interpolation)
36 # return the keyword in a stringified form ready for storage
38 @type + "/" + Keyword.unescape(@values.join("<=or=>"))
41 # deserialize the stringified form to an object
42 def Keyword.restore(str)
43 if str =~ /^(\S+?)\/(.*)$/
45 vals = $2.split("<=or=>")
46 return Keyword.new(type, vals)
51 # values:: array of values to add
52 # add values to a keyword
54 if(@values.length > 1 || values.length > 1)
59 @values[0] += " or " + values[0]
63 # unescape special words/characters in a keyword
64 def Keyword.unescape(str)
65 str.gsub(/\\\|/, "|").gsub(/ \\is /, " is ").gsub(/ \\are /, " are ").gsub(/\\\?(\s*)$/, "?\1")
68 # escape special words/characters in a keyword
69 def Keyword.escape(str)
70 str.gsub(/\|/, "\\|").gsub(/ is /, " \\is ").gsub(/ are /, " \\are ").gsub(/\?(\s*)$/, "\\?\1")
76 # Handles all that stuff like "bot: foo is bar", "bot: foo?"
78 # Fallback after core and auth have had a look at a message and refused to
79 # handle it, checks for a keyword command or lookup, otherwise the message
80 # is delegated to plugins
83 # create a new Keywords instance, associated to bot +bot+
86 @statickeywords = Hash.new
88 @keywords = DBTree.new bot, "keyword"
92 # import old format keywords into DBHash
93 if(File.exist?("#{@bot.botclass}/keywords.rbot"))
94 puts "auto importing old keywords.rbot"
95 IO.foreach("#{@bot.botclass}/keywords.rbot") do |line|
96 if(line =~ /^(.*?)\s*<=(is|are)?=?>\s*(.*)$/)
100 mhs = "is" unless mhs
101 rhs = Keyword.escape rhs
102 values = rhs.split("<=or=>")
103 @keywords[lhs] = Keyword.new(mhs, values).dump
106 File.delete("#{@bot.botclass}/keywords.rbot")
110 # drop static keywords and reload them from files, picking up any new
111 # keyword files that have been added
113 @statickeywords = Hash.new
117 # load static keywords from files, picking up any new keyword files that
120 # first scan for old DBHash files, and convert them
121 Dir["#{@bot.botclass}/keywords/*"].each {|f|
122 next unless f =~ /\.db$/
123 puts "upgrading keyword db #{f} (rbot 0.9.5 or prior) database format"
124 newname = f.gsub(/\.db$/, ".kdb")
125 old = BDB::Hash.open f, nil,
126 "r+", 0600, "set_pagesize" => 1024,
127 "set_cachesize" => [0, 32 * 1024, 0]
128 new = BDB::CIBtree.open newname, nil,
129 BDB::CREATE | BDB::EXCL | BDB::TRUNCATE,
130 0600, "set_pagesize" => 1024,
131 "set_cachesize" => [0, 32 * 1024, 0]
140 # then scan for current DBTree files, and load them
141 Dir["#{@bot.botclass}/keywords/*"].each {|f|
142 next unless f =~ /\.kdb$/
143 hsh = DBTree.new @bot, f, true
144 key = File.basename(f).gsub(/\.kdb$/, "")
145 debug "keywords module: loading DBTree file #{f}, key #{key}"
146 @statickeywords[key] = hsh
149 # then scan for non DB files, and convert/import them and delete
150 Dir["#{@bot.botclass}/keywords/*"].each {|f|
151 next if f =~ /\.kdb$/
153 puts "auto converting keywords from #{f}"
154 key = File.basename(f)
155 unless @statickeywords.has_key?(key)
156 @statickeywords[key] = DBHash.new @bot, "#{f}.db", true
158 IO.foreach(f) {|line|
159 if(line =~ /^(.*?)\s*<?=(is|are)?=?>\s*(.*)$/)
163 # support infobot style factfiles, by fixing them up here
164 rhs.gsub!(/\$who/, "<who>")
165 mhs = "is" unless mhs
166 rhs = Keyword.escape rhs
167 values = rhs.split("<=or=>")
168 @statickeywords[key][lhs] = Keyword.new(mhs, values).dump
172 @statickeywords[key].flush
176 # upgrade data files found in old rbot formats to current
178 if File.exist?("#{@bot.botclass}/keywords.db")
179 puts "upgrading old keywords (rbot 0.9.5 or prior) database format"
180 old = BDB::Hash.open "#{@bot.botclass}/keywords.db", nil,
181 "r+", 0600, "set_pagesize" => 1024,
182 "set_cachesize" => [0, 32 * 1024, 0]
183 new = BDB::CIBtree.open "#{@bot.botclass}/keyword.db", nil,
184 BDB::CREATE | BDB::EXCL | BDB::TRUNCATE,
185 0600, "set_pagesize" => 1024,
186 "set_cachesize" => [0, 32 * 1024, 0]
192 File.delete("#{@bot.botclass}/keywords.db")
196 # save dynamic keywords to file
201 File.open("#{@bot.botclass}/keywords.rbot", "w") do |file|
202 @keywords.each do |key, value|
203 file.puts "#{key}<=#{value.type}=>#{value.dump}"
208 # lookup keyword +key+, return it or nil
210 debug "keywords module: looking up key #{key}"
211 if(@keywords.has_key?(key))
212 return Keyword.restore(@keywords[key])
214 # key name order for the lookup through these
215 @statickeywords.keys.sort.each {|k|
216 v = @statickeywords[k]
218 return Keyword.restore(v[key])
225 # does +key+ exist as a keyword?
227 if @keywords.has_key?(key) && Keyword.restore(@keywords[key]) != nil
230 @statickeywords.each {|k,v|
231 if v.has_key?(key) && Keyword.restore(v[key]) != nil
238 # m:: PrivMessage containing message info
239 # key:: key being queried
240 # dunno:: optional, if true, reply "dunno" if +key+ not found
242 # handle a message asking about a keyword
243 def keyword(m, key, dunno=true)
244 unless(kw = self[key])
245 m.reply @bot.lang.get("dunno") if (dunno)
249 response.gsub!(/<who>/, m.sourcenick)
250 if(response =~ /^<reply>\s*(.*)/)
252 elsif(response =~ /^<action>\s*(.*)/)
253 @bot.action m.replyto, "#$1"
254 elsif(m.public? && response =~ /^<topic>\s*(.*)/)
256 @bot.topic m.target, topic
258 m.reply "#{key} #{kw.type} #{response}"
263 # m:: PrivMessage containing message info
264 # target:: channel/nick to tell about the keyword
265 # key:: key being queried
267 # handle a message asking the bot to tell someone about a keyword
268 def keyword_tell(m, target, key)
269 unless(kw = self[key])
270 @bot.say m.sourcenick, @bot.lang.get("dunno_about_X") % key
274 response.gsub!(/<who>/, m.sourcenick)
275 if(response =~ /^<reply>\s*(.*)/)
276 @bot.say target, "#{m.sourcenick} wanted me to tell you: (#{key}) #$1"
277 m.reply "okay, I told #{target}: (#{key}) #$1"
278 elsif(response =~ /^<action>\s*(.*)/)
279 @bot.action target, "#$1 (#{m.sourcenick} wanted me to tell you)"
280 m.reply "okay, I told #{target}: * #$1"
282 @bot.say target, "#{m.sourcenick} wanted me to tell you that #{key} #{kw.type} #{response}"
283 m.reply "okay, I told #{target} that #{key} #{kw.type} #{response}"
287 # handle a message which alters a keyword
288 # like "foo is bar", or "no, foo is baz", or "foo is also qux"
289 def keyword_command(sourcenick, target, lhs, mhs, rhs, quiet=false)
290 debug "got keyword command #{lhs}, #{mhs}, #{rhs}"
292 overwrite = true if(lhs.gsub!(/^no,\s*/, ""))
293 also = true if(rhs.gsub!(/^also\s+/, ""))
294 values = rhs.split(/\s+\|\s+/)
295 lhs = Keyword.unescape lhs
296 if(overwrite || also || !has_key?(lhs))
297 if(also && has_key?(lhs))
300 @keywords[lhs] = kw.dump
302 @keywords[lhs] = Keyword.new(mhs, values).dump
304 @bot.okay target if !quiet
307 @bot.say target, "but #{lhs} #{kw.type} #{kw.desc}" if kw && !quiet
311 # return help string for Keywords with option topic +topic+
315 return "set: <keyword> is <definition>, overide: no, <keyword> is <definition>, add to definition: <keyword> is also <definition>, random responses: <keyword> is <definition> | <definition> [| ...], plurals: <keyword> are <definition>, escaping: \\is, \\are, \\|, specials: <reply>, <action>, <who>"
317 return "set => <keyword> is <definition>"
319 return "plurals => <keywords> are <definition>"
321 return "overide => no, <keyword> is <definition>"
323 return "also => <keyword> is also <definition>"
325 return "random responses => <keyword> is <definition> | <definition> [| ...]"
327 return "asking for keywords => (with addressing) \"<keyword>?\", (without addressing) \"'<keyword>\""
329 return "tell <nick> about <keyword> => if <keyword> is known, tell <nick>, via /msg, its definition"
331 return "forget <keyword> => forget fact <keyword>"
333 return "keywords => show current keyword counts"
335 return "<reply> => normal response is \"<keyword> is <definition>\", but if <definition> begins with <reply>, the response will be \"<definition>\""
337 return "<action> => makes keyword respnse \"/me <definition>\""
339 return "<who> => replaced with questioner in reply"
341 return "<topic> => respond by setting the topic to the rest of the definition"
343 return "Keyword module (Fact learning and regurgitation) topics: overview, set, plurals, override, also, random, get, tell, forget, keywords, <reply>, <action>, <who>, <topic>"
350 if(!(m.message =~ /\\\?\s*$/) && m.message =~ /^(.*\S)\s*\?\s*$/)
351 keyword m, $1 if(@bot.auth.allow?("keyword", m.source, m.replyto))
352 elsif(m.message =~ /^(.*?)\s+(is|are)\s+(.*)$/)
353 keyword_command(m.sourcenick, m.replyto, $1, $2, $3) if(@bot.auth.allow?("keycmd", m.source, m.replyto))
354 elsif (m.message =~ /^tell\s+(\S+)\s+about\s+(.+)$/)
355 keyword_tell(m, $1, $2) if(@bot.auth.allow?("keyword", m.source, m.replyto))
356 elsif (m.message =~ /^forget\s+(.*)$/)
358 if((@bot.auth.allow?("keycmd", m.source, m.replyto)) && @keywords.has_key?(key))
359 @keywords.delete(key)
362 elsif (m.message =~ /^keywords$/)
363 if(@bot.auth.allow?("keyword", m.source, m.replyto))
365 @statickeywords.each {|k,v|
368 m.reply "There are currently #{@keywords.length} keywords, #{length} static facts defined."
372 # in channel message, not to me
373 if(m.message =~ /^'(.*)$/ || (@bot.config["NO_KEYWORD_ADDRESS"] == "true" && m.message =~ /^(.*\S)\s*\?\s*$/))
374 keyword m, $1, false if(@bot.auth.allow?("keyword", m.source))
375 elsif(@bot.config["KEYWORD_LISTEN"] == "true" && (m.message =~ /^(.*?)\s+(is|are)\s+(.*)$/))
376 # TODO MUCH more selective on what's allowed here
377 keyword_command(m.sourcenick, m.replyto, $1, $2, $3, true) if(@bot.auth.allow?("keycmd", m.source))