Class: Net::IRC::Message
- Inherits:
-
Object
- Object
- Net::IRC::Message
- Includes:
- Net::IRC
- Defined in:
- lib/net/irc/message.rb
Defined Under Namespace
Classes: InvalidMessage, ModeParser, ServerConfig
Constant Summary
Constants included from Net::IRC
Instance Attribute Summary collapse
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
Class Method Summary collapse
-
.parse(str) ⇒ Object
Parse string and return new Message.
Instance Method Summary collapse
-
#[](n) ⇒ Object
Same as @params.
-
#ctcp? ⇒ Boolean
If the message is CTCP, return true.
- #ctcps ⇒ Object
-
#each(&block) ⇒ Object
Iterate params.
-
#initialize(prefix, command, params) ⇒ Message
constructor
A new instance of Message.
- #inspect ⇒ Object
-
#to_a ⇒ Object
Same as params.
-
#to_s ⇒ Object
(also: #to_str)
Stringfy message to raw IRC message.
Methods included from Net::IRC
ctcp_decode, ctcp_dequote, ctcp_encode, ctcp_quote
Constructor Details
Instance Attribute Details
#command ⇒ Object (readonly)
Returns the value of attribute command.
6 7 8 |
# File 'lib/net/irc/message.rb', line 6 def command @command end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
6 7 8 |
# File 'lib/net/irc/message.rb', line 6 def params @params end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
6 7 8 |
# File 'lib/net/irc/message.rb', line 6 def prefix @prefix end |
Class Method Details
.parse(str) ⇒ Object
Parse string and return new Message. If the string is invalid message, this method raises Net::IRC::Message::InvalidMessage.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/net/irc/message.rb', line 10 def self.parse(str) _, prefix, command, *rest = *PATTERN::MESSAGE_PATTERN.match(str) raise InvalidMessage, "Invalid message: #{str.dump}" unless _ case when rest[0] && !rest[0].empty? middle, trailer, = *rest when rest[2] && !rest[2].empty? middle, trailer, = *rest[2, 2] when rest[1] params = [] trailer = rest[1] when rest[3] params = [] trailer = rest[3] else params = [] end params ||= middle.split(/ /)[1..-1] params << trailer if trailer new(prefix, command, params) end |
Instance Method Details
#[](n) ⇒ Object
Same as @params.
42 43 44 |
# File 'lib/net/irc/message.rb', line 42 def [](n) @params[n] end |
#ctcp? ⇒ Boolean
If the message is CTCP, return true.
80 81 82 83 84 |
# File 'lib/net/irc/message.rb', line 80 def ctcp? #message = @params[1] #message[0] == ?\01 && message[-1] == ?\01 /\x01(?>[^\x00\x01\r\n]*)\x01/ === @params[1] end |
#ctcps ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'lib/net/irc/message.rb', line 86 def ctcps = [] @params[1].gsub!(/\x01(?>[^\x00\x01\r\n]*)\x01/) do << ctcp_decode($&) "" end end |
#each(&block) ⇒ Object
Iterate params.
47 48 49 |
# File 'lib/net/irc/message.rb', line 47 def each(&block) @params.each(&block) end |
#inspect ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'lib/net/irc/message.rb', line 95 def inspect "#<%s:0x%x prefix:%s command:%s params:%s>" % [ self.class, self.object_id, @prefix, @command, @params.inspect ] end |
#to_a ⇒ Object
Same as params.
75 76 77 |
# File 'lib/net/irc/message.rb', line 75 def to_a @params.dup end |
#to_s ⇒ Object Also known as: to_str
Stringfy message to raw IRC message.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/net/irc/message.rb', line 52 def to_s str = "" str << ":#{@prefix} " unless @prefix.empty? str << @command if @params f = false @params.each do |param| f = !f && (param.empty? || param[0] == ?: || param.include?(" ")) str << " " str << ":" if f str << param end end str << "\x0D\x0A" str end |