class Tsquery
Constants
- ESCAPE_PATTERNS
Copied from addons.teamspeak.com/directory/addon/integration/TeamSpeak-3-PHP-Framework.html
- ESCAPE_PATTERNS_REGEXP
- INVERTED_ESCAPE_PATTERNS
- INVERTED_ESCAPE_PATTERNS_REGEXP
Public Class Methods
new(logger: Logger.new(STDOUT))
click to toggle source
# File lib/tsquery.rb, line 6 def initialize(logger: Logger.new(STDOUT)) @logger = logger end
Public Instance Methods
close()
click to toggle source
# File lib/tsquery.rb, line 75 def close @telnet.close end
connect(server: '127.0.0.1', port: '10011', telnet_class: Net::Telnet)
click to toggle source
# File lib/tsquery.rb, line 69 def connect(server: '127.0.0.1', port: '10011', telnet_class: Net::Telnet) @telnet = telnet_class.new('Host' => server, 'Port' => port, 'Waittime' => 0.1) @telnet.waitfor 'Match' => /^TS3\n/ end
execute(command, *args)
click to toggle source
# File lib/tsquery.rb, line 11 def execute(command, *args) args = args.each_with_object([]) do |arg, array| case arg when Integer array << arg.to_s when String array << arg when Hash arg.each do |key, value| array << "#{key}=#{value.to_s.gsub(ESCAPE_PATTERNS_REGEXP, ESCAPE_PATTERNS)}" end end end full_command = ([command] + args).join(' ') @logger.info "=> #{full_command}" case command when /list$/ parse_list(@telnet.cmd( 'String' => full_command, 'Timeout' => 3, 'Match' => /error id=\d+/ )) when /info$/, 'whoami', 'version', 'clientgetdbidfromuid' parse_info(@telnet.cmd( 'String' => full_command, 'Timeout' => 3, 'Match' => /error id=\d+/ )) else parse(@telnet.cmd( 'String' => full_command, 'Timeout' => 3, 'Match' => /^error id=\d+/ )) end end
login(username: 'serveradmin', password:)
click to toggle source
# File lib/tsquery.rb, line 51 def login(username: 'serveradmin', password:) execute 'login', username, password true rescue Error false end
method_missing(command, *args)
click to toggle source
# File lib/tsquery.rb, line 59 def method_missing(command, *args) execute(command.to_s, *args) end
respond_to_missing?(command, *)
click to toggle source
# File lib/tsquery.rb, line 64 def respond_to_missing?(command, *) !!(command =~ /[[:alnum:]]$/) end
Private Instance Methods
check_response!(response)
click to toggle source
# File lib/tsquery.rb, line 159 def check_response!(response) raise Error, 'response is nil' if response.nil? raise UnknownCommand, deserialize_arguments(response.gsub(/^error\s/, ''))['msg'] if response =~ /error id=256\D/ end
deserialize_arguments(string)
click to toggle source
# File lib/tsquery.rb, line 141 def deserialize_arguments(string) string.split.each_with_object({}) do |string, hash| key, value = string.split('=') hash[key] = case value when nil nil when /^\d+$/ value.to_i when /^\d+\.\d+$/ value.to_f else value = value.gsub(INVERTED_ESCAPE_PATTERNS_REGEXP, INVERTED_ESCAPE_PATTERNS) end end end
parse(response)
click to toggle source
# File lib/tsquery.rb, line 127 def parse(response) check_response! response @logger.info "<= #{response}" # Response always starts with "error", so we just remove it response = response.gsub(/^error\s/, '') arguments = deserialize_arguments(response) raise Error, arguments['msg'] if Integer(arguments['id']) != 0 arguments['msg'] == 'ok' end
parse_info(response)
click to toggle source
# File lib/tsquery.rb, line 116 def parse_info(response) check_response! response first, last = response.split(/\n\r?/) @logger.info "<= #{first}" parse last deserialize_arguments(first) end
parse_list(response)
click to toggle source
# File lib/tsquery.rb, line 102 def parse_list(response) check_response! response first, last = response.split(/\n\r?/) @logger.info "<= #{first}" return nil unless last parse last first.split('|').map do |arguments| deserialize_arguments(arguments) end end