class Daneel::Bot

Attributes

adapter[R]
data[R]
debug_mode[RW]
full_name[R]
logger[R]
name[R]
scripts[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/daneel/bot.rb, line 10
def initialize(options = {})
  @logger = options[:logger] || Daneel::Logger.new
  @name = options[:name] || "daneel"
  @full_name = options[:full_name] || options[:name] || "R. Daneel Olivaw"
  @debug_mode = options[:verbose] && options[:adapter] && options[:adapter] != "shell"

  @data = Data.new
  logger.debug "Data source #{data.class}"

  Script.files.each{|file| try_require file }
  # TODO add script priorities to replicate this
  list = Script.list
  list.push list.delete(Scripts::ImageSearch)
  list.push list.delete(Scripts::Chatty)
  @scripts = list.map{|s| s.new(self) }
  logger.debug "Booted with scripts: #{@scripts.map(&:class).inspect}"

  @adapter = Adapter.named(options[:adapter] || "shell").new(self)
  logger.debug "Using the #{adapter.class} adapter"
end

Public Instance Methods

inspect() click to toggle source
# File lib/daneel/bot.rb, line 68
def inspect
  %|#<#{self.class}:#{object_id} @name="#{name}" @adapter=#{adapter.class}>|
end
receive(room, message, user) click to toggle source
# File lib/daneel/bot.rb, line 31
def receive(room, message, user)
  logger.debug "[room #{room.id}] #{user.name}: #{message.text}"
  message.command = command_from(message.text)

  scripts.each do |script|
    next unless script.accepts?(room, message, user)
    script.receive(room, message, user)
    break if message.done
  end
  message
rescue => e
  msg = %|#{e.class}: #{e.message}\n  #{e.backtrace.join("\n  ")}|
  logger.error msg
  adapter.announce "crap, something went wrong. :(", msg if @debug_mode
end
run() click to toggle source
# File lib/daneel/bot.rb, line 47
def run
  # TODO add i18n so that people can customize their bot's attitude
  # http://titusd.co.uk/2010/03/04/i18n-internationalization-without-rails/
  # TODO add Confabulator processing so the bot can be chatty without being static

  # Heroku cycles every process at least once per day by sending it a TERM
  trap(:TERM) do
    @adapter.announce "asked to stop, brb"
    exit
  end

  @adapter.announce "hey guys"
  @adapter.run
rescue Interrupt
  adapter.leave
end
user() click to toggle source
# File lib/daneel/bot.rb, line 64
def user
  @adapter.me
end

Private Instance Methods

command_from(text) click to toggle source
# File lib/daneel/bot.rb, line 74
def command_from(text)
  return if text.nil? || text.empty?
  m = text.match(/^@#{name}\s+(.*)/i)
  m ||= text.match(/^#{name}(?:[,:]\s*|\s+)(.*)/i)
  m ||= text.match(/^\s*(.*?)(?:,\s*)?\b#{name}[.!?\s]*$/i)
  m && m[1]
end
try_require(name) click to toggle source
# File lib/daneel/bot.rb, line 82
def try_require(name)
  require name
rescue Script::DepError => e
  logger.warn "Couldn't load #{File.basename(name)}: #{e.message}"
end