Class: Blur::Client

Inherits:
Object
  • Object
show all
Includes:
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

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

Constructor Details

#initialize(options) ⇒ Client

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

Parameters:

  • options (Hash)

    the options for the client.

Options Hash (options):

  • networks (Array)

    list of hashes that contain network options.



26
27
28
29
30
31
32
33
34
35
36
# File 'library/blur/client.rb', line 26

def initialize options
  @options   = options
  @scripts   = []
  @networks  = []
  @callbacks = {}
  
  @networks = @options[:networks].map {|options| Network.new options }
  
  load_scripts
  trap 2, &method(:quit)
end

Instance Attribute Details

#networksArray

Returns a list of instantiated networks.

Returns:

  • (Array)

    a list of instantiated networks.



18
19
20
# File 'library/blur/client.rb', line 18

def networks
  @networks
end

#optionsArray

Returns the options that is passed upon initialization.

Returns:

  • (Array)

    the options that is passed upon initialization.



14
15
16
# File 'library/blur/client.rb', line 14

def options
  @options
end

#scriptsArray

Returns a list of scripts that is loaded during runtime.

Returns:

  • (Array)

    a list of scripts that is loaded during runtime.



16
17
18
# File 'library/blur/client.rb', line 16

def scripts
  @scripts
end

Instance Method Details

#connectObject

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



40
41
42
43
44
45
46
47
48
49
50
51
# File 'library/blur/client.rb', line 40

def connect
  networks = @networks.select {|network| not network.connected? }
  
  EventMachine.run do
    EventMachine.error_handler{|e| p e }

    networks.each do |network|
      network.delegate = self
      network.connect
    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.

Parameters:

  • network (Network)

    the network that received the command.

  • command (Network::Command)

    the received command.



59
60
61
62
63
64
65
66
# File 'library/blur/client.rb', line 59

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_scriptsObject

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



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

def load_scripts
  # Load script extensions.
  Script.load_extensions!

  # Load the scripts.
  script_path = File.dirname $0
  
  Dir.glob("#{script_path}/scripts/*.rb").each do |path|
    script = Script.new path
    script.__client = self
    
    @scripts << script
  end
end

#network_connection_closed(network) ⇒ Object

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



97
98
99
# File 'library/blur/client.rb', line 97

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.



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

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:



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

def unload_scripts
  # Unload script extensions.
  Script.unload_extensions!

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