Net::IRC::Client (Class)
# File lib/net/irc/client.rb, line 8
8: def initialize(host, port, opts={})
9: @host = host
10: @port = port
11: @opts = OpenStruct.new(opts)
12: @log = @opts.logger || Logger.new($stdout)
13: @server_config = Message::ServerConfig.new
14: @channels = {
15: # "#channel" => {
16: # :modes => [],
17: # :users => [],
18: # }
19: }
20: @channels.extend(MonitorMixin)
21: end
Close connection to server.
# File lib/net/irc/client.rb, line 53
53: def finish
54: begin
55: @socket.close
56: rescue
57: end
58: on_disconnected
59: end
Do nothing. This is for avoiding error on calling super. So you can always call super at subclass.
# File lib/net/irc/client.rb, line 86
86: def method_missing(name, *args)
87: end
Call when socket connected.
# File lib/net/irc/client.rb, line 90
90: def on_connected
91: end
Call when socket closed.
# File lib/net/irc/client.rb, line 94
94: def on_disconnected
95: end
Catch all messages. If this method return true, aother callback will not be called.
# File lib/net/irc/client.rb, line 63
63: def on_message(m)
64: end
Default PING callback. Response PONG.
# File lib/net/irc/client.rb, line 79
79: def on_ping(m)
80: post PONG, @prefix ? @prefix.nick : ""
81: end
Default RPL_ISUPPORT callback. This detects server‘s configurations.
# File lib/net/irc/client.rb, line 74
74: def on_rpl_isupport(m)
75: @server_config.set(m)
76: end
Default RPL_WELCOME callback. This sets @prefix from the message.
# File lib/net/irc/client.rb, line 68
68: def on_rpl_welcome(m)
69: @prefix = Prefix.new(m[1][/\S+$/])
70: end
Connect to server and start loop.
# File lib/net/irc/client.rb, line 24
24: def start
25: # reset config
26: @server_config = Message::ServerConfig.new
27: @socket = TCPSocket.open(@host, @port)
28: on_connected
29: post PASS, @opts.pass if @opts.pass
30: post NICK, @opts.nick
31: post USER, @opts.user, "0", "*", @opts.real
32: while l = @socket.gets
33: begin
34: @log.debug "RECEIVE: #{l.chomp}"
35: m = Message.parse(l)
36: next if on_message(m) === true
37: name = "on_#{(COMMANDS[m.command.upcase] || m.command).downcase}"
38: send(name, m) if respond_to?(name)
39: rescue Exception => e
40: warn e
41: warn e.backtrace.join("\r\t")
42: raise
43: rescue Message::InvalidMessage
44: @log.error "MessageParse: " + l.inspect
45: end
46: end
47: rescue IOError
48: ensure
49: finish
50: end