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.

186 lines
6.6 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. servername = find_in_dicts( "registerName", conf, default, "noname" );
  86. if not servername:
  87. # RegistrationName was found in the dicts, but is an empty string
  88. servername = "noname";
  89. values = {
  90. "name": servername,
  91. "srvid": id,
  92. "dbus": dbusName,
  93. "addr": find_in_dicts( ( "registerHostame", "host" ), conf, default, "0.0.0.0" ),
  94. "port": find_in_dicts( "port", conf, default ),
  95. "url": find_in_dicts( "registerUrl", conf, default ),
  96. "motd": find_in_dicts( "welcometext", conf, default ),
  97. "passwd": find_in_dicts( "password", conf, default ),
  98. "supw": '',
  99. "users": find_in_dicts( "users", conf, default ),
  100. "bwidth": find_in_dicts( "bandwidth", conf, default ),
  101. "sslcrt": find_in_dicts( "certificate", conf, default ),
  102. "sslkey": find_in_dicts( "key", conf, default ),
  103. "booted": ( id in bootedIDs ),
  104. }
  105. if values['addr'].find( ':' ) != -1:
  106. # The addr is a hostname which actually contains a port number, but we already got that from
  107. # the port field, so we can simply drop it.
  108. values['addr'] = values['addr'].split(':')[0];
  109. if v:
  110. print 'Found new Murmur "%s" running on %s:%s.' % ( values['name'], values['addr'], values['port'] );
  111. # now create a model for the record set.
  112. instance = models.Mumble( **values );
  113. instance.save( dontConfigureMurmur=True );
  114. else:
  115. if v > 1:
  116. print "This instance is already listed in the database.";
  117. # Now search for players on this server that have not yet been registered
  118. if v > 1:
  119. print "Looking for registered Players on Server id %d." % id;
  120. if id in bootedIDs:
  121. murmurinstance = dbus.Interface(
  122. bus.get_object( 'net.sourceforge.mumble.murmur', '/%d'%id ),
  123. 'net.sourceforge.mumble.Murmur'
  124. );
  125. players = murmurinstance.getRegisteredPlayers('');
  126. for playerdata in players:
  127. if playerdata[0] == 0:
  128. continue;
  129. if v > 1:
  130. print "Checking Player with id %d and name '%s'." % playerdata[:2];
  131. try:
  132. models.MumbleUser.objects.get( server=instance, mumbleid=playerdata[0] );
  133. except models.MumbleUser.DoesNotExist:
  134. if v:
  135. print 'Found new Player "%s".' % playerdata[1];
  136. playerinstance = models.MumbleUser(
  137. mumbleid = playerdata[0],
  138. name = playerdata[1],
  139. password = '',
  140. server = instance,
  141. owner = None
  142. );
  143. playerinstance.isAdmin = playerinstance.getAdmin();
  144. playerinstance.save( dontConfigureMurmur=True );
  145. else:
  146. if v > 1:
  147. print "This player is already listed in the database.";
  148. if v > 1:
  149. print "Successfully finished Servers and Players detection.";
  150. return True;
  151. signals.post_syncdb.connect( find_existing_instances, sender=models );