Class: Blur::Script::Commands::Command

Inherits:
Object
  • Object
show all
Defined in:
library/blur/script/commands.rb

Constant Summary collapse

DefaultOptions =
{ prefix: '.', hostmask: nil }

Instance Method Summary collapse

Constructor Details

#initialize(triggers, options = {}, &block) ⇒ Command

Returns a new instance of Command.



11
12
13
14
15
# File 'library/blur/script/commands.rb', line 11

def initialize triggers, options = {}, &block
  @triggers = Array === triggers ? triggers : [triggers]
  @options = DefaultOptions.merge options
  @block = block
end

Instance Method Details

#matches_trigger?(command) ⇒ Boolean (protected)

Returns:

  • (Boolean)


51
52
53
# File 'library/blur/script/commands.rb', line 51

def matches_trigger? command
  return @triggers.find{|trigger| trigger.to_s == command }
end

#received_message(user, channel, message) ⇒ Object

Called by the Commands module.

Calls the command block if the trigger matches the criteria.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'library/blur/script/commands.rb', line 20

def received_message user, channel, message
  prefix = @options[:prefix]

  # Return if the prefix don't match.
  return unless message.start_with? prefix

  # Return if the hostmask don't match.
  # FIXME: Maybe use globbing instead of regular expressions?
  unless @options[:hostmask].nil?
    hostmask = "#{user.nick}!#{user.name}@#{user.host}"

    return unless hostmask =~ @options[:hostmask]
  end

  command, args = split_message message

  # Strip the prefix and compare the trigger name.
  if self.matches_trigger? command
    @block.call user, channel, args
  end
end

#split_message(message) ⇒ Object (protected)



44
45
46
47
48
49
# File 'library/blur/script/commands.rb', line 44

def split_message message
  prefix = @options[:prefix]
  command, args = message[prefix.length..-1].split $;, 2

  return command, args
end