In Files

Parent

Cinch::IRC

Attributes

isupport[R]

@return [ISupport]

Public Class Methods

new(bot) click to toggle source
# File lib/cinch/irc.rb, line 8
def initialize(bot)
  @bot      = bot
  @isupport = ISupport.new
end

Public Instance Methods

connect() click to toggle source

Establish a connection.

@return [void]

# File lib/cinch/irc.rb, line 16
def connect
  @registration = []

  @whois_updates = Hash.new {|h, k| h[k] = {}}
  @in_lists      = Set.new

  tcp_socket = nil
  begin
    Timeout::timeout(@bot.config.timeouts.connect) do
      tcp_socket = TCPSocket.new(@bot.config.server, @bot.config.port, @bot.config.local_host)
    end
  rescue Timeout::Error
    @bot.logger.debug("Timed out while connecting")
    return
  rescue => e
    @bot.logger.log_exception(e)
    return
  end

  if @bot.config.ssl == true || @bot.config.ssl == false
    @bot.logger.debug "Deprecation warning: Beginning from version 1.1.0, @config.ssl should be a set of options, not a boolean value!"
  end

  if @bot.config.ssl == true || (@bot.config.ssl.is_a?(OpenStruct) && @bot.config.ssl.use)
    require 'openssl'

    ssl_context = OpenSSL::SSL::SSLContext.new

    if @bot.config.ssl.is_a?(OpenStruct)
      if @bot.config.ssl.client_cert
        ssl_context.cert = OpenSSL::X509::Certificate.new(File.read(@bot.config.ssl.client_cert))
        ssl_context.key = OpenSSL::PKey::RSA.new(File.read(@bot.config.ssl.client_cert))
      end
      ssl_context.ca_path = @bot.config.ssl.ca_path
      ssl_context.verify_mode = @bot.config.ssl.verify ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
    else
      ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end
    @bot.logger.debug "Using SSL with #{@bot.config.server}:#{@bot.config.port}"

    @socket = OpenSSL::SSL::SSLSocket.new(tcp_socket, ssl_context)
    @socket.sync = true
    @socket.connect
  else
    @socket = tcp_socket
  end

  @socket = Net::BufferedIO.new(@socket)
  @socket.read_timeout = @bot.config.timeouts.read

  @queue = MessageQueue.new(@socket, @bot)
  message "PASS #{@bot.config.password}" if @bot.config.password
  message "NICK #{@bot.generate_next_nick}"
  message "USER #{@bot.config.user} 0 * :#{@bot.config.realname}"

  reading_thread = Thread.new do
    begin
      while line = @socket.readline
        begin
          line = Cinch.encode_incoming(line, @bot.config.encoding)
          parse line
        rescue => e
          @bot.logger.log_exception(e)
        end
      end
    rescue Timeout::Error
      @bot.logger.debug "Connection timed out."
    rescue EOFError
      @bot.logger.debug "Lost connection."
    rescue => e
      @bot.logger.log_exception(e)
    end

    @socket.close
    @bot.dispatch(:disconnect)
    @bot.handler_threads.each { |t| t.join(10); t.kill }
  end

  @sending_thread = Thread.new do
    begin
      @queue.process!
    rescue => e
      @bot.logger.log_exception(e)
    end
  end

  ping_thread = Thread.new do
    while true
      sleep @bot.config.ping_interval
      message("PING 0") # PING requires a single argument. In our
                        # case the value doesn't matter though.
    end
  end

  reading_thread.join
  @sending_thread.kill
  ping_thread.kill
end
message(msg) click to toggle source

Send a message. @return [void]

# File lib/cinch/irc.rb, line 165
def message(msg)
  @queue.queue(msg)
end
parse(input) click to toggle source

@api private @return [void]

# File lib/cinch/irc.rb, line 117
def parse(input)
  @bot.logger.log(input, :incoming) if @bot.config.verbose
  msg          = Message.new(input, @bot)
  events       = [[:catchall]]

  if ("001".."004").include? msg.command
    @registration << msg.command
    if registered?
      events << [:connect]
      @bot.last_connection_was_successful = true
    end
  elsif ["PRIVMSG", "NOTICE"].include?(msg.command)
    events << [:ctcp] if msg.ctcp?
    if msg.channel?
      events << [:channel]
    else
      events << [:private]
    end

    if msg.command == "PRIVMSG"
      events << [:message] << [:privmsg]
    else
      events << [:notice]
    end
  else
    meth = "on_#{msg.command.downcase}"
    __send__(meth, msg, events) if respond_to?(meth, true)

    if msg.error?
      events << [:error]
    else
      events << [msg.command.downcase.to_sym]
    end
  end

  msg.instance_variable_set(:@events, events.map(&:first))
  events.each do |event, *args|
    @bot.dispatch(event, msg, *args)
  end
end
registered?() click to toggle source

@return [Boolean] true if we successfully registered yet

# File lib/cinch/irc.rb, line 159
def registered?
  (("001".."004").to_a - @registration).empty?
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.