Class: Net::IRC::Message

Inherits:
Object
  • Object
show all
Includes:
Net::IRC
Defined in:
lib/net/irc/message.rb

Defined Under Namespace

Classes: InvalidMessage, ModeParser, ServerConfig

Constant Summary

Constants included from Net::IRC

COMMANDS, VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Net::IRC

ctcp_decode, ctcp_dequote, ctcp_encode, ctcp_quote

Constructor Details

#initialize(prefix, command, params) ⇒ Message

Returns a new instance of Message.



35
36
37
38
39
# File 'lib/net/irc/message.rb', line 35

def initialize(prefix, command, params)
	@prefix  = Prefix.new(prefix.to_s)
	@command = command
	@params  = params
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



6
7
8
# File 'lib/net/irc/message.rb', line 6

def command
  @command
end

#paramsObject (readonly)

Returns the value of attribute params.



6
7
8
# File 'lib/net/irc/message.rb', line 6

def params
  @params
end

#prefixObject (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.

Raises:



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.

Returns:

  • (Boolean)


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

#ctcpsObject



86
87
88
89
90
91
92
93
# File 'lib/net/irc/message.rb', line 86

def ctcps
	messages = []
	@params[1].gsub!(/\x01(?>[^\x00\x01\r\n]*)\x01/) do
		messages << ctcp_decode($&)
		""
	end
	messages
end

#each(&block) ⇒ Object

Iterate params.



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

def each(&block)
	@params.each(&block)
end

#inspectObject



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_aObject

Same as params.



75
76
77
# File 'lib/net/irc/message.rb', line 75

def to_a
	@params.dup
end

#to_sObject 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