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

Error =

Raise a client error.

Class.new StandardError
DefaultOptions =

The default client options.

{
  environment: (ENV['BLUR_ENV'] || 'development'),
  config: 'config.yml'
}.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, #on

Constructor Details

#initialize(options) ⇒ Client

Instantiates the client, stores the options, instantiates the networks and then loads available scripts.

Options Hash (options):

  • :config (String)

    path to a configuration file.

  • :environment (String)

    the client environment.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'library/blur/client.rb', line 36

def initialize options
  @scope     = Scope.new self
  @scripts   = []
  @options   = DefaultOptions.merge options
  @networks  = []
  @config    = {}

  if options[:config]
    load_config! options[:config]
  end

  unless @config.key? 'blur'
    @config['blur'] = {}
  end
  
  networks = @config['blur']['networks']
  if networks and networks.any?
    networks.each do |network_options|
      p network_options
      @networks.<< Network.new network_options, self
    end
  end

  load_scripts!
  trap 2, &method(:quit)
end

Instance Attribute Details

#configHash



28
29
30
# File 'library/blur/client.rb', line 28

def config
  @config
end

#networksArray



26
27
28
# File 'library/blur/client.rb', line 26

def networks
  @networks
end

#optionsArray



24
25
26
# File 'library/blur/client.rb', line 24

def options
  @options
end

Instance Method Details

#connectObject

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



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

def connect
  networks = @networks.select {|network| not network.connected? }
  
  EventMachine.run do
    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_command(network, command) ⇒ 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.



84
85
86
87
88
89
90
91
# File 'library/blur/client.rb', line 84

def got_command network, command
  log "#{'' ^ :green} #{command.name.to_s.ljust(8, ' ') ^ :light_gray} #{command.params.map(&:inspect).join ' '}"
  name = :got_#{command.name.downcase}"
  
  if respond_to? name
    __send__ name, network, command
  end
end

#load_config!(path) ⇒ Object

Load the user-specified configuration file.



140
141
142
143
144
145
146
147
148
149
150
151
# File 'library/blur/client.rb', line 140

def load_config! path
  config = YAML.load_file path
  environment = @options[:environment]

  if config.key? environment
    @config = config[environment]

    emit :config_loaded
  else
    raise Error, "No configuration found for specified environment `#{environment}'"
  end
end

#load_scripts!Object

Searches for scripts in working_directory/scripts and then loads them.



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'library/blur/client.rb', line 94

def load_scripts!
  # Load the scripts.
  script_path = File.dirname $0
  
  Dir.glob("#{script_path}/scripts/*.rb").each do |path|
    begin
      @scope.eval_file path
    rescue => exception
      puts "Error occured when loading script `#{path}': #{exception}"
      puts exception.backtrace
    end
  end
end

#network_connection_closed(network) ⇒ Object

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



118
119
120
# File 'library/blur/client.rb', line 118

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.



126
127
128
129
130
131
132
133
134
135
# File 'library/blur/client.rb', line 126

def quit signal = :SIGINT
  unload_scripts
  
  @networks.each do |network|
    network.transmit :QUIT, "Got SIGINT?"
    network.disconnect
  end
  
  EventMachine.stop
end

#unload_scriptsObject

Unload all scripts gracefully that have been loaded into the client.

See Also:

  • Script#unload!


111
112
113
114
115
# File 'library/blur/client.rb', line 111

def unload_scripts
  @scripts.each do |script|
    script.unload!
  end.clear
end