Class: IRCd::Message

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

Direct Known Subclasses

Command::Command, Reply::Reply

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.



62
63
64
65
66
# File 'lib/rupircd/message.rb', line 62

def initialize(prefix, command, params)
  @prefix  = prefix
  @command = command
  @params  = params
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



67
68
69
# File 'lib/rupircd/message.rb', line 67

def command
  @command
end

#paramsObject (readonly)

Returns the value of attribute params.



67
68
69
# File 'lib/rupircd/message.rb', line 67

def params
  @params
end

#prefixObject (readonly)

Returns the value of attribute prefix.



67
68
69
# File 'lib/rupircd/message.rb', line 67

def prefix
  @prefix
end

Class Method Details

.build(prefix, command, params) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rupircd/message.rb', line 50

def self.build(prefix, command, params)
  cmd = command
  cmd.upcase! if String === cmd
  if Command::Commands.include?(cmd)
    Command::Commands[cmd.upcase].new(prefix, cmd, params)
  elsif Reply::Replies.include?(cmd)
    Reply::Replies[cmd].new(prefix, cmd, params)
  else
    raise UnknownCommand, command
  end
end

.parse(msg) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rupircd/message.rb', line 25

def self.parse(msg)
  prefix = nil
  command = ""
  tmp = []
  if msg[0] == ?:
    prefix, command, *tmp = msg[1..-1].split(" ")
  else
    command, *tmp = msg.split(" ")
  end
  command
  params = []
  eoc = false
  tmp.each_with_index{|ar,ind|
    if eoc
      params[-1] += ' ' + ar
    elsif ar[0] == ?:
      eoc = true
      params << ar[1..-1]
    else
      params << ar
    end
  }
  self.build(prefix, command, params)
end

Instance Method Details

#inspectObject



107
108
109
110
# File 'lib/rupircd/message.rb', line 107

def inspect
  sprintf('#<%s:0x%x prefix:%s command:%s params:%s>',
          self.class, self.object_id, @prefix, @command, @params.inspect)
end

#to_aObject



103
104
105
# File 'lib/rupircd/message.rb', line 103

def to_a
  [@prefix, @command, @params]
end

#to_sObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rupircd/message.rb', line 69

def to_s
  str = ''
  if @prefix && !@prefix.to_s.empty?
    str << ':'
    str << @prefix
    str << ' '
  end

  str << @command

  if @params
    f = false
    @params.each_with_index do |param, ind|
      param = param.to_s
      str << ' '
      if !f && (param.empty? || /[: ]/ =~ param)
        str << ':'
        str << param
        f = true
      elsif !f && ind == params.size - 1
        str << ':'
        str << param
      else
        str << param
      end
    end
  end

  str << "\x0D\x0A"

  str
end