class BeerBot::Kernel

Constants

BotMsg

BotMsg’s are hashes or arrays of hashes and represent a bot response (usually to some input eg from an irc server).

The hash is generally expected to have the following keys:

:msg
:to

but may carry additional ones like

:action (will be used instead of :msg)

The array-form of the botmsg might look like this:

[msg:'ho!',to:'#chan1']

and it is easy to add extra messages this way:

[msg:'ho!',to:'#chan1'] + [msg:'ho again!',to:'#chan1']

etc

InOut

Represents a thread that waits on an in-queue, processes any things received and puts them on an out-queue.

Utils

Attributes

bot[RW]
codec[RW]
config[RW]
conn[RW]
dispatcher[RW]
scheduler[RW]

Public Class Methods

new(config, conn, codec, bot, dispatcher, scheduler) click to toggle source

Initialize all parts of the system here.

config should be a hash, normally BeerBot::Config.

Note BeerBot::Config should be loaded before we initialize here.

# File lib/beerbot/kernel.rb, line 40
def initialize config, conn, codec, bot, dispatcher, scheduler

  @echo = true
  @path = File.expand_path(File.dirname(__FILE__)+'/..')
  @module_path = config['moduledir']
  @config = config
  @bot = bot
  @conn = conn
  @codec = codec
  @dispatcher = dispatcher
  @scheduler = scheduler


  # Dispatcher thread takes stuff coming from the connection and does
  # something with it...

  @dispatcher_thread = InOut.new(inq:@conn.queue,outq:@conn.writeq) {|input|
    str,raw = input
    event,*args = @codec.decode(str)
    replies = @dispatcher.receive(event,args)
    @codec.encode(replies)
  }
  @dispatcher_thread.start!

  # Schedule dispatcher thread.
  #
  # These are responses that were prepared and scheduled earlier and
  # which also need to be dispatched.

  @scheduler_thread = InOut.new(inq:@scheduler.queue,outq:@conn.writeq) {|cron_job|
    puts "<< scheduler #{cron_job.inspect}" if @echo
    puts "<< scheduler #{@scheduler.time}" if @echo
    @codec.encode(cron_job.run)
  }
  @scheduler_thread.start!

  # Active messaging queue.
  #
  # 'config' will be injected into bot modules.
  # config.out should be a queue that we can dequeue.

  @active_thread = InOut.new(inq:@config.out,outq:@conn.writeq) {|replies|
    puts "<< active #{replies}" if @echo
    # TODO: almost identical logic in the dispatcher class (in
    # @dispatcher_thread).
    case replies
    when String # assume protocol string eg irc
      replies
    when Hash,Array,Proc
      @codec.encode(BotMsg.to_a(replies))
    else
      []
    end
  }
  @active_thread.start!

  # Set up a repl in a separate thread.
  #
  # In pry, you can then do:
  #   @conn.writeq.enq @codec.join('#chan1')
  #   @conn.write @codec.join('#chan1')

  Pry.config.prompt = Proc.new {|_| "pry> "}
  @pry_thread = Thread.new {
    binding.pry
  }

  # Initialize bot and its modules...

  @bot.init(@config)
  @bot.update_config(@config)

  # Do stuff once we've identified with the server...
  #
  # Join channels.
  # Start the scheduler.

  @conn.ready? {
    channels = config['channels']
    if channels then
      channels.each{|chan|
        self.join(chan)
      }
    end
    @scheduler.start
  }
end

Public Instance Methods

action(to,msg) click to toggle source

Convenience method to do something (/me).

# File lib/beerbot/kernel.rb, line 151
def action to,msg
  @conn.writeq.enq(@codec.action(to,msg))
end
echo() click to toggle source

Toggle whether inputs and outputs show on the repl screen.

Call this from the pry repl.

# File lib/beerbot/kernel.rb, line 132
def echo
  @echo = !@echo
  @conn.echo = @echo
end
join(chan) click to toggle source

Convenience method to join a channel.

# File lib/beerbot/kernel.rb, line 157
def join chan
  @conn.writeq.enq(@codec.join(chan))
end
leave(chan) click to toggle source

Convenience method to leave a channel.

# File lib/beerbot/kernel.rb, line 163
def leave chan
  @conn.writeq.enq(@codec.leave(chan))
end
say(to,msg) click to toggle source

Convenience method to say something to channel or someone.

# File lib/beerbot/kernel.rb, line 145
def say to,msg
  @conn.writeq.enq(@codec.msg(to,msg))
end
start() click to toggle source

Start the connection.

# File lib/beerbot/kernel.rb, line 139
def start
  @conn.open.join
end