Class: IRCConnection
- Inherits:
-
Object
- Object
- IRCConnection
- Defined in:
- lib/IRCConnection.rb
Overview
Handles connection to IRC Server
Constant Summary collapse
- @@quit =
0
- @@readsockets =
Array.new(0)
- @@output_buffer =
Array.new(0)
- @@events =
Hash.new()
- @@last_send =
Time.now.to_f
- @@message_delay =
Default delay to 1 fifth of a second.
0.2
Class Method Summary collapse
-
.add_IO_socket(socket, &event_generator) ⇒ Object
Adds a new socket to the list of sockets to monitor for new data.
- .create_tcp_socket(server, port) ⇒ Object
- .delay=(delay) ⇒ Object
-
.do_one_loop ⇒ Object
Makes one single loop pass, checking all sockets for data to read, and yields the data to the sockets event handler.
-
.get_user_info(user) ⇒ Object
Retrieves user info from the server.
-
.handle_connection(server, port, nick = 'ChangeMe', realname = 'MeToo', options = nil) ⇒ Object
Creates a socket connection and then yields.
-
.main ⇒ Object
This loop monitors all IO_Sockets IRCConnection controls (including the IRC socket) and yields events to the IO_Sockets event handler.
-
.output_push(line) ⇒ Object
Adds data an output buffer.
-
.quit ⇒ Object
Ends connection to the irc server.
- .remove_IO_socket(sock) ⇒ Object
-
.send_to_server(line) ⇒ Object
Sends a line of text to the server.
Class Method Details
.add_IO_socket(socket, &event_generator) ⇒ Object
Adds a new socket to the list of sockets to monitor for new data.
125 126 127 128 |
# File 'lib/IRCConnection.rb', line 125 def IRCConnection.add_IO_socket(socket, &event_generator) @@readsockets.push(socket) @@events[socket.object_id.to_i] = event_generator end |
.create_tcp_socket(server, port) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/IRCConnection.rb', line 41 def IRCConnection.create_tcp_socket(server, port) # Now with SSL Support. Thanks to [email protected] for the idea on this. tcpsocket = TCPSocket.open(server, port) if [:use_ssl] require 'openssl' # Only require OpenSSL if we need to use it. ssl_context = OpenSSL::SSL::SSLContext.new() ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE @@socket = OpenSSL::SSL::SSLSocket.new(tcpsocket, ssl_context) @@socket.sync = true @@socket.connect else @@socket = tcpsocket end if block_given? yield @@socket.close return end return @@socket end |
.delay=(delay) ⇒ Object
116 117 118 |
# File 'lib/IRCConnection.rb', line 116 def IRCConnection.delay=(delay) = delay.to_f end |
.do_one_loop ⇒ Object
Makes one single loop pass, checking all sockets for data to read, and yields the data to the sockets event handler.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/IRCConnection.rb', line 87 def IRCConnection.do_one_loop read_sockets = select(@@readsockets, nil, nil, 0.1); if !read_sockets.nil? read_sockets[0].each {|sock| if sock.eof? && sock == @@socket remove_IO_socket(sock) sleep 10 handle_connection(@server, @port, @nick, @realname) else yield @@events[sock.object_id.to_i].call(sock) end } end if @@output_buffer.length > 0 timer = Time.now.to_f if (timer > @@last_send + ) = @@output_buffer.shift(); if !.nil? IRCConnection.send_to_server(); @@last_send = timer end end end end |
.get_user_info(user) ⇒ Object
Retrieves user info from the server
120 121 122 |
# File 'lib/IRCConnection.rb', line 120 def IRCConnection.get_user_info(user) IRCConnection.send_to_server("WHOIS #{user}") end |
.handle_connection(server, port, nick = 'ChangeMe', realname = 'MeToo', options = nil) ⇒ Object
Creates a socket connection and then yields.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/IRCConnection.rb', line 12 def IRCConnection.handle_connection(server, port, nick='ChangeMe', realname='MeToo', = nil) @server = server; @port = port @nick = nick @realname = realname = if .nil? = Array.new(0) end socket = create_tcp_socket(server, port) add_IO_socket(socket) {|sock| begin IRCEvent.new(sock.readline.chomp) rescue Errno::ECONNRESET # Catches connection reset by peer, attempts to reconnect # after sleeping for 10 second. remove_IO_socket(sock) sleep 10 handle_connection(@server, @port, @nick, @realname, ) end } send_to_server "NICK #{nick}" send_to_server "USER #{nick} 8 * :#{realname}" if block_given? yield @@socket.close end end |
.main ⇒ Object
This loop monitors all IO_Sockets IRCConnection controls (including the IRC socket) and yields events to the IO_Sockets event handler.
77 78 79 80 81 82 83 |
# File 'lib/IRCConnection.rb', line 77 def IRCConnection.main while(@@quit == 0) do_one_loop { |event| yield event } end end |
.output_push(line) ⇒ Object
Adds data an output buffer. This let’s us keep a handle on how fast we send things. Yay.
70 71 72 |
# File 'lib/IRCConnection.rb', line 70 def IRCConnection.output_push(line) @@output_buffer.push(line) end |
.quit ⇒ Object
Ends connection to the irc server
113 114 115 |
# File 'lib/IRCConnection.rb', line 113 def IRCConnection.quit @@quit = 1 end |
.remove_IO_socket(sock) ⇒ Object
130 131 132 133 |
# File 'lib/IRCConnection.rb', line 130 def IRCConnection.remove_IO_socket(sock) sock.close @@readsockets.delete_if {|item| item == sock } end |
.send_to_server(line) ⇒ Object
Sends a line of text to the server
64 65 66 |
# File 'lib/IRCConnection.rb', line 64 def IRCConnection.send_to_server(line) @@socket.write(line + "\n") end |