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.

157 lines
5.5 KiB

16 years ago
16 years ago
  1. from django.contrib.auth.models import User
  2. from django.db import models
  3. from mmobjects import mmServer
  4. import dbus
  5. import socket
  6. class Mumble( models.Model ):
  7. name = models.CharField( 'Server Name', max_length = 200 );
  8. dbus = models.CharField( 'DBus base', max_length = 200, default = 'net.sourceforge.mumble.murmur' );
  9. srvid = models.IntegerField( 'Server ID', editable = False );
  10. addr = models.CharField( 'Server Address', max_length = 200 );
  11. port = models.IntegerField( 'Server Port', blank = True, null = True );
  12. url = models.CharField( 'Website URL', max_length = 200, blank = True );
  13. motd = models.TextField( 'Welcome Message', blank = True );
  14. passwd = models.CharField( 'Server Password', max_length = 200, blank = True );
  15. users = models.IntegerField( 'Max. Users', blank = True, null = True );
  16. bwidth = models.IntegerField( 'Bandwidth [Bps]', blank = True, null = True );
  17. sslcrt = models.CharField( 'SSL Certificate', max_length = 200, blank = True );
  18. sslkey = models.CharField( 'SSL Key', max_length = 200, blank = True );
  19. booted = models.BooleanField( 'Boot Server', default = True );
  20. def getDbusMeta( self ):
  21. return dbus.SystemBus().get_object( self.dbus, '/' );
  22. def getDbusObject( self ):
  23. "Connects to DBus and returns an mmServer object representing this Murmur instance."
  24. bus = dbus.SystemBus();
  25. murmur = bus.get_object( self.dbus, '/' );
  26. if self.srvid not in murmur.getBootedServers():
  27. raise Exception, 'No murmur process with the given server ID (%d) is running and attached to system dbus under %s.' % ( self.srvid, self.dbus );
  28. return bus.get_object( self.dbus, '/%d' % self.srvid );
  29. def getServerObject( self ):
  30. return mmServer( self.srvid, self.getDbusObject(), self.name );
  31. def __unicode__( self ):
  32. return u'Murmur "%s" (%d)' % ( self.name, self.srvid );
  33. def save( self ):
  34. # Prior to saving the model, connect to murmur via dbus and update its settings.
  35. murmur = self.getDbusMeta();
  36. # check if this server already exists, if not call newServer and set my srvid first
  37. if self.id is None:
  38. self.srvid = murmur.newServer();
  39. srvid = dbus.Int32( self.srvid );
  40. murmur.setConf( srvid, 'host', socket.gethostbyname( self.addr ) );
  41. murmur.setConf( srvid, 'registerName', self.name );
  42. murmur.setConf( srvid, 'registerUrl', self.url );
  43. murmur.setConf( srvid, 'welcometext', self.motd );
  44. murmur.setConf( srvid, 'password', self.passwd );
  45. murmur.setConf( srvid, 'sslCert', self.sslcrt );
  46. murmur.setConf( srvid, 'sslKey', self.sslkey );
  47. if self.port is not None:
  48. murmur.setConf( srvid, 'port', str(self.port) );
  49. else:
  50. murmur.setConf( srvid, 'port', '' );
  51. if self.users is not None:
  52. murmur.setConf( srvid, 'users', str(self.users) );
  53. else:
  54. murmur.setConf( srvid, 'users', '' );
  55. if self.bwidth is not None:
  56. murmur.setConf( srvid, 'bandwidth', str(self.port) );
  57. else:
  58. murmur.setConf( srvid, 'bandwidth', '' );
  59. # registerHostname needs to take the port no into account
  60. if self.port and self.port != 64738:
  61. murmur.setConf( srvid, 'registerHostname', "%s:%d" % ( self.addr, self.port ) );
  62. else:
  63. murmur.setConf( srvid, 'registerHostname', self.addr );
  64. if self.booted != murmur.isBooted( dbus.Int32(self.srvid) ):
  65. if self.booted:
  66. murmur.start( srvid );
  67. else:
  68. murmur.stop( srvid );
  69. # Now allow django to save the record set
  70. return models.Model.save( self );
  71. def deleteServer( self ):
  72. srvid = dbus.Int32( self.srvid );
  73. murmur = self.getDbusMeta();
  74. if murmur.isBooted( srvid ):
  75. murmur.stop( srvid );
  76. murmur.deleteServer( srvid );
  77. @staticmethod
  78. def pre_delete_listener( **kwargs ):
  79. kwargs['instance'].deleteServer();
  80. class MumbleUser( models.Model ):
  81. mumbleid = models.IntegerField( 'Mumble player_id', editable = False, default = -1 );
  82. name = models.CharField( 'User name and Login', max_length = 200 );
  83. password = models.CharField( 'Login password', max_length = 200 );
  84. server = models.ForeignKey( Mumble );
  85. owner = models.ForeignKey( User );
  86. def __unicode__( self ):
  87. return u"Mumble user %s on %s owned by Django user %s" % ( self.name, self.server, self.owner );
  88. def save( self ):
  89. # Before the record set is saved, update Murmur via dbus.
  90. murmur = self.server.getDbusObject();
  91. if self.id is None:
  92. # This is a new user record, so Murmur doesn't know about it yet
  93. self.mumbleid = murmur.registerPlayer( dbus.String( self.name ) );
  94. # Update user's registration
  95. murmur.setRegistration(
  96. dbus.Int32( self.mumbleid ),
  97. dbus.String( self.name ),
  98. dbus.String( self.owner.email ),
  99. dbus.String( self.password )
  100. );
  101. # Don't save the users' passwords, we don't need them anyway
  102. self.password = '';
  103. # Now allow django to save the record set
  104. return models.Model.save( self );
  105. @staticmethod
  106. def pre_delete_listener( **kwargs ):
  107. kwargs['instance'].unregister();
  108. def unregister( self ):
  109. # Unregister this player in Murmur via dbus.
  110. murmur = self.server.getDbusObject();
  111. murmur.unregisterPlayer( dbus.Int32( self.mumbleid ) );
  112. from django.db.models import signals
  113. signals.pre_delete.connect( Mumble.pre_delete_listener, sender=Mumble );
  114. signals.pre_delete.connect( MumbleUser.pre_delete_listener, sender=MumbleUser );