Class: Miu::Nodes::IRC::Connection

Inherits:
Object
  • Object
show all
Includes:
Celluloid::IO
Defined in:
lib/miu/nodes/irc/connection.rb

Direct Known Subclasses

Client

Constant Summary collapse

BUFFER_SIZE =
1024 * 4

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options)
  @host = options[:host]
  @port = options[:port] || 6667
  @nick = options[:nick]
  @user = options[:user] || @nick
  @real = options[:real] || @nick
  @pass = options[:pass]
  @encoding = options[:encoding] || 'UTF-8'

  @socket = TCPSocket.new @host, @port
end

Instance Attribute Details

#encodingObject (readonly)

Returns the value of attribute encoding.



15
16
17
# File 'lib/miu/nodes/irc/connection.rb', line 15

def encoding
  @encoding
end

#hostObject (readonly)

Returns the value of attribute host.



13
14
15
# File 'lib/miu/nodes/irc/connection.rb', line 13

def host
  @host
end

#nickObject (readonly)

Returns the value of attribute nick.



14
15
16
# File 'lib/miu/nodes/irc/connection.rb', line 14

def nick
  @nick
end

#passObject (readonly)

Returns the value of attribute pass.



14
15
16
# File 'lib/miu/nodes/irc/connection.rb', line 14

def pass
  @pass
end

#portObject (readonly)

Returns the value of attribute port.



13
14
15
# File 'lib/miu/nodes/irc/connection.rb', line 13

def port
  @port
end

#realObject (readonly)

Returns the value of attribute real.



14
15
16
# File 'lib/miu/nodes/irc/connection.rb', line 14

def real
  @real
end

#socketObject (readonly)

Returns the value of attribute socket.



16
17
18
# File 'lib/miu/nodes/irc/connection.rb', line 16

def socket
  @socket
end

#userObject (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

#attachObject



66
67
68
69
70
# File 'lib/miu/nodes/irc/connection.rb', line 66

def attach
  send_message 'PASS', @pass if @pass
  send_message 'NICK', @nick
  send_message 'USER', @user, '*', '*', @real
end

#closeObject



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 on_message(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

#runObject



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}"
      on_message msg
    end
  end
end

#send_message(*args) ⇒ Object



34
35
36
37
38
# File 'lib/miu/nodes/irc/connection.rb', line 34

def send_message(*args)
  msg = Ircp::Message.new(*args)
  Miu::Logger.debug "<< #{msg.to_s.strip}"
  @socket.write msg.to_irc
end