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.

165 lines
4.8 KiB

  1. # -*- coding: utf-8 -*-
  2. """
  3. * Copyright © 2009-2010, 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 os, getpass
  16. from django.db import DatabaseError
  17. from django.conf import settings
  18. from mumble import models
  19. from mumble.mctl import MumbleCtlBase
  20. def find_in_dicts( keys, conf, default, valueIfNotFound=None ):
  21. if not isinstance( keys, tuple ):
  22. keys = ( keys, );
  23. for keyword in keys:
  24. if keyword in conf:
  25. return conf[keyword];
  26. for keyword in keys:
  27. keyword = keyword.lower();
  28. if keyword in default:
  29. return default[keyword];
  30. return valueIfNotFound;
  31. def find_existing_instances( **kwargs ):
  32. if "verbosity" in kwargs:
  33. v = kwargs['verbosity'];
  34. else:
  35. v = 1;
  36. if v > 1:
  37. print "Starting Mumble servers and players detection now.";
  38. triedEnviron = False;
  39. online = False;
  40. while not online:
  41. if not triedEnviron and 'MURMUR_CONNSTR' in os.environ:
  42. dbusName = os.environ['MURMUR_CONNSTR'];
  43. triedEnviron = True;
  44. if v > 1:
  45. print "Trying environment setting", dbusName;
  46. else:
  47. print "--- Murmur connection info ---"
  48. print " 1) DBus -- net.sourceforge.mumble.murmur"
  49. print " 2) ICE -- Meta:tcp -h 127.0.0.1 -p 6502"
  50. print "Enter 1 or 2 for the defaults above, nothing to skip Server detection,"
  51. print "and if the defaults do not fit your needs, enter the correct string."
  52. print "Whether to use DBus or ICE will be detected automatically from the"
  53. print "string's format."
  54. print
  55. dbusName = raw_input( "Service string: " ).strip();
  56. if not dbusName:
  57. if v:
  58. print 'Be sure to run "python manage.py syncdb" with Murmur running before'
  59. print "trying to use this app! Otherwise, existing Murmur servers won't be"
  60. print 'configurable!';
  61. return False;
  62. elif dbusName == "1":
  63. dbusName = "net.sourceforge.mumble.murmur";
  64. elif dbusName == "2":
  65. dbusName = "Meta:tcp -h 127.0.0.1 -p 6502";
  66. icesecret = getpass.getpass("Please enter the Ice secret (if any): ");
  67. try:
  68. ctl = MumbleCtlBase.newInstance( dbusName, settings.SLICE, icesecret );
  69. except Exception, instance:
  70. if v:
  71. print "Unable to connect using name %s. The error was:" % dbusName;
  72. print instance;
  73. print
  74. else:
  75. online = True;
  76. if v > 1:
  77. print "Successfully connected to Murmur via connection string %s, using %s." % ( dbusName, ctl.method );
  78. servIDs = ctl.getAllServers();
  79. try:
  80. meta = models.MumbleServer.objects.get( dbus=dbusName );
  81. except models.MumbleServer.DoesNotExist:
  82. meta = models.MumbleServer( dbus=dbusName );
  83. finally:
  84. meta.secret = icesecret;
  85. meta.save();
  86. for id in servIDs:
  87. if v > 1:
  88. print "Checking Murmur instance with id %d." % id;
  89. # first check that the server has not yet been inserted into the DB
  90. try:
  91. instance = models.Mumble.objects.get( server=meta, srvid=id );
  92. except models.Mumble.DoesNotExist:
  93. values = {
  94. "server": meta,
  95. "srvid": id,
  96. }
  97. if v > 1:
  98. print "Found new Murmur instance %d on bus '%s'... " % ( id, dbusName ),
  99. # now create a model for the record set.
  100. instance = models.Mumble( **values );
  101. else:
  102. if v > 1:
  103. print "Syncing Murmur instance... ",
  104. if v > 1:
  105. print instance.name;
  106. try:
  107. instance.configureFromMurmur();
  108. except DatabaseError, err:
  109. try:
  110. # Find instances with the same address/port
  111. dup = models.Mumble.objects.get( addr=instance.addr, port=instance.port )
  112. except Mumble.DoesNotExist:
  113. # None exist - this must've been something else.
  114. raise err
  115. else:
  116. print "ERROR: There is already another server instance registered"
  117. print " on the same address and port."
  118. print " New Server Name:", instance.name
  119. print " Address:", instance.addr
  120. print " Port:", instance.port
  121. print " Connection string:", instance.server.dbus
  122. print "Duplicate Server Name:", dup.name
  123. print " Address:", dup.addr
  124. print " Port:", dup.port
  125. print " Connection string:", dup.server.dbus
  126. return False
  127. # Now search for players on this server that have not yet been registered
  128. if instance.booted:
  129. if v > 1:
  130. print "Looking for registered Players on Server id %d." % id;
  131. instance.readUsersFromMurmur( verbose=v );
  132. elif v > 1:
  133. print "This server is not running, can't sync players.";
  134. if v > 1:
  135. print "Successfully finished Servers and Players detection.";
  136. return True;