lib/marvin/abstract_client.rb in Sutto-marvin-0.1.20081120 vs lib/marvin/abstract_client.rb in Sutto-marvin-0.2.0
- old
+ new
@@ -6,19 +6,20 @@
class AbstractClient
include Marvin::Dispatchable
def initialize(opts = {})
+ self.original_opts = opts.dup # Copy the options so we can use them to reconnect.
self.server = opts[:server]
self.port = opts[:port]
self.default_channels = opts[:channels]
self.nicks = opts[:nicks] || []
self.pass = opts[:pass]
end
cattr_accessor :events, :configuration, :logger, :is_setup, :connections
- attr_accessor :channels, :nickname, :server, :port, :nicks, :pass
+ attr_accessor :channels, :nickname, :server, :port, :nicks, :pass, :disconnect_expected, :original_opts
# Set the default values for the variables
self.events = []
self.configuration = OpenStruct.new
self.configuration.channels = []
@@ -41,11 +42,16 @@
def process_disconnect
logger.info "Handling disconnect for #{self.server}:#{self.port}"
self.connections.delete(self) if self.connections.include?(self)
dispatch :client_disconnected
- Marvin::Loader.stop! if self.connections.blank?
+ unless self.disconnect_expected
+ logger.warn "Lost connection to server - adding reconnect"
+ self.class.add_reconnect self.original_opts
+ else
+ Marvin::Loader.stop! if self.connections.blank?
+ end
end
# Sets the current class-wide settings of this IRC Client
# to either an OpenStruct or the results of #to_hash on
# any other value that is passed in.
@@ -183,10 +189,11 @@
end
def join(channel)
channel = Marvin::Util.channel_name(channel)
# Record the fact we're entering the room.
+ # TODO: Refactor to only add the channel when we receive confirmation we've joined.
self.channels << channel
command :JOIN, channel
logger.info "Joined channel #{channel}"
dispatch :outgoing_join, :target => channel
end
@@ -201,9 +208,10 @@
logger.warn "Tried to disconnect from #{channel} - which you aren't a part of"
end
end
def quit(reason = nil)
+ self.disconnect_expected = true
logger.info "Preparing to part from #{self.channels.size} channels"
self.channels.to_a.each do |chan|
logger.info "Parting from #{chan}"
self.part chan, reason
end
\ No newline at end of file