Class: IRC::Client::UnregisteredState

Inherits:
ConnectedState show all
Defined in:
lib/irc/client/unregistered_state.rb

Instance Method Summary collapse

Methods inherited from ConnectedState

#connect, #disconnect, #on_disconnect, #on_error, #on_nick, #on_ping

Methods inherited from ConnectionState

#connect, #disconnect, #on_ping, #on_welcome, #send_command, #send_command_via_queue

Methods included from 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

#initializeUnregisteredState

Returns a new instance of UnregisteredState.



43
44
45
# File 'lib/irc/client/unregistered_state.rb', line 43

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

Instance Method Details

#change_nick(context, nick) ⇒ Object

Changes the nick name.

Raises:



48
49
50
# File 'lib/irc/client/unregistered_state.rb', line 48

def change_nick(context, nick)
  raise ClientError.new("Can't change nick name. The connection is not yet registered.")
end

#join(context, channels) ⇒ Object

Joins the given channels.

Raises:



53
54
55
# File 'lib/irc/client/unregistered_state.rb', line 53

def join(context, channels)
  raise ClientError.new("Can't join any channel. The connection is not yet registered.")
end

#on_server_response(context, message) ⇒ Object

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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/irc/client/unregistered_state.rb', line 84

def on_server_response(context, message)
      
  case message.code
  
    # Ignore notice messages.
    when IRC::Messages::NoticeMessage::CODE
      return
      
    # Ignore ping messages.
    when IRC::Messages::PingMessage::CODE
      return
            
    # The given nick name is not valid.  
    when IRC::Messages::ErrorErroneusNickNameMessage::CODE
      registration_failure(context, message)   
      return            
              
    # We didn't send a valid command. There are some parameters missing.       
    when IRC::Messages::ErrorNeedMoreParametersMessage::CODE
      registration_failure(context, message)     
      return
                  
    # Nick name collision.  
    when IRC::Messages::ErrorNickCollisionMessage::CODE
      registration_failure(context, message)                        
      return
                  
    # The chosen nick name is already in use.  
    when IRC::Messages::ErrorNickNameInUseMessage::CODE
      registration_failure(context, message)            
      return
                  
    # We didn't send a valid nick command. The nick name was missing.
    when IRC::Messages::ErrorNoNickNameGivenMessage::CODE
      registration_failure(context, message)       
      return
  
  end          
  
    # Everything ok. Change to the registered state.            
    change_state(context, RegisteredState.instance)
      
    # Notify all connection listeners that we are successfully connected now.
    context.connection_listeners.each do |connection_listener|
      connection_listener.on_registration(context)
    end  

end

#part(context, channels) ⇒ Object

Leaves the given channels.

Raises:



58
59
60
# File 'lib/irc/client/unregistered_state.rb', line 58

def part(context, channels)
  raise ClientError.new("Can't leave any channel. The connection is not yet registered.")
end

#private_message(context, target, message) ⇒ Object

Sends a private message to the target (a nick or a channel).

Raises:



63
64
65
# File 'lib/irc/client/unregistered_state.rb', line 63

def private_message(context, target, message)
  raise ClientError.new("Can't send  a private message. The connection is not yet registered.")
end

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

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



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/irc/client/unregistered_state.rb', line 68

def register(context, nick, , realname)

  if context.server.password
    IRC::Commands::PasswordCommand.new(context.server.password).execute(context)
  end          
  
  IRC::Commands::NickCommand.new(nick).execute(context)
  IRC::Commands::UserCommand.new(nick, Socket.gethostname, context.server.hostname, realname).execute(context)
  
  context.nick, context., context.realname = nick, , realname

end