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.

247 lines
6.2 KiB

  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. """
  4. * Copyright (C) 2009, 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. # Set this to the same path you used in settings.py, or None for auto-detection.
  17. MUMBLE_DJANGO_ROOT = None;
  18. ### DO NOT CHANGE ANYTHING BELOW THIS LINE ###
  19. import os, sys
  20. from os.path import join, dirname, abspath, exists
  21. # Path auto-detection
  22. if not MUMBLE_DJANGO_ROOT or not exists( MUMBLE_DJANGO_ROOT ):
  23. MUMBLE_DJANGO_ROOT = dirname(abspath(__file__));
  24. # environment variables
  25. sys.path.append( MUMBLE_DJANGO_ROOT )
  26. sys.path.append( join( MUMBLE_DJANGO_ROOT, 'pyweb' ) )
  27. os.environ['DJANGO_SETTINGS_MODULE'] = 'pyweb.settings'
  28. # If you get an error about Python not being able to write to the Python
  29. # egg cache, the egg cache path might be set awkwardly. This should not
  30. # happen under normal circumstances, but every now and then, it does.
  31. # Uncomment this line to point the egg cache to /tmp.
  32. #os.environ['PYTHON_EGG_CACHE'] = '/tmp/pyeggs'
  33. from django.db.models.fields.related import ForeignKey
  34. from mumble.models import *
  35. def getNum( prompt, **kwargs ):
  36. id = None;
  37. while type(id) != int:
  38. print
  39. try:
  40. id = raw_input( "%s >>> " % prompt ).strip();
  41. if id == 'q':
  42. return None;
  43. elif id in kwargs:
  44. return kwargs[id];
  45. id = int( id );
  46. except Exception, instance:
  47. print "Error reading input. Did you type a number?";
  48. print instance;
  49. return id;
  50. def util_editModel( model, blacklist = None ):
  51. while True:
  52. print "Current settings"
  53. print "================"
  54. for field in model._meta.fields:
  55. if blacklist and field.name in blacklist:
  56. continue;
  57. print "#%-5d %-30s %s" % ( model._meta.fields.index( field ), field.verbose_name, getattr( model, field.name ) );
  58. print "================"
  59. print "Enter the index of the parameter you would like to change,"
  60. print "or q to return."
  61. idx = getNum( "Index" );
  62. if idx is None:
  63. save = raw_input( "save? Y/n >>> " );
  64. if not save or save.lower() == 'y':
  65. print "saving changes.";
  66. model.save();
  67. else:
  68. print "NOT saving changes."
  69. return;
  70. field = model._meta.fields[idx];
  71. if blacklist and field.name in blacklist:
  72. print "This field can not be changed.";
  73. elif isinstance( field, ForeignKey ):
  74. print "This is a ForeignKey.";
  75. print field.rel.to.objects.all();
  76. else:
  77. value = None;
  78. while value is None:
  79. print
  80. try:
  81. value = field.to_python( raw_input( "%s >>> " % field.name ).strip() );
  82. except Exception, instance:
  83. print instance;
  84. setattr( model, field.name, value );
  85. def act_serverDetails( server ):
  86. "View or edit server settings."
  87. util_editModel( server, ( "id", "sslcrt", "sslkey" ) );
  88. def act_registeredUsers( server ):
  89. "View or edit user registrations."
  90. mumbleusers_list = server.mumbleuser_set.all();
  91. print "Currently registered accounts";
  92. print "=============================";
  93. for mu in mumbleusers_list:
  94. if mu.owner is not None:
  95. print "#%-5d %-20s Owner: %-20s Admin: %s" % ( mu.id, mu.name, mu.owner.username, mu.getAdmin() );
  96. else:
  97. print "#%-5d %-20s" % ( mu.id, mu.name );
  98. print "=============================";
  99. print "Enter the ID of the account you would like to change, n to create a new one, or q to return."
  100. while True:
  101. idx = getNum( "ID", n=-1 );
  102. if idx is None:
  103. return;
  104. if idx == -1:
  105. mu = MumbleUser();
  106. mu.server = server;
  107. else:
  108. mu = mumbleusers_list.get( id=idx );
  109. util_editModel( mu, ( "id", "mumbleid", "server" ) );
  110. def act_listChannels( server ):
  111. "Display a channel tree."
  112. def printItem( item, level ):
  113. print "%s%s" % ( " "*level, item );
  114. server.rootchan.visit( printItem );
  115. def act_chanDetails( server ):
  116. "Display detailed information about one specific channel."
  117. print "Please choose the channel by entering the according ID (the number in parentheses)."
  118. act_listChannels( server );
  119. id = getNum( "ID" );
  120. if id is None: return;
  121. print "Channel name: %s" % server.channels[id].name
  122. print "Channel ID: %d" % server.channels[id].chanid
  123. print "Users online: %d" % len( server.channels[id].players )
  124. print "Linked chans: %d" % len( server.channels[id].linked )
  125. def cli_chooseServer():
  126. mumble_all = Mumble.objects.all().order_by( 'name', 'id' );
  127. print "Please choose a Server instance by typing the corresponding ID.\n";
  128. for mm in mumble_all:
  129. print "#%d\t%s" % ( mm.id, mm.name );
  130. print "n: Create new instance";
  131. print "q: Exit";
  132. id = getNum( "ID", n = -1 );
  133. if id is None:
  134. return;
  135. elif id == -1:
  136. return Mumble();
  137. return Mumble.objects.get( id=id );
  138. def cli_chooseAction( server ):
  139. actions = {
  140. "LISTCHAN": act_listChannels,
  141. "CHANINFO": act_chanDetails,
  142. "EDITSERVER": act_serverDetails,
  143. "EDITUSERS": act_registeredUsers,
  144. };
  145. while True:
  146. print "What do you want to do?"
  147. keys = actions.keys();
  148. for act in keys:
  149. print "#%-5d %-20s %s" % ( keys.index(act), act, actions[act].__doc__ );
  150. print "q: Return to server selection";
  151. idx = getNum( "Index" );
  152. if idx is None:
  153. return;
  154. # call action function
  155. func = actions[ keys[idx] ]
  156. func( server );
  157. print
  158. def main():
  159. print
  160. while True:
  161. mumble = cli_chooseServer();
  162. if mumble is None:
  163. print "Bye.";
  164. return;
  165. print "Selected %s." % mumble;
  166. print
  167. cli_chooseAction( mumble );
  168. print
  169. if __name__ == '__main__':
  170. #parser = OptionParser();
  171. #parser.add_option( "-v", "--verbose", help="verbose output messages", default=False, action="store_true" );
  172. #parser.add_option( "-n", "--num", help="size of the Matrix", default=4, type = 'int' );
  173. #parser.add_option( "-s", "--sure", help="don't prompt if num >= 10", default=False, action="store_true" );
  174. #options, args = parser.parse_args();
  175. main();