Class: IRCMachine::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/ircmachine/message.rb

Constant Summary collapse

SHORTNAME =

Based on RFC 2812, section 2.3.1

/[a-z0-9][a-z0-9-]*[a-z0-9]*/in
HOSTNAME =
/#{SHORTNAME}(?:\.#{SHORTNAME})*/n
SERVERNAME =
/#{HOSTNAME}/n
SPECIAL =
/[\x5b-\x60\x7b-\x7d]/n
NICKNAME =
/[a-z\x5b-\x60\x7b-\x7d][a-z0-9\x5b-\x60\x7b-\x7d-]{,8}/in
USER =
/[\x01-\x09\x0b-\x0c\x0e-\x1f\x21-\x3f\x41-\xff]+/n
IP4ADDR =
/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/n
IP6ADDR =
/(?:[0-9a-f]+(?::[0-9a-f]+){7})|(?:0:0:0:0:0:(?:0|ffff):#{IP4ADDR})/in
HOSTADDR =
/#{IP4ADDR}|#{IP6ADDR}/n
HOST =
/#{HOSTNAME}|#{HOSTADDR}/n
PREFIX =
/#{SERVERNAME}|(?:#{NICKNAME}((?:!#{USER})?@#{HOST})?)/n
COMMAND =
/[a-z]+|[0-9]{3}/in
NOSPCRLFCL =
/[\x01-\x09\x0b-\x0c\x0e-\x1f\x21-\x39\x3b-\xff]/n
TRAILING =
/(?:[: ]|#{NOSPCRLFCL})*/n
MIDDLE =
/#{NOSPCRLFCL}(?::|#{NOSPCRLFCL})*/n
PARAMS =
/(?:(?: #{MIDDLE}){,14}(?: :#{TRAILING})?)|(?:(?: #{MIDDLE}){,14}(?: :?#{TRAILING})?)/n
MESSAGE =
/^(?<prefix>:#{PREFIX} )?(?<command>#{COMMAND})(?<params>#{PARAMS})?\r\n$/n

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix, command, params) ⇒ Message

Returns a new instance of Message.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ircmachine/message.rb', line 24

def initialize(prefix, command, params)
  @prefix   = prefix && prefix.strip
  @command  = command

  params = (params || '').strip
  @params =
    if params[0] == ':'
      [ params ]
    elsif (trail_index = params.index(' :')).nil?
      params.split(/ +/)
    else
      params[0...trail_index].split(/ +/).push(params[trail_index..-1].strip)
    end
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



22
23
24
# File 'lib/ircmachine/message.rb', line 22

def command
  @command
end

#paramsObject (readonly)

Returns the value of attribute params.



22
23
24
# File 'lib/ircmachine/message.rb', line 22

def params
  @params
end

#prefixObject (readonly)

Returns the value of attribute prefix.



22
23
24
# File 'lib/ircmachine/message.rb', line 22

def prefix
  @prefix
end

Class Method Details

.parse(str) ⇒ Object



55
56
57
58
59
# File 'lib/ircmachine/message.rb', line 55

def self.parse(str)
  unless (m = MESSAGE.match(str)).nil?
    Message.new(m[:prefix], m[:command], m[:params])
  end
end

Instance Method Details

#command?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/ircmachine/message.rb', line 51

def command?
  !reply?
end

#reply?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/ircmachine/message.rb', line 47

def reply?
  !!(@command =~ /^\d+$/)
end

#to_sObject



39
40
41
42
43
44
45
# File 'lib/ircmachine/message.rb', line 39

def to_s
  str = ''
  str << "#{prefix} " unless prefix.nil?
  str << command
  str << " #{params.join(' ')}" unless params.empty?
  str << "\r\n"
end