Class: IRC::Client::ConnectedState

Inherits:
ConnectionState show all
Defined in:
lib/irc/client/connected_state.rb

Direct Known Subclasses

RegisteredState, UnregisteredState

Instance Method Summary collapse

Methods inherited from ConnectionState

#join, #on_welcome, #part, #private_message, #send_command, #send_command_via_queue

Methods included from ConnectionListener

#on_connect, #on_join, #on_join_failure, #on_kick, #on_nick_already_in_use, #on_notice, #on_part, #on_part_failure, #on_pong, #on_private_message, #on_registration, #on_registration_failure, #on_server_response, #on_welcome

Constructor Details

#initializeConnectedState

Returns a new instance of ConnectedState.



42
43
44
# File 'lib/irc/client/connected_state.rb', line 42

def initialize
  @log = Log4r::Logger.new('IRC::Client::ConnectedState')  
end

Instance Method Details

#change_nick(context, nick) ⇒ Object

Changes the nick name.



47
48
# File 'lib/irc/client/connected_state.rb', line 47

def change_nick(context, nick)
end

#connect(context, server) ⇒ Object

Connects to the server.

Raises:



51
52
53
# File 'lib/irc/client/connected_state.rb', line 51

def connect(context, server)
  raise ClientError.new("Can't connect to server. Already connected to a server.")
end

#disconnect(context, message = nil) ⇒ Object

Disconnects from the currently connected server.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/irc/client/connected_state.rb', line 56

def disconnect(context, message = nil)

  @log.debug("#{network_name(context)} Disconnecting from server #{context.server.hostname}.")

  # Send the quit command. If no quit message is given, the nick name will be
  # send as the default quit message.
  send_command(context, IRC::Commands::QuitCommand.new(message || context.nick))
  
  # Stop the command & message handlers.
  context.stop_command_handler
  context.stop_message_handler
  
  # Set network & server to nil.
  context.network = context.server = nil
  
  # Close input/output socket and set them to nil.
  context.input_socket.close if context.input_socket != nil && !context.input_socket.closed?
  context.input_socket = nil

  context.output_socket.close if context.output_socket != nil && !context.output_socket.closed?
  context.output_socket = nil

  # Change to the disconnected state.
  change_state(context, DisconnectedState.instance)
  
end

#on_disconnect(context, server, reason = nil) ⇒ Object

This method gets called when disconnected from a server.



114
115
116
# File 'lib/irc/client/connected_state.rb', line 114

def on_disconnect(context, server, reason = nil)
  log.debug("#{network_name(context)} Successfully disconnected from #{server}. #{reason != nil ? reason + '.' : ''}")      
end

#on_error(context, reason) ⇒ Object

ConnectionListener



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/irc/client/connected_state.rb', line 90

def on_error(context, reason)

  @log.debug("#{network_name(context)} Got disconnected from server #{context.server.hostname}. #{reason.capitalize}.")
  
  # Stop the command & message handlers.
  context.stop_command_handler
  context.stop_message_handler
          
  # Set network & server to nil.
  context.network = context.server = nil
  
  # Close input/output socket and set them to nil.
  context.input_socket.close if context.input_socket != nil && !context.input_socket.closed?
  context.input_socket = nil

  context.output_socket.close if context.output_socket != nil && !context.output_socket.closed?
  context.output_socket = nil

  # Change to the disconnected state.
  change_state(context, DisconnectedState.instance)
        
end

#on_nick(context, before, after) ⇒ Object

This method gets called when a user changes it’s nick name.



119
120
121
122
123
# File 'lib/irc/client/connected_state.rb', line 119

def on_nick(context, before, after)
  return if before.nick.strip.downcase != context.nick.strip.downcase
  context.nick = after.nick
  @log.debug("#{network_name(context)} Successfully changed nick name from #{before.nick} to #{after.nick}.")              
end

#on_ping(context, servers) ⇒ Object

This method gets called when a ping message was received from the server.



126
127
128
# File 'lib/irc/client/connected_state.rb', line 126

def on_ping(context, servers)
  IRC::Commands::PongCommand.new(servers[0], servers[1]).execute(context)
end

#register(context, nick, login, realname) ⇒ Object

Register the connection by sending the password, nick and user commands.

Raises:



84
85
86
# File 'lib/irc/client/connected_state.rb', line 84

def register(context, nick, , realname)
  raise ClientError.new("Can't register connection. Connection registration is only possible in the unregistered state.")
end