Class: Irc::Socket

Inherits:
Object show all
Defined in:
lib/rbot/ircsocket.rb

Overview

wrapped TCPSocket for communication with the server. emulates a subset of TCPSocket functionality

Defined Under Namespace

Classes: IdentityFilter

Constant Summary collapse

MAX_IRC_SEND_PENALTY =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server_list, host, opts = {}) ⇒ Socket

server_list

list of servers to connect to

host

optional local host to bind to (ruby 1.7+ required)

create a new Irc::Socket



277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/rbot/ircsocket.rb', line 277

def initialize(server_list, host, opts={})
  @server_list = server_list.dup
  @server_uri = nil
  @conn_count = 0
  @host = host
  @sock = nil
  @filter = IdentityFilter.new
  @spooler = false
  @lines_sent = 0
  @lines_received = 0
  @ssl = opts[:ssl]
  @penalty_pct = opts[:penalty_pct] || 100
end

Instance Attribute Details

#bytes_receivedObject (readonly)

total number of bytes received from the irc server



243
244
245
# File 'lib/rbot/ircsocket.rb', line 243

def bytes_received
  @bytes_received
end

#bytes_sentObject (readonly)

total number of bytes sent to the irc server



240
241
242
# File 'lib/rbot/ircsocket.rb', line 240

def bytes_sent
  @bytes_sent
end

#filterObject

an optional filter object. we call @filter.in(data) for all incoming data and @filter.out(data) for all outgoing data



250
251
252
# File 'lib/rbot/ircsocket.rb', line 250

def filter
  @filter
end

#lines_receivedObject (readonly)

total number of lines received from the irc server



237
238
239
# File 'lib/rbot/ircsocket.rb', line 237

def lines_received
  @lines_received
end

#lines_sentObject (readonly)

total number of lines sent to the irc server



234
235
236
# File 'lib/rbot/ircsocket.rb', line 234

def lines_sent
  @lines_sent
end

#penalty_pctObject

penalty multiplier (percent)



256
257
258
# File 'lib/rbot/ircsocket.rb', line 256

def penalty_pct
  @penalty_pct
end

#server_uriObject (readonly)

normalized uri of the current server



253
254
255
# File 'lib/rbot/ircsocket.rb', line 253

def server_uri
  @server_uri
end

#throttle_bytesObject (readonly)

accumulator for the throttle



246
247
248
# File 'lib/rbot/ircsocket.rb', line 246

def throttle_bytes
  @throttle_bytes
end

Instance Method Details

#clearqObject



379
380
381
# File 'lib/rbot/ircsocket.rb', line 379

def clearq
  @sendq.clear
end

#connectObject

open a TCP connection to the server



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/rbot/ircsocket.rb', line 296

def connect
  if connected?
    warning "reconnecting while connected"
    return
  end
  srv_uri = @server_list[@conn_count % @server_list.size].dup
  srv_uri = 'irc://' + srv_uri if !(srv_uri =~ /:\/\//)
  @conn_count += 1
  @server_uri = URI.parse(srv_uri)
  @server_uri.port = 6667 if !@server_uri.port
  debug "connection attempt \##{@conn_count} (#{@server_uri.host}:#{@server_uri.port})"

  if(@host)
    begin
      sock=TCPSocket.new(@server_uri.host, @server_uri.port, @host)
    rescue ArgumentError => e
      error "Your version of ruby does not support binding to a "
      error "specific local address, please upgrade if you wish "
      error "to use HOST = foo"
      error "(this option has been disabled in order to continue)"
      sock=TCPSocket.new(@server_uri.host, @server_uri.port)
    end
  else
    sock=TCPSocket.new(@server_uri.host, @server_uri.port)
  end
  if(@ssl)
    require 'openssl'
    ssl_context = OpenSSL::SSL::SSLContext.new()
    ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
    sock = OpenSSL::SSL::SSLSocket.new(sock, ssl_context)
    sock.sync_close = true
    sock.connect
  end
  @sock = sock
  @last_send = Time.new
  @flood_send = Time.new
  @burst = 0
  @sock.extend(MonitorMixin)
  @sendq = MessageQueue.new
  @qthread = Thread.new { writer_loop }
end

#connected?Boolean

Returns:

  • (Boolean)


291
292
293
# File 'lib/rbot/ircsocket.rb', line 291

def connected?
  !@sock.nil?
end

#emergency_puts(message, penalty = false) ⇒ Object

used to send lines to the remote IRCd by skipping the queue message: IRC message to send it should only be used for stuff that *must not* be queued, i.e. the initial PASS, NICK and USER command or the final QUIT message



343
344
345
346
347
348
# File 'lib/rbot/ircsocket.rb', line 343

def emergency_puts(message, penalty = false)
  @sock.synchronize do
    # debug "In puts - got @sock"
    puts_critical(message, penalty)
  end
end

#flushObject

flush the TCPSocket



384
385
386
# File 'lib/rbot/ircsocket.rb', line 384

def flush
  @sock.flush
end

#getsObject

get the next line from the server (blocks)



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/rbot/ircsocket.rb', line 359

def gets
  if @sock.nil?
    warning "socket get attempted while closed"
    return nil
  end
  begin
    reply = @filter.in(@sock.gets)
    @lines_received += 1
    reply.strip! if reply
    debug "RECV: #{reply.inspect}"
    return reply
  rescue Exception => e
    handle_socket_error(:RECV, e)
  end
end

#handle_socket_error(string, e) ⇒ Object

Raises:

  • (SocketError)


350
351
352
353
354
355
356
# File 'lib/rbot/ircsocket.rb', line 350

def handle_socket_error(string, e)
  error "#{string} failed: #{e.pretty_inspect}"
  # We assume that an error means that there are connection
  # problems and that we should reconnect, so we
  shutdown
  raise SocketError.new(e.inspect)
end

#queue(msg, chan = nil, ring = 0) ⇒ Object



375
376
377
# File 'lib/rbot/ircsocket.rb', line 375

def queue(msg, chan=nil, ring=0)
  @sendq.push msg, chan, ring
end

#select(timeout = nil) ⇒ Object

Wraps Kernel.select on the socket



389
390
391
# File 'lib/rbot/ircsocket.rb', line 389

def select(timeout=nil)
  Kernel.select([@sock], nil, nil, timeout)
end

#shutdown(how = 2) ⇒ Object

shutdown the connection to the server



394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/rbot/ircsocket.rb', line 394

def shutdown(how=2)
  return unless connected?
  @qthread.kill
  @qthread = nil
  begin
    @sock.close
  rescue Exception => e
    error "error while shutting down: #{e.pretty_inspect}"
  end
  @sock = nil
  @sendq.clear
end