teamspeak-ruby 0.0.4 → 1.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8515925df3f7f08ef7b8e72d05f3f5800ebbea4d
4
- data.tar.gz: c0181831098873a702cffa107914bd905e45951d
3
+ metadata.gz: 5e597f9317f78597ad033ee1a497c7a9cf13459d
4
+ data.tar.gz: 73c37f6efc8231e5fe46ae2fd35c0ec64fd8f23c
5
5
  SHA512:
6
- metadata.gz: 26244c88b234219155e319a2d0b15a094752c379db516e7edbe5fd60cba4083233e543145ecc580459d21778fffc2c7c20f53670ca93f3fa21eb21e358748a98
7
- data.tar.gz: 974d530f0504404c21a4b8603e2aa98b711c956c02e72255fc377a591fc4465bbd9c5807237d0b4854534faf290d3d92d0115f91c8d4a7e125dbbe5f5ceb9424
6
+ metadata.gz: f244e7925b258b707da5f52084f9e555911ea65104d5f783e00e0a47a5081af66e8822e55b27aa656342e4c2d1bf206bd59316749c46533cbd7f15cda3e6c98e
7
+ data.tar.gz: 8dcfc08728b28be8c707a2a522ac9128843ab6733546a2be68eab17859f112a3bf43877b96b84d42404bf9938225611f33d696a1d7e11f568ea9a1f58b5e44cd
@@ -1,118 +1,3 @@
1
- require 'socket'
2
-
3
- class Teamspeak
4
- def initialize(host = 'localhost', port = 10011)
5
- self.connect(host, port)
6
- end
7
-
8
- def connect(host = 'localhost', port = 10011)
9
- @sock = TCPSocket.new(host, port)
10
-
11
- # Check if the response is the same as a normal teamspeak 3 server.
12
- if @sock.gets.strip != 'TS3'
13
- puts 'This is not responding as a TeamSpeak 3 server.'
14
-
15
- return false
16
- end
17
-
18
- # Remove useless text from the buffer.
19
- @sock.gets
20
- end
21
-
22
- def disconnect
23
- @sock.puts 'quit'
24
- @sock.close
25
- end
26
-
27
- def login(user, pass)
28
- self.command('login', {'client_login_name' => user, 'client_login_password' => pass})
29
- end
30
-
31
- def command(cmd, params = {}, options = '')
32
- out = ''
33
- response = ''
34
-
35
- out += cmd
36
-
37
- params.each_pair do |key, value|
38
- out += " #{key}=#{encode_param(value.to_s)}"
39
- end
40
-
41
- out += ' ' + options
42
-
43
- @sock.puts out
44
-
45
- while true
46
- response += @sock.gets
47
-
48
- if response.index('msg=')
49
- break
50
- end
51
- end
52
-
53
- # Array of commands that are expected to return as an array.
54
- # Not sure - clientgetids
55
- should_be_array = ['bindinglist', 'serverlist', 'servergrouplist', 'servergroupclientlist',
56
- 'servergroupsbyclientid', 'servergroupclientlist', 'logview', 'channellist',
57
- 'channelfind', 'channelgrouplist', 'channelgrouppermlist', 'channelpermlist', 'clientlist',
58
- 'clientfind', 'clientdblist', 'clientdbfind', 'channelclientpermlist', 'permissionlist',
59
- 'permoverview', 'privilegekeylist', 'messagelist', 'complainlist', 'banlist', 'ftlist',
60
- 'custominfo']
61
-
62
- parsed_response = parse_response(response)
63
-
64
- return should_be_array.include?(cmd) ? parsed_response : parsed_response.first
65
- end
66
-
67
- def parse_response(response)
68
- out = []
69
-
70
- response = response.split('error id=').first
71
-
72
- response.split('|').each do |key|
73
- data = {}
74
-
75
- key.split(' ').each do |key|
76
- value = key.split('=')
77
-
78
- data[value[0]] = decode_param(value[1].to_s)
79
- end
80
-
81
- out.push(data)
82
- end
83
-
84
- return out
85
- end
86
-
87
- def decode_param(param)
88
- param = param.gsub('\\\\', '\\')
89
- param = param.gsub('\\/', '/')
90
- param = param.gsub('\\s', ' ')
91
- param = param.gsub('\\p', '|')
92
- param = param.gsub('\\a', '\a')
93
- param = param.gsub('\\b', '\b')
94
- param = param.gsub('\\f', '\f')
95
- param = param.gsub('\\n', '\n')
96
- param = param.gsub('\\r', '\r')
97
- param = param.gsub('\\t', '\t')
98
- param = param.gsub('\\v', '\v')
99
-
100
- return param == '' ? nil : param
101
- end
102
-
103
- def encode_param(param)
104
- param = param.gsub('\\', '\\\\')
105
- param = param.gsub('/', '\\/')
106
- param = param.gsub(' ', '\\s')
107
- param = param.gsub('|', '\\p')
108
- param = param.gsub('\a', '\\a')
109
- param = param.gsub('\b', '\\b')
110
- param = param.gsub('\f', '\\f')
111
- param = param.gsub('\n', '\\n')
112
- param = param.gsub('\r', '\\r')
113
- param = param.gsub('\t', '\\t')
114
- param = param.gsub('\v', '\\v')
115
-
116
- return param
117
- end
118
- end
1
+ require 'teamspeak-ruby/client'
2
+ require 'teamspeak-ruby/exceptions'
3
+ require 'teamspeak-ruby/version'
@@ -0,0 +1,144 @@
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 = ['bindinglist', 'serverlist', 'servergrouplist', 'servergroupclientlist',
68
+ 'servergroupsbyclientid', 'servergroupclientlist', 'logview', 'channellist',
69
+ 'channelfind', 'channelgrouplist', 'channelgrouppermlist', 'channelpermlist', 'clientlist',
70
+ 'clientfind', 'clientdblist', 'clientdbfind', 'channelclientpermlist', 'permissionlist',
71
+ 'permoverview', 'privilegekeylist', 'messagelist', 'complainlist', 'banlist', 'ftlist',
72
+ 'custominfo']
73
+
74
+ parsed_response = parse_response(response)
75
+
76
+ return should_be_array.include?(cmd) ? parsed_response : parsed_response.first
77
+ end
78
+
79
+ def parse_response(response)
80
+ out = []
81
+
82
+ response.split('|').each do |key|
83
+ data = {}
84
+
85
+ key.split(' ').each do |key|
86
+ value = key.split('=')
87
+
88
+ data[value[0]] = decode_param(value[1])
89
+ end
90
+
91
+ out.push(data)
92
+ end
93
+
94
+ check_response_error(out)
95
+
96
+ return out
97
+ end
98
+
99
+ def decode_param(param)
100
+ return nil unless param
101
+ # Return as integer if possible
102
+ return param.to_i if param.to_i.to_s == param
103
+
104
+ param.gsub!('\\\\', '\\')
105
+ param.gsub!('\\/', '/')
106
+ param.gsub!('\\s', ' ')
107
+ param.gsub!('\\p', '|')
108
+ param.gsub!('\\a', '\a')
109
+ param.gsub!('\\b', '\b')
110
+ param.gsub!('\\f', '\f')
111
+ param.gsub!('\\n', '\n')
112
+ param.gsub!('\\r', '\r')
113
+ param.gsub!('\\t', '\t')
114
+ param.gsub!('\\v', '\v')
115
+
116
+ return param
117
+ end
118
+
119
+ def encode_param(param)
120
+ param.gsub!('\\', '\\\\')
121
+ param.gsub!('/', '\\/')
122
+ param.gsub!(' ', '\\s')
123
+ param.gsub!('|', '\\p')
124
+ param.gsub!('\a', '\\a')
125
+ param.gsub!('\b', '\\b')
126
+ param.gsub!('\f', '\\f')
127
+ param.gsub!('\n', '\\n')
128
+ param.gsub!('\r', '\\r')
129
+ param.gsub!('\t', '\\t')
130
+ param.gsub!('\v', '\\v')
131
+
132
+ return param
133
+ end
134
+
135
+ def check_response_error(response)
136
+ id = response.first['id']
137
+ message = response.first['msg']
138
+
139
+ raise ServerError.new(id, message) unless id == 0
140
+ end
141
+
142
+ private(:parse_response, :decode_param, :encode_param, :check_response_error)
143
+ end
144
+ end
@@ -0,0 +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
@@ -0,0 +1,3 @@
1
+ module Teamspeak
2
+ VERSION = '1.0.0'
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: 0.0.4
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Harrison
@@ -16,6 +16,9 @@ executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
+ - lib/teamspeak-ruby/client.rb
20
+ - lib/teamspeak-ruby/exceptions.rb
21
+ - lib/teamspeak-ruby/version.rb
19
22
  - lib/teamspeak-ruby.rb
20
23
  homepage: http://pyrohail.com
21
24
  licenses:
@@ -42,3 +45,4 @@ signing_key:
42
45
  specification_version: 4
43
46
  summary: Ruby interface for TeamSpeak 3's server query api.
44
47
  test_files: []
48
+ has_rdoc: