Forked mumble-django project from https://bitbucket.org/Svedrin/mumble-django
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
6.4 KiB

  1. """ This file is part of the mumble-django application.
  2. Copyright (C) 2009, Michael "Svedrin" Ziegler <diese-addy@funzt-halt.net>
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. - Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer.
  9. - Redistributions in binary form must reproduce the above copyright notice,
  10. this list of conditions and the following disclaimer in the documentation
  11. and/or other materials provided with the distribution.
  12. - Neither the name of the Mumble Developers nor the names of its
  13. contributors may be used to endorse or promote products derived from this
  14. software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. """
  27. import models
  28. import dbus
  29. from django.db.models import signals
  30. def find_in_dicts( keys, conf, default, valueIfNotFound=None ):
  31. if not isinstance( keys, tuple ):
  32. keys = ( keys, );
  33. for keyword in keys:
  34. if keyword in conf:
  35. if isinstance( conf[keyword], dbus.String ):
  36. return unicode(conf[keyword]);
  37. elif isinstance( conf[keyword], dbus.Int32 ):
  38. return int(conf[keyword]);
  39. return conf[keyword];
  40. for keyword in keys:
  41. keyword = keyword.lower();
  42. if keyword in default:
  43. if isinstance( default[keyword], dbus.String ):
  44. return unicode(default[keyword]);
  45. elif isinstance( default[keyword], dbus.Int32 ):
  46. return int(default[keyword]);
  47. return default[keyword];
  48. return valueIfNotFound;
  49. def find_existing_instances( **kwargs ):
  50. bus = dbus.SystemBus();
  51. if "verbosity" in kwargs:
  52. v = kwargs['verbosity'];
  53. else:
  54. v = 1;
  55. if v > 1:
  56. print "Starting Mumble servers and players detection now.";
  57. dbusName = 'net.sourceforge.mumble.murmur';
  58. online = False;
  59. while not online:
  60. try:
  61. murmur = dbus.Interface( bus.get_object( dbusName, '/' ), 'net.sourceforge.mumble.Meta' );
  62. except dbus.exceptions.DBusException:
  63. if v:
  64. print "Unable to connect to DBus using name %s. Is Murmur even running!?" % dbusName;
  65. dbusName = raw_input( "DBus Service name (or empty to skip Servers/Players detection): " );
  66. if not dbusName:
  67. if v:
  68. print 'Be sure to run "python manage.py syncdb" with Murmur running before trying to use this app! Otherwise, existing Murmur servers won\'t be configurable!';
  69. return False;
  70. else:
  71. online = True;
  72. if v > 1:
  73. print "Successfully connected to Murmur via DBus (%s)." % dbusName;
  74. default = murmur.getDefaultConf();
  75. servIDs = murmur.getAllServers();
  76. bootedIDs = murmur.getBootedServers();
  77. for id in servIDs:
  78. if v > 1:
  79. print "Checking Murmur instance with id %d." % id;
  80. # first check that the server has not yet been inserted into the DB
  81. try:
  82. instance = models.Mumble.objects.get( srvid=id );
  83. except models.Mumble.DoesNotExist:
  84. conf = murmur.getAllConf( dbus.Int32( id ) );
  85. values = {
  86. "name": find_in_dicts( "registerName", conf, default, "noname" ),
  87. "srvid": id,
  88. "dbus": dbusName,
  89. "addr": find_in_dicts( ( "registerHostame", "host" ), conf, default, "0.0.0.0" ),
  90. "port": find_in_dicts( "port", conf, default ),
  91. "url": find_in_dicts( "registerUrl", conf, default ),
  92. "motd": find_in_dicts( "welcometext", conf, default ),
  93. "passwd": find_in_dicts( "password", conf, default ),
  94. "supw": '',
  95. "users": find_in_dicts( "users", conf, default ),
  96. "bwidth": find_in_dicts( "bandwidth", conf, default ),
  97. "sslcrt": find_in_dicts( "certificate", conf, default ),
  98. "sslkey": find_in_dicts( "key", conf, default ),
  99. "booted": ( id in bootedIDs ),
  100. }
  101. if values['addr'].find( ':' ) != -1:
  102. # The addr is a hostname which actually contains a port number, but we already got that from
  103. # the port field, so we can simply drop it.
  104. values['addr'] = values['addr'].split(':')[0];
  105. if v:
  106. print 'Found new Murmur "%s" running on %s:%s.' % ( values['name'], values['addr'], values['port'] );
  107. # now create a model for the record set.
  108. instance = models.Mumble( **values );
  109. instance.save( dontConfigureMurmur=True );
  110. else:
  111. if v > 1:
  112. print "This instance is already listed in the database.";
  113. # Now search for players on this server that have not yet been registered
  114. if v > 1:
  115. print "Looking for registered Players on Server id %d." % id;
  116. if id in bootedIDs:
  117. murmurinstance = dbus.Interface(
  118. bus.get_object( 'net.sourceforge.mumble.murmur', '/%d'%id ),
  119. 'net.sourceforge.mumble.Murmur'
  120. );
  121. players = murmurinstance.getRegisteredPlayers('');
  122. for playerdata in players:
  123. if playerdata[0] == 0:
  124. continue;
  125. if v > 1:
  126. print "Checking Player with id %d and name '%s'." % playerdata[:2];
  127. try:
  128. models.MumbleUser.objects.get( server=instance, mumbleid=playerdata[0] );
  129. except models.MumbleUser.DoesNotExist:
  130. if v:
  131. print 'Found new Player "%s".' % playerdata[1];
  132. playerinstance = models.MumbleUser(
  133. mumbleid = playerdata[0],
  134. name = playerdata[1],
  135. password = '',
  136. server = instance,
  137. owner = None
  138. );
  139. playerinstance.isAdmin = playerinstance.getAdmin();
  140. playerinstance.save( dontConfigureMurmur=True );
  141. else:
  142. if v > 1:
  143. print "This player is already listed in the database.";
  144. if v > 1:
  145. print "Successfully finished Servers and Players detection.";
  146. return True;
  147. signals.post_syncdb.connect( find_existing_instances, sender=models );