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.

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