Class: Blur::Client
- Inherits:
-
Object
- Object
- Blur::Client
- Defined in:
- library/blur/client.rb,
library/blur/handling.rb
Overview
The Client
class is the controller of the low-level access.
It stores networks, scripts and callbacks, and is also encharge of distributing the incoming commands to the right networks and scripts.
Defined Under Namespace
Modules: Handling
Constant Summary collapse
- Error =
Client error.
Class.new StandardError
- ENVIRONMENT =
The default environment.
ENV['BLUR_ENV'] || 'development'.freeze
- DEFAULT_CONFIG =
The default configuration.
{ 'blur' => { 'cache_dir' => 'cache/', 'scripts_dir' => 'scripts/', 'networks' => [] }, 'scripts' => {}, }.freeze
Constants included from Logging
Instance Attribute Summary collapse
-
#config ⇒ Hash
Client configuration.
-
#config_path ⇒ String
The path to the currently used config file.
-
#networks ⇒ Array
A list of instantiated networks.
-
#scripts ⇒ Hash
Initialized scripts.
-
#verbose ⇒ Boolean
Whether verbose logging is enabled.
Instance Method Summary collapse
-
#connect ⇒ Object
Connect to each network available that is not already connected, then proceed to start the run-loop.
-
#got_message(network, message) ⇒ Object
Is called when a command have been received and parsed, this distributes the command to the loader, which then further distributes it to events and scripts.
-
#initialize(options = {}) ⇒ Client
constructor
Instantiates the client, stores the options, instantiates the networks and then loads available scripts.
-
#initialize_superscripts ⇒ Object
Instantiates each
SuperScript
in theBlur.scripts
list by manually allocating an instance and calling #initialize on it, then the instance is stored in Client#scripts. -
#load_script_file(file_path) ⇒ Object
Loads the given
file_path
as a Ruby script, wrapping it in an anonymous module to protect our global namespace. -
#load_scripts! ⇒ Object
Loads all scripts in the script directory.
-
#network_connection_closed(network) ⇒ Object
Called when a network connection is either closed, or terminated.
-
#quit(signal = :SIGINT) ⇒ Object
Try to gracefully disconnect from each network, unload all scripts and exit properly.
-
#reload! ⇒ Object
Reloads configuration file and scripts.
-
#unload_scripts! ⇒ Object
Unloads initialized scripts and superscripts.
Methods included from Logging
Methods included from Handling
#got_001, #got_005, #got_900, #got_904, #got_authenticate, #got_cap, #got_channel_topic, #got_end_of_motd, #got_join, #got_kick, #got_mode, #got_name_reply, #got_nick, #got_part, #got_ping, #got_privmsg, #got_quit, #got_topic
Methods included from Callbacks
#callbacks, #emit, #notify_scripts, #on
Constructor Details
#initialize(options = {}) ⇒ Client
Instantiates the client, stores the options, instantiates the networks and then loads available scripts.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'library/blur/client.rb', line 48 def initialize = {} @scripts = {} @networks = [] @config_path = [:config_path] @environment = [:environment] @verbose = [:verbose] == true unless @config_path raise ConfigError, 'missing config file path in :config_path option' end load_config! networks = @config['blur']['networks'] if networks&.any? networks.each do || @networks.<< Network.new , self end end trap 2, &method(:quit) end |
Instance Attribute Details
#config ⇒ Hash
Returns client configuration.
34 35 36 |
# File 'library/blur/client.rb', line 34 def config @config end |
#config_path ⇒ String
Returns the path to the currently used config file.
40 41 42 |
# File 'library/blur/client.rb', line 40 def config_path @config_path end |
#networks ⇒ Array
Returns a list of instantiated networks.
32 33 34 |
# File 'library/blur/client.rb', line 32 def networks @networks end |
#scripts ⇒ Hash
Returns initialized scripts.
36 37 38 |
# File 'library/blur/client.rb', line 36 def scripts @scripts end |
#verbose ⇒ Boolean
Returns whether verbose logging is enabled.
38 39 40 |
# File 'library/blur/client.rb', line 38 def verbose @verbose end |
Instance Method Details
#connect ⇒ Object
Connect to each network available that is not already connected, then proceed to start the run-loop.
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'library/blur/client.rb', line 74 def connect networks = @networks.reject &:connected? EventMachine.run do load_scripts! networks.each &:connect EventMachine.error_handler do |exception| log.error "#{exception. ^ :bold} on line #{exception.line.to_s ^ :bold}" puts exception.backtrace.join "\n" end end end |
#got_message(network, message) ⇒ Object
Is called when a command have been received and parsed, this distributes the command to the loader, which then further distributes it to events and scripts.
94 95 96 97 98 99 100 101 102 103 |
# File 'library/blur/client.rb', line 94 def network, if @verbose log "#{'←' ^ :green} #{.command.to_s.ljust(8, ' ') ^ :light_gray} #{.parameters.map(&:inspect).join ' '}" end name = :"got_#{.command.downcase}" if respond_to? name __send__ name, network, end end |
#initialize_superscripts ⇒ Object
Instantiates each SuperScript
in the Blur.scripts
list by manually allocating an instance and calling #initialize on it, then the instance is stored in Client#scripts.
177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'library/blur/client.rb', line 177 def initialize_superscripts scripts_config = @config['scripts'] scripts_cache_dir = File. @config['blur']['cache_dir'] Blur.scripts.each do |name, superscript| script = superscript.allocate script.cache = ScriptCache.load name, scripts_cache_dir script.config = scripts_config.fetch name, {} script._client_ref = self script.send :initialize @scripts[name] = script end end |
#load_script_file(file_path) ⇒ Object
Loads the given file_path
as a Ruby script, wrapping it in an anonymous module to protect our global namespace.
162 163 164 165 166 167 168 169 |
# File 'library/blur/client.rb', line 162 def load_script_file file_path load file_path, true rescue Exception => exception warn "The script `#{file_path}' failed to load" warn "#{exception.class}: #{exception.}" warn '' warn 'Backtrace:', '---', exception.backtrace end |
#load_scripts! ⇒ Object
Loads all scripts in the script directory.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'library/blur/client.rb', line 135 def load_scripts! scripts_dir = File. @config['blur']['scripts_dir'] script_file_paths = Dir.glob File.join scripts_dir, '*.rb' # Sort the script file paths by file name so they load by alphabetical # order. # # This will make it possible to create a script called '10_database.rb' # which will be loaded before '20_settings.rb' and non-numeric prefixes # will be loaded after that. script_file_paths = script_file_paths.sort do |a, b| File.basename(a) <=> File.basename(b) end script_file_paths.each { |script_path| load_script_file script_path } initialize_superscripts emit :scripts_loaded end |
#network_connection_closed(network) ⇒ Object
Called when a network connection is either closed, or terminated.
106 107 108 |
# File 'library/blur/client.rb', line 106 def network_connection_closed network emit :connection_close, network end |
#quit(signal = :SIGINT) ⇒ Object
Try to gracefully disconnect from each network, unload all scripts and exit properly.
114 115 116 117 118 119 120 121 |
# File 'library/blur/client.rb', line 114 def quit signal = :SIGINT @networks.each do |network| network.transmit :QUIT, 'Got SIGINT?' network.disconnect end EventMachine.stop end |
#reload! ⇒ Object
Reloads configuration file and scripts.
124 125 126 127 128 129 130 131 132 |
# File 'library/blur/client.rb', line 124 def reload! EM.schedule do unload_scripts! load_config! load_scripts! yield if block_given? end end |
#unload_scripts! ⇒ Object
Unloads initialized scripts and superscripts.
This method will call #unloaded on the instance of each loaded script to give it a chance to clean up any resources.
196 197 198 199 200 201 202 |
# File 'library/blur/client.rb', line 196 def unload_scripts! @scripts.each do |name, script| script.__send__ :unloaded if script.respond_to? :unloaded end.clear Blur.reset_scripts! end |