Report abuse

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
=begin
    1. robust
        a) ensure data on disk is always in a good state (atomic saving), ensure disk is relatively up to date with state
        b) ensure bot gracefully reconnects if kicked etc.
    2. async. session-based plug-ins
        a) each time a plug-in is invoked, it is done so in a thread, and this plug-in can read() from the channel (whcih by default is filtered to the user initiating the command, but this filter could be changed by plug-in)
        b) plug-in can have a main() or similar and should be allowed to post to the channel w/o being invoked (e.g. RSS feed)
    3. data schema: bot should log all data in a nice relational database or similar and provide an API to query this (with JOINs and SELECTs)
=end

def main
  line = read_line_from_channel()
  if plugin = plugins.find { |p| p.can_handle? line }
    Thread.new { plugin.new(line, channel, db, io) }
  end
end

class SeenPlugIn
  def initialize(line, channel, db, io)
    author = $1 if line =~ /\$seen (\w+)/
    if res = db.query("SELECT date FROM messages WHERE author = #{line.author.nick} ORDER BY date DESC LIMIT 1")
      io << "#{author} was last seen #{res['date']}"
    else
      authors = db.query("SELECT nick FROM authors")
      candidates = sort_by_edit_distance(author, authors)
      candidates.each do |candidate|
        io << "did you mean #{candidate}?"
        if io.read == 'yes'break
        end
      end
    end
  endend