Class: IRCConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/vendor/irc/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

Class Method Details

.add_IO_socket(socket, &event_generator) ⇒ Object

Adds a new socket to the list of sockets to monitor for new data.



123
124
125
126
# File 'lib/vendor/irc/lib/IRCConnection.rb', line 123

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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/vendor/irc/lib/IRCConnection.rb', line 40

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 @@options[:use_ssl]
    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



114
115
116
# File 'lib/vendor/irc/lib/IRCConnection.rb', line 114

def IRCConnection.delay=(delay)
  @@message_delay = delay.to_f
end

.do_one_loopObject

Makes one single loop pass, checking all sockets for data to read, and yields the data to the sockets event handler.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/vendor/irc/lib/IRCConnection.rb', line 85

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 + @@message_delay)
      message = @@output_buffer.shift();
      if !message.nil?
        IRCConnection.send_to_server(message);
        @@last_send = timer
      end
    end
  end
end

.get_user_info(user) ⇒ Object

Retrieves user info from the server



118
119
120
# File 'lib/vendor/irc/lib/IRCConnection.rb', line 118

def IRCConnection.(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.



11
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
# File 'lib/vendor/irc/lib/IRCConnection.rb', line 11

def IRCConnection.handle_connection(server, port, nick='ChangeMe', realname='MeToo', options = nil)
  @server = server;
  @port = port
  @nick = nick
  @realname = realname
  @@options = options
  if options.nil?
    @@options = 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, @@options)
    end
  }
  send_to_server "NICK #{nick}"
  send_to_server "USER #{nick} 8 * :#{realname}"
  if block_given?
    yield
    @@socket.close
  end
end

.mainObject

This loop monitors all IO_Sockets IRCConnection controls (including the IRC socket) and yields events to the IO_Sockets event handler.



75
76
77
78
79
80
81
# File 'lib/vendor/irc/lib/IRCConnection.rb', line 75

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.



68
69
70
# File 'lib/vendor/irc/lib/IRCConnection.rb', line 68

def IRCConnection.output_push(line)
  @@output_buffer.push(line)
end

.quitObject

Ends connection to the irc server



111
112
113
# File 'lib/vendor/irc/lib/IRCConnection.rb', line 111

def IRCConnection.quit
  @@quit = 1
end

.remove_IO_socket(sock) ⇒ Object



128
129
130
131
# File 'lib/vendor/irc/lib/IRCConnection.rb', line 128

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



62
63
64
# File 'lib/vendor/irc/lib/IRCConnection.rb', line 62

def IRCConnection.send_to_server(line)
  @@socket.write(line + "\n")
end