home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / TeamSpeak / TeamSpeak3-Client-win32-3.0.0.exe / plugins / clientquery_plugin / examples / demo.py < prev    next >
Text File  |  2011-08-03  |  7KB  |  234 lines

  1. #
  2. # TeamSpeak 3 ClientQuery example
  3. # Copyright (c) 2010 TeamSpeak Systems GmbH
  4. #
  5. # Demonstration how to monitor clients in the current server
  6. #
  7.  
  8. import sys, string
  9. from clientquery import *
  10.  
  11. HOST = 'localhost'
  12. PORT = 25639
  13.  
  14. STATUS_NOT_TALKING = 0
  15. STATUS_TALKING = 1
  16. STATUS_TALKING_WHILE_DISABLED = 2
  17.  
  18. clientUIDsGamingWith = [ 'z3vfjIEaWUgvkeD5vikiNhwg1e0=' ]
  19.  
  20. # Global variables
  21. ownClientID = 0
  22. ownClientUID = ''
  23. currentChannelID = 0
  24. clients = {}
  25.  
  26. class Client:
  27.     def __init__(self, id, uid, name, channelID):
  28.         self.id = id
  29.         self.uid = uid
  30.         self.name = name
  31.         self.channelID = channelID
  32.  
  33.     def __str__(self):
  34.         return '%s (%d) %s' % (self.name, self.id, self.uid)
  35.  
  36. def getDataForCurrentServerTab(s):
  37.     global ownClientID, ownClientUID, currentChannelID, clients
  38.     
  39.     # Clear data
  40.     ownClientID = 0
  41.     ownClientUID = ''
  42.     currentChannelID = 0
  43.     clients = {}
  44.  
  45.     # Get own clientID and current channel ID
  46.     send(s, 'whoami')
  47.     (data, err) = receive(s)
  48.     if err:
  49.         print 'Error getting whoami info'
  50.         return False
  51.     ownClientID = int(getParamValue(data[0], 'clid'))
  52.     print 'ownClientID:', ownClientID
  53.     currentChannelID = int(getParamValue(data[0], 'cid'))
  54.     print 'currentChannelID:', currentChannelID
  55.  
  56.     # Get list of clients in current channel
  57.     send(s, 'clientlist -uid')
  58.     (data, err) = receive(s)
  59.     if err:
  60.         print 'Error getting clientlist'
  61.         return False
  62.     cl = data[0].split('|')
  63.     for c in cl:
  64.         clientID = int(getParamValue(c, 'clid'))
  65.         channelID = int(getParamValue(c, 'cid'))
  66.         clients[clientID] = Client(clientID, getParamValue(c, 'client_unique_identifier'), getParamValue(c, 'client_nickname'), channelID)
  67.     for c in clients:
  68.         print clients[c]
  69.         
  70.     # Own client unique id
  71.     ownClientUID = clients[ownClientID].uid
  72.     print 'ownClientUID', ownClientUID
  73.  
  74.     return True
  75.         
  76. #
  77. # Demonstrate connecting to a local TeamSpeak 3 clientquery
  78. #
  79. def main():
  80.     # Create TCP socket and connect to clientquery on localhost:25639
  81.     s = connectTS3Query(HOST, PORT)
  82.     if not s:
  83.         return
  84.  
  85.     # Once connected, check if we receive "TS3 Client".
  86.     # If not, this is no TS3 clientquery
  87.     (data, err) = receive(s)
  88.     if err or not data or not string.lower(data[0]).startswith('ts3 client'):
  89.         print 'Not a TS ClientQuery'
  90.         return
  91.  
  92.     # Get current server tab
  93.     send(s, 'currentschandlerid')
  94.     (data, err) = receive(s)
  95.     if err:
  96.         print 'Error getting currentschandlerid'
  97.         return
  98.     currentScHandlerID = int(getParamValue(data[0], 'schandlerid'))
  99.     print 'currentScHandlerID:', currentScHandlerID
  100.     if not getDataForCurrentServerTab(s):
  101.         print 'Error getting data for current server tab'
  102.         return
  103.     
  104.     # Register for talk status change events
  105.     send(s, 'clientnotifyregister schandlerid=0 event=notifytalkstatuschange')
  106.     (data, err) = receive(s)
  107.     if err:
  108.         print 'Error registering for talk status change events'
  109.         return
  110.         
  111.     # Register for client enter view events
  112.     send(s, 'clientnotifyregister schandlerid=0 event=notifycliententerview')
  113.     (data, err) = receive(s)
  114.     if err:
  115.         print 'Error registering for client enter view events'
  116.         return
  117.         
  118.     # Register for client enter view events
  119.     send(s, 'clientnotifyregister schandlerid=0 event=notifyclientleftview')
  120.     (data, err) = receive(s)
  121.     if err:
  122.         print 'Error registering for client left view events'
  123.         return
  124.     
  125.     # Register for client move events
  126.     send(s, 'clientnotifyregister schandlerid=0 event=notifyclientmoved')
  127.     (data, err) = receive(s)
  128.     if err:
  129.         print 'Error registering for client move events'
  130.         return
  131.         
  132.     # Register for server tab change in the UI
  133.     send(s, 'clientnotifyregister schandlerid=0 event=notifycurrentserverconnectionchanged')
  134.     (data, err) = receive(s)
  135.     if err:
  136.         print 'Error registering for current server tab changed'
  137.         return
  138.     
  139.     try:
  140.         while True:
  141.             (dataLines, err) = receive(s)
  142.             if not dataLines:
  143.                 continue
  144.             for data in dataLines:
  145.                 if data.startswith('notifytalkstatuschange'):
  146.                     scHandlerID = int(getParamValue(data, 'schandlerid'))
  147.                     if scHandlerID != currentScHandlerID:
  148.                         continue
  149.                     status = int(getParamValue(data, 'status'))
  150.                     if status == STATUS_TALKING:
  151.                         verb = 'starts'
  152.                     elif status == STATUS_NOT_TALKING:
  153.                         verb = 'stops'
  154.                     elif status == STATUS_TALKING_WHILE_DISABLED:
  155.                         continue  # Ignore
  156.                     cid = int(getParamValue(data, 'clid'))
  157.                     if cid == ownClientID:
  158.                         # Own client, just display a message and continue
  159.                         print 'Own client %s talking.' % verb
  160.                     else:
  161.                         # Other
  162.                         try:
  163.                             client = clients[cid]
  164.                         except KeyError:
  165.                             print 'Error, unknown clientID not in list:', cid
  166.                             continue
  167.                         # Here we check if the client is in our list of UIDs of clients we see in the game.
  168.                         # This info would be fed from the game, for testing purpose I added a UID manually.
  169.                         if not client.uid in clientUIDsGamingWith:
  170.                             print 'Client %s not in game, ignoring' % client.uid
  171.                             continue  # Client not in game, ignore
  172.                         print '"%s" %s talking.' % (client.name, verb)
  173.                 elif data.startswith('notifyclientmoved'):
  174.                     scHandlerID = int(getParamValue(data, 'schandlerid'))
  175.                     if scHandlerID != currentScHandlerID:
  176.                         continue
  177.                     cid = int(getParamValue(data, 'clid'))
  178.                     newChannelID = int(getParamValue(data, 'ctid'))
  179.                     try:
  180.                         client = clients[cid]
  181.                     except KeyError:
  182.                         print 'Error, unknown clientID not in list:', cid
  183.                         continue
  184.                     client.channelID = newChannelID  # Adjust channelID in client
  185.                     if cid == ownClientID:
  186.                         currentChannelID = newChannelID  # Own client moved channel
  187.                     elif newChannelID == currentChannelID:
  188.                         print '"%s" entered your channel' % client.name
  189.                     else:
  190.                         print '"%s" left your channel' % client.name
  191.                 elif data.startswith('notifycliententerview'):
  192.                     scHandlerID = int(getParamValue(data, 'schandlerid'))
  193.                     if scHandlerID != currentScHandlerID:
  194.                         continue
  195.                     newChannelID = int(getParamValue(data, 'ctid'))
  196.                     cl = data.split('|')
  197.                     for c in cl:
  198.                         cid = int(getParamValue(c, 'clid'))
  199.                         client = Client(cid, getParamValue(c, 'client_unique_identifier'), getParamValue(c, 'client_nickname'), newChannelID)
  200.                         clients[cid] = client
  201.                         print '"%s" entered your channel' % client.name
  202.                 elif data.startswith('notifyclientleftview'):
  203.                     scHandlerID = int(getParamValue(data, 'schandlerid'))
  204.                     if scHandlerID != currentScHandlerID:
  205.                         continue
  206.                     cl = data.split('|')
  207.                     for c in cl:
  208.                         cid = int(getParamValue(c, 'clid'))
  209.                         try:
  210.                             name = clients[cid].name
  211.                             del clients[cid]  # Remove from clients list
  212.                         except KeyError:
  213.                             name = 'Unknown'
  214.                         print '"%s" left your channel' % name
  215.                 elif data.startswith('notifycurrentserverconnectionchanged'):
  216.                     currentScHandlerID = int(getParamValue(data, 'schandlerid'))
  217.                     print "Tab changed, currentScHandlerID:", currentScHandlerID
  218.                     send(s, 'use %d' % currentScHandlerID)  # Let clientquery use new tab
  219.                     (data, err) = receive(s)
  220.                     print "********", data
  221.                     if err:
  222.                         print 'Error switching to new server connection'
  223.                         return
  224.                     if not getDataForCurrentServerTab(s):
  225.                         print 'Error getting data for current server tab'
  226.                         return
  227.                     
  228.     except KeyboardInterrupt:
  229.         send(s, 'quit')
  230.         sys.exit()
  231.  
  232. if __name__ == '__main__':
  233.     main()
  234.