Class: Teamspeak::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initializes Client

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 initialize(host = 'localhost', port = 10011)
  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

Instance Method Details

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

Sends command to the TeamSpeak 3 server and returns the response

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


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
# File 'lib/teamspeak-ruby/client.rb', line 58

def command(cmd, params = {}, options = '')
  if @flood_protection
    @flood_current += 1

    flood_time_reached = Time.now - @flood_timer < @flood_time
    flood_limit_reached = @flood_current == @flood_limit

    if flood_time_reached && flood_limit_reached
      sleep(@flood_time)
    end

    if flood_limit_reached
      # Reset flood protection
      @flood_timer = Time.now
      @flood_current = 0
    end
  end

  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)


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

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



43
44
45
46
# File 'lib/teamspeak-ruby/client.rb', line 43

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

#login(user, pass) ⇒ Object

Authenticates with the TeamSpeak 3 server

('serveradmin', 'H8YlK1f9')


51
52
53
# File 'lib/teamspeak-ruby/client.rb', line 51

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