mirror of
https://github.com/nikdoof/python-ts3.git
synced 2025-12-17 11:59:27 +00:00
cleanup
This commit is contained in:
35
ts3.py
35
ts3.py
@@ -30,7 +30,6 @@ import telnetlib
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
class ConnectionError():
|
class ConnectionError():
|
||||||
|
|
||||||
def __init__(self, ip, port):
|
def __init__(self, ip, port):
|
||||||
self.ip = ip
|
self.ip = ip
|
||||||
self.port = port
|
self.port = port
|
||||||
@@ -53,17 +52,17 @@ class TS3Response():
|
|||||||
def __init__(self, response):
|
def __init__(self, response):
|
||||||
self.response = TS3Proto.parse_command(response)
|
self.response = TS3Proto.parse_command(response)
|
||||||
|
|
||||||
def successful(self):
|
def is_successful(self):
|
||||||
if isinstance(self.response, dict):
|
if isinstance(self.response, dict):
|
||||||
return self.response['keys']['msg'] == 'ok'
|
return self.response['keys']['msg'] == 'ok'
|
||||||
|
|
||||||
# if the response is a list, it has to be successful
|
# if the response is a list, it has to be successful
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def response(self):
|
||||||
|
return self.response
|
||||||
|
|
||||||
class TS3Proto():
|
class TS3Proto():
|
||||||
_timeout = 0
|
|
||||||
_connected = False
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._log = logging.getLogger('%s.%s' % (__name__, self.__class__.__name__))
|
self._log = logging.getLogger('%s.%s' % (__name__, self.__class__.__name__))
|
||||||
pass
|
pass
|
||||||
@@ -71,12 +70,14 @@ class TS3Proto():
|
|||||||
def connect(self, ip, port, timeout=5):
|
def connect(self, ip, port, timeout=5):
|
||||||
self._telnet = telnetlib.Telnet(ip, port)
|
self._telnet = telnetlib.Telnet(ip, port)
|
||||||
self._timeout = timeout
|
self._timeout = timeout
|
||||||
|
self._connected = False
|
||||||
|
|
||||||
data = self._telnet.read_until("TS3\n", self._timeout)
|
data = self._telnet.read_until("TS3\n", self._timeout)
|
||||||
|
|
||||||
if data.endswith("TS3\n"):
|
if data.endswith("TS3\n"):
|
||||||
self._connected = True
|
self._connected = True
|
||||||
return True
|
|
||||||
|
return self._connected
|
||||||
|
|
||||||
def disconnect(self):
|
def disconnect(self):
|
||||||
self.send_command("quit")
|
self.send_command("quit")
|
||||||
@@ -91,6 +92,9 @@ class TS3Proto():
|
|||||||
|
|
||||||
return TS3Response(self._telnet.read_until("\n", self._timeout))
|
return TS3Response(self._telnet.read_until("\n", self._timeout))
|
||||||
|
|
||||||
|
def is_connected(self):
|
||||||
|
return self._connected
|
||||||
|
|
||||||
def construct_command(self, command, keys=None, opts=None):
|
def construct_command(self, command, keys=None, opts=None):
|
||||||
"""
|
"""
|
||||||
Constructs a TS3 formatted command string
|
Constructs a TS3 formatted command string
|
||||||
@@ -198,12 +202,6 @@ class TS3Proto():
|
|||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
def send(self, payload):
|
|
||||||
if self._connected:
|
|
||||||
self._log.debug('Sent: %s' % payload)
|
|
||||||
self._sockfile.write(payload)
|
|
||||||
|
|
||||||
|
|
||||||
class TS3Server(TS3Proto):
|
class TS3Server(TS3Proto):
|
||||||
def __init__(self, ip, port, id=0):
|
def __init__(self, ip, port, id=0):
|
||||||
"""
|
"""
|
||||||
@@ -231,10 +229,8 @@ class TS3Server(TS3Proto):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
response = self.send_command('login', keys={'client_login_name': username, 'client_login_password': password })
|
response = self.send_command('login', keys={'client_login_name': username, 'client_login_password': password })
|
||||||
|
|
||||||
print response
|
|
||||||
|
|
||||||
if response.successful():
|
if response.is_successful():
|
||||||
self._log.info('Login error: %s.')
|
self._log.info('Login error: %s.')
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
@@ -245,8 +241,7 @@ class TS3Server(TS3Proto):
|
|||||||
"""
|
"""
|
||||||
Get a list of all Virtual Servers on the connected TS3 instance
|
Get a list of all Virtual Servers on the connected TS3 instance
|
||||||
"""
|
"""
|
||||||
if self._connected:
|
return self.send_command('serverlist')
|
||||||
return self.send_command('serverlist')
|
|
||||||
|
|
||||||
def gm(self, msg):
|
def gm(self, msg):
|
||||||
"""
|
"""
|
||||||
@@ -255,8 +250,7 @@ class TS3Server(TS3Proto):
|
|||||||
@param msg: Message
|
@param msg: Message
|
||||||
@type ip: str
|
@type ip: str
|
||||||
"""
|
"""
|
||||||
if self._connected:
|
return self.send_command('gm', keys={'msg': msg})
|
||||||
return self.send_command('gm', keys={'msg': msg})
|
|
||||||
|
|
||||||
def use(self, id):
|
def use(self, id):
|
||||||
"""
|
"""
|
||||||
@@ -265,5 +259,4 @@ class TS3Server(TS3Proto):
|
|||||||
@param id: Virtual Server ID
|
@param id: Virtual Server ID
|
||||||
@type id: int
|
@type id: int
|
||||||
"""
|
"""
|
||||||
if self._connected and id > 0:
|
self.send_command('use', keys={'sid': id})
|
||||||
self.send_command('use', keys={'sid': id})
|
|
||||||
Reference in New Issue
Block a user