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.

178 lines
5.7 KiB

  1. # -*- coding: utf-8 -*-
  2. """
  3. * Copyright (C) 2009, 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
  16. import models
  17. from django.db.models import signals
  18. from mctl import *
  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. else:
  44. print "--- Murmur connection info ---"
  45. print " 1) DBus -- net.sourceforge.mumble.murmur"
  46. print " 2) ICE -- Meta:tcp -h 127.0.0.1 -p 6502"
  47. print "Enter 1 or 2 for the defaults above, nothing to skip Server detection,"
  48. print "and if the defaults do not fit your needs, enter the correct string."
  49. print "Whether to use DBus or ICE will be detected automatically from the"
  50. print "string's format."
  51. print
  52. dbusName = raw_input( "Service string: " ).strip();
  53. if not dbusName:
  54. if v:
  55. print 'Be sure to run "python manage.py syncdb" with Murmur running before trying to use this app! Otherwise, existing Murmur servers won\'t be configurable!';
  56. return False;
  57. elif dbusName == "1":
  58. dbusName = "net.sourceforge.mumble.murmur";
  59. elif dbusName == "2":
  60. dbusName = "Meta:tcp -h 127.0.0.1 -p 6502";
  61. try:
  62. ctl = MumbleCtlBase.newInstance( dbusName );
  63. except Exception, instance:
  64. if v:
  65. print "Unable to connect using name %s. The error was:" % dbusName;
  66. print instance;
  67. print
  68. else:
  69. online = True;
  70. if v > 1:
  71. print "Successfully connected to Murmur via connection string %s, using %s." % ( dbusName, ctl.method );
  72. default = ctl.getDefaultConf();
  73. servIDs = ctl.getAllServers();
  74. bootedIDs = ctl.getBootedServers();
  75. for id in servIDs:
  76. if v > 1:
  77. print "Checking Murmur instance with id %d." % id;
  78. # first check that the server has not yet been inserted into the DB
  79. try:
  80. instance = models.Mumble.objects.get( dbus=dbusName, srvid=id );
  81. except models.Mumble.DoesNotExist:
  82. conf = ctl.getAllConf(id);
  83. servername = find_in_dicts( "registername", conf, default, "noname" );
  84. if not servername:
  85. # RegistrationName was found in the dicts, but is an empty string
  86. servername = "noname";
  87. values = {
  88. "name": servername,
  89. "srvid": id,
  90. "dbus": dbusName,
  91. "addr": find_in_dicts( ( "registerhostame", "host" ), conf, default, "0.0.0.0" ),
  92. "port": find_in_dicts( "port", conf, default ),
  93. "url": find_in_dicts( "registerurl", conf, default ),
  94. "motd": find_in_dicts( "welcometext", conf, default ),
  95. "passwd": find_in_dicts( "password", conf, default ),
  96. "supw": '',
  97. "users": find_in_dicts( "users", conf, default ),
  98. "bwidth": find_in_dicts( "bandwidth", conf, default ),
  99. "sslcrt": find_in_dicts( "certificate", conf, default ),
  100. "sslkey": find_in_dicts( "key", conf, default ),
  101. "booted": ( id in bootedIDs ),
  102. }
  103. if values['addr'].find( ':' ) != -1:
  104. # The addr is a hostname which actually contains a port number, but we already got that from
  105. # the port field, so we can simply drop it.
  106. values['addr'] = values['addr'].split(':')[0];
  107. if v:
  108. print 'Found new Murmur "%s" running on %s:%s.' % ( values['name'], values['addr'], values['port'] );
  109. # now create a model for the record set.
  110. instance = models.Mumble( **values );
  111. instance.save( dontConfigureMurmur=True );
  112. else:
  113. if v > 1:
  114. print "This instance is already listed in the database.";
  115. # Now search for players on this server that have not yet been registered
  116. if v > 1:
  117. print "Looking for registered Players on Server id %d." % id;
  118. if id in bootedIDs:
  119. players = ctl.getRegisteredPlayers(id);
  120. for playerdata in players:
  121. if playerdata[0] == 0:
  122. continue;
  123. if v > 1:
  124. print "Checking Player with id %d and name '%s'." % ( int(playerdata[0]), playerdata[1] );
  125. try:
  126. models.MumbleUser.objects.get( server=instance, mumbleid=playerdata[0] );
  127. except models.MumbleUser.DoesNotExist:
  128. if v:
  129. print 'Found new Player "%s".' % playerdata[1];
  130. playerinstance = models.MumbleUser(
  131. mumbleid = playerdata[0],
  132. name = playerdata[1],
  133. password = '',
  134. server = instance,
  135. owner = None
  136. );
  137. playerinstance.isAdmin = playerinstance.getAdmin();
  138. playerinstance.save( dontConfigureMurmur=True );
  139. else:
  140. if v > 1:
  141. print "This player is already listed in the database.";
  142. if v > 1:
  143. print "Successfully finished Servers and Players detection.";
  144. return True;
  145. signals.post_syncdb.connect( find_existing_instances, sender=models );