class Balotelli::Base

Public Class Methods

inherited(subclass) click to toggle source
Calls superclass method
# File lib/balotelli/base.rb, line 78
def inherited(subclass)
  super

  subclass.setup
end
mod_match(str, priv = false) click to toggle source
# File lib/balotelli/base.rb, line 99
def mod_match(str, priv = false)
  if str =~ /\A[^a-zA-Z0-9\s]?(\S+) ?(.*)/
    if (mod = @cache[:modules][Regexp.last_match(1).downcase])
      mod.match(Regexp.last_match(2), priv)
    end
  elsif str == :join
    match = nil
    @modules.each do |_, m|
      if (match = m.match(:join))
        break(match)
      end
    end
    match
  end
end
new(block, message, matchdata) click to toggle source
# File lib/balotelli/base.rb, line 18
def initialize(block, message, matchdata)
  @message = message
  @block = block
  @matchdata = matchdata
end
register(mod, namespace = nil) click to toggle source
# File lib/balotelli/base.rb, line 84
def register(mod, namespace = nil)
  if namespace
    mod.instance_variable_set(:@commands_namespace, namespace.intern)
    mod.instance_exec(namespace) do |n|
      define_method(:commands_namespace) { n.intern }
    end
  end
  @modules[mod.commands_namespace] ||= mod
  @cache[:modules] = Hash[@modules.map { |k, v| [k.to_s, v] }]
end
register_special(mod) click to toggle source
# File lib/balotelli/base.rb, line 95
def register_special(mod)
  @routes.merge!(mod.instance_variable_get(:@routes))
end
run() click to toggle source
# File lib/balotelli/base.rb, line 115
def run
  while (str = sgets)
    process(str)
  end
end
setup() click to toggle source
# File lib/balotelli/base.rb, line 69
def setup
  setup_router

  @pool = Thread::Pool.new(Thread::Pool.cpu_count)
  @modules = {}
  @cache = Hash.new { |hash, key| hash[key] = {} }
  @cvs = {}
end

Private Class Methods

new_response(user, channel, message, route_match, klass, priv = nil) click to toggle source
# File lib/balotelli/base.rb, line 158
def new_response(user, channel, message, route_match, klass, priv = nil)
  message = klass.new(*[user, channel, message, priv].compact)
  new(route_match.block, message, route_match.match_data).tap do |response|
    response.extend(route_match.mod_name) if route_match.mod_name.class == ::Module
  end.execute!
end
process(str) click to toggle source
# File lib/balotelli/base.rb, line 123
def process(str)
  if str =~ /\APING (.*)\z/
    pong Regexp.last_match(1)
  elsif @on_connect && str =~ %r{End of \/MOTD command}i
    instance_eval(&remove_instance_variable(:@on_connect))
  elsif !(str =~ /(JOIN\s|PRIVMSG\s)/)
    process_table(str)
  else
    @pool.process { process_message(str) }
  end
end
process_join(str, user, channel) click to toggle source
# File lib/balotelli/base.rb, line 151
def process_join(str, user, channel)
  if (matched = match(:join)) || (matched = mod_match(:join))
    new_response(user, channel, nil, matched, Core::Join)
  end
end
process_message(str) click to toggle source
# File lib/balotelli/base.rb, line 135
def process_message(str)
  if str =~ Core::Utils::PRIV_MSG_REGEX
    process_private_message(str, *Regexp.last_match[1..3])
  elsif str =~ Core::Utils::JOIN_REGEX
    process_join(str, *Regexp.last_match[1..2])
  end
end
process_private_message(str, user, channel, message) click to toggle source
# File lib/balotelli/base.rb, line 143
def process_private_message(str, user, channel, message)
  privacy = !(channel =~ /#.+/)
  if (matched = match(message, privacy)) ||
     (matched = mod_match(message, privacy))
    new_response(user, channel, message, matched, Core::PrivMsg, privacy)
  end
end
process_table(string) click to toggle source
# File lib/balotelli/base.rb, line 165
def process_table(string)
  if @cvs.any? && string =~ (regex = Core::Utils.table_regex(@nick))
    channel = Regexp.last_match(2)
    users = Regexp.last_match(3)
    until (str = sgets) =~ %r{End of \/NAME}i
      users << " #{str.match(regex).captures.first}"
    end
    @cache[:user_list][channel] = users.split(' ')
    signal_cvs(channel)
  end
end
signal_cvs(channel) click to toggle source
# File lib/balotelli/base.rb, line 177
def signal_cvs(channel)
  @cvs[channel].shift.signal
  @cvs.delete(channel) if @cvs[channel].empty?
end

Public Instance Methods

execute()
Alias for: execute!
execute!() click to toggle source
# File lib/balotelli/base.rb, line 24
def execute!
  instance_exec(@message, @matchdata, &@block)
end
Also aliased as: execute
get_user_list(channel) click to toggle source
# File lib/balotelli/base.rb, line 38
def get_user_list(channel)
  execute_in_mutex(channel) do
    self.class.names(channel)
  end
  cache[:user_list][channel]
end
kick(params) click to toggle source
# File lib/balotelli/base.rb, line 34
def kick(params)
  Core::Kick.new(params, self.class).execute!
end
method_missing(method, *args, **params, &block) click to toggle source
Calls superclass method
# File lib/balotelli/base.rb, line 57
def method_missing(method, *args, **params, &block)
  if method =~ /__([[[:upper:]]_]+)__/
    self.class.instance_variable_get("@#{Regexp.last_match(1)}".downcase)
  else
    super
  end
end
out(message_string, destination = nil) click to toggle source
# File lib/balotelli/base.rb, line 30
def out(message_string, destination = nil)
  self.class.privmsg((destination || @message.responder), message_string)
end

Private Instance Methods

execute_in_mutex(channel) { || ... } click to toggle source
# File lib/balotelli/base.rb, line 45
def execute_in_mutex(channel)
  cv = ConditionVariable.new
  if cvs.key? channel
    cvs[channel] << cv
  else
    cvs[channel] = [cv]
  end
  yield
  mutex.synchronize { cv.wait(mutex) }
end