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.

193 lines
7.5 KiB

16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
  1. """ This file is part of the mumble-django application.
  2. Copyright (C) 2009, Michael "Svedrin" Ziegler <diese-addy@funzt-halt.net>
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. - Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer.
  9. - Redistributions in binary form must reproduce the above copyright notice,
  10. this list of conditions and the following disclaimer in the documentation
  11. and/or other materials provided with the distribution.
  12. - Neither the name of the Mumble Developers nor the names of its
  13. contributors may be used to endorse or promote products derived from this
  14. software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. """
  27. from django.contrib.auth.models import User
  28. from django.db import models
  29. from mmobjects import mmServer
  30. import dbus
  31. import socket
  32. class Mumble( models.Model ):
  33. name = models.CharField( 'Server Name', max_length = 200 );
  34. dbus = models.CharField( 'DBus base', max_length = 200, default = 'net.sourceforge.mumble.murmur' );
  35. srvid = models.IntegerField( 'Server ID', editable = False );
  36. addr = models.CharField( 'Server Address', max_length = 200 );
  37. port = models.IntegerField( 'Server Port', blank = True, null = True );
  38. url = models.CharField( 'Website URL', max_length = 200, blank = True );
  39. motd = models.TextField( 'Welcome Message', blank = True );
  40. passwd = models.CharField( 'Server Password', max_length = 200, blank = True );
  41. supw = models.CharField( 'Superuser Password', max_length = 200, blank = True );
  42. users = models.IntegerField( 'Max. Users', blank = True, null = True );
  43. bwidth = models.IntegerField( 'Bandwidth [Bps]', blank = True, null = True );
  44. sslcrt = models.CharField( 'SSL Certificate', max_length = 200, blank = True );
  45. sslkey = models.CharField( 'SSL Key', max_length = 200, blank = True );
  46. booted = models.BooleanField( 'Boot Server', default = True );
  47. def getDbusMeta( self ):
  48. return dbus.Interface( dbus.SystemBus().get_object( self.dbus, '/' ), 'net.sourceforge.mumble.Meta' );
  49. def getDbusObject( self ):
  50. "Connects to DBus and returns an mmServer object representing this Murmur instance."
  51. bus = dbus.SystemBus();
  52. murmur = dbus.Interface( bus.get_object( self.dbus, '/' ), 'net.sourceforge.mumble.Meta')
  53. if self.srvid not in murmur.getBootedServers():
  54. raise Exception, 'No murmur process with the given server ID (%d) is running and attached to system dbus under %s.' % ( self.srvid, self.dbus );
  55. return dbus.Interface( bus.get_object( self.dbus, '/%d' % self.srvid ), 'net.sourceforge.mumble.Murmur' );
  56. def getServerObject( self ):
  57. return mmServer( self.srvid, self.getDbusObject(), self.name );
  58. def __unicode__( self ):
  59. return u'Murmur "%s" (%d)' % ( self.name, self.srvid );
  60. def save( self ):
  61. # Prior to saving the model, connect to murmur via dbus and update its settings.
  62. murmur = self.getDbusMeta();
  63. # check if this server already exists, if not call newServer and set my srvid first
  64. if self.id is None:
  65. self.srvid = murmur.newServer();
  66. srvid = dbus.Int32( self.srvid );
  67. murmur.setConf( srvid, 'host', socket.gethostbyname( self.addr ) );
  68. murmur.setConf( srvid, 'registerName', self.name );
  69. murmur.setConf( srvid, 'registerUrl', self.url );
  70. murmur.setConf( srvid, 'welcometext', self.motd );
  71. murmur.setConf( srvid, 'password', self.passwd );
  72. murmur.setConf( srvid, 'sslCert', self.sslcrt );
  73. murmur.setConf( srvid, 'sslKey', self.sslkey );
  74. if self.port is not None:
  75. murmur.setConf( srvid, 'port', str(self.port) );
  76. else:
  77. murmur.setConf( srvid, 'port', '' );
  78. if self.users is not None:
  79. murmur.setConf( srvid, 'users', str(self.users) );
  80. else:
  81. murmur.setConf( srvid, 'users', '' );
  82. if self.bwidth is not None:
  83. murmur.setConf( srvid, 'bandwidth', str(self.port) );
  84. else:
  85. murmur.setConf( srvid, 'bandwidth', '' );
  86. # registerHostname needs to take the port no into account
  87. if self.port and self.port != 64738:
  88. murmur.setConf( srvid, 'registerHostname', "%s:%d" % ( self.addr, self.port ) );
  89. else:
  90. murmur.setConf( srvid, 'registerHostname', self.addr );
  91. if self.supw:
  92. murmur.setSuperUserPassword( srvid, self.supw );
  93. if self.booted != murmur.isBooted( dbus.Int32(self.srvid) ):
  94. if self.booted:
  95. murmur.start( srvid );
  96. else:
  97. murmur.stop( srvid );
  98. # Now allow django to save the record set
  99. return models.Model.save( self );
  100. def deleteServer( self ):
  101. srvid = dbus.Int32( self.srvid );
  102. murmur = self.getDbusMeta();
  103. if murmur.isBooted( srvid ):
  104. murmur.stop( srvid );
  105. murmur.deleteServer( srvid );
  106. @staticmethod
  107. def pre_delete_listener( **kwargs ):
  108. kwargs['instance'].deleteServer();
  109. class MumbleUser( models.Model ):
  110. mumbleid = models.IntegerField( 'Mumble player_id', editable = False, default = -1 );
  111. name = models.CharField( 'User name and Login', max_length = 200 );
  112. password = models.CharField( 'Login password', max_length = 200 );
  113. server = models.ForeignKey( Mumble );
  114. owner = models.ForeignKey( User );
  115. def __unicode__( self ):
  116. return u"Mumble user %s on %s owned by Django user %s" % ( self.name, self.server, self.owner );
  117. def save( self ):
  118. # Before the record set is saved, update Murmur via dbus.
  119. murmur = self.server.getDbusObject();
  120. if self.id is None:
  121. # This is a new user record, so Murmur doesn't know about it yet
  122. self.mumbleid = murmur.registerPlayer( dbus.String( self.name ) );
  123. # Update user's registration
  124. murmur.setRegistration(
  125. dbus.Int32( self.mumbleid ),
  126. dbus.String( self.name ),
  127. dbus.String( self.owner.email ),
  128. dbus.String( self.password )
  129. );
  130. # Don't save the users' passwords, we don't need them anyway
  131. self.password = '';
  132. # Now allow django to save the record set
  133. return models.Model.save( self );
  134. @staticmethod
  135. def pre_delete_listener( **kwargs ):
  136. kwargs['instance'].unregister();
  137. def unregister( self ):
  138. # Unregister this player in Murmur via dbus.
  139. murmur = self.server.getDbusObject();
  140. murmur.unregisterPlayer( dbus.Int32( self.mumbleid ) );
  141. from django.db.models import signals
  142. signals.pre_delete.connect( Mumble.pre_delete_listener, sender=Mumble );
  143. signals.pre_delete.connect( MumbleUser.pre_delete_listener, sender=MumbleUser );