teamspeak-ruby 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a1d8e641ccb877703ab18aa73f28d2de90e990d7
4
- data.tar.gz: 0d468dd3fa41c8641b193e57c5a69a5dce4d7115
3
+ metadata.gz: 48a7dab7b91961d54bacdea425abe4a996dc24f0
4
+ data.tar.gz: 03f19e56293c044f8fb44e416f6d03ce245072e9
5
5
  SHA512:
6
- metadata.gz: 920afa92b2e547f2af1454168fd6a159a6dadccfb73bebc594879dbe83de476d4decb0f819471444f681c8c577f18ff4bca3600e4b5720be84c023702a1ea7b0
7
- data.tar.gz: 24823d5c09834c13629036aa28bb7d32b1843d5568b6a7d5ccdbf35dde2d40bf503c4531c385ffb56329157b74863339534213376c4966608f94e52bf95db903
6
+ metadata.gz: acc5f352ce5c12942d29d498aa0cb68bcc241295ed2dd469fc3ef2c991afe5a6fbedd158149d3634e59f18e62f08711a90107a35517de64e42bb7b79c18b988e
7
+ data.tar.gz: 5e7cfecfc5f0bca28bac5a8e3034f1c76299fbd6d88879614b06809ac501ee9a7de6c82661bba11081e8a789920664c69b27e92173f5c2be091653989676f603
@@ -1,3 +1,3 @@
1
- require 'teamspeak-ruby/client'
2
- require 'teamspeak-ruby/exceptions'
3
- require 'teamspeak-ruby/version'
1
+ require 'teamspeak-ruby/client'
2
+ require 'teamspeak-ruby/exceptions'
3
+ require 'teamspeak-ruby/version'
@@ -1,146 +1,178 @@
1
- require 'socket'
2
-
3
- module Teamspeak
4
- class Client
5
- # Initializes Client
6
- #
7
- # connect('voice.domain.com', 88888)
8
- def initialize(host = 'localhost', port = 10011)
9
- connect(host, port)
10
- end
11
-
12
- # Connects to a TeamSpeak 3 server
13
- #
14
- # connect('voice.domain.com', 88888)
15
- def connect(host = 'localhost', port = 10011)
16
- @sock = TCPSocket.new(host, port)
17
-
18
- # Check if the response is the same as a normal teamspeak 3 server.
19
- if @sock.gets.strip != 'TS3'
20
- raise InvalidServer, 'Server is not responding as a normal TeamSpeak 3 server.'
21
- end
22
-
23
- # Remove useless text from the buffer.
24
- @sock.gets
25
- end
26
-
27
- # Disconnects from the TeamSpeak 3 server
28
- def disconnect
29
- @sock.puts 'quit'
30
- @sock.close
31
- end
32
-
33
- # Authenticates with the TeamSpeak 3 server
34
- #
35
- # login('serveradmin', 'H8YlK1f9')
36
- def login(user, pass)
37
- command('login', {'client_login_name' => user, 'client_login_password' => pass})
38
- end
39
-
40
- # Sends command to the TeamSpeak 3 server and returns the response
41
- #
42
- # command('use', {'sid' => 1}, '-away')
43
- def command(cmd, params = {}, options = '')
44
- out = ''
45
- response = ''
46
-
47
- out += cmd
48
-
49
- params.each_pair do |key, value|
50
- out += " #{key}=#{encode_param(value.to_s)}"
51
- end
52
-
53
- out += ' ' + options
54
-
55
- @sock.puts out
56
-
57
- while true
58
- response += @sock.gets
59
-
60
- if response.index(' msg=')
61
- break
62
- end
63
- end
64
-
65
- # Array of commands that are expected to return as an array.
66
- # Not sure - clientgetids
67
- should_be_array = [
68
- 'bindinglist', 'serverlist', 'servergrouplist', 'servergroupclientlist',
69
- 'servergroupsbyclientid', 'servergroupclientlist', 'logview', 'channellist',
70
- 'channelfind', 'channelgrouplist', 'channelgrouppermlist', 'channelpermlist', 'clientlist',
71
- 'clientfind', 'clientdblist', 'clientdbfind', 'channelclientpermlist', 'permissionlist',
72
- 'permoverview', 'privilegekeylist', 'messagelist', 'complainlist', 'banlist', 'ftlist',
73
- 'custominfo'
74
- ]
75
-
76
- parsed_response = parse_response(response)
77
-
78
- return should_be_array.include?(cmd) ? parsed_response : parsed_response.first
79
- end
80
-
81
- def parse_response(response)
82
- out = []
83
-
84
- response.split('|').each do |key|
85
- data = {}
86
-
87
- key.split(' ').each do |key|
88
- value = key.split('=', 2)
89
-
90
- data[value[0]] = decode_param(value[1])
91
- end
92
-
93
- out.push(data)
94
- end
95
-
96
- check_response_error(out)
97
-
98
- out
99
- end
100
-
101
- def decode_param(param)
102
- return nil unless param
103
- # Return as integer if possible
104
- return param.to_i if param.to_i.to_s == param
105
-
106
- param.gsub!('\\\\', '\\')
107
- param.gsub!('\\/', '/')
108
- param.gsub!('\\s', ' ')
109
- param.gsub!('\\p', '|')
110
- param.gsub!('\\a', '\a')
111
- param.gsub!('\\b', '\b')
112
- param.gsub!('\\f', '\f')
113
- param.gsub!('\\n', '\n')
114
- param.gsub!('\\r', '\r')
115
- param.gsub!('\\t', '\t')
116
- param.gsub!('\\v', '\v')
117
-
118
- param
119
- end
120
-
121
- def encode_param(param)
122
- param.gsub!('\\', '\\\\')
123
- param.gsub!('/', '\\/')
124
- param.gsub!(' ', '\\s')
125
- param.gsub!('|', '\\p')
126
- param.gsub!('\a', '\\a')
127
- param.gsub!('\b', '\\b')
128
- param.gsub!('\f', '\\f')
129
- param.gsub!('\n', '\\n')
130
- param.gsub!('\r', '\\r')
131
- param.gsub!('\t', '\\t')
132
- param.gsub!('\v', '\\v')
133
-
134
- param
135
- end
136
-
137
- def check_response_error(response)
138
- id = response.first['id'] || 0
139
- message = response.first['msg'] || 0
140
-
141
- raise ServerError.new(id, message) unless id == 0
142
- end
143
-
144
- private(:parse_response, :decode_param, :encode_param, :check_response_error)
145
- end
146
- end
1
+ require 'socket'
2
+
3
+ module Teamspeak
4
+ class Client
5
+ # Should commands be throttled? Default is true
6
+ attr_writer(:flood_protection)
7
+ # Number of commands within flood_time before pausing. Default is 10
8
+ attr_writer(:flood_limit)
9
+ # Length of time before flood_limit is reset in seconds. Default is 3
10
+ attr_writer(:flood_time)
11
+
12
+ # Initializes Client
13
+ #
14
+ # connect('voice.domain.com', 88888)
15
+ def initialize(host = 'localhost', port = 10011)
16
+ connect(host, port)
17
+
18
+ # Throttle commands by default unless connected to localhost
19
+ @flood_protection = true unless host
20
+ @flood_limit = 10
21
+ @flood_time = 3
22
+
23
+ @flood_timer = Time.new
24
+ @flood_current = 0
25
+ end
26
+
27
+ # Connects to a TeamSpeak 3 server
28
+ #
29
+ # connect('voice.domain.com', 88888)
30
+ def connect(host = 'localhost', port = 10011)
31
+ @sock = TCPSocket.new(host, port)
32
+
33
+ # Check if the response is the same as a normal teamspeak 3 server.
34
+ if @sock.gets.strip != 'TS3'
35
+ raise InvalidServer, 'Server is not responding as a normal TeamSpeak 3 server.'
36
+ end
37
+
38
+ # Remove useless text from the buffer.
39
+ @sock.gets
40
+ end
41
+
42
+ # Disconnects from the TeamSpeak 3 server
43
+ def disconnect
44
+ @sock.puts 'quit'
45
+ @sock.close
46
+ end
47
+
48
+ # Authenticates with the TeamSpeak 3 server
49
+ #
50
+ # login('serveradmin', 'H8YlK1f9')
51
+ def login(user, pass)
52
+ command('login', {'client_login_name' => user, 'client_login_password' => pass})
53
+ end
54
+
55
+ # Sends command to the TeamSpeak 3 server and returns the response
56
+ #
57
+ # command('use', {'sid' => 1}, '-away')
58
+ def command(cmd, params = {}, options = '')
59
+ if @flood_protection
60
+ @flood_current += 1
61
+
62
+ flood_time_reached = Time.now - @flood_timer < @flood_time
63
+ flood_limit_reached = @flood_current == @flood_limit
64
+
65
+ if flood_time_reached && flood_limit_reached
66
+ sleep(@flood_time)
67
+ end
68
+
69
+ if flood_limit_reached
70
+ # Reset flood protection
71
+ @flood_timer = Time.now
72
+ @flood_current = 0
73
+ end
74
+ end
75
+
76
+ out = ''
77
+ response = ''
78
+
79
+ out += cmd
80
+
81
+ params.each_pair do |key, value|
82
+ out += " #{key}=#{encode_param(value.to_s)}"
83
+ end
84
+
85
+ out += ' ' + options
86
+
87
+ @sock.puts out
88
+
89
+ while true
90
+ response += @sock.gets
91
+
92
+ if response.index(' msg=')
93
+ break
94
+ end
95
+ end
96
+
97
+ # Array of commands that are expected to return as an array.
98
+ # Not sure - clientgetids
99
+ should_be_array = [
100
+ 'bindinglist', 'serverlist', 'servergrouplist', 'servergroupclientlist',
101
+ 'servergroupsbyclientid', 'servergroupclientlist', 'logview', 'channellist',
102
+ 'channelfind', 'channelgrouplist', 'channelgrouppermlist', 'channelpermlist', 'clientlist',
103
+ 'clientfind', 'clientdblist', 'clientdbfind', 'channelclientpermlist', 'permissionlist',
104
+ 'permoverview', 'privilegekeylist', 'messagelist', 'complainlist', 'banlist', 'ftlist',
105
+ 'custominfo'
106
+ ]
107
+
108
+ parsed_response = parse_response(response)
109
+
110
+ return should_be_array.include?(cmd) ? parsed_response : parsed_response.first
111
+ end
112
+
113
+ def parse_response(response)
114
+ out = []
115
+
116
+ response.split('|').each do |key|
117
+ data = {}
118
+
119
+ key.split(' ').each do |key|
120
+ value = key.split('=', 2)
121
+
122
+ data[value[0]] = decode_param(value[1])
123
+ end
124
+
125
+ out.push(data)
126
+ end
127
+
128
+ check_response_error(out)
129
+
130
+ out
131
+ end
132
+
133
+ def decode_param(param)
134
+ return nil unless param
135
+ # Return as integer if possible
136
+ return param.to_i if param.to_i.to_s == param
137
+
138
+ param.gsub!('\\\\', '\\')
139
+ param.gsub!('\\/', '/')
140
+ param.gsub!('\\s', ' ')
141
+ param.gsub!('\\p', '|')
142
+ param.gsub!('\\a', '\a')
143
+ param.gsub!('\\b', '\b')
144
+ param.gsub!('\\f', '\f')
145
+ param.gsub!('\\n', '\n')
146
+ param.gsub!('\\r', '\r')
147
+ param.gsub!('\\t', '\t')
148
+ param.gsub!('\\v', '\v')
149
+
150
+ param
151
+ end
152
+
153
+ def encode_param(param)
154
+ param.gsub!('\\', '\\\\')
155
+ param.gsub!('/', '\\/')
156
+ param.gsub!(' ', '\\s')
157
+ param.gsub!('|', '\\p')
158
+ param.gsub!('\a', '\\a')
159
+ param.gsub!('\b', '\\b')
160
+ param.gsub!('\f', '\\f')
161
+ param.gsub!('\n', '\\n')
162
+ param.gsub!('\r', '\\r')
163
+ param.gsub!('\t', '\\t')
164
+ param.gsub!('\v', '\\v')
165
+
166
+ param
167
+ end
168
+
169
+ def check_response_error(response)
170
+ id = response.first['id'] || 0
171
+ message = response.first['msg'] || 0
172
+
173
+ raise ServerError.new(id, message) unless id == 0
174
+ end
175
+
176
+ private(:parse_response, :decode_param, :encode_param, :check_response_error)
177
+ end
178
+ end
@@ -1,18 +1,18 @@
1
- module Teamspeak
2
- # Raised when the connected server does not respond as a normal TeamSpeak 3 would.
3
- #
4
- # raise InvalidServer, 'Server is not responding as a normal TeamSpeak 3 server.'
5
- class InvalidServer < StandardError; end
6
-
7
- # Raised when the server returns an error code other than 0.
8
- #
9
- # raise ServerError.new(1, 'Some generic error message')
10
- class ServerError < StandardError
11
- attr_reader(:code, :message)
12
-
13
- def initialize(code, message)
14
- @code = code
15
- @message = message
16
- end
17
- end
18
- end
1
+ module Teamspeak
2
+ # Raised when the connected server does not respond as a normal TeamSpeak 3 would.
3
+ #
4
+ # raise InvalidServer, 'Server is not responding as a normal TeamSpeak 3 server.'
5
+ class InvalidServer < StandardError; end
6
+
7
+ # Raised when the server returns an error code other than 0.
8
+ #
9
+ # raise ServerError.new(1, 'Some generic error message')
10
+ class ServerError < StandardError
11
+ attr_reader(:code, :message)
12
+
13
+ def initialize(code, message)
14
+ @code = code
15
+ @message = message
16
+ end
17
+ end
18
+ end
@@ -1,3 +1,3 @@
1
- module Teamspeak
2
- VERSION = '1.0.2'
3
- end
1
+ module Teamspeak
2
+ VERSION = '1.0.3'
3
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: teamspeak-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Harrison
@@ -16,10 +16,10 @@ executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
+ - lib/teamspeak-ruby.rb
19
20
  - lib/teamspeak-ruby/client.rb
20
21
  - lib/teamspeak-ruby/exceptions.rb
21
22
  - lib/teamspeak-ruby/version.rb
22
- - lib/teamspeak-ruby.rb
23
23
  homepage: http://pyrohail.com
24
24
  licenses:
25
25
  - MIT
@@ -40,7 +40,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
40
40
  version: '0'
41
41
  requirements: []
42
42
  rubyforge_project:
43
- rubygems_version: 2.0.14
43
+ rubygems_version: 2.2.2
44
44
  signing_key:
45
45
  specification_version: 4
46
46
  summary: Ruby interface for TeamSpeak 3's server query api.