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.

189 lines
6.9 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, signals
  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. env_icesecret = None
  43. if not triedEnviron and 'MURMUR_CONNSTR' in os.environ:
  44. dbusName = os.environ['MURMUR_CONNSTR']
  45. if 'MURMUR_ICESECRET' in os.environ:
  46. env_icesecret = os.environ['MURMUR_ICESECRET']
  47. triedEnviron = True
  48. if v > 1:
  49. print "Trying environment setting", dbusName
  50. else:
  51. print "--- Murmur connection info ---"
  52. print " 1) DBus -- net.sourceforge.mumble.murmur"
  53. print " 2) ICE -- Meta:tcp -h 127.0.0.1 -p 6502"
  54. print "Enter 1 or 2 for the defaults above, nothing to skip Server detection,"
  55. print "and if the defaults do not fit your needs, enter the correct string."
  56. print "Whether to use DBus or Ice will be detected automatically from the"
  57. print "string's format."
  58. print
  59. dbusName = raw_input( "Service string: " ).strip()
  60. if not dbusName:
  61. if v:
  62. print 'Be sure to run "python manage.py syncdb" with Murmur running before'
  63. print "trying to use this app! Otherwise, existing Murmur servers won't be"
  64. print 'configurable!'
  65. return False
  66. elif dbusName == "1":
  67. dbusName = "net.sourceforge.mumble.murmur"
  68. elif dbusName == "2":
  69. dbusName = "Meta:tcp -h 127.0.0.1 -p 6502"
  70. if env_icesecret is None:
  71. icesecret = getpass.getpass("Please enter the Ice secret (if any): ")
  72. else:
  73. icesecret = env_icesecret
  74. try:
  75. ctl = MumbleCtlBase.newInstance( dbusName, settings.SLICE, icesecret )
  76. except Exception, instance:
  77. if v:
  78. print "Unable to connect using name %s. The error was:" % dbusName
  79. print instance
  80. print
  81. else:
  82. online = True
  83. if v > 1:
  84. print "Successfully connected to Murmur via connection string %s, using %s." % ( dbusName, ctl.method )
  85. servIDs = ctl.getAllServers()
  86. unseen_ids = [rec["srvid"] for rec in Mumble.objects.values( "srvid" )]
  87. try:
  88. meta = MumbleServer.objects.get( dbus=dbusName )
  89. except MumbleServer.DoesNotExist:
  90. meta = MumbleServer( dbus=dbusName )
  91. finally:
  92. meta.secret = icesecret
  93. meta.save()
  94. for id in servIDs:
  95. if id in unseen_ids:
  96. unseen_ids.remove(id)
  97. if v > 1:
  98. print "Checking Murmur instance with id %d." % id
  99. # first check that the server has not yet been inserted into the DB
  100. try:
  101. instance = Mumble.objects.get( server=meta, srvid=id )
  102. except Mumble.DoesNotExist:
  103. values = {
  104. "server": meta,
  105. "srvid": id,
  106. }
  107. if v:
  108. print "Found new Murmur instance %d on bus '%s'... " % ( id, dbusName )
  109. # now create a model for the record set.
  110. instance = Mumble( **values )
  111. else:
  112. if v:
  113. print "Syncing Murmur instance %d: '%s'... " % ( instance.id, instance.name )
  114. try:
  115. instance.configureFromMurmur()
  116. except DatabaseError, err:
  117. try:
  118. # Find instances with the same address/port
  119. dup = Mumble.objects.get( addr=instance.addr, port=instance.port )
  120. except Mumble.DoesNotExist:
  121. # None exist - this must've been something else.
  122. print "Server ID / Name: %d / %s" % ( instance.srvid, instance.name )
  123. raise err
  124. else:
  125. print "ERROR: There is already another server instance registered"
  126. print " on the same address and port."
  127. print " -------------"
  128. print " New Server ID:", instance.srvid,
  129. print " New Server Name:", instance.name
  130. print " Address:", instance.addr
  131. print " Port:", instance.port
  132. print " Connection string:", instance.server.dbus
  133. print " -------------"
  134. print " Duplicate Server ID:", dup.srvid,
  135. print "Duplicate Server Name:", dup.name
  136. print " Address:", dup.addr
  137. print " Port:", dup.port
  138. print " Connection string:", dup.server.dbus
  139. return False
  140. except Exception, err:
  141. print "Server ID / Name: %d / %s" % ( instance.srvid, instance.name )
  142. raise err
  143. # Now search for players on this server that have not yet been registered
  144. if instance.booted:
  145. if v > 1:
  146. print "Looking for registered Players on Server id %d." % id
  147. instance.readUsersFromMurmur( verbose=v )
  148. elif v:
  149. print "This server is not running, can't sync players."
  150. signals.pre_delete.disconnect( Mumble.pre_delete_listener, sender=Mumble )
  151. for srvid in unseen_ids:
  152. mm = Mumble.objects.get( srvid=srvid )
  153. if v:
  154. print 'Found stale Mumble instance "%s".' % mm.name
  155. mm.delete()
  156. signals.pre_delete.connect( Mumble.pre_delete_listener, sender=Mumble )
  157. print "Successfully finished Servers and Players detection."
  158. print "To add more servers, run this command again."
  159. return True