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.

170 lines
5.5 KiB

  1. # -*- coding: utf-8 -*-
  2. """
  3. * Copyright (C) 2009, Michael "Svedrin" Ziegler <diese-addy@funzt-halt.net>
  4. *
  5. * Mumble-Django is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This package is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. """
  15. import models
  16. import dbus
  17. from django.db.models import signals
  18. def find_in_dicts( keys, conf, default, valueIfNotFound=None ):
  19. if not isinstance( keys, tuple ):
  20. keys = ( keys, );
  21. for keyword in keys:
  22. if keyword in conf:
  23. if isinstance( conf[keyword], dbus.String ):
  24. return unicode(conf[keyword]);
  25. elif isinstance( conf[keyword], dbus.Int32 ):
  26. return int(conf[keyword]);
  27. return conf[keyword];
  28. for keyword in keys:
  29. keyword = keyword.lower();
  30. if keyword in default:
  31. if isinstance( default[keyword], dbus.String ):
  32. return unicode(default[keyword]);
  33. elif isinstance( default[keyword], dbus.Int32 ):
  34. return int(default[keyword]);
  35. return default[keyword];
  36. return valueIfNotFound;
  37. def find_existing_instances( **kwargs ):
  38. bus = dbus.SystemBus();
  39. if "verbosity" in kwargs:
  40. v = kwargs['verbosity'];
  41. else:
  42. v = 1;
  43. if v > 1:
  44. print "Starting Mumble servers and players detection now.";
  45. dbusName = 'net.sourceforge.mumble.murmur';
  46. online = False;
  47. while not online:
  48. try:
  49. murmur = dbus.Interface( bus.get_object( dbusName, '/' ), 'net.sourceforge.mumble.Meta' );
  50. except dbus.exceptions.DBusException:
  51. if v:
  52. print "Unable to connect to DBus using name %s. Is Murmur even running!?" % dbusName;
  53. dbusName = raw_input( "DBus Service name (or empty to skip Servers/Players detection): " );
  54. if not dbusName:
  55. if v:
  56. 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!';
  57. return False;
  58. else:
  59. online = True;
  60. if v > 1:
  61. print "Successfully connected to Murmur via DBus (%s)." % dbusName;
  62. default = murmur.getDefaultConf();
  63. servIDs = murmur.getAllServers();
  64. bootedIDs = murmur.getBootedServers();
  65. for id in servIDs:
  66. if v > 1:
  67. print "Checking Murmur instance with id %d." % id;
  68. # first check that the server has not yet been inserted into the DB
  69. try:
  70. instance = models.Mumble.objects.get( srvid=id );
  71. except models.Mumble.DoesNotExist:
  72. conf = murmur.getAllConf( dbus.Int32( id ) );
  73. servername = find_in_dicts( "registerName", conf, default, "noname" );
  74. if not servername:
  75. # RegistrationName was found in the dicts, but is an empty string
  76. servername = "noname";
  77. values = {
  78. "name": servername,
  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( server=instance, 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.isAdmin = playerinstance.getAdmin();
  132. playerinstance.save( dontConfigureMurmur=True );
  133. else:
  134. if v > 1:
  135. print "This player is already listed in the database.";
  136. if v > 1:
  137. print "Successfully finished Servers and Players detection.";
  138. return True;
  139. signals.post_syncdb.connect( find_existing_instances, sender=models );