Module: Teamspeak

Defined in:
lib/teamspeak.rb

Defined Under Namespace

Classes: Association, Base, Channel, Client, Group, Server, Token

Constant Summary collapse

TEXTMESSAGETARGET_CLIENT =
1
TEXTMESSAGETARGET_CHANNEL =
2
TEXTMESSAGETARGET_SERVER =
3

Class Method Summary collapse

Class Method Details

._to_hash(data, strip_key = '') ⇒ Object


404
405
406
# File 'lib/teamspeak.rb', line 404

def _to_hash(data, strip_key = '')
	data.split(' ').collect{|c| c.split('=')}.inject({}) {|h, el| h[el[0].to_s.gsub(strip_key, '').to_sym] = el[1]; h}
end

.cmd(command, *attributes) ⇒ Object


310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/teamspeak.rb', line 310

def cmd(command, *attributes)
	connect if !connected?

	puts attributes.inspect
	attributes = attributes[0] || []

	case command
		when :use
			nvid = attributes.to_i
			if @current_vid && @current_vid == nvid
				puts "use(): current_vid == vid (#{nvid}) - bailing"
				return false
			else
				puts "selecting new vid: #{nvid}"
			end

			_attributes = nvid
		when :login
			_attributes = "#{attributes[:user]} #{attributes[:password]}"
		when :gm
			msg = attributes[:msg].gsub(' ', '\s')
			_attributes = "msg=#{msg}"
		else
			attribute_string = attributes.collect{|a,v| "#{a}=#{v}"}.join(' ')
			_attributes = attributes.any? ? attribute_string : ''
	end

	command_string = !_attributes.to_s.empty? ? "#{command} #{_attributes}" : command

	puts "cmd: #{command_string}"
	socket.puts command_string

	eof = false
	data = ''
	begin
		ret = socket.gets
		if ret
			puts "RAW: #{ret.inspect}"
			rargs = ret.split(' ')

			case rargs[0]
			when 'error'
				error = ret.scan(/error id=(\d+) msg=(.*)/)[0]
				error_id = error[0]
				error_msg = error[1]
	
				puts "err: #{error_id} (#{error_msg})"
				eof = true
			
				history << command
			else
				data += ret
			end
		else
				puts "socket empty: #{socket.inspect}"
		end
	end while !eof

	# TODO make concise intelligent code, you fucktard.
	case command.to_sym
		when :clientlist
			clientlist = data.split('|').map{|client| 
				clientdata = _to_hash(client, 'client_')
				clientdata[:vid] = @current_vid
				Client.new(clientdata)
			}
		when :serverlist
			serverlist = data.split("|").map{|server|
				serverdata = _to_hash(server, 'virtualserver_')
				Server.new(serverdata)
			}
		when :servergrouplist
			grouplist = data.split('|').map{|group|
				groupdata = _to_hash(group)
				Group.new(groupdata)
			}
		when :channellist
			channellist = data.split('|').map{|channel|
				channeldata = _to_hash(channel, 'channel_')
				Channel.new(channeldata)
			}
		when :tokenlist
			tokenlist = data.split('|').map{|token|
				tokendata = _to_hash(token, 'token_')
				Token.new(tokendata)
			}
		when :tokenadd
			token = _to_hash(data.strip.gsub("\\", ''))
			return token
		else
			return data
	end
end

.configObject


248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/teamspeak.rb', line 248

def self.config
	config_paths = ['config/teamspeak.yml']
	config_paths << "#{RAILS_ROOT}config/teamspeak.yml" if defined?(RAILS_ROOT)

	if !@config
		config_paths.each {|config_path| 
			if File.exists?(config_path)
				cfg = YAML::load(File.open(config_path)) 
				cfg.each {|key, val|
					@config ||= {}
					@config[key.to_sym] = val
				}
				@config[:port] ||= 10011
				break
			end
		} 
	end
		
	@config ||= false
end

.config=(cfg = {}) ⇒ Object


269
270
271
# File 'lib/teamspeak.rb', line 269

def self.config=(cfg = {})
	@config = cfg
end

.connectObject

Raises:

  • (Exception)

286
287
288
289
290
291
292
# File 'lib/teamspeak.rb', line 286

def connect
	raise Exception if !config

	@socket = TCPSocket.new(config[:host], config[:port])
	ret = @socket.gets # "TS3"
	cmd(:login, :user => config[:user], :password => config[:password])
end

.connected?Boolean

Returns:

  • (Boolean)

282
283
284
# File 'lib/teamspeak.rb', line 282

def connected?
	@socket ? true : false
end

.disconnectObject


294
295
296
297
298
299
# File 'lib/teamspeak.rb', line 294

def disconnect
	if connected?
		@socket.close
		@socket = nil
	end
end

.gm(msg = '') ⇒ Object


306
307
308
# File 'lib/teamspeak.rb', line 306

def gm(msg = '')
	cmd(:gm, :msg => msg)
end

.historyObject


273
274
275
# File 'lib/teamspeak.rb', line 273

def self.history
	@history ||= []
end

.serversObject


408
409
410
411
# File 'lib/teamspeak.rb', line 408

def servers
	@servers ||= cmd(:serverlist)
	return @servers
end

.socketObject


301
302
303
304
# File 'lib/teamspeak.rb', line 301

def socket
	connect if !connected?
	@socket
end