]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - rbot/keywords.rb
initial import of rbot
[user/henk/code/ruby/rbot.git] / rbot / keywords.rb
1 module Irc
2
3   # Keyword class
4   #
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
8   class Keyword
9     
10     # type of keyword (e.g. "is" or "are")
11     attr_reader :type
12     
13     # type::   type of keyword (e.g "is" or "are")
14     # values:: array of values
15     # 
16     # create a keyword of type +type+ with values +values+
17     def initialize(type, values)
18       @type = type.downcase
19       @values = values
20     end
21
22     # pick a random value for this keyword and return it
23     def to_s
24       if(@values.length > 1)
25         Keyword.unescape(@values[rand(@values.length)])
26       else
27         Keyword.unescape(@values[0])
28       end
29     end
30
31     # describe the keyword (show all values without interpolation)
32     def desc
33       @values.join(" | ")
34     end
35
36     # return the keyword in a stringified form ready for storage
37     def dump
38       @type + "/" + Keyword.unescape(@values.join("<=or=>"))
39     end
40
41     # deserialize the stringified form to an object
42     def Keyword.restore(str)
43       if str =~ /^(\S+?)\/(.*)$/
44         type = $1
45         vals = $2.split("<=or=>")
46         return Keyword.new(type, vals)
47       end
48       return nil
49     end
50
51     # values:: array of values to add
52     # add values to a keyword
53     def <<(values)
54       if(@values.length > 1 || values.length > 1)
55         values.each {|v|
56           @values << v
57         }
58       else
59         @values[0] += " or " + values[0]
60       end
61     end
62
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")
66     end
67
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")
71     end
72   end
73
74   # keywords class. 
75   #
76   # Handles all that stuff like "bot: foo is bar", "bot: foo?"
77   #
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
81   class Keywords
82     
83     # create a new Keywords instance, associated to bot +bot+
84     def initialize(bot)
85       @bot = bot
86       @statickeywords = Hash.new
87       upgrade_data
88       @keywords = DBTree.new bot, "keyword"
89
90       scan
91       
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*(.*)$/)
97             lhs = $1
98             mhs = $2
99             rhs = $3
100             mhs = "is" unless mhs
101             rhs = Keyword.escape rhs
102             values = rhs.split("<=or=>")
103             @keywords[lhs] = Keyword.new(mhs, values).dump
104           end
105         end
106         File.delete("#{@bot.botclass}/keywords.rbot")
107       end
108     end
109     
110     # drop static keywords and reload them from files, picking up any new
111     # keyword files that have been added
112     def rescan
113       @statickeywords = Hash.new
114       scan
115     end
116
117     # load static keywords from files, picking up any new keyword files that
118     # have been added
119     def scan
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]
132         old.each {|k,v|
133           new[k] = v
134         }
135         old.close
136         new.close
137         File.delete(f)
138       }
139       
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
147       }
148       
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$/
152         next if f =~ /CVS$/
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
157         end
158         IO.foreach(f) {|line|
159           if(line =~ /^(.*?)\s*<?=(is|are)?=?>\s*(.*)$/)
160             lhs = $1
161             mhs = $2
162             rhs = $3
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
169           end
170         }
171         File.delete(f)
172         @statickeywords[key].flush
173       }
174     end
175
176     # upgrade data files found in old rbot formats to current
177     def upgrade_data
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]
187         old.each {|k,v|
188           new[k] = v
189         }
190         old.close
191         new.close
192         File.delete("#{@bot.botclass}/keywords.db")
193       end
194     end
195
196     # save dynamic keywords to file
197     def save
198       @keywords.flush
199     end
200     def oldsave
201       File.open("#{@bot.botclass}/keywords.rbot", "w") do |file|
202         @keywords.each do |key, value|
203           file.puts "#{key}<=#{value.type}=>#{value.dump}"
204         end
205       end
206     end
207     
208     # lookup keyword +key+, return it or nil
209     def [](key)
210       debug "keywords module: looking up key #{key}"
211       if(@keywords.has_key?(key))
212         return Keyword.restore(@keywords[key])
213       else
214         # key name order for the lookup through these
215         @statickeywords.keys.sort.each {|k|
216           v = @statickeywords[k]
217           if v.has_key?(key)
218             return Keyword.restore(v[key])
219           end
220         }
221       end
222       return nil
223     end
224
225     # does +key+ exist as a keyword?
226     def has_key?(key)
227       if @keywords.has_key?(key) && Keyword.restore(@keywords[key]) != nil
228         return true
229       end
230       @statickeywords.each {|k,v|
231         if v.has_key?(key) && Keyword.restore(v[key]) != nil
232           return true
233         end
234       }
235       return false
236     end
237
238     # m::     PrivMessage containing message info
239     # key::   key being queried
240     # dunno:: optional, if true, reply "dunno" if +key+ not found
241     # 
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)
246          return
247        end
248        response = kw.to_s
249        response.gsub!(/<who>/, m.sourcenick)
250        if(response =~ /^<reply>\s*(.*)/)
251          m.reply "#$1"
252        elsif(response =~ /^<action>\s*(.*)/)
253          @bot.action m.replyto, "#$1"
254        elsif(m.public? && response =~ /^<topic>\s*(.*)/)
255          topic = $1
256          @bot.topic m.target, topic
257        else
258          m.reply "#{key} #{kw.type} #{response}"
259        end
260     end
261
262     
263     # m::      PrivMessage containing message info
264     # target:: channel/nick to tell about the keyword
265     # key::    key being queried
266     # 
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
271         return
272       end
273       response = kw.to_s
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"
281       else
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}"
284       end
285     end
286
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}"
291       overwrite = false
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))
298           kw = self[lhs]
299           kw << values
300           @keywords[lhs] = kw.dump
301         else
302           @keywords[lhs] = Keyword.new(mhs, values).dump
303         end
304         @bot.okay target if !quiet
305       elsif(has_key?(lhs))
306         kw = self[lhs]
307         @bot.say target, "but #{lhs} #{kw.type} #{kw.desc}" if kw && !quiet
308       end
309     end
310
311     # return help string for Keywords with option topic +topic+
312     def help(topic="")
313       case topic
314         when "overview"
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>"
316         when "set"
317           return "set => <keyword> is <definition>"
318         when "plurals"
319           return "plurals => <keywords> are <definition>"
320         when "override"
321           return "overide => no, <keyword> is <definition>"
322         when "also"
323           return "also => <keyword> is also <definition>"
324         when "random"
325           return "random responses => <keyword> is <definition> | <definition> [| ...]"
326         when "get"
327           return "asking for keywords => (with addressing) \"<keyword>?\", (without addressing) \"'<keyword>\""
328         when "tell"
329           return "tell <nick> about <keyword> => if <keyword> is known, tell <nick>, via /msg, its definition"
330         when "forget"
331           return "forget <keyword> => forget fact <keyword>"
332         when "keywords"
333           return "keywords => show current keyword counts"
334         when "<reply>"
335           return "<reply> => normal response is \"<keyword> is <definition>\", but if <definition> begins with <reply>, the response will be \"<definition>\""
336         when "<action>"
337           return "<action> => makes keyword respnse \"/me <definition>\""
338         when "<who>"
339           return "<who> => replaced with questioner in reply"
340         when "<topic>"
341           return "<topic> => respond by setting the topic to the rest of the definition"
342         else
343           return "Keyword module (Fact learning and regurgitation) topics: overview, set, plurals, override, also, random, get, tell, forget, keywords, <reply>, <action>, <who>, <topic>"
344       end
345     end
346
347     # privmsg handler
348     def privmsg(m)
349       if(m.address?)
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+(.*)$/)
357           key = $1
358           if((@bot.auth.allow?("keycmd", m.source, m.replyto)) && @keywords.has_key?(key))
359             @keywords.delete(key)
360             @bot.okay m.replyto
361           end
362         elsif (m.message =~ /^keywords$/)
363           if(@bot.auth.allow?("keyword", m.source, m.replyto))
364             length = 0
365             @statickeywords.each {|k,v|
366               length += v.length
367             }
368             m.reply "There are currently #{@keywords.length} keywords, #{length} static facts defined."
369           end
370         end
371       else
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))
378         end
379       end
380     end
381   end
382 end