class Net::IRC::Client

Attributes

channels[R]
host[R]
opts[R]
port[R]
prefix[R]

Public Class Methods

new(host, port, opts={}) click to toggle source
# File lib/net/irc/client.rb, line 8
        def initialize(host, port, opts={})
                @host          = host
                @port          = port
                @opts          = OpenStruct.new(opts)
                @log           = @opts.logger || Logger.new($stdout)
                @server_config = Message::ServerConfig.new
                @channels = {
#                       "#channel" => {
#                               :modes => [],
#                               :users => [],
#                       }
                }
                @channels.extend(MonitorMixin)
        end

Public Instance Methods

finish() click to toggle source

Close connection to server.

# File lib/net/irc/client.rb, line 53
def finish
        begin
                @socket.close
        rescue
        end
        on_disconnected
end
on_connected() click to toggle source

Call when socket connected.

# File lib/net/irc/client.rb, line 84
def on_connected
end
on_disconnected() click to toggle source

Call when socket closed.

# File lib/net/irc/client.rb, line 88
def on_disconnected
end
on_message(m) click to toggle source

Catch all messages. If this method return true, aother callback will not be called.

# File lib/net/irc/client.rb, line 63
def on_message(m)
end
on_ping(m) click to toggle source

Default PING callback. Response PONG.

# File lib/net/irc/client.rb, line 79
def on_ping(m)
        post PONG, @prefix ? @prefix.nick : ""
end
on_rpl_isupport(m) click to toggle source

Default RPL_ISUPPORT callback. This detects server's configurations.

# File lib/net/irc/client.rb, line 74
def on_rpl_isupport(m)
        @server_config.set(m)
end
on_rpl_welcome(m) click to toggle source

Default RPL_WELCOME callback. This sets @prefix from the message.

# File lib/net/irc/client.rb, line 68
def on_rpl_welcome(m)
        @prefix = Prefix.new(m[1][/\S+\z/])
end
start() click to toggle source

Connect to server and start loop.

# File lib/net/irc/client.rb, line 24
def start
        # reset config
        @server_config = Message::ServerConfig.new
        @socket = TCPSocket.open(@host, @port)
        on_connected
        post PASS, @opts.pass if @opts.pass
        post NICK, @opts.nick
        post USER, @opts.user, "0", "*", @opts.real
        while l = @socket.gets
                begin
                        @log.debug "RECEIVE: #{l.chomp}"
                        m = Message.parse(l)
                        next if on_message(m) === true
                        name = "on_#{(COMMANDS[m.command.upcase] || m.command).downcase}"
                        send(name, m) if respond_to?(name)
                rescue Exception => e
                        warn e
                        warn e.backtrace.join("\r\t")
                        raise
                rescue Message::InvalidMessage
                        @log.error "MessageParse: " + l.inspect
                end
        end
rescue IOError
ensure
        finish
end

Private Instance Methods

post(command, *params) click to toggle source

Post message to server.

include Net::IRC::Constants
post PRIVMSG, "#channel", "foobar"
# File lib/net/irc/client.rb, line 97
def post(command, *params)
        m = Message.new(nil, command, params.map {|s|
                if s
                        t = s.dup.force_encoding("ASCII-8BIT") if s.respond_to? :force_encoding
                        #s.gsub(/\r\n|[\r\n]/, " ")
                        t.tr("\r\n", " ")
                else
                        ""
                end
        })

        @log.debug "SEND: #{m.to_s.chomp}"
        @socket << m
end