Advertisement
pcgod

gametracker.com Mumble daemon

Oct 7th, 2011
856
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.40 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8
  3. #
  4. # Copyright (C) 2011 Benjamin Jemlich <[email protected]>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as
  8. # published by the Free Software Foundation, either version 3 of the
  9. # License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  
  19. import json
  20. import asyncore
  21. import socket
  22.  
  23. # Settings
  24. prxstr = "Meta:tcp -h 127.0.0.1 -p 6502"
  25. serverid = 1
  26. listen_ip = '0.0.0.0'
  27. listen_port = 64777
  28. slicefile = "Murmur.ice"
  29.  
  30. def loadUser(u):
  31.     user = {}
  32.     user['session'] = u.session
  33.     user['userid'] = u.userid
  34.     user['mute'] = u.mute
  35.     user['deaf'] = u.deaf
  36.     user['suppress'] = u.suppress
  37.     user['selfMute'] = u.selfMute
  38.     user['selfDeaf'] = u.selfDeaf
  39.     user['channel'] = u.channel
  40.     user['name'] = u.name
  41.     user['onlinesecs'] = u.onlinesecs
  42.     user['idlesecs'] = u.idlesecs
  43.     user['os'] = u.os
  44.     user['release'] = u.release
  45.     user['bytespersec'] = u.bytespersec
  46.  
  47.     return user
  48.  
  49. def loadChannel(c):
  50.     channel = {}
  51.     channel['id'] = c.c.id
  52.     channel['name'] = c.c.name
  53.     channel['parent'] = c.c.parent
  54.     channel['links'] = []
  55.     channel['position'] = c.c.position
  56.  
  57.     channel['channels'] = [loadChannel(child) for child in c.children]
  58.     channel['users'] = [loadUser(u) for u in c.users]
  59.  
  60.     return channel
  61.  
  62.  
  63.  
  64. import Ice
  65.  
  66. Ice.loadSlice("", ["-I" + Ice.getSliceDir(), "--checksum", slicefile])
  67.  
  68. import Murmur
  69.  
  70. #ice = Ice.initialize(['--Ice.Plugin.IceSSL=IceSSL:createIceSSL', '--IceSSL.Ciphers=ADH'])
  71. ice = Ice.initialize()
  72. prx = ice.stringToProxy(prxstr)
  73. m = Murmur.MetaPrx.checkedCast(prx)
  74.  
  75. s = m.getServer(serverid)
  76.  
  77. class CvpHandler(asyncore.dispatcher_with_send):
  78.     def __init__(self, sock):
  79.         asyncore.dispatcher_with_send.__init__(self, sock)
  80.  
  81.  
  82.     def handle_read(self):
  83.         data = self.recv(8192)
  84.         if data.strip() == "json":
  85.             tree = s.getTree()
  86.  
  87.             version = m.getVersion()
  88.             conf = s.getAllConf()
  89.             defconf = m.getDefaultConf()
  90.             if not 'port' in conf:
  91.                 conf['port'] = defconf['port']
  92.  
  93.             if not 'users' in conf:
  94.                 conf['users'] = defconf['users']
  95.  
  96.             server = {}
  97.             server['name'] = conf["registername"]
  98. #           server['name'] = "GameTracker"
  99. #           server['id'] = tree.c.id
  100.             server['id'] = serverid
  101.             server["x_gtmurmur_connectport"] = conf['port']
  102.             server["x_gtmurmur_max_users"] = conf['users']
  103.             server["x_gtmurmur_server_version"] = "%d.%d.%d" % (version[0], version[1], version[2])
  104.             server['x_uptime'] = s.getUptime()
  105.             server['root'] = loadChannel(tree)
  106.  
  107.             data = json.dumps(server, indent = 4)
  108.             server["x_gtmurmur_doclen"] = len(data) + 4 + 2 + 18 + 1 + 1 + 1 + 1 + len(str(len(data)))
  109.  
  110. #           data = json.dumps(server)
  111. #           server["x_gtmurmur_doclen"] = len(data) + 2 + 18 + 1 + 1 + 1 + len(str(len(data)))
  112.  
  113.             print "sending reply"
  114.             self.send(json.dumps(server, indent = 4))
  115. #           self.send(json.dumps(server))
  116.  
  117.     def handle_write(self):
  118.         asyncore.dispatcher_with_send.handle_write(self)
  119.         if len(self.out_buffer) == 0:
  120.             self.handle_close()
  121.  
  122.     def handle_close(self):
  123.         print "Closed connection"
  124.         self.close()
  125.  
  126. class CvpServer(asyncore.dispatcher):
  127.     def __init__(self, host, port):
  128.         asyncore.dispatcher.__init__(self)
  129.         self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
  130.         self.set_reuse_addr()
  131.         self.bind((host, port))
  132.         self.listen(5)
  133.  
  134.     def handle_accept(self):
  135.         pair = self.accept()
  136.         if pair is None:
  137.             pass
  138.         else:
  139.             sock, addr = pair
  140.             print 'Incoming connection from %s' % repr(addr)
  141.             handler = CvpHandler(sock)
  142.  
  143. server = CvpServer(listen_ip, listen_port)
  144. asyncore.loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement