Class: Marvin::Parsers::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/marvin/parsers/command.rb

Overview

A single incoming / outgoing irc command, with handy utilities to convert it to a Marvin::IRC::Event instance.

Constant Summary collapse

@@commands =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Command

Create a new command from the given raw message.



14
15
16
17
# File 'lib/marvin/parsers/command.rb', line 14

def initialize(raw)
  self.raw = raw
  self.params = []
end

Instance Attribute Details

#codeObject

Returns the value of attribute code.



10
11
12
# File 'lib/marvin/parsers/command.rb', line 10

def code
  @code
end

#paramsObject

Returns the value of attribute params.



10
11
12
# File 'lib/marvin/parsers/command.rb', line 10

def params
  @params
end

#prefixObject

Returns the value of attribute prefix.



10
11
12
# File 'lib/marvin/parsers/command.rb', line 10

def prefix
  @prefix
end

#rawObject

Returns the value of attribute raw.



10
11
12
# File 'lib/marvin/parsers/command.rb', line 10

def raw
  @raw
end

Instance Method Details

#to_eventObject

From the given command and arguments / params, attempt to recognize the command and convert it to an event which can be used for other stuff.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/marvin/parsers/command.rb', line 22

def to_event
  # If we have a numeric...
  if @code =~ /^\d+$/
    ev = @@commands[:numeric].dup
    data = @params[0..-2]
    data << "#{@params.last.include?(" ") ? ":" : ""}#{@params.last}"
    ev.raw_arguments = [self.code.to_s, data.join(" ")]
  elsif code == "PRIVMSG" && params.last =~ /^\001(.*)\001$/
    results = $1.to_s
    if results =~ /^ACTION /
      name, value = :action, results.gsub(/^ACTION /, '')
    else
      name, value = :ctcp, results
    end
    self.params[-1] = value
    ev = @@commands[name].dup
    ev.raw_arguments = self.params
  else
    ev = @@commands[self.code.to_sym]
    return nil if ev.nil?
    ev = ev.dup
    ev.raw_arguments = self.params
  end
  ev.prefix = self.prefix
  return ev
end