Class: Rubot::Core::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/core/dispatcher.rb

Overview

The middle man. The Dispatcher takes incomming messages from the server and determines the appropriate action to take, handing the messages off to commands and listeners.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Dispatcher

Creates an instance of Dispatcher using the given config hash. Values expected to be in this hash are:

  • function_character - The character used to denote a command

  • auth_list - Comma separated string of authenticated users

Parameters

config<Hash>

Hash containing config values



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/core/dispatcher.rb', line 35

def initialize(config)
  @config = config
  @function_character = @config["function_character"]

  @auth_list = (config["auth_list"] || "").split(",")
  @resource_lock = Mutex.new
  
  reload
  # runners are only run on server connection, so there's no need them to be in reload 
  load_dir "runners", @runners = {}
  load_dir "exiters", @exiters = {}
end

Instance Attribute Details

#commandsObject (readonly)

Hash that holds instances of commands registered with the dispatcher. There is a single instance of each command, with its name, and aliases, as keys to that instance.



12
13
14
# File 'lib/core/dispatcher.rb', line 12

def commands
  @commands
end

#configObject (readonly)

The config hash that was used to create the Dispatcher instance.



22
23
24
# File 'lib/core/dispatcher.rb', line 22

def config
  @config
end

#function_characterObject (readonly)

The value used to denote a command, this is pulled from config.



19
20
21
# File 'lib/core/dispatcher.rb', line 19

def function_character
  @function_character
end

#listenersObject (readonly)

Hash that holds instances of listeners registered with the dispatcher. There is a single instance of each listener, with its name as the key to that instance.



16
17
18
# File 'lib/core/dispatcher.rb', line 16

def listeners
  @listeners
end

#resource_lockObject (readonly)

Mutex that should be used when accessing anything in /resources. This will most likely be moved to the resource manager when it is implemented.



26
27
28
# File 'lib/core/dispatcher.rb', line 26

def resource_lock
  @resource_lock
end

Instance Method Details

#add_auth(nick) ⇒ Object

Adds nick to the authenticated list.

Parameters

nick<String>

The nick to add to the authenticated list



109
110
111
# File 'lib/core/dispatcher.rb', line 109

def add_auth(nick)
  @auth_list << nick unless authenticated?(nick)
end

#authenticated?(nick) ⇒ Boolean

Determines if the given nick has authenticated with the bot.

Parameters

nick<String>

The nick to check for auth privledges

Returns:

  • (Boolean)


101
102
103
# File 'lib/core/dispatcher.rb', line 101

def authenticated?(nick)
  @auth_list.include?(nick)
end

#command_from_message(message) ⇒ Object

Finds a command based on the message alias, and determines if the invoking user is authenticated.

Parameters

message<Rubot::Irc::Message>

The message to be used



91
92
93
94
95
# File 'lib/core/dispatcher.rb', line 91

def command_from_message(message)
  command = @commands[message.alias]        
  message.authenticated = authenticated?(message.from) unless command.nil?
  command
end

#connected(server) ⇒ Object

Called when successful connection is made to a server. This is when the runners are executed.

Parameters

server<Rubot::Irc::Server>

The server instance that has successfully connected



53
54
55
# File 'lib/core/dispatcher.rb', line 53

def connected(server)
  run_runners(server)
end

#handle_message(server, message) ⇒ Object

Determines how to handle a message from the server. If the message fits the format of a command, an attempt is made to find and execute that command. Otherwise, the message is passed to the listeners.

Parameters

server<Rubot::Irc::Server>

The server where the messge was received

message<Rubot:Irc::Message>

The message to handle



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/core/dispatcher.rb', line 70

def handle_message(server, message)
  Thread.new do
    if message.body.start_with?(@function_character) && message.body =~ /^.([a-z_]+)( .+)?$/i
      message.body = $2.nil? ? "" : $2.strip # remove the function name from the message
      message.alias = $1.underscore.to_sym

      command = command_from_message(message)
      command.run(server, message) unless command.nil?
    elsif message.from != server.nick
      @listeners.each_value do |listener|
        listener.execute(server, message)
      end
    end
  end
end

#on_quit(server) ⇒ Object

Handles the on quit event, which is triggered when the server recieves the quit message or an interrupt is triggered.

Parameters

server<Rubot::Irc::Server>

The server that is quiting.



126
127
128
# File 'lib/core/dispatcher.rb', line 126

def on_quit(server)
  execute_exiters(server)
end

#reloadObject

Exposed method to reload all commands and listeners.



58
59
60
61
# File 'lib/core/dispatcher.rb', line 58

def reload
  load_dir "commands", @commands = {}
  load_dir "listeners", @listeners = {}
end

#remove_auth(nick) ⇒ Object

Removes nick from the authenticated list.

Parameters

nick<String>

The nick to remove from the authenticated list



117
118
119
# File 'lib/core/dispatcher.rb', line 117

def remove_auth(nick)
  @auth_list.delete nick
end