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.

143 lines
4.0 KiB

  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.conf import settings
  17. from mumble import models
  18. from mumble.mctl import MumbleCtlBase
  19. def find_in_dicts( keys, conf, default, valueIfNotFound=None ):
  20. if not isinstance( keys, tuple ):
  21. keys = ( keys, );
  22. for keyword in keys:
  23. if keyword in conf:
  24. return conf[keyword];
  25. for keyword in keys:
  26. keyword = keyword.lower();
  27. if keyword in default:
  28. return default[keyword];
  29. return valueIfNotFound;
  30. def find_existing_instances( **kwargs ):
  31. if "verbosity" in kwargs:
  32. v = kwargs['verbosity'];
  33. else:
  34. v = 1;
  35. if v > 1:
  36. print "Starting Mumble servers and players detection now.";
  37. triedEnviron = False;
  38. online = False;
  39. while not online:
  40. if not triedEnviron and 'MURMUR_CONNSTR' in os.environ:
  41. dbusName = os.environ['MURMUR_CONNSTR'];
  42. triedEnviron = True;
  43. if v > 1:
  44. print "Trying environment setting", dbusName;
  45. else:
  46. print "--- Murmur connection info ---"
  47. print " 1) DBus -- net.sourceforge.mumble.murmur"
  48. print " 2) ICE -- Meta:tcp -h 127.0.0.1 -p 6502"
  49. print "Enter 1 or 2 for the defaults above, nothing to skip Server detection,"
  50. print "and if the defaults do not fit your needs, enter the correct string."
  51. print "Whether to use DBus or ICE will be detected automatically from the"
  52. print "string's format."
  53. print
  54. dbusName = raw_input( "Service string: " ).strip();
  55. if not dbusName:
  56. if v:
  57. print 'Be sure to run "python manage.py syncdb" with Murmur running before'
  58. print "trying to use this app! Otherwise, existing Murmur servers won't be"
  59. print 'configurable!';
  60. return False;
  61. elif dbusName == "1":
  62. dbusName = "net.sourceforge.mumble.murmur";
  63. elif dbusName == "2":
  64. dbusName = "Meta:tcp -h 127.0.0.1 -p 6502";
  65. icesecret = getpass.getpass("Please enter the Ice secret (if any): ");
  66. try:
  67. ctl = MumbleCtlBase.newInstance( dbusName, settings.SLICE, icesecret );
  68. except Exception, instance:
  69. if v:
  70. print "Unable to connect using name %s. The error was:" % dbusName;
  71. print instance;
  72. print
  73. else:
  74. online = True;
  75. if v > 1:
  76. print "Successfully connected to Murmur via connection string %s, using %s." % ( dbusName, ctl.method );
  77. servIDs = ctl.getAllServers();
  78. try:
  79. meta = models.MumbleServer.objects.get( dbus=dbusName );
  80. except models.MumbleServer.DoesNotExist:
  81. meta = models.MumbleServer( dbus=dbusName, secret=icesecret );
  82. meta.save();
  83. for id in servIDs:
  84. if v > 1:
  85. print "Checking Murmur instance with id %d." % id;
  86. # first check that the server has not yet been inserted into the DB
  87. try:
  88. instance = models.Mumble.objects.get( server=meta, srvid=id );
  89. except models.Mumble.DoesNotExist:
  90. values = {
  91. "server": meta,
  92. "srvid": id,
  93. }
  94. if v > 1:
  95. print "Found new Murmur instance %d on bus '%s'... " % ( id, dbusName ),
  96. # now create a model for the record set.
  97. instance = models.Mumble( **values );
  98. else:
  99. if v > 1:
  100. print "Syncing Murmur instance... ",
  101. instance.configureFromMurmur();
  102. if v > 1:
  103. print instance.name;
  104. instance.save( dontConfigureMurmur=True );
  105. # Now search for players on this server that have not yet been registered
  106. if instance.booted:
  107. if v > 1:
  108. print "Looking for registered Players on Server id %d." % id;
  109. instance.readUsersFromMurmur( verbose=v );
  110. elif v > 1:
  111. print "This server is not running, can't sync players.";
  112. if v > 1:
  113. print "Successfully finished Servers and Players detection.";
  114. return True;