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.

171 lines
6.0 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. return conf[keyword];
  36. for keyword in keys:
  37. keyword = keyword.lower();
  38. if keyword in default:
  39. return default[keyword];
  40. return valueIfNotFound;
  41. def find_existing_instances( **kwargs ):
  42. bus = dbus.SystemBus();
  43. if "verbosity" in kwargs:
  44. v = kwargs['verbosity'];
  45. else:
  46. v = 1;
  47. if v > 1:
  48. print "Starting Mumble servers and players detection now.";
  49. dbusName = 'net.sourceforge.mumble.murmur';
  50. online = False;
  51. while not online:
  52. try:
  53. murmur = dbus.Interface( bus.get_object( dbusName, '/' ), 'net.sourceforge.mumble.Meta' );
  54. except dbus.exceptions.DBusException:
  55. if v:
  56. print "Unable to connect to DBus using name %s. Is Murmur even running!?" % dbusName;
  57. dbusName = raw_input( "DBus Service name (or empty to skip Servers/Players detection): " );
  58. if not dbusName:
  59. if v:
  60. 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!';
  61. return False;
  62. else:
  63. online = True;
  64. if v > 1:
  65. print "Successfully connected to Murmur via DBus (%s)." % dbusName;
  66. default = murmur.getDefaultConf();
  67. servIDs = murmur.getAllServers();
  68. bootedIDs = murmur.getBootedServers();
  69. for id in servIDs:
  70. if v > 1:
  71. print "Checking Murmur instance with id %d." % id;
  72. # first check that the server has not yet been inserted into the DB
  73. try:
  74. instance = models.Mumble.objects.get( srvid=id );
  75. except models.Mumble.DoesNotExist:
  76. conf = murmur.getAllConf( dbus.Int32( id ) );
  77. values = {
  78. "name": find_in_dicts( "registerName", conf, default, "noname" ),
  79. "srvid": id,
  80. "dbus": dbusName,
  81. "addr": find_in_dicts( ( "registerHostame", "host" ), conf, default, "0.0.0.0" ),
  82. "port": find_in_dicts( "port", conf, default ),
  83. "url": find_in_dicts( "registerUrl", conf, default ),
  84. "motd": find_in_dicts( "welcometext", conf, default ),
  85. "passwd": find_in_dicts( "password", conf, default ),
  86. "supw": '',
  87. "users": find_in_dicts( "users", conf, default ),
  88. "bwidth": find_in_dicts( "bandwidth", conf, default ),
  89. "sslcrt": find_in_dicts( "certificate", conf, default ),
  90. "sslkey": find_in_dicts( "key", conf, default ),
  91. "booted": ( id in bootedIDs ),
  92. }
  93. if values['addr'].find( ':' ) != -1:
  94. # The addr is a hostname which actually contains a port number, but we already got that from
  95. # the port field, so we can simply drop it.
  96. values['addr'] = values['addr'].split(':')[0];
  97. if v:
  98. print 'Found new Murmur "%s" running on %s:%s.' % ( values['name'], values['addr'], values['port'] );
  99. # now create a model for the record set.
  100. instance = models.Mumble( **values );
  101. instance.save( dontConfigureMurmur=True );
  102. else:
  103. if v > 1:
  104. print "This instance is already listed in the database.";
  105. # Now search for players on this server that have not yet been registered
  106. if v > 1:
  107. print "Looking for registered Players on Server id %d." % id;
  108. if id in bootedIDs:
  109. murmurinstance = dbus.Interface(
  110. bus.get_object( 'net.sourceforge.mumble.murmur', '/%d'%id ),
  111. 'net.sourceforge.mumble.Murmur'
  112. );
  113. players = murmurinstance.getRegisteredPlayers('');
  114. for playerdata in players:
  115. if playerdata[0] == 0:
  116. continue;
  117. if v > 1:
  118. print "Checking Player with id %d and name '%s'." % playerdata[:2];
  119. try:
  120. models.MumbleUser.objects.get( mumbleid=playerdata[0] );
  121. except models.MumbleUser.DoesNotExist:
  122. if v:
  123. print 'Found new Player "%s".' % playerdata[1];
  124. playerinstance = models.MumbleUser(
  125. mumbleid = playerdata[0],
  126. name = playerdata[1],
  127. password = '',
  128. server = instance,
  129. owner = None
  130. );
  131. playerinstance.save( dontConfigureMurmur=True );
  132. else:
  133. if v > 1:
  134. print "This player is already listed in the database.";
  135. if v > 1:
  136. print "Successfully finished Servers and Players detection.";
  137. return True;
  138. signals.post_syncdb.connect( find_existing_instances, sender=models );