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 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
Public Instance Methods
finish()
click to toggle source
Close connection to server.
# File lib/net/irc/client.rb 53 def finish 54 begin 55 @socket.close 56 rescue 57 end 58 on_disconnected 59 end
on_connected()
click to toggle source
Call when socket connected.
# File lib/net/irc/client.rb 84 def on_connected 85 end
on_disconnected()
click to toggle source
Call when socket closed.
# File lib/net/irc/client.rb 88 def on_disconnected 89 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 63 def on_message(m) 64 end
on_ping(m)
click to toggle source
Default PING callback. Response PONG.
# File lib/net/irc/client.rb 79 def on_ping(m) 80 post PONG, @prefix ? @prefix.nick : "" 81 end
on_rpl_isupport(m)
click to toggle source
Default RPL_ISUPPORT callback. This detects server’s configurations.
# File lib/net/irc/client.rb 74 def on_rpl_isupport(m) 75 @server_config.set(m) 76 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 68 def on_rpl_welcome(m) 69 @prefix = Prefix.new(m[1][/\S+\z/]) 70 end
start()
click to toggle source
Connect to server and start loop.
# File lib/net/irc/client.rb 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
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 97 def post(command, *params) 98 m = Message.new(nil, command, params.map {|s| 99 if s 100 t = s.dup.force_encoding("ASCII-8BIT") if s.respond_to? :force_encoding 101 #s.gsub(/\r\n|[\r\n]/, " ") 102 t.tr("\r\n", " ") 103 else 104 "" 105 end 106 }) 107 108 @log.debug "SEND: #{m.to_s.chomp}" 109 @socket << m 110 end