lib/marvin/irc/client.rb in Sutto-marvin-0.1.20081115 vs lib/marvin/irc/client.rb in Sutto-marvin-0.1.20081120

- old
+ new

@@ -43,56 +43,80 @@ # ==== Adding handlers # To add an object as a handler, you simply call # the class method, register_handler with the # handler as the only argument. class Client < Marvin::AbstractClient + cattr_accessor :stopped attr_accessor :em_connection class EMConnection < EventMachine::Protocols::LineAndTextProtocol - attr_accessor :client + attr_accessor :client, :server, :port - def initialize - super - self.client = Marvin::IRC::Client.new + def initialize(*args) + opts = args.extract_options! + super(*args) + self.client = Marvin::IRC::Client.new(opts) self.client.em_connection = self end def post_init client.process_connect + super end def unbind client.process_disconnect + super end def receive_line(line) + Marvin::Logger.debug "<< #{line.strip}" self.client.receive_line(line) end end def send_line(*args) + args.each { |line| Marvin::Logger.debug ">> #{line.strip}" } em_connection.send_data *args end ## Client specific details # Starts the EventMachine loop and hence starts up the actual # networking portion of the IRC Client. def self.run self.setup # So we have options etc - EventMachine::run do - logger.debug "Connecting to #{self.configuration.server}:#{self.configuration.port}" - EventMachine::connect self.configuration.server, self.configuration.port, Marvin::IRC::Client::EMConnection + settings = YAML.load_file(Marvin::Settings.root / "config/connections.yml") + if settings.is_a?(Hash) + EventMachine.epoll + EventMachine::run do + settings.each do |name, options| + settings = options.symbolize_keys! + settings[:server] ||= name + settings.reverse_merge!(:port => 6667, :channels => []) + connect settings + end + end + else + logger.fatal "config/connections.yml couldn't be loaded. Exiting" end end + def self.connect(opts = {}) + logger.info "Connecting to #{opts[:server]}:#{opts[:port]} - Channels: #{opts[:channels].join(", ")}" + EventMachine::connect(opts[:server], opts[:port], EMConnection, opts) + logger.info "Connection created for #{opts[:server]}:#{opts[:port]}" + end + def self.stop + return if self.stopped logger.debug "Telling all connections to quit" self.connections.dup.each { |connection| connection.quit } logger.debug "Telling Event Machine to Stop" EventMachine::stop_event_loop logger.debug "Stopped." + self.stopped = true end # Registers a callback handle that will be periodically run. def periodically(timing, event_callback) callback = proc { self.dispatch event_callback.to_sym } \ No newline at end of file