teamspeak3 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 047e1ed175699cf16ec1e920abb755a80716aef5
4
+ data.tar.gz: 4dcb207489d5047c177b0e032ad53787d81cbdbc
5
+ SHA512:
6
+ metadata.gz: b9453878d62e0eb4974a27c1207c1d7b4d706ad2f6a4816a26eb6916d23cdd273043619c45bda59b1a388464da7120c600540cf6c4410d0f1bde2274e3fb536d
7
+ data.tar.gz: d590572ef38941d77bb302d42a4e12fae015143e54485907e9583e09d99f1cc3cdb7e2520cadd0bfb74f60f1d9ebeb7c15c3c1e12ba04396fd473b1f18e3c0ec
@@ -0,0 +1,8 @@
1
+ # the standard exception is the parent of all exceptions
2
+ require_relative 'exceptions/standard_exception.rb'
3
+
4
+ # alphabetical order of exceptions
5
+ require_relative 'exceptions/command_execution_failed.rb'
6
+ require_relative 'exceptions/not_connected.rb'
7
+ require_relative 'exceptions/query_login_failed.rb'
8
+ require_relative 'exceptions/server_connection_failed.rb'
@@ -0,0 +1,14 @@
1
+ module TeamSpeak3
2
+ module Exceptions
3
+ class CommandExecutionFailed < TeamSpeak3::Exceptions::StandardException
4
+ attr_reader :error_id
5
+ attr_reader :command
6
+
7
+ def initialize(error_id, message, command)
8
+ @error_id = error_id.to_i
9
+ @message = message
10
+ @command = command
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,9 @@
1
+ module TeamSpeak3
2
+ module Exceptions
3
+ class NotConnected < TeamSpeak3::Exceptions::StandardException
4
+ def initialize(message)
5
+ @message = message
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module TeamSpeak3
2
+ module Exceptions
3
+ class QueryLoginFailed < TeamSpeak3::Exceptions::StandardException
4
+ def initialize(message)
5
+ @message = message
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ module TeamSpeak3
2
+ module Exceptions
3
+ class ServerConnectionFailed < TeamSpeak3::Exceptions::StandardException
4
+ def initialize(ip_address, query_port, message)
5
+ @ip_address = ip_address
6
+ @query_port = query_port
7
+ @message = message
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module TeamSpeak3
2
+ module Exceptions
3
+ class StandardException < Exception
4
+ attr_reader :message
5
+
6
+ def to_s
7
+ @message
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,70 @@
1
+ module TeamSpeak3
2
+ class Server
3
+ attr_reader :ip_address
4
+ attr_reader :query_port
5
+ attr_reader :socket
6
+
7
+ def initialize(ip_address, query_port, opts = {})
8
+ @ip_address = ip_address
9
+ @query_port = query_port
10
+ @opts = opts
11
+ end
12
+
13
+ def connect
14
+ begin
15
+ @socket = Net::Telnet::new(
16
+ 'Host' => @ip_address,
17
+ 'Port' => @query_port,
18
+ 'Timeout' => @opts[:timeout] || 3,
19
+ 'Telnetmode' => false
20
+ )
21
+
22
+ @socket.waitfor(/Welcome to the TeamSpeak 3 ServerQuery interface/)
23
+ rescue Net::ReadTimeout => err
24
+ raise TeamSpeak3::Exceptions::ServerConnectionFailed.new(@ip_address, @query_port, \
25
+ "Timeout while waiting for TeamSpeak 3 welcome message.")
26
+ rescue Net::OpenTimeout, Errno::ECONNREFUSED => err
27
+ raise TeamSpeak3::Exceptions::ServerConnectionFailed.new(@ip_address, @query_port, \
28
+ "Could not open connection to server at #{@ip_address}:#{@query_port}")
29
+ end
30
+
31
+ true
32
+ end
33
+
34
+ def login(query_user, query_pass)
35
+ verify_connection
36
+
37
+ begin
38
+ execute "login client_login_name=#{query_user} client_login_password=#{query_pass}"
39
+ rescue TeamSpeak3::Exceptions::CommandExecutionFailed => err
40
+ raise TeamSpeak3::Exceptions::QueryLoginFailed, err.message
41
+ end
42
+
43
+ true
44
+ end
45
+
46
+ private
47
+
48
+ def verify_connection
49
+ raise TeamSpeak3::Exceptions::NotConnected, 'Not connected to a TeamSpeak 3 server.' unless @socket
50
+ end
51
+
52
+ def execute(command)
53
+ @socket.puts(command)
54
+
55
+ # every response contains an error information. so we wait until we receive a response
56
+ response = @socket.waitfor(/error id=.*/)
57
+
58
+ response = TeamSpeak3::ServerResponse.parse(response)
59
+ if response[:errors][:msg] != 'ok'
60
+ raise TeamSpeak3::Exceptions::CommandExecutionFailed.new(
61
+ response[:errors][:id],
62
+ response[:errors][:msg],
63
+ command,
64
+ )
65
+ end
66
+
67
+ response
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,84 @@
1
+ module TeamSpeak3
2
+ class ServerResponse
3
+ class << self
4
+ def parse(response)
5
+ data, errors = split_data_from_errors(response)
6
+
7
+ data = split_keys_from_values(data) if data
8
+ errors = split_keys_from_values(errors, ignore_array_result: true) if errors
9
+
10
+ { data: data, errors: errors }
11
+ end
12
+
13
+ private
14
+
15
+ def split_keys_from_values(data, opts = {})
16
+ # some commands return an array as response (indicated by a pipe)
17
+ response_list = data.split('|')
18
+ result_list = []
19
+
20
+ response_list.each do |data|
21
+ # split data packets (e.g.: cid=1 channel_name=test)
22
+ data = data.split(' ')
23
+ result = {}
24
+
25
+ data.each do |key_and_value|
26
+ # the start of the error section is always indicated with 'error'. As there is no '=' and no
27
+ # data we have to split, we can safely skip this iteration
28
+ next if key_and_value == "error"
29
+
30
+ trim_pos = key_and_value.index('=')
31
+
32
+ if trim_pos
33
+ key = key_and_value[0..trim_pos-1]
34
+ value = key_and_value[trim_pos+1..-1]
35
+ else
36
+ key = key_and_value
37
+ value = nil
38
+ end
39
+
40
+ result[key.to_sym] = parse_value(value)
41
+ end
42
+
43
+ return result if opts[:ignore_array_result]
44
+ result_list.push(result)
45
+ end
46
+
47
+ result_list
48
+ end
49
+
50
+ def parse_value(value)
51
+ return unless value
52
+
53
+ value.gsub!("\\\\", "\\")
54
+ value.gsub!("\/", "/")
55
+ value.gsub!("\\s", " ")
56
+ value.gsub!("\\p", "|")
57
+ value.gsub!("\\a", "\a")
58
+ value.gsub!("\\b", "\b")
59
+ value.gsub!("\\f", "\f")
60
+ value.gsub!("\\n", "\n")
61
+ value.gsub!("\\r", "\r")
62
+ value.gsub!("\\t", "\t")
63
+ value.gsub!("\\v", "\v")
64
+ value
65
+ end
66
+
67
+ def split_data_from_errors(response)
68
+ data, errors = response.split("\n")
69
+
70
+ # the response always consists of an error information (even if everything was fine) divided
71
+ # by a new line. normally, 'errors' contains the error information, but in case there is only
72
+ # an error and no data has been responded, the 'data' variable actually contains our error
73
+ # information
74
+
75
+ if data && !errors
76
+ errors = data
77
+ data = nil
78
+ end
79
+
80
+ [ data, errors ]
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,5 @@
1
+ require 'net/telnet'
2
+
3
+ require_relative 'exceptions.rb'
4
+ require_relative 'server_response.rb'
5
+ require_relative 'server.rb'
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: teamspeak3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Manuel Schnitzer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-03-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: An OOP library to query and manage TeamSpeak 3 servers in Ruby.
14
+ email: webmaster@mschnitzer.de
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/exceptions.rb
20
+ - lib/exceptions/command_execution_failed.rb
21
+ - lib/exceptions/not_connected.rb
22
+ - lib/exceptions/query_login_failed.rb
23
+ - lib/exceptions/server_connection_failed.rb
24
+ - lib/exceptions/standard_exception.rb
25
+ - lib/server.rb
26
+ - lib/server_response.rb
27
+ - lib/teamspeak3.rb
28
+ homepage: https://github.com/mschnitzer/teamspeak3
29
+ licenses:
30
+ - MIT
31
+ metadata: {}
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 2.2.5
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: A TeamSpeak 3 Query Library
52
+ test_files: []