Class: IRC::Client::Connection::Context

Inherits:
IRC::Client::ConnectionListener
  • Object
show all
Defined in:
lib/irc/client/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nick = nil, login = nil, realname = nil) ⇒ Context

Returns a new instance of Context.



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
# File 'lib/irc/client/context.rb', line 85

def initialize(nick = nil,  = nil, realname = nil)
  
  # Initialize logging framework.
  @log = Log4r::Logger.new('IRC::Client::Connection::Context')  

  # Initialize nick, login & realname. If a value is missing it
  # is taken from the environemnt settings.
  @nick = nick || ENV['USER'] || ENV['USERNAME'] || "i-AM-TOO-LAME"
  @login =  || ENV['USER'] || ENV['USERNAME'] || nick
  @realname = realname || ENV['USER'] || ENV['USERNAME'] || nick
  
  # Enable automatic nick change. Use the nick name generator.
  @auto_nick_change = true
  @nick_generator = IRC::Util::NickGenerator.new(@nick)
  
  # A hash to keep track of currently joined channels.
  @joined_channels = Hash.new
  
  # All connection listeners will get notified when a message was received from the IRC server.
  @connection_listeners = Array.new

  # Add the context as a connection listener. The context doesn't handle messages from the server 
  # directly. Instead method calls to the ConnectionListner module get forwarded to the current
  # state object. When the context changes it's state the delegation object is allso changed to 
  # the new state.
  add_connection_listener(self)
  
  # Change into the disconnected state.
  change_state(DisconnectedState.instance)
  
  # The options from the isupport messages.
  @options = Hash.new
  
end

Instance Attribute Details

#auto_nick_changeObject

When set to true the connection object changes the nick name automatically when the chosen name is already in use.



61
62
63
# File 'lib/irc/client/context.rb', line 61

def auto_nick_change
  @auto_nick_change
end

#command_handlerObject (readonly)

The command handler that is responsible for sending commands to the IRC server.



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

def command_handler
  @command_handler
end

#connection_listenersObject (readonly)

The connection listeners that get notified when a message was received by the IRC server.



49
50
51
# File 'lib/irc/client/context.rb', line 49

def connection_listeners
  @connection_listeners
end

#input_socketObject

Returns the socket, that is used to receive messages from the server.



172
173
174
# File 'lib/irc/client/context.rb', line 172

def input_socket
  return @input_socket
end

#loginObject

The login name that is used when connected to an IRC server.



64
65
66
# File 'lib/irc/client/context.rb', line 64

def 
  @login
end

#networkObject

The network to which the context is connected, or nil if no connection has been established.



67
68
69
# File 'lib/irc/client/context.rb', line 67

def network
  @network
end

#nickObject

The nick name that is used when connected to an IRC server.



70
71
72
# File 'lib/irc/client/context.rb', line 70

def nick
  @nick
end

#nick_generatorObject (readonly)

The nick name generator that used, when automatic nick name change is enabled.



52
53
54
# File 'lib/irc/client/context.rb', line 52

def nick_generator
  @nick_generator
end

#optionsObject

The options that are supported by the server.



76
77
78
# File 'lib/irc/client/context.rb', line 76

def options
  @options
end

#output_socketObject

Returns the socket, that is used to send commands to the server.



177
178
179
# File 'lib/irc/client/context.rb', line 177

def output_socket
  return @output_socket
end

#realnameObject

The real name that is used when connected to an IRC server.



73
74
75
# File 'lib/irc/client/context.rb', line 73

def realname
  @realname
end

#serverObject

The IRC server the context is connected to, or nil if no connection has been established.



79
80
81
# File 'lib/irc/client/context.rb', line 79

def server
  @server
end

#stateObject (readonly)

The context’s current state. The initial state is disconnected.



55
56
57
# File 'lib/irc/client/context.rb', line 55

def state
  @state
end

Instance Method Details

#<<(command) ⇒ Object

Sends the command to the server using the command queue.



127
128
129
# File 'lib/irc/client/context.rb', line 127

def <<(command)
  send_command_via_queue(command)
end

#add_connection_listener(connection_listener) ⇒ Object

Adds a new connection listener. The connection listener gets notified when a message was received from the IRC server.



122
123
124
# File 'lib/irc/client/context.rb', line 122

def add_connection_listener(connection_listener)
  connection_listeners << connection_listener unless connection_listeners.include?(connection_listener)
end

#change_nick(new_nick) ⇒ Object

Changes the context’s state to the next state,



132
133
134
# File 'lib/irc/client/context.rb', line 132

def change_nick(new_nick)
  state.change_nick(self, new_nick)
end

#change_state(next_state) ⇒ Object

Changes the context’s state to the next state,



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/irc/client/context.rb', line 137

def change_state(next_state)

  # Log the state change.
  if state == nil
    @log.debug("Setting initial state to #{next_state.class.name}.")    
  else
    @log.debug("Changing state from #{state.class.name} to #{next_state.class.name}.")
  end

  # Change the context's state.
  @state = next_state          
  
  # The context doesn't handle messages from the server directly. Method calls to the 
  # ConnectionListener module get forwarded to the current state object.
  __setobj__(@state)
  
end

#connect(server) ⇒ Object

Connects to the server.



156
157
158
159
# File 'lib/irc/client/context.rb', line 156

def connect(server)
  @network = server.network
  state.connect(self, server)
end

#connected?Boolean

Returns true if a connection to an IRC server has been established.

Returns:

  • (Boolean)


162
163
164
# File 'lib/irc/client/context.rb', line 162

def connected?
  state.kind_of?(ConnectedState)  
end

#disconnect(message = nil) ⇒ Object

Disconnects from the currently connected server.



167
168
169
# File 'lib/irc/client/context.rb', line 167

def disconnect(message = nil)
  state.disconnect(self, message)        
end

#join(channels) ⇒ Object

Joins the given channels.



182
183
184
# File 'lib/irc/client/context.rb', line 182

def join(channels)
  state.join(self, channels)
end

#join_succeeded(channel) ⇒ Object

Add the channel to the list of joined channels.



192
193
194
# File 'lib/irc/client/context.rb', line 192

def join_succeeded(channel)
  @joined_channels[channel] = channel
end

#joined?(channel) ⇒ Boolean

Returns true, if the given channel is currently joined.

Returns:

  • (Boolean)


187
188
189
# File 'lib/irc/client/context.rb', line 187

def joined?(channel)
  return @joined_channels.include?(channel)
end

#joined_channelsObject

Returns an Array of all currently joined channels.



202
203
204
# File 'lib/irc/client/context.rb', line 202

def joined_channels
  return @joined_channels.values
end

#lookup_channel(channel) ⇒ Object

Lookup or create a channel object.



207
208
209
# File 'lib/irc/client/context.rb', line 207

def lookup_channel(channel)
  return network.lookup_or_create_channel(channel)
end

#lookup_server(server) ⇒ Object

Lookup or create a server object.



212
213
214
# File 'lib/irc/client/context.rb', line 212

def lookup_server(server)
  return network.lookup_or_create_server(server)
end

#lookup_user(user) ⇒ Object

Lookup or create a user object.



217
218
219
# File 'lib/irc/client/context.rb', line 217

def lookup_user(user)
  return network.lookup_or_create_user(user)
end

#part(channels) ⇒ Object

Leaves the given channels.



227
228
229
# File 'lib/irc/client/context.rb', line 227

def part(channels)
  state.part(self, channels)
end

#part_succeeded(channel) ⇒ Object

Remove the channel from the list of joined channels.



197
198
199
# File 'lib/irc/client/context.rb', line 197

def part_succeeded(channel)
  @joined_channels.delete(channel)
end

#register(nick = nil, login = nil, realname = nil) ⇒ Object

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



232
233
234
# File 'lib/irc/client/context.rb', line 232

def register(nick = nil,  = nil, realname = nil)
  state.register(self, nick || self.nick,  || self., realname || self.realname)
end

#registered?Boolean

Returns true if a connection to an IRC server has been established and the connection has been successfully registered.

Returns:

  • (Boolean)


238
239
240
# File 'lib/irc/client/context.rb', line 238

def registered?
  state.kind_of?(RegisteredState)  
end

#remove_connection_listener(connection_listener) ⇒ Object

Removes a previously added connection listener. The connection listener will not get notified any longer when a message was received from the IRC server.



244
245
246
# File 'lib/irc/client/context.rb', line 244

def remove_connection_listener(connection_listener)
  connection_listeners.delete(connection_listener)
end

#send_command(command) ⇒ Object

Sends the command to the server bypassing the command queue.



249
250
251
# File 'lib/irc/client/context.rb', line 249

def send_command(command)
  state.send_command(self, command)          
end

#send_command_via_queue(command) ⇒ Object

Sends the command to the server using the command queue.



254
255
256
# File 'lib/irc/client/context.rb', line 254

def send_command_via_queue(command)
  state.send_command_via_queue(self, command)  
end

#start_command_handlerObject

Initializes and starts the command handler.



259
260
261
262
# File 'lib/irc/client/context.rb', line 259

def start_command_handler        
  @command_handler = CommandHandler.new(self)
  @command_handler.start
end

#start_message_handlerObject

Initializes and starts the message handler.



265
266
267
268
# File 'lib/irc/client/context.rb', line 265

def start_message_handler        
  @message_handler = MessageHandler.new(self)
  @message_handler.start
end

#stop_command_handlerObject

Stops the command handler.



271
272
273
274
# File 'lib/irc/client/context.rb', line 271

def stop_command_handler        
  @command_handler.stop
  @command_handler = nil
end

#stop_message_handlerObject

Stops the message handler.



277
278
279
280
# File 'lib/irc/client/context.rb', line 277

def stop_message_handler        
  @message_handler.stop
  @message_handler = nil
end