ircparser 0.6.2 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/ircparser.rb +1 -1
- data/lib/ircparser/stream.rb +6 -2
- data/lib/ircparser/wire/rfc.rb +43 -46
- data/test/test_message.rb +3 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c6b7566a57bb2e617b579c0ce2a174b66f96af0da0ed94efdeae3688f7a23cdb
|
4
|
+
data.tar.gz: 90e612dea1ea55b93896ebfdfb4d44880b021191120d2c49119785883b2e6bbc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ec325e4e96dbd73405c3b4815be8028e0243855af8c7f3889a34ad1ab7a3325bd64c11e8fcd08b63eea7444ec08bfe1dea56a6534f7dfe6dd7640b78baa04079
|
7
|
+
data.tar.gz: 53a1b4419baf3a2e56f51f5ff4e6bc586ac9b7217904704a80ae6a1bc603681b87a5f0d9e174ef2edc8dc73f54fcd05e35ffe2884d603b37bfe980912e3c27df
|
data/lib/ircparser.rb
CHANGED
data/lib/ircparser/stream.rb
CHANGED
@@ -38,8 +38,12 @@ module IRCParser
|
|
38
38
|
def append data
|
39
39
|
(@buffer ||= '') << data
|
40
40
|
while @buffer.slice! /(.*?)\r?\n/
|
41
|
-
|
42
|
-
|
41
|
+
begin
|
42
|
+
message = IRCParser::Message.parse $1
|
43
|
+
@block.call message, nil
|
44
|
+
rescue IRCParser::Error => error
|
45
|
+
@block.call nil, error
|
46
|
+
end
|
43
47
|
end
|
44
48
|
end
|
45
49
|
end
|
data/lib/ircparser/wire/rfc.rb
CHANGED
@@ -18,6 +18,41 @@ module IRCParser
|
|
18
18
|
# Internal: Implements objectification and stringification for the RFC wire format.
|
19
19
|
module RFCWireFormat
|
20
20
|
|
21
|
+
class MessageTokenizer
|
22
|
+
|
23
|
+
def initialize message
|
24
|
+
@message = message
|
25
|
+
# Skip any preceding whitespace. This is technically invalid but
|
26
|
+
# is implemented by several servers in the wild.
|
27
|
+
@position = message.index(/\S/) || 0
|
28
|
+
end
|
29
|
+
|
30
|
+
# Internal: Retrieves a space delimited token from the message.
|
31
|
+
def read_middle
|
32
|
+
return nil if @position >= @message.length
|
33
|
+
old_position = @position
|
34
|
+
@position = @message.index(' ', old_position) || @message.length
|
35
|
+
return nil unless @position - old_position > 0
|
36
|
+
token = @message.slice old_position...@position
|
37
|
+
@position = @message.index(/\S+/, @position) || @message.length
|
38
|
+
return token
|
39
|
+
end
|
40
|
+
|
41
|
+
# Internal: Retrieves a space delimited token that may be a <trailing> parameter.
|
42
|
+
#
|
43
|
+
# message - The message to retrieve the token from.
|
44
|
+
def read_trailing
|
45
|
+
return nil if @position >= @message.length
|
46
|
+
if @message[@position] == ':'
|
47
|
+
token = @message[@position+1..-1]
|
48
|
+
@position = @message.length
|
49
|
+
return token
|
50
|
+
end
|
51
|
+
return read_middle
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
21
56
|
# Internal: Objectifies a message from the RFC wire format to an IRCParser::Message.
|
22
57
|
#
|
23
58
|
# str - A String containing a message in the RFC wire format.
|
@@ -28,31 +63,28 @@ module IRCParser
|
|
28
63
|
raise IRCParser::Error.new(str), "message is not a String"
|
29
64
|
end
|
30
65
|
|
31
|
-
# Skip any preceding whitespace. This is technically invalid but
|
32
|
-
# is implemented by several servers in the wild.
|
33
|
-
message = str.lstrip
|
34
|
-
|
35
66
|
# Split the message up into an array of tokens.
|
36
|
-
|
67
|
+
tokenizer = MessageTokenizer.new str
|
68
|
+
current_token = tokenizer.read_middle
|
37
69
|
components = Hash.new
|
38
70
|
|
39
71
|
# Have we encountered IRCv3 message tags?
|
40
72
|
components[:tags] = Hash.new
|
41
73
|
if current_token != nil && current_token[0] == '@'
|
42
74
|
components[:tags] = self.__objectify_tags current_token
|
43
|
-
current_token =
|
75
|
+
current_token = tokenizer.read_middle
|
44
76
|
end
|
45
77
|
|
46
78
|
# Have we encountered the prefix of this message?
|
47
79
|
if current_token != nil && current_token[0] == ':'
|
48
80
|
components[:prefix] = self.__objectify_prefix current_token
|
49
|
-
current_token =
|
81
|
+
current_token = tokenizer.read_middle
|
50
82
|
end
|
51
83
|
|
52
84
|
# The command parameter is mandatory.
|
53
85
|
if current_token != nil
|
54
86
|
components[:command] = current_token.upcase
|
55
|
-
current_token =
|
87
|
+
current_token = tokenizer.read_trailing
|
56
88
|
else
|
57
89
|
raise IRCParser::Error.new(str), 'message is missing the command name'
|
58
90
|
end
|
@@ -61,7 +93,7 @@ module IRCParser
|
|
61
93
|
components[:parameters] = Array.new
|
62
94
|
while current_token != nil
|
63
95
|
components[:parameters] << current_token
|
64
|
-
current_token =
|
96
|
+
current_token = tokenizer.read_trailing
|
65
97
|
end
|
66
98
|
|
67
99
|
return IRCParser::Message.new components
|
@@ -119,40 +151,6 @@ module IRCParser
|
|
119
151
|
'\n' => "\n",
|
120
152
|
}
|
121
153
|
|
122
|
-
# Internal: Retrieves a space delimited token from the buffer.
|
123
|
-
#
|
124
|
-
# buffer - The buffer to retrieve the token from.
|
125
|
-
def self.__get_token buffer
|
126
|
-
return nil if buffer.empty?
|
127
|
-
position = buffer.index ' '
|
128
|
-
if position == nil
|
129
|
-
token = buffer.clone
|
130
|
-
buffer.clear
|
131
|
-
return token
|
132
|
-
end
|
133
|
-
token = buffer.slice! 0...position
|
134
|
-
position = buffer.index /\S+/
|
135
|
-
if position == nil
|
136
|
-
buffer.clear
|
137
|
-
else
|
138
|
-
buffer.slice! 0...position
|
139
|
-
end
|
140
|
-
return token
|
141
|
-
end
|
142
|
-
|
143
|
-
# Internal: Retrieves a space delimited token that may be a <trailing> parameter.
|
144
|
-
#
|
145
|
-
# buffer - The buffer to retrieve the token from.
|
146
|
-
def self.__get_final_token buffer
|
147
|
-
return nil if buffer.empty?
|
148
|
-
if buffer[0] == ':'
|
149
|
-
token = buffer[1..-1]
|
150
|
-
buffer.clear
|
151
|
-
return token
|
152
|
-
end
|
153
|
-
return self.__get_token buffer
|
154
|
-
end
|
155
|
-
|
156
154
|
# Internal: Objectifies the prefix from the RFC wire format to an IRCParser::Prefix.
|
157
155
|
#
|
158
156
|
# token - A String containing the prefix in the RFC wire format.
|
@@ -170,10 +168,9 @@ module IRCParser
|
|
170
168
|
tags = Hash.new
|
171
169
|
token[1..-1].split(';').each do |tag|
|
172
170
|
if tag =~ MATCH_TAG
|
173
|
-
value =
|
171
|
+
value = String.new
|
174
172
|
value_index = 0
|
175
173
|
while $~['value'] != nil && value_index < $~['value'].size
|
176
|
-
value ||= String.new
|
177
174
|
if $~['value'][value_index] == '\\'
|
178
175
|
escape = $~['value'].slice(value_index, 2)
|
179
176
|
if TAG_ESCAPES.include? escape
|
@@ -233,7 +230,7 @@ module IRCParser
|
|
233
230
|
tags.each.with_index do |tag, idx|
|
234
231
|
key, value = tag
|
235
232
|
buffer += key
|
236
|
-
unless value.nil?
|
233
|
+
unless value.nil? || value.empty?
|
237
234
|
buffer += '='
|
238
235
|
value.each_char do |chr|
|
239
236
|
if TAG_ESCAPES.has_value? chr
|
data/test/test_message.rb
CHANGED
@@ -25,7 +25,7 @@ describe IRCParser::Message do
|
|
25
25
|
@message = IRCParser::Message.parse @text
|
26
26
|
end
|
27
27
|
it 'should consist of the correct components' do
|
28
|
-
tags = { 'tag1' => 'value1', 'tag2' =>
|
28
|
+
tags = { 'tag1' => 'value1', 'tag2' => '', 'vendor1/tag3' => 'value2', 'vendor2/tag4' => '' }
|
29
29
|
parameters = [ 'param1', 'param2', 'param3 param3' ]
|
30
30
|
|
31
31
|
_(@message.tags).must_equal tags
|
@@ -64,7 +64,7 @@ describe IRCParser::Message do
|
|
64
64
|
@message = IRCParser::Message.parse @text
|
65
65
|
end
|
66
66
|
it 'should consist of the correct components' do
|
67
|
-
tags = { 'tag1' => 'value1', 'tag2' =>
|
67
|
+
tags = { 'tag1' => 'value1', 'tag2' => '', 'vendor1/tag3' => 'value2', 'vendor2/tag4' => '' }
|
68
68
|
parameters = [ 'param1', 'param2', 'param3 param3' ]
|
69
69
|
|
70
70
|
_(@message.tags).must_equal tags
|
@@ -83,7 +83,7 @@ describe IRCParser::Message do
|
|
83
83
|
@message = IRCParser::Message.parse @text
|
84
84
|
end
|
85
85
|
it 'should consist of the correct components' do
|
86
|
-
tags = { 'tag1' => 'value1', 'tag2' =>
|
86
|
+
tags = { 'tag1' => 'value1', 'tag2' => '', 'vendor1/tag3' => 'value2', 'vendor2/tag4' => '' }
|
87
87
|
parameters = [ 'param1', 'param2', 'param3 param3' ]
|
88
88
|
|
89
89
|
_(@message.tags).must_equal tags
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ircparser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sadie Powell
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-01-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|