Class: Raibo::IrcConnection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of IrcConnection.



5
6
7
8
9
10
11
# File 'lib/raibo/irc_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/irc_connection.rb', line 3

def channel
  @channel
end

#nick(nick) ⇒ Object

Returns the value of attribute nick.



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

def nick
  @nick
end

#portObject

Returns the value of attribute port.



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

def port
  @port
end

#serverObject

Returns the value of attribute server.



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

def server
  @server
end

#verboseObject

Returns the value of attribute verbose.



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

def verbose
  @verbose
end

Instance Method Details

#closeObject



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

def close
  @connection.close if @connection
end

#construct_message(msg) ⇒ Object



73
74
75
# File 'lib/raibo/irc_connection.rb', line 73

def construct_message(msg)
  Raibo::IrcMessage.new(msg)
end

#handle_linesObject



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/raibo/irc_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
rescue IOError
  close
end

#join(channel) ⇒ Object



52
53
54
# File 'lib/raibo/irc_connection.rb', line 52

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

#msg(dest, msg) ⇒ Object



64
65
66
# File 'lib/raibo/irc_connection.rb', line 64

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/irc_connection.rb', line 13

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

  handle_lines do |line|
    case line
    when /:Nickname is already in use/
      @connection.close
      raise "Connection error: Nickname is already in use."
    when /001/
      join @channel
      break
    end
  end
end

#part(channel) ⇒ Object



56
57
58
# File 'lib/raibo/irc_connection.rb', line 56

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

#say(*msgs) ⇒ Object



60
61
62
# File 'lib/raibo/irc_connection.rb', line 60

def say(*msgs)
  msgs.each { |msg| msg(@channel, msg) }
end

#send(str) ⇒ Object



68
69
70
71
# File 'lib/raibo/irc_connection.rb', line 68

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