Class: IRC::Commands::Command

Inherits:
Monitor
  • Object
show all
Includes:
IRC::Client::ConnectionListener
Defined in:
lib/irc/commands/command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from IRC::Client::ConnectionListener

#on_connect, #on_disconnect, #on_error, #on_join, #on_join_failure, #on_kick, #on_nick, #on_nick_already_in_use, #on_notice, #on_part, #on_part_failure, #on_ping, #on_pong, #on_private_message, #on_registration, #on_registration_failure, #on_welcome

Constructor Details

#initialize(raw_command = "") ⇒ Command

Returns a new instance of Command.



41
42
43
44
# File 'lib/irc/commands/command.rb', line 41

def initialize(raw_command = "")
  super()
  @raw_command = raw_command
end

Instance Attribute Details

#responseObject (readonly)

The message that was sent by the IRC server as a response to the execution of the command.



39
40
41
# File 'lib/irc/commands/command.rb', line 39

def response
  @response
end

Instance Method Details

#commandObject

Returns the command as a string.



47
48
49
# File 'lib/irc/commands/command.rb', line 47

def command
  return @raw_command
end

#execute(connection) ⇒ Object

Execute the command. The method registers the command as an connection listener at the connection object and sends the command. The method returns the command.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/irc/commands/command.rb', line 53

def execute(connection)

  # Register the command as a connection listener, so we can receive response messages
  # from the server.
  connection.add_connection_listener(self)
  
  # Send the message.
  connection << self
  
  # Create a conditinal monitor variable.
  self.synchronize do
    @response_received ||= self.new_cond
  end
  
  return self
  
end

#on_server_response(connection, message) ⇒ Object

This method gets called when a message from the server is received. This method should be overridden in subclasses to handle the command specific response messages from the server.



102
103
104
105
106
107
108
109
# File 'lib/irc/commands/command.rb', line 102

def on_server_response(connection, message)
  
  if valid_response?(message)
    connection.remove_connection_listener(self)
    @response = message
  end
  
end

#to_sObject

Returns the command as a string.



72
73
74
# File 'lib/irc/commands/command.rb', line 72

def to_s
  return command
end

#valid_response?(message) ⇒ Boolean

Returns true, if the message is a valid response to the command. This method should get overriddn in subclasses. For this class every response is valid.

Returns:

  • (Boolean)


78
79
80
# File 'lib/irc/commands/command.rb', line 78

def valid_response?(message)
  return true
end

#waitObject

Waits until a response for the command was sent by the IRC server. This method expects, that the command has already been sent to the server by the execute method. A call to this method blocks until a response was reseived from the server.

Raises:

  • (Exception)


85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/irc/commands/command.rb', line 85

def wait

  # If the command has not been sent, or a response was already received. a
  # call to this method doesn't make any sense.
  raise Exception.new("Can't wait for response. The command was not send yet, or a response was already received.") if @response_received == nil
  
  # Wait until a response was received from the server.
  synchronize do
    @response_received.wait_until { response != nil }
  end
  
end