Class: Blur::Client

Inherits:
Object
  • Object
show all
Includes:
Callbacks, Handling, Logging
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'
DEFAULT_CONFIG =

The default configuration.

{
  'blur' => {
    'cache_dir' => 'cache/',
    'scripts_dir' => 'scripts/',
    'networks' => []
  },
  'scripts' => {},
}.freeze

Constants included from Logging

Logging::Levels

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Handling

#got_005, #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 Logging

#log

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.

Parameters:

  • options (Hash) (defaults to: {})

    the options for the client.

Options Hash (options):

  • :config_path (String)

    path to a configuration file.

  • :environment (String)

    the client environment.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'library/blur/client.rb', line 45

def initialize options = {}
  @scripts = {}
  @networks = []
  @config_path = options[:config_path]
  @environment = options[:environment]
  @verbose = options[:verbose] == true

  load_config!

  networks = @config['blur']['networks']

  if networks and networks.any?
    networks.each do |network_options|
      @networks.<< Network.new network_options, self
    end
  end

  trap 2, &method(:quit)
end

Instance Attribute Details

#configHash

Returns client configuration.

Returns:

  • (Hash)

    client configuration.



33
34
35
# File 'library/blur/client.rb', line 33

def config
  @config
end

#networksArray

Returns a list of instantiated networks.

Returns:

  • (Array)

    a list of instantiated networks.



31
32
33
# File 'library/blur/client.rb', line 31

def networks
  @networks
end

#scriptsHash

Returns initialized scripts.

Returns:

  • (Hash)

    initialized scripts.



35
36
37
# File 'library/blur/client.rb', line 35

def scripts
  @scripts
end

#verboseObject

Returns the value of attribute verbose.



37
38
39
# File 'library/blur/client.rb', line 37

def verbose
  @verbose
end

Instance Method Details

#connectObject

Connect to each network available that is not already connected, then proceed to start the run-loop.



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'library/blur/client.rb', line 67

def connect
  networks = @networks.reject &:connected?
  
  EventMachine.run do
    load_scripts!
    networks.each &:connect

    EventMachine.error_handler do |exception|
      log.error "#{exception.message ^ :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.

Parameters:

  • network (Network)

    the network that received the command.

  • command (Network::Command)

    the received command.



87
88
89
90
91
92
93
94
95
96
# File 'library/blur/client.rb', line 87

def got_message network, message
  if @verbose
    log "#{'' ^ :green} #{message.command.to_s.ljust(8, ' ') ^ :light_gray} #{message.parameters.map(&:inspect).join ' '}"
  end
  name = :"got_#{message.command.downcase}"

  if respond_to? name
    __send__ name, network, message
  end
end

#load_scripts!Object

Loads all scripts in the script directory.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'library/blur/client.rb', line 128

def load_scripts!
  scripts_dir = File.expand_path @config['blur']['scripts_dir']
  scripts_cache_dir = File.expand_path @config['blur']['cache_dir']

  Dir.glob File.join(scripts_dir, '*.rb') do |file|
    begin
      load file, true
    rescue Exception => e
      STDERR.puts "The script `#{file}' failed to load"
      STDERR.puts "#{e.class}: #{e.message}"
      STDERR.puts
      STDERR.puts 'Backtrace:', '---', e.backtrace
    end
  end

  scripts_config = @config['scripts']

  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

  emit :scripts_loaded
end

#network_connection_closed(network) ⇒ Object

Called when a network connection is either closed, or terminated.



99
100
101
# File 'library/blur/client.rb', line 99

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.

Parameters:

  • signal (optional, Symbol) (defaults to: :SIGINT)

    The signal received by the system, if any.



107
108
109
110
111
112
113
114
# File 'library/blur/client.rb', line 107

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.



117
118
119
120
121
122
123
124
125
# File 'library/blur/client.rb', line 117

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.



162
163
164
165
166
167
168
# File 'library/blur/client.rb', line 162

def unload_scripts!
  @scripts.each do |name, script|
    script.__send__ :unloaded if script.respond_to? :unloaded
  end.clear

  Blur.reset_scripts!
end