Class: Teamspeak::Client

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

Constant Summary collapse

SPECIAL_CHARS =

First is escaped char, second is real char.

[
  ['\\\\', '\\'],
  ['\\/', '/'],
  ['\\s', ' '],
  ['\\p', '|'],
  ['\\a', '\a'],
  ['\\b', '\b'],
  ['\\f', '\f'],
  ['\\n', '\n'],
  ['\\r', '\r'],
  ['\\t', '\t'],
  ['\\v', '\v']
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initializes Client

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


32
33
34
35
36
37
38
39
40
41
42
# File 'lib/teamspeak-ruby/client.rb', line 32

def initialize(host = 'localhost', port = 10_011)
  connect(host, port)

  # Throttle commands by default unless connected to localhost
  @flood_protection = true unless host
  @flood_limit = 10
  @flood_time = 3

  @flood_timer = Time.new
  @flood_current = 0
end

Instance Attribute Details

#flood_limit=(value) ⇒ Object (writeonly)

Number of commands within flood_time before pausing. Default is 10



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

def flood_limit=(value)
  @flood_limit = value
end

#flood_protection=(value) ⇒ Object (writeonly)

Should commands be throttled? Default is true



6
7
8
# File 'lib/teamspeak-ruby/client.rb', line 6

def flood_protection=(value)
  @flood_protection = value
end

#flood_time=(value) ⇒ Object (writeonly)

Length of time before flood_limit is reset in seconds. Default is 3



10
11
12
# File 'lib/teamspeak-ruby/client.rb', line 10

def flood_time=(value)
  @flood_time = value
end

#sockObject (readonly)

access the raw socket



12
13
14
# File 'lib/teamspeak-ruby/client.rb', line 12

def sock
  @sock
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')


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/teamspeak-ruby/client.rb', line 76

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

  out = ''
  response = ''

  out += cmd

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

  out += ' ' + options

  @sock.puts out

  if cmd == 'servernotifyregister'
    2.times { response += @sock.gets }
    return parse_response(response)
  end

  loop do
    response += @sock.gets

    break if response.index(' msg=')
  end

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

  parsed_response = parse_response(response)

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

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

Connects to a TeamSpeak 3 server

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


47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/teamspeak-ruby/client.rb', line 47

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

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

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

#disconnectObject

Disconnects from the TeamSpeak 3 server



61
62
63
64
# File 'lib/teamspeak-ruby/client.rb', line 61

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

#login(user, pass) ⇒ Object

Authenticates with the TeamSpeak 3 server

('serveradmin', 'H8YlK1f9')


69
70
71
# File 'lib/teamspeak-ruby/client.rb', line 69

def (user, pass)
  command('login', client_login_name: user, client_login_password: pass)
end