Class: Raibo::IrcConnection
- Inherits:
-
Object
- Object
- Raibo::IrcConnection
- Defined in:
- lib/raibo/irc_connection.rb
Instance Attribute Summary collapse
-
#channel ⇒ Object
Returns the value of attribute channel.
-
#nick(nick) ⇒ Object
Returns the value of attribute nick.
-
#port ⇒ Object
Returns the value of attribute port.
-
#server ⇒ Object
Returns the value of attribute server.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Instance Method Summary collapse
- #close ⇒ Object
- #construct_message(msg) ⇒ Object
- #handle_lines ⇒ Object
-
#initialize(server, opts = {}) ⇒ IrcConnection
constructor
A new instance of IrcConnection.
- #join(channel) ⇒ Object
- #msg(dest, msg) ⇒ Object
- #open ⇒ Object
- #part(channel) ⇒ Object
- #say(*msgs) ⇒ Object
- #send(str) ⇒ Object
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
#channel ⇒ Object
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 |
#port ⇒ Object
Returns the value of attribute port.
3 4 5 |
# File 'lib/raibo/irc_connection.rb', line 3 def port @port end |
#server ⇒ Object
Returns the value of attribute server.
3 4 5 |
# File 'lib/raibo/irc_connection.rb', line 3 def server @server end |
#verbose ⇒ Object
Returns the value of attribute verbose.
3 4 5 |
# File 'lib/raibo/irc_connection.rb', line 3 def verbose @verbose end |
Instance Method Details
#close ⇒ Object
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 (msg) Raibo::IrcMessage.new(msg) end |
#handle_lines ⇒ Object
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 |
#open ⇒ Object
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 |