Class: Raibo::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/raibo/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server, opts = {}) ⇒ Connection

Returns a new instance of Connection.



5
6
7
8
9
10
11
# File 'lib/raibo/connection.rb', line 5

def initialize(server, opts={})
  @server   = server
  @port     = opts[:port]     || 6667
  @nick     = opts[:nick]     || 'Raibo'
  @channel  = opts[:channel]  || '#raibo'
  @verbose  = !!opts[:verbose]
end

Instance Attribute Details

#channelObject

Returns the value of attribute channel.



3
4
5
# File 'lib/raibo/connection.rb', line 3

def channel
  @channel
end

#nick(nick) ⇒ Object

Returns the value of attribute nick.



3
4
5
# File 'lib/raibo/connection.rb', line 3

def nick
  @nick
end

#portObject

Returns the value of attribute port.



3
4
5
# File 'lib/raibo/connection.rb', line 3

def port
  @port
end

#serverObject

Returns the value of attribute server.



3
4
5
# File 'lib/raibo/connection.rb', line 3

def server
  @server
end

Instance Method Details

#closeObject



30
31
32
# File 'lib/raibo/connection.rb', line 30

def close
  @connection.close if @connection
end

#handle_linesObject



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/raibo/connection.rb', line 34

def handle_lines
  while (line = @connection.gets)
    puts "--> #{line}" if @verbose

    if line =~ /^PING (.*)/
      send "PONG #{$1}"
    else
      yield line
    end
  end
end

#join(channel) ⇒ Object



50
51
52
# File 'lib/raibo/connection.rb', line 50

def join(channel)
  send "JOIN #{channel}"
end

#msg(dest, msg) ⇒ Object



62
63
64
# File 'lib/raibo/connection.rb', line 62

def msg(dest, msg)
  send "PRIVMSG #{dest} :#{msg}"
end

#openObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/raibo/connection.rb', line 13

def open
  @connection = TCPSocket.new(@server, @port)
  send "USER #{@nick} 0 * :Hi"
  nick @nick

  handle_lines do |line|
    case line
    when /:Nickname is already in use./
      @nick = "#{@nick}_"
      nick @nick
    when /001/
      join @channel
      break
    end
  end
end

#part(channel) ⇒ Object



54
55
56
# File 'lib/raibo/connection.rb', line 54

def part(channel)
  send "PART #{channel}"
end

#say(msg) ⇒ Object



58
59
60
# File 'lib/raibo/connection.rb', line 58

def say(msg)
  msg(@channel, msg)
end

#send(str) ⇒ Object



66
67
68
69
# File 'lib/raibo/connection.rb', line 66

def send(str)
  puts "<-- #{str}" if @verbose
  @connection.puts(str)
end