Class: Marvin::IRC::Client
- Inherits:
-
AbstractClient
- Object
- AbstractClient
- Marvin::IRC::Client
- Defined in:
- lib/marvin/irc/client.rb
Overview
Marvin::IRC::Client
An EventMachine protocol implementation built to serve as a basic, single server IRC client.
Operates on the principal of Events as well as handlers.
Events
Events are things that can happen (e.g. an incoming message). All outgoing events are automatically handled from within the client class. Incoming events are currently based on regular expression based matches of incoming messages. the Client#register_event method takes either an instance of Marvin::IRC::Event or a set of arguments which will then be used in the constructor of a new Marvin::IRC::Event instance (see, for example, the source code for this class for examples).
Handlers
Handlers on the other hand do as the name suggests
-
they listen for dispatched events and act accordingly.
Handlers are simply objects which follow a certain set of guidelines. Typically, a handler will at minimum respond to #handle(event_name, details) where event_name is a symbol for the current event (e.g. :incoming_event) whilst details is a a hash of details about the current event (e.g. message target and the message itself).
Getting the current client instance
If the object responds to client=, The client will call it with the current instance of itself enabling the handler to do things such as respond. Also, if a method handle_ exists, it will be called instead of handle.
Adding handlers
To add an object as a handler, you simply call the class method, register_handler with the handler as the only argument.
Defined Under Namespace
Classes: EMConnection
Instance Attribute Summary collapse
-
#em_connection ⇒ Object
Returns the value of attribute em_connection.
Attributes inherited from AbstractClient
#channels, #disconnect_expected, #nickname, #nicks, #original_opts, #pass, #port, #server
Class Method Summary collapse
- .add_reconnect(opts = {}) ⇒ Object
- .connect(opts = {}) ⇒ Object
-
.run ⇒ Object
Starts the EventMachine loop and hence starts up the actual networking portion of the IRC Client.
- .stop ⇒ Object
Instance Method Summary collapse
-
#periodically(timing, event_callback) ⇒ Object
Registers a callback handle that will be periodically run.
- #send_line(*args) ⇒ Object
Methods inherited from AbstractClient
#action, #command, configuration=, #default_channels, #default_channels=, #handle_client_connected, #handle_incoming_numeric, #handle_incoming_ping, #handle_nick_taken, #handle_welcome, #initialize, #join, #msg, #nick, #part, #pong, #process_connect, #process_disconnect, #quit, #receive_line, setup
Methods included from Dispatchable
Constructor Details
This class inherits a constructor from Marvin::AbstractClient
Instance Attribute Details
#em_connection ⇒ Object
Returns the value of attribute em_connection.
49 50 51 |
# File 'lib/marvin/irc/client.rb', line 49 def em_connection @em_connection end |
Class Method Details
.add_reconnect(opts = {}) ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/marvin/irc/client.rb', line 121 def self.add_reconnect(opts = {}) Marvin::Logger.warn "Adding entry to reconnect to #{opts[:server]}:#{opts[:port]} in 15 seconds" EventMachine::add_timer(15) do Marvin::Logger.warn "Attempting to reconnect to #{opts[:server]}:#{opts[:port]}" Marvin::IRC::Client.connect(opts) end end |
.connect(opts = {}) ⇒ Object
105 106 107 108 109 |
# File 'lib/marvin/irc/client.rb', line 105 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 |
.run ⇒ Object
Starts the EventMachine loop and hence starts up the actual networking portion of the IRC Client.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/marvin/irc/client.rb', line 87 def self.run self.setup # So we have options etc settings = YAML.load_file(Marvin::Settings.root / "config/connections.yml") if settings.is_a?(Hash) EventMachine.epoll EventMachine::run do settings.each do |name, | settings = .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 |
.stop ⇒ Object
111 112 113 114 115 116 117 118 119 |
# File 'lib/marvin/irc/client.rb', line 111 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 |
Instance Method Details
#periodically(timing, event_callback) ⇒ Object
Registers a callback handle that will be periodically run.
130 131 132 133 |
# File 'lib/marvin/irc/client.rb', line 130 def periodically(timing, event_callback) callback = proc { self.dispatch event_callback.to_sym } EventMachine::add_periodic_timer(timing, &callback) end |
#send_line(*args) ⇒ Object
78 79 80 81 |
# File 'lib/marvin/irc/client.rb', line 78 def send_line(*args) args.each { |line| Marvin::Logger.debug ">> #{line.strip}" } em_connection.send_data *args end |