Class: Codebot::IRCConnection

Inherits:
ThreadController show all
Defined in:
lib/codebot/irc_connection.rb

Overview

This class manages an IRC connection running in a separate thread.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ThreadController

#join, #running?, #start!, #stop, #stop!

Constructor Details

#initialize(core, network) ⇒ IRCConnection

Constructs a new IRC connection.

Parameters:

  • core (Core)

    the bot this connection belongs to

  • network (Network)

    the network to connect to



22
23
24
25
26
27
# File 'lib/codebot/irc_connection.rb', line 22

def initialize(core, network)
  @core     = core
  @network  = network
  @messages = Queue.new
  @ready    = Queue.new
end

Instance Attribute Details

#coreCore (readonly)

Returns the bot this connection belongs to.

Returns:

  • (Core)

    the bot this connection belongs to



13
14
15
# File 'lib/codebot/irc_connection.rb', line 13

def core
  @core
end

#networkNetwork (readonly)

Returns the connected network.

Returns:

  • (Network)

    the connected network



16
17
18
# File 'lib/codebot/irc_connection.rb', line 16

def network
  @network
end

Instance Method Details

#configure_nickserv_identification(net, conn) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/codebot/irc_connection.rb', line 50

def configure_nickserv_identification(net, conn)
  return unless net.nickserv?

  conn.plugins.plugins = [Cinch::Plugins::Identify]
  conn.plugins.options[Cinch::Plugins::Identify] = {
    username: nil_or_empty_string(net.nickserv_username),
    password: net.nickserv_password,
    type: :nickserv
  }
end

#enqueue(message) ⇒ Object

Schedules a message for delivery.

Parameters:

  • message (Message)

    the message



32
33
34
# File 'lib/codebot/irc_connection.rb', line 32

def enqueue(message)
  @messages << message
end

#set_ready!Object

Sets this connection to be available for delivering messages.



37
38
39
# File 'lib/codebot/irc_connection.rb', line 37

def set_ready!
  @ready << true if @ready.empty?
end

#startThread?

Starts a new managed thread if no thread is currently running. The thread invokes the run method of the class that manages it.

Returns:

  • (Thread, nil)

    the newly created thread, or nil if there was already a running thread



46
47
48
# File 'lib/codebot/irc_connection.rb', line 46

def start(*)
  super(self)
end