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.

173 lines
5.2 KiB

15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
  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.models import MumbleServer, Mumble
  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 = MumbleServer.objects.get( dbus=dbusName );
  81. except MumbleServer.DoesNotExist:
  82. meta = 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 = Mumble.objects.get( server=meta, srvid=id );
  92. except 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 = 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 = Mumble.objects.get( addr=instance.addr, port=instance.port )
  112. except Mumble.DoesNotExist:
  113. # None exist - this must've been something else.
  114. print "Server ID / Name: %d / %s" % ( instance.srvid, instance.name )
  115. raise err
  116. else:
  117. print "ERROR: There is already another server instance registered"
  118. print " on the same address and port."
  119. print " -------------"
  120. print " New Server ID:", instance.srvid,
  121. print " New Server Name:", instance.name
  122. print " Address:", instance.addr
  123. print " Port:", instance.port
  124. print " Connection string:", instance.server.dbus
  125. print " -------------"
  126. print " Duplicate Server ID:", dup.srvid,
  127. print "Duplicate Server Name:", dup.name
  128. print " Address:", dup.addr
  129. print " Port:", dup.port
  130. print " Connection string:", dup.server.dbus
  131. return False
  132. except Exception, err:
  133. print "Server ID / Name: %d / %s" % ( instance.srvid, instance.name )
  134. raise err
  135. # Now search for players on this server that have not yet been registered
  136. if instance.booted:
  137. if v > 1:
  138. print "Looking for registered Players on Server id %d." % id;
  139. instance.readUsersFromMurmur( verbose=v );
  140. elif v > 1:
  141. print "This server is not running, can't sync players.";
  142. if v > 1:
  143. print "Successfully finished Servers and Players detection.";
  144. return True;