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.

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