Class: Blur::Network::Command

Inherits:
Object
  • Object
show all
Defined in:
library/blur/network/command.rb

Overview

The Command class is used for encapsulating the command-lines.

Blur is using regular-expression for parsing, this is to be replaced with a more native way of parsing, making it much, much easier on the processor.

Constant Summary

Pattern =

The arbitrary regex pattern.

/^(?:[:@]([^\s]+) )?([^\s]+)(?: ((?:[^:\s][^\s]* ?)*))?(?: ?:(.*))?$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, params = []) ⇒ Command

Instantiate a command.

See Also:



45
46
47
# File 'library/blur/network/command.rb', line 45

def initialize name, params = []
  @name, @params = name, params
end

Instance Attribute Details

#nameSymbol, Fixnum

Returns the command name.

Examples:

332 or :quit


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

def name
  @name
end

#paramsArray



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

def params
  @params
end

#prefixString



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

def prefix
  @prefix
end

Class Method Details

.parse(data) ⇒ Command

Parse a line and encapsulate it as a Command.

Examples:

Command.parse "ChanServ!ChanServ@services.uplink.io MODE #uplink +v mk"
# => #<Blur::Network::Command … >


29
30
31
32
33
34
35
36
37
# File 'library/blur/network/command.rb', line 29

def self.parse data
  match = data.strip.match Pattern
  prefix, name, args, extra = match.captures
  params = extra ? args.split << extra : args.split

  new(name, params).tap do |this|
    this.prefix = prefix
  end
end

Instance Method Details

#[](index) ⇒ Object

Get a parameter by its index.



40
# File 'library/blur/network/command.rb', line 40

def [] index; @params[index] end

#senderString, OpenStruct

Note:

the return value is a string if it's a hostname, and an openstruct with the attributes #nickname, #username and #hostname if it's a hostmask.

Get the sender of the command.



56
57
58
59
60
61
62
63
64
# File 'library/blur/network/command.rb', line 56

def sender
  return @sender if @sender

  if prefix =~ /^(\S+)!(\S+)@(\S+)$/
    @sender = OpenStruct.new nickname: $1, username: $2, hostname: $3
  else
    @sender = prefix
  end
end

#to_sString

Convert it to an IRC-compliant line.



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

def to_s
  String.new.tap do |line|
    line << "#{prefix} " if prefix
    line << name.to_s

    params.each_with_index do |param, index|
      line << ' '
      line << ?: if index == params.length - 1 and param =~ /[ :]/
      line << param.to_s
    end
  end
end