summaryrefslogtreecommitdiff
path: root/data/rbot/plugins/keywords.rb
blob: 0c1b35262ca693c1e265b468294f528116bc201e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
require 'pp'

# Keyword class
#
# Encapsulates a keyword ("foo is bar" is a keyword called foo, with type
# is, and has a single value of bar).
# Keywords can have multiple values, to_s() will choose one at random
class Keyword

  # 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
    @values = values
  end

  # pick a random value for this keyword and return it
  def to_s
    if(@values.length > 1)
      Keyword.unescape(@values[rand(@values.length)])
    else
      Keyword.unescape(@values[0])
    end
  end

  # describe the keyword (show all values without interpolation)
  def desc
    @values.join(" | ")
  end

  # return the keyword in a stringified form ready for storage
  def dump
    @type + "/" + Keyword.unescape(@values.join("<=or=>"))
  end

  # deserialize the stringified form to an object
  def Keyword.restore(str)
    if str =~ /^(\S+?)\/(.*)$/
      type = $1
      vals = $2.split("<=or=>")
      return Keyword.new(type, vals)
    end
    return nil
  end

  # values:: array of values to add
  # add values to a keyword
  def <<(values)
    if(@values.length > 1 || values.length > 1)
      values.each {|v|
        @values << v
      }
    else
      @values[0] += " or " + values[0]
    end
  end

  # unescape special words/characters in a keyword
  def Keyword.unescape(str)
    str.gsub(/\\\|/, "|").gsub(/ \\is /, " is ").gsub(/ \\are /, " are ").gsub(/\\\?(\s*)$/, "?\1")
  end

  # escape special words/characters in a keyword
  def Keyword.escape(str)
    str.gsub(/\|/, "\\|").gsub(/ is /, " \\is ").gsub(/ are /, " \\are ").gsub(/\?(\s*)$/, "\\?\1")
  end
end

# keywords class.
#
# Handles all that stuff like "bot: foo is bar", "bot: foo?"
#
# Fallback after core and auth have had a look at a message and refused to
# handle it, checks for a keyword command or lookup, otherwise the message
# is delegated to plugins
class Keywords < Plugin
  BotConfig.register BotConfigBooleanValue.new('keyword.listen',
    :default => false,
    :desc => "Should the bot listen to all chat and attempt to automatically detect keywords? (e.g. by spotting someone say 'foo is bar')")
  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")
  BotConfig.register BotConfigIntegerValue.new('keyword.search_results',
    :default => 3,
    :desc => "How many search results to display at a time")

  # create a new KeywordPlugin instance, associated to bot +bot+
  def initialize
    super

    @statickeywords = Hash.new
    @keywords = @registry.sub_registry('keywords') # DBTree.new bot, "keyword"
    upgrade_data

    scan

    # import old format keywords into DBHash
    if(File.exist?("#{@bot.botclass}/keywords.rbot"))
      log "auto importing old keywords.rbot"
      IO.foreach("#{@bot.botclass}/keywords.rbot") do |line|
        if(line =~ /^(.*?)\s*<=(is|are)?=?>\s*(.*)$/)
          lhs = $1
          mhs = $2
          rhs = $3
          mhs = "is" unless mhs
          rhs = Keyword.escape rhs
          values = rhs.split("<=or=>")
          @keywords[lhs] = Keyword.new(mhs, values).dump
        end
      end
      File.rename("#{@bot.botclass}/keywords.rbot", "#{@bot.botclass}/keywords.rbot.old")
    end
  end

  # drop static keywords and reload them from files, picking up any new
  # keyword files that have been added
  def rescan
    @statickeywords = Hash.new
    scan
  end

  # load static keywords from files, picking up any new keyword files that
  # have been added
  def scan
    # first scan for old DBHash files, and convert them
    Dir["#{@bot.botclass}/keywords/*"].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.each {|k,v|
        new[k] = v
      }
      old.close
      new.close
      File.delete(f)
    }

    # then scan for current DBTree files, and load them
    Dir["#{@bot.botclass}/keywords/*"].each {|f|
      next unless f =~ /\.kdb$/
      hsh = DBTree.new @bot, f, true
      key = File.basename(f).gsub(/\.kdb$/, "")
      debug "keywords module: loading DBTree file #{f}, key #{key}"
      @statickeywords[key] = hsh
    }

    # then scan for non DB files, and convert/import them and delete
    Dir["#{@bot.botclass}/keywords/*"].each {|f|
      next if f =~ /\.kdb$/
      next if f =~ /CVS$/
      log "auto converting keywords from #{f}"
      key = File.basename(f)
      unless @statickeywords.has_key?(key)
        @statickeywords[key] = DBHash.new @bot, "#{f}.db", true
      end
      IO.foreach(f) {|line|
        if(line =~ /^(.*?)\s*<?=(is|are)?=?>\s*(.*)$/)
          lhs = $1
          mhs = $2
          rhs = $3
          # support infobot style factfiles, by fixing them up here
          rhs.gsub!(/\$who/, "<who>")
          mhs = "is" unless mhs
          rhs = Keyword.escape rhs
          values = rhs.split("<=or=>")
          @statickeywords[key][lhs] = Keyword.new(mhs, values).dump
        end
      }
      File.delete(f)
      @statickeywords[key].flush
    }
  end

  # upgrade data files found in old rbot formats to current
  def upgrade_data
    if File.exist?("#{@bot.botclass}/keywords.db")
      log "upgrading old keywords (rbot 0.9.5 or prior) database format"
      old = BDB::Hash.open "#{@bot.botclass}/keywords.db", 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")
    end

    if File.exist?("#{@bot.botclass}/keyword.db")
      log "upgrading old keywords (rbot 0.9.9 or prior) database format"
      old = BDB::CIBtree.open "#{@bot.botclass}/keyword.db", 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")
    end
  end

  # save dynamic keywords to file
  def save
    @keywords.flush
  end

  def oldsave
    File.open("#{@bot.botclass}/keywords.rbot", "w") do |file|
      @keywords.each do |key, value|
        file.puts "#{key}<=#{value.type}=>#{value.dump}"
      end
    end
  end

  # 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])
    else
      # key name order for the lookup through these
      @statickeywords.keys.sort.each {|k|
        v = @statickeywords[k]
        if v.has_key?(key)
          return Keyword.restore(v[key])
        end
      }
    end
    return nil
  end

  # does +key+ exist as a keyword?
  def has_key?(key)
    if @keywords.has_key?(key) && Keyword.restore(@keywords[key]) != nil
      return true
    end
    @statickeywords.each {|k,v|
      if v.has_key?(key) && Keyword.restore(v[key]) != nil
        return true
      end
    }
    return false
  end

  # m::     PrivMessage containing message info
  # key::   key being queried
  # quiet:: optional, if false, complain if +key+ is not found
  #
  # handle a message asking about a keyword
  def keyword_lookup(m, key, quiet = false)
    return if key.nil?
    unless(kw = self[key])
      m.reply "sorry, I don't know about \"#{key}\"" unless quiet
      return
    end

    response = kw.to_s
    response.gsub!(/<who>/, m.sourcenick)

    if(response =~ /^<reply>\s*(.*)/)
      m.reply $1
    elsif(response =~ /^<action>\s*(.*)/)
      m.act $1
    elsif(m.public? && response =~ /^<topic>\s*(.*)/)
      @bot.topic m.target, $1
    else
      m.reply "#{key} #{kw.type} #{response}"
    end
  end


  # handle a message which alters a keyword
  # like "foo is bar" or "foo is also qux"
  def keyword_command(m, lhs, mhs, rhs, quiet = false)
    debug "got keyword command #{lhs}, #{mhs}, #{rhs}"
    return if lhs.strip.empty?

    overwrite = false
    overwrite = true if(lhs.gsub!(/^no,\s*/, ""))
    also = false
    also = true if(rhs.gsub!(/^also\s+/, ""))

    values = rhs.split(/\s+\|\s+/)
    lhs = Keyword.unescape lhs

    if(overwrite || also || !has_key?(lhs))
      if(also && has_key?(lhs))
        kw = self[lhs]
        kw << values
        @keywords[lhs] = kw.dump
      else
        @keywords[lhs] = Keyword.new(mhs, values).dump
      end
      m.okay if !quiet
    elsif(has_key?(lhs))
      kw = self[lhs]
      m.reply "but #{lhs} #{kw.type} #{kw.desc}" if kw && !quiet
    end
  end

  # return help string for Keywords with option topic +topic+
  def help(plugin, topic = '')
    case plugin
    when /keyword/
      case topic
      when 'lookup'
        'keyword [lookup] <keyword> => look up the definition for a keyword; writing "lookup" is optional'
      when 'set'
        'keyword set <keyword> is/are <definition> => define a keyword, definition can contain "|" to separate multiple randomly chosen replies'
      when 'forget'
        'keyword forget <keyword> => forget a keyword'
      when 'tell'
        'keyword tell <nick> about <keyword> => tell somebody about a keyword'
      when 'search'
        'keyword search [--all] [--full] <pattern> => search keywords for <pattern>, which can be a regular expression. If --all is set, search static keywords too, if --full is set, search definitions too.'
      when 'listen'
        'when the config option "keyword.listen" is set to false, rbot will try to extract keyword definitions from regular channel messages'
      when 'address'
        'when the config option "keyword.address" is set to true, rbot will try to answer channel questions of the form "<keyword>?"'
      when '<reply>'
        '<reply> => normal response is "<keyword> is <definition>", but if <definition> begins with <reply>, the response will be "<definition>"'
      when '<action>'
        '<action> => makes keyword respond with "/me <definition>"'
      when '<who>'
        '<who> => replaced with questioner in reply'
      when '<topic>'
        '<topic> => 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, <reply>, <action>, <who>, <topic>'
      end
    when "forget"
      'forget <keyword> => forget a keyword'
    when "tell"
      'tell <nick> about <keyword> => tell somebody about a keyword'
    when "learn"
      'learn that <keyword> is/are <definition> => define a keyword, definition can contain "|" to separate multiple randomly chosen replies'
    else
      'keyword module (fact learning and regurgitation) topics: lookup, set, forget, tell, search, listen, address, <reply>, <action>, <who>, <topic>'
    end
  end

  # handle a message asking the bot to tell someone about a keyword
  def keyword_tell(m, target, key)
    unless(kw = self[key])
      m.reply @bot.lang.get("dunno_about_X") % key
      return
    end
    if target == @bot.nick
      m.reply "very funny, trying to make me tell something to myself"
      return
    end

    response = kw.to_s
    response.gsub!(/<who>/, m.sourcenick)
    if(response =~ /^<reply>\s*(.*)/)
      @bot.say target, "#{m.sourcenick} wanted me to tell you: (#{key}) #$1"
      m.reply "okay, I told #{target}: (#{key}) #$1"
    elsif(response =~ /^<action>\s*(.*)/)
      @bot.action target, "#$1 (#{m.sourcenick} wanted me to tell you)"
      m.reply "okay, I told #{target}: * #$1"
    else
      @bot.say target, "#{m.sourcenick} wanted me to tell you that #{key} #{kw.type} #{response}"
      m.reply "okay, I told #{target} that #{key} #{kw.type} #{response}"
    end
  end

  # return the number of known keywords
  def keyword_stats(m)
    length = 0
    @statickeywords.each {|k,v|
      length += v.length
    }
    m.reply "There are currently #{@keywords.length} keywords, #{length} static facts defined."
  end

  # search for keywords, optionally also the definition and the static keywords
  def keyword_search(m, key, full = false, all = false, from = 1)
    begin
      if key =~ /^\/(.+)\/$/
        re = Regexp.new($1, Regexp::IGNORECASE)
      else
        re = Regexp.new(Regexp.escape(key), Regexp::IGNORECASE)
      end

      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 matches.length == 1
        rkw = matches[0]
        m.reply "#{rkw[0]} #{rkw[1].type} #{rkw[1].desc}"
      elsif matches.length > 0
        if from > matches.length
          m.reply "#{matches.length} found, can't tell you about #{from}"
          return
        end
        i = 1
        matches.each {|rkw|
          m.reply "[#{i}/#{matches.length}] #{rkw[0]} #{rkw[1].type} #{rkw[1].desc}" if i >= from
          i += 1
          break if i == from+@bot.config['keyword.search_results']
        }
      else
        m.reply "no keywords match #{key}"
      end
    rescue RegexpError => e
      m.reply "no keywords match #{key}: #{e}"
    rescue
      debug e.inspect
      m.reply "no keywords match #{key}: an error occurred"
    end
  end

  # forget one of the dynamic keywords
  def keyword_forget(m, key)
    if(@keywords.has_key?(key))
      @keywords.delete(key)
      @bot.okay m.replyto
    end
  end

  # privmsg handler
  def privmsg(m)
    case m.plugin
    when "keyword"
      case m.params
      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+(.+)$/
        keyword_forget(m, $1) if @bot.auth.allow?('keycmd', m.source, m.replyto)
      when /^lookup\s+(.+)$/
        keyword_lookup(m, $1) if @bot.auth.allow?('keyword', m.source, m.replyto)
      when /^stats\s*$/
        keyword_stats(m) if @bot.auth.allow?('keyword', m.source, m.replyto)
      when /^search\s+(.+)$/
        key = $1
        full = key.sub!('--full ', '')
        all = key.sub!('--all ', '')
        if key.sub!(/--from (\d+) /, '')
          from = $1.to_i
        else
          from = 1
        end
        from = 1 unless from > 0
        keyword_search(m, key, full, all, from) if @bot.auth.allow?('keyword', m.source, m.replyto)
      when /^tell\s+(\S+)\s+about\s+(.+)$/
        keyword_tell(m, $1, $2) if @bot.auth.allow?('keyword', m.source, m.replyto)
      else
        keyword_lookup(m, m.params) if @bot.auth.allow?('keyword', m.source, m.replyto)
      end
    when "forget"
      keyword_forget(m, m.params) if @bot.auth.allow?('keycmd', m.source, m.replyto)
    when "tell"
      if m.params =~ /(\S+)\s+about\s+(.+)$/
        keyword_tell(m, $1, $2) if @bot.auth.allow?('keyword', m.source, m.replyto)
      else
        m.reply "wrong 'tell' syntax"
      end
    when "learn"
      if m.params =~ /^that\s+(.+?)\s+(is|are)\s+(.+)$/
        keyword_command(m, $1, $2, $3) if @bot.auth.allow?('keycmd', m.source, m.replyto)
      else
        m.reply "wrong 'learn' syntax"
      end
    end
  end

  def listen(m)
    return if m.address?
    # in channel message, not to me
    # TODO option to do if(m.message =~ /^(.*)$/, ie try any line as a
    # keyword lookup.
    if !@bot.config["keyword.address"] && m.message =~ /^(.*\S)\s*\?\s*$/
      keyword_lookup m, $1, true if @bot.auth.allow?("keyword", m.source)
    elsif @bot.config["keyword.listen"] && (m.message =~ /^(.*?)\s+(is|are)\s+(.*)$/)
      # TODO MUCH more selective on what's allowed here
      keyword_command m, $1, $2, $3, true if @bot.auth.allow?("keycmd", m.source)
    end
  end
end

plugin = Keywords.new
plugin.register 'keyword'
plugin.register 'forget'
plugin.register 'tell'
plugin.register 'learn'