Class: Teamspeak::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/teamspeak-ruby/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(host = 'localhost', port = 10011) ⇒ Client

Initializes Client

connect('voice.domain.com', 88888)


8
9
10
# File 'lib/teamspeak-ruby/client.rb', line 8

def initialize(host = 'localhost', port = 10011)
  connect(host, port)
end

Instance Method Details

#command(cmd, params = {}, options = '') ⇒ Object

Sends command to the TeamSpeak 3 server and returns the response

command('use', {'sid' => 1}, '-away')


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/teamspeak-ruby/client.rb', line 43

def command(cmd, params = {}, options = '')
  out = ''
  response = ''

  out += cmd

  params.each_pair do |key, value|
    out += " #{key}=#{encode_param(value.to_s)}"
  end

  out += ' ' + options

  @sock.puts out

  while true
    response += @sock.gets
    
    if response.index('msg=')
      break
    end
  end

  # Array of commands that are expected to return as an array.
  # Not sure - clientgetids
  should_be_array = ['bindinglist', 'serverlist', 'servergrouplist', 'servergroupclientlist',
      'servergroupsbyclientid', 'servergroupclientlist', 'logview', 'channellist',
      'channelfind', 'channelgrouplist', 'channelgrouppermlist', 'channelpermlist', 'clientlist',
      'clientfind', 'clientdblist', 'clientdbfind', 'channelclientpermlist', 'permissionlist',
      'permoverview', 'privilegekeylist', 'messagelist', 'complainlist', 'banlist', 'ftlist',
      'custominfo']

  parsed_response = parse_response(response)

  return should_be_array.include?(cmd) ? parsed_response : parsed_response.first
end

#connect(host = 'localhost', port = 10011) ⇒ Object

Connects to a TeamSpeak 3 server

connect('voice.domain.com', 88888)


15
16
17
18
19
20
21
22
23
24
25
# File 'lib/teamspeak-ruby/client.rb', line 15

def connect(host = 'localhost', port = 10011)
  @sock = TCPSocket.new(host, port)

  # Check if the response is the same as a normal teamspeak 3 server.
  if @sock.gets.strip != 'TS3'
    raise InvalidServer, 'Server is not responding as a normal TeamSpeak 3 server.'
  end

  # Remove useless text from the buffer.
  @sock.gets
end

#disconnectObject

Disconnects from the TeamSpeak 3 server



28
29
30
31
# File 'lib/teamspeak-ruby/client.rb', line 28

def disconnect
  @sock.puts 'quit'
  @sock.close
end

#login(user, pass) ⇒ Object

Authenticates with the TeamSpeak 3 server

('serveradmin', 'H8YlK1f9')


36
37
38
# File 'lib/teamspeak-ruby/client.rb', line 36

def (user, pass)
  command('login', {'client_login_name' => user, 'client_login_password' => pass})
end