Class: Miu::Nodes::IRC::Connection
- Inherits:
-
Object
- Object
- Miu::Nodes::IRC::Connection
- Includes:
- Celluloid::IO
- Defined in:
- lib/miu/nodes/irc/connection.rb
Direct Known Subclasses
Constant Summary collapse
- BUFFER_SIZE =
1024 * 4
Instance Attribute Summary collapse
-
#encoding ⇒ Object
readonly
Returns the value of attribute encoding.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#nick ⇒ Object
readonly
Returns the value of attribute nick.
-
#pass ⇒ Object
readonly
Returns the value of attribute pass.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#real ⇒ Object
readonly
Returns the value of attribute real.
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
- #attach ⇒ Object
- #close ⇒ Object
- #encode(str, encoding) ⇒ Object
-
#initialize(options) ⇒ Connection
constructor
A new instance of Connection.
- #on_message(msg) ⇒ Object
- #readlines(buffer_size = BUFFER_SIZE, &block) ⇒ Object
- #run ⇒ Object
- #send_message(*args) ⇒ Object
Constructor Details
#initialize(options) ⇒ Connection
Returns a new instance of Connection.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/miu/nodes/irc/connection.rb', line 18 def initialize() @host = [:host] @port = [:port] || 6667 @nick = [:nick] @user = [:user] || @nick @real = [:real] || @nick @pass = [:pass] @encoding = [:encoding] || 'UTF-8' @socket = TCPSocket.new @host, @port end |
Instance Attribute Details
#encoding ⇒ Object (readonly)
Returns the value of attribute encoding.
15 16 17 |
# File 'lib/miu/nodes/irc/connection.rb', line 15 def encoding @encoding end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
13 14 15 |
# File 'lib/miu/nodes/irc/connection.rb', line 13 def host @host end |
#nick ⇒ Object (readonly)
Returns the value of attribute nick.
14 15 16 |
# File 'lib/miu/nodes/irc/connection.rb', line 14 def nick @nick end |
#pass ⇒ Object (readonly)
Returns the value of attribute pass.
14 15 16 |
# File 'lib/miu/nodes/irc/connection.rb', line 14 def pass @pass end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
13 14 15 |
# File 'lib/miu/nodes/irc/connection.rb', line 13 def port @port end |
#real ⇒ Object (readonly)
Returns the value of attribute real.
14 15 16 |
# File 'lib/miu/nodes/irc/connection.rb', line 14 def real @real end |
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
16 17 18 |
# File 'lib/miu/nodes/irc/connection.rb', line 16 def socket @socket end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
14 15 16 |
# File 'lib/miu/nodes/irc/connection.rb', line 14 def user @user end |
Instance Method Details
#attach ⇒ Object
66 67 68 69 70 |
# File 'lib/miu/nodes/irc/connection.rb', line 66 def attach 'PASS', @pass if @pass 'NICK', @nick 'USER', @user, '*', '*', @real end |
#close ⇒ Object
30 31 32 |
# File 'lib/miu/nodes/irc/connection.rb', line 30 def close @socket.close if @socket && !@socket.closed? end |
#encode(str, encoding) ⇒ Object
61 62 63 64 |
# File 'lib/miu/nodes/irc/connection.rb', line 61 def encode(str, encoding) str.force_encoding(encoding).encode!(:invalid => :replace, :undef => :replace) str.chars.select { |c| c.valid_encoding? }.join end |
#on_message(msg) ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/miu/nodes/irc/connection.rb', line 72 def (msg) begin method = "on_#{msg.command.to_s.downcase}" __send__ method, msg if respond_to? method rescue => e Miu::Logger.exception e end end |
#readlines(buffer_size = BUFFER_SIZE, &block) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/miu/nodes/irc/connection.rb', line 51 def readlines(buffer_size = BUFFER_SIZE, &block) readbuf = ''.force_encoding('ASCII-8BIT') loop do readbuf << @socket.readpartial(buffer_size).force_encoding('ASCII-8BIT') while data = readbuf.slice!(/.+\r\n/) block.call encode(data, @encoding) end end end |
#run ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/miu/nodes/irc/connection.rb', line 40 def run attach readlines do |data| msg = Ircp.parse data rescue nil if msg Miu::Logger.debug ">> #{msg.to_s.strip}" msg end end end |
#send_message(*args) ⇒ Object
34 35 36 37 38 |
# File 'lib/miu/nodes/irc/connection.rb', line 34 def (*args) msg = Ircp::Message.new(*args) Miu::Logger.debug "<< #{msg.to_s.strip}" @socket.write msg.to_irc end |