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.

169 lines
5.1 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:
  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:
  103. print "Syncing Murmur instance %d: '%s'... " % ( instance.id, instance.name )
  104. try:
  105. instance.configureFromMurmur();
  106. except DatabaseError, err:
  107. try:
  108. # Find instances with the same address/port
  109. dup = Mumble.objects.get( addr=instance.addr, port=instance.port )
  110. except Mumble.DoesNotExist:
  111. # None exist - this must've been something else.
  112. print "Server ID / Name: %d / %s" % ( instance.srvid, instance.name )
  113. raise err
  114. else:
  115. print "ERROR: There is already another server instance registered"
  116. print " on the same address and port."
  117. print " -------------"
  118. print " New Server ID:", instance.srvid,
  119. print " New Server Name:", instance.name
  120. print " Address:", instance.addr
  121. print " Port:", instance.port
  122. print " Connection string:", instance.server.dbus
  123. print " -------------"
  124. print " Duplicate Server ID:", dup.srvid,
  125. print "Duplicate Server Name:", dup.name
  126. print " Address:", dup.addr
  127. print " Port:", dup.port
  128. print " Connection string:", dup.server.dbus
  129. return False
  130. except Exception, err:
  131. print "Server ID / Name: %d / %s" % ( instance.srvid, instance.name )
  132. raise err
  133. # Now search for players on this server that have not yet been registered
  134. if instance.booted:
  135. if v > 1:
  136. print "Looking for registered Players on Server id %d." % id;
  137. instance.readUsersFromMurmur( verbose=v );
  138. elif v:
  139. print "This server is not running, can't sync players.";
  140. if v > 1:
  141. print "Successfully finished Servers and Players detection.";
  142. return True;