Class: Codebot::IRCClient

Inherits:
Object
  • Object
show all
Defined in:
lib/codebot/irc_client.rb

Overview

This class manages an IRC client.

Instance Method Summary collapse

Constructor Details

#initialize(core) ⇒ IRCClient

Creates a new IRC client.

Parameters:

  • core (Core)

    the bot this client belongs to



11
12
13
14
15
16
17
# File 'lib/codebot/irc_client.rb', line 11

def initialize(core)
  @active = false
  @checkpoint = []
  @connections = []
  @core = core
  @semaphore = Mutex.new
end

Instance Method Details

#dispatch(request) ⇒ Object

Dispatches a new request.

Parameters:

  • request (Request)

    the request to dispatch



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/codebot/irc_client.rb', line 22

def dispatch(request)
  request.each_network do |network, channels|
    connection = connection_to(network)
    next if connection.nil?

    channels.each do |channel|
      message = request.to_message_for channel
      connection.enqueue message
    end
  end
end

#joinObject

Waits for active connections to finish.



50
51
52
# File 'lib/codebot/irc_client.rb', line 50

def join
  @connections.each(&:join)
end

#migrate!Object

Connects to and disconnects from networks as necessary in order for the list of connections to reflect changes to the configuration.



56
57
58
59
60
61
62
63
64
65
# File 'lib/codebot/irc_client.rb', line 56

def migrate!
  @semaphore.synchronize do
    return unless @active

    networks = @core.config.networks
    (@checkpoint - networks).each { |network| disconnect_from network }
    (networks - @checkpoint).each { |network| connect_to      network }
    @checkpoint = networks
  end
end

#startObject

Starts the IRC client.



35
36
37
38
# File 'lib/codebot/irc_client.rb', line 35

def start
  @active = true
  migrate!
end

#stopObject

Stops the IRC client.



41
42
43
44
45
46
47
# File 'lib/codebot/irc_client.rb', line 41

def stop
  @active = false
  @checkpoint.clear
  @connections.each(&:stop)
  join
  @connections.clear
end