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
6.1 KiB

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